ajax 微信code获取_vue 获取微信授权成功code值

Category : 前端

Views : 669

Comments : 2

Posted on : 2019-06-12

export default {

name: 'app',

mounted() {

let script1 = document.createElement("script");

script1.type = "text/javascript";

script1.src = "http://res.wx.qq.com/open/js/jweixin-1.2.0.js";

document.body.appendChild(script1);

let ua = navigator.userAgent.toLowerCase();

if(ua.match(/MicroMessenger/i) == "micromessenger") {

if(window.location.search.indexOf('code') < 0) {

window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx71c3c2414bc6ab7b&redirect_uri=' + encodeURIComponent(location.href) + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';

} else if(window.location.search.indexOf('code') > -1) {

let code = this.getUrlKey("code");

alert('前端获取到的code为:' + code);

localStorage.setItem('wechatcode', code);

}

}

},

methods: {

getUrlKey(name) {

return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;

}

}

}

Posted by : 雨落秋垣

Tags : 本文未设置标签

您可以自由的转载和修改,但请务必注明文章来源并且不可用于商业目的。

本站部分内容收集于互联网,如果有侵权内容、不妥之处,请联系我们删除。敬请谅解!

已有 2 条评论

回复 小白: 这个是线上的代码。我直接复制出来的。一个商城的商品使用微信商户付款中,需要获取code的代码。没有改过哦

添加新评论

提交评论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值