微信支付(当前url未注册)

285 阅读1分钟

阅读前:本文主要介绍微信支付支付目录配置正确,调起支付时,仍然提示当前url未注册(可能只是其中的一种,希望对你有帮助),着急的朋友:ctrl+f   (解决方案


我的支付目录:http://xxx.lll.com/#/pages/

因为我是用vue写的页面,所以hash模式下,链接中会有  #,这也是问题的关键,因为微信支付检测当前url时,对hash很不友好,如:我的链接为

http://xxx.lll.com/#/pages/index

换取code后被重定向的地址会变成

http://xxx.lll.com/#/pages/index/?code=11111111111111&state=123#/pages/index

此时调起支付识别的支付地址就是:

http://xxx.lll.com/

完美提示 当前url未注册


解决方案:

 if (!code) {//未获取code时重定向            
    var urlNow = encodeURIComponent(window.location.href);            
    var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=********&redirect_uri=" + urlNow + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";            
    window.location.replace(url);        
}else{         
    //关于code的其他操作
    //do something with code 

    //纠正url     
    console.log("location",location);           
    let { href, protocol, host, pathname, search, hash } = window.location           
    if(hash=='#/'){//首页重定向,路由已设置可忽略             
        hash='#/pages/index'           
    }           
    let newHref = `${protocol}//${host}${pathname}${hash}`            
    location.href = newHref         
};


经过处理后,链接就会变成

http://xxx.lll.com/#/pages/index/#/pages/index?code=11111111111111&state=123

此时就能够正常识别了