微信端h5页面重定向获取code

微信打开h5页面时,获取微信code作为参数使用
global.js

let environment = true; //配置正式环境和测试环境的appid(微信公众号的)
export default {
 	oauth_url_weixin: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + (environment ? 'wx3c9600b2c259e4c8' : 'wxc541f115b5e6857f') + "&redirect_uri=REDIRECTURI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect",
}

getUrl.js

// 将连接后面的参数 对象化
export default function getQueryString(url) { 
    var theRequest = new Object();  
    if (url.indexOf("?") != -1) {  
       var str = url.substr(1);  
       var strs = str.split("&");  
       for(var i = 0; i < strs.length; i ++) {  
        //    decodeURI  乱码转中文
          theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1])
       }  
    }  
    return theRequest;  
}
 

main.js

import getQueryString from '@/utils/getUrl.js'
router.beforeEach((to, from, next) => {
    let cusId = window.sessionStorage.getItem("cusId");
    //获取code
    let code = getQueryString(window.location.search).code;
    //获取链接后面参数
    let channel = getQueryString(window.location.search).channel;
    let productId = getQueryString(window.location.search).productId;
    let g = getQueryString(window.location.search).g;
    //code五分钟失效
    if (code == undefined || code == 'undefined' || code == null) {
        if (cusId == undefined || cusId == 'undefined' || cusId == null) {
            window.localStorage.clear()
            //重定向的链接及拼接参数
            let url = "https://youxuan.91betterwei.com/smhk/index.html?channel=" + channel + '&productId=' + productId + '&g=' + g 
            window.location.href = global.oauth_url_weixin.replace('REDIRECTURI', encodeURIComponent(url));
        }
    }
    if (to.meta.title) {
        document.title = to.meta.title
    }
    next()
})
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值