微信H5授权重定向后返回无限循环?
发布于 6 年前 作者 iyan 7208 次浏览 来自 官方Issues

代码片段: https://developers.weixin.qq.com/s/A1pfVqmC79fz

授权所需测试号:

因需要维护多个公众号,不同入口的授权,整合了一个授权拼接页

授权拼接页功能:拼接微信H5授权链接,重定向至微信授权链接进行授权

拼接页代码:

import qs from 'qs'
import {request} from '[@j](/user/j)/request'
import {tools} from '[@j](/user/j)/tools'

function init(e) {
  var search = tools.getUrlData(e.search) //获取所有&后面的参数包括重定向
  queryAppId({ //获取appid
    url: e.href,
    srcChannel: search.wxChannel,
  }).then(({appid}) => {
    var target = search.redirectUrl //获取重定向链接 包括?
    var scope = search.scope //获取授权类型
    delete search.redirectUrl //删除当前页面参数
    delete search.wxChannel //删除当前页面参数
    delete search.scope 
    var curUrl = `${e.origin}${target}&${qs.stringify(search)}` //拼接重定向链接 域名 + 重定向地址 + 参数
    try {
      curUrl = decodeURIComponent(curUrl)
    } catch(error) {
      alert('参数中请勿使用非UTF8编码字符组合')
    }
    curUrl = encodeURIComponent(curUrl) //转义
    location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${curUrl}&response_type=code&scope=snsapi_${scope}&state=23432#wechat_redirect`)
  })
}

问题描述:

通过拼接页重定向至微信授权链接,无法在页面栈中去除拼接页的历史记录,入口或授权链接页返回上一页会重新进入拼接页,然后无限循环

(会无限循环:微信H5,开发者工具H5,部分手机小程序,开发者工具小程序webview)

(会停留在拼接页不再授权,显示空白页:部分手机小程序)

1 回复

时灵时不灵?

--↓↓👍点赞是回答的动力哦

回到顶部