记得之前写过一次,在来一次吧
由于‘https://api.weixin.qq.com’不能加入request白名单
这个openid只能由后台或者云小程序获取,
这段代码可以在不限制域名的情况,测试开发使用
// 微信授权登录
weixinLogin() {
// 获取code
var that = this
Util.showLoading()
wx.login({
success: (res) => {
that.get_openid(res.code)
},
})
},
// 获取openid
get_openid(code) {
var that = this
var appid = config.appid
var secret = config.secret
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',//请求微信服务器获取openid
data: {},
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log("Openid为:" + res.data.openid) // oHGn55L0ayMhtYxzAiEYHZT4_EIU
that.login(res.data.openid)
},
})
},
网友评论