1.獲取微信登錄憑證
wx.login({
success: (res) => {
this.setData({
wxCode: res.code,
})
}
2.登錄憑證傳回后端獲取openid,前端直接調(diào)用接口獲取openid正式上線代碼審核過不去。
wx.request({
url:'http://localhost:8087/controll/getOpenID/'+this.data.wxCode,
method: 'GET',
success: (res) => {
if(res.statusCode == "200"){
this.setData({
wxOpenId: res.data.openid
})
}else{
wx.showToast({
title: res.statusCode,
icon: 'none',
duration: 2000//持續(xù)的時(shí)間
})
}
}
})
3.后端代碼(直接返回的openid中含有secret,可發(fā)布,但是會提醒你存在安全漏洞,所以對openid進(jìn)行截取拼接)
@CrossOrigin
@GetMapping("/getOpenID/{code}")
public String getAppID(@PathVariable String code) {
String result= HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session?appid="
+ "AppID" + "&secret=" + "AppSecret"
+ "&js_code=" + code + "&grant_type=authorization_code");
// return result.openid;
result="{\""+result.substring(result.indexOf("openid"));
return result;
}
3.獲取AppSecret和AppID
登錄微信公眾平臺
開發(fā)——>開發(fā)管理——>開發(fā)設(shè)置
文章來源:http://www.zghlxwxcb.cn/news/detail-719957.html
AppSecret重置后記住文章來源地址http://www.zghlxwxcb.cn/news/detail-719957.html
到了這里,關(guān)于微信小程序如何獲取微信號的唯一標(biāo)識(openid)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!