微信 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://segmentfault.com/a/11...

阅读 4.3k
3 个回答
    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');
  }

我....顶你个。在公司打开了你的连接...

重定向使用window.location.replace

let href = window.location.href;
if(href.indexOf('groupmessage') > -1 || href.indexOf('singlemessage') > -1 || href.indexOf('timeline') > -1){
    href = href.replace(/\?from=(groupmessage|singlemessage|timeline)(\S*)#/, '#');
    window.location.replace(href);
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏