vue微信内h5页面微信授权登录

问题描述

微信内h5页面微信授权登录,获取code换取登录token

解决方案:

1.用户在微信内授权登录,这里用的是vue开发,登录之后code在回调的url中,然后拿着code向后端换取token即可.
在这里插入图片描述
wxgetMobileCode() {
const appid = “wxxxxxxx”;
const redirect_uri = encodeURIComponent(window.location.href);
const scope = “snsapi_userinfo”
const urls =
https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid} &redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=STATE#wechat_redirect
window.location.href = urls
},
2.在回调的url中获取code
在这里插入图片描述
created() {
if(this.$route.query.code) {
this.getWeixin(this.$route.query.code); //执行换取token
} else {
//因为code返回的时候不能直接通过参数拿到,所以要对code进行截取处理
let href = window.location.href
let origin = window.location.origin
let hash = window.location.hash
if(href.includes(“code”)) {
this.code = href.split(‘?’)[1].split(‘=’)[1].split(‘&’)[0];
window.location.href = ${origin}/${hash}&code=${this.code}
}
}
},

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值