Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

微信 ios二次分享,页面打开空白,已做授权重定向处理

背景:分享出去的外链,会自动给加上微信标识,导致二次分享失败,
如打开页面:

`https://xxx/#/deepCoupling/v-main?aaa=5555`

分享出去链接:

https://xxx/from=groupmessage&isappinstalled=0#/deepCoupling/v-main?aaa=5555

解决办法:

R1、替换路径,简单粗暴(query字段只对微信有用,对我们没用)

  const origin = isIOS() ? window.location.origin + '/#/' : window.location.origin;
  window.location.href = environment.wxAuthUrl + '/third/authForLogin?authorizerAppid=' + wxAppId + '&businessURI=' + encodeURIComponent(origin);
  alert(window.location.href + '授权后地址333');
  if (isIOS()) {
    alert(getCookie('deepCouplingVmain') + '获取getCookie3333');
    if (window.location.href.indexOf('from') !== -1 || window.location.href.indexOf('isappinstalled') !== -1) {
      if (window.location.href.indexOf('deepCoupling/v-main') !== -1) {
        // window.location.href = getCookie('deepCouplingVmain') + '?random=' + Math.floor(Math.random() * 1048576);
        window.history.replaceState(null, '', getCookie('deepCouplingVmain'));
      }
    
    } else {
      if (window.location.href.indexOf('deepCoupling/v-main') !== -1) {
        if (!(getCookie('deepCouplingVmain'))) {
          setCookie('deepCouplingVmain', window.location.href);
        }
      }
    }
  }

但是页面重定向后没有刷新,需要手动刷新

ios第二次分享打不开

如果url不带?aaa=5555即正常
但是后面带了参数第二次分享就打不开了

 https://xxx/#/deepCoupling/v-main?aaa=5555

解决方案参考 https://ostack.cn/a/11...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
    window.location.replace(href);

试用了也是没效果 也是需要刷新

解决方案:

由于url带?a=555 参数都会导致 ios在第二次分享的时候打开页面空白状态
最后将a=555,进来的时候就setcookie,然后在ulr 带 from等因分享附带的参数 情况下,重定向,重定向url不带?a=555,而将?a=555放到分享的链接上。

代码如下:

export const getSearchUrl = (url) => {
    if (url.indexOf('?') !== -1) {
      const afterUrl = url.substring(url.indexOf('?') + 1);
      const cookieKey = afterUrl.substring(0 , afterUrl.indexOf('='));
      const cookieVal = afterUrl.substring(afterUrl.indexOf('=') + 1);
      setCookie(cookieKey, cookieVal);
    }
};
export const redirect = (url) => {
  if (url.indexOf('?') !== -1) {
    const beforeUrl = url.substring(0, url.indexOf('?'));
    if (window.location.href.indexOf('from') !== -1 || window.location.href.indexOf('isappinstalled') !== -1) {
      if (window.location.href.indexOf(beforeUrl) !== -1) {
        window.location.href =  environment.domainUrl + beforeUrl;
      }
    }
  }
}
export const wxOAuth = (wxAppId: string, url: string) => {
  sessionStorage.setItem(LAST_URL, url);
  getSearchUrl(url);
  const origin = isIOS() ? window.location.origin + '/#/' : window.location.origin;
  window.location.href = environment.wxAuthUrl + '/third/authForLogin?authorizerAppid=' + wxAppId + '&businessURI=' + encodeURIComponent(origin);
  redirect(url);
};

页面进来,获取id:

activateInfo.queryParams.subscribe(queryParams => {
      if (queryParams.mediaActivityParticipantId === '' || queryParams.mediaActivityParticipantId === null  || queryParams.mediaActivityParticipantId === undefined) {
        this.tutorId = getCookie('mediaActivityParticipantId');
      } else {
        this.tutorId = queryParams.mediaActivityParticipantId;
      }
    });

分享代码:

 wxShare({
        title: '',
        desc: '',
        link: window.location.origin + '/#/deepCoupling/v-main?mediaActivityParticipantId=' + this.tutorId,
        imgUrl: window.location.origin + '/assets/img/deep/share.jpg'
      });

页面关闭,清楚改页面cookie

ngOnDestroy() {
    delCookie('mediaActivityParticipantId');
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...