真的是这个问题困扰了我好久,网上所有能查到的模式我都试过了,但是不管用,而且网上大部分说的都是history模式,很少提及hash模式安卓没问题,但是IOS签名失败的问题,只能自己排查了,挨个alert()之后发现原来是hash链接里的#被截断了。
解决方式: 将分享链接中的 search 参数解析,拼接为 hash 参数
magicLink函数为拼接函数
magicLink(link) {
var hashIndex = link.indexOf(‘#/’)
var nativeUrl = link.substring(0, hashIndex)
var spaUrl = link.substring(hashIndex + 1, link.length)
var targetUrl = ‘’
if (/?/.test(nativeUrl)) {
targetUrl = nativeUrl + ‘&spaurl=’ + encodeURIComponent(spaUrl)
} else {
targetUrl = nativeUrl + ‘?spaurl=’ + encodeURIComponent(spaUrl)
}
return targetUrl
}
然后在做微信授权的时候
getloginInfo() {
const that = this
const AppId = ‘xxxxxxxxxxxxxx’ // 公众号的AppId
const code = that.getUrlParam(‘code’) // 获取请求路径中带code字段参数的方法
const local = that.magicLink(window.location.href) // 获取当前的页面路径,这就是回调的地址
var state = Math.ceil(Math.random() * 10000)
if (code == null || code === ‘’) {
window.location.href =
‘https://open.weixin.qq.com/connect/oauth2/authorize?appid=’ +
AppId +
‘&redirect_uri=’ +
encodeURIComponent(local) +
‘&response_type=code&scope=snsapi_userinfo&state=’ +
state +
‘#wechat_redirect’
} else {
console.log(window.location.href)
// 通过获取到的code,调用后台的接口,取得openId
const res1 = await that.$http({
url: ‘/sp_trunk/login/wxCallback’,
method: ‘get’,
params: {
code: code
}
})
getloginInfo() 这个函数我没写完,网上一搜一大把,我这里只是告诉大家在哪里使用magicLink这个函数,好了,希望这篇文章能帮助到你,哎,真不容易啊。