微信小程序如何获取OpenID
前端获取openid的方式(openid为用户唯一标识)
- app.js
App({
/**
* 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
*/
onLaunch: function () {
var that = this;
wx.login({
success:res=>{ // 每次用户登录都会拿到一个code
wx.request({
url: that.globalData.wx_url_1 + res.code + that.globalData.wx_url_2,
success:res=>{
that.globalData.openid = res.data.openid;
}
})
}
});
},
/**
* 设置全局变量
*/
globalData: {
openid:0,
wx_url_1: 'https://api.weixin.qq.com/sns/jscode2session?appid=自己的APPID&secret=自己的SECRET&js_code=',
wx_url_2: '&grant_type=authorization_code'
}
})
后端获取openid的方式
- app.js
App({
/**
* 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
*/
onLaunch: function () {
var that = this;
wx.login({
success: res => { 每次用户登录都会拿到一个code
console.log('用户code');
console.log(res.code);
wx.request({
url: 'http://localhost:8082/UserCenter',
method: 'POST',
header: { "content-type": 'application/json' },
data: {
"head": {
"appVerNo": "1.0.0",
"functionNo": "WeChatLoginVerification"
},
"param": {
"weChatCode": res.code
}
},
dataType: 'json',
responseType: 'text',
success(res) {
console.log('666');
console.log(res);
that.globalData.openid = res.data.data.openId;
},
fail(res) { console.log(res) },
})
}
});
},
/**
* 设置全局变量
*/
globalData: {
openid: null,
userInfo:null
}
})
- 后台
/**
*@description:微信登录
*/
Public void weChatLoginVerificationRequestProcess(){
String weChatCode=dataCenterService.getData("weChatCode");
System.out.println("------weChatCode------"+weChatCode);
//获取sessionKeyAndOpenId的api
String getSesionKeyURL=getSessionKeyAPI+"?appid="+appId+"&secret="+appsecret+"&js_code="
+weChatCode+"&grant_type=authorization_code";
System.out.println("------getSesionKeyURL---------"+getSesionKeyURL);
//获取到sessionKeyAndOpenId的json数据
String returnSesionKeyAndOpendIdJson=CommonUtil.sendGetRequest(getSesionKeyURL);
//连接超时
if(returnSesionKeyAndOpendIdJson==null){
ExceptionUtil.setFailureMsgAndThrow(cn.edu.fjnu.towide.ww.usercenter.enums.ReasonOfFailure.REQUEST_TIMED_OUT_CODE);
}
System.out.println("---------returnSesionKeyAndOpendIdJson--------"+returnSesionKeyAndOpendIdJson);
JSONObject jsonObject=JSONObject.parseObject(returnSesionKeyAndOpendIdJson);
String errcode=jsonObject.getString("errcode");
if(WeChatReasonOfFailureConstant.ERRMSG_CODE.equals(errcode)){
ExceptionUtil.setFailureMsgAndThrow(ReasonOfFailure.INVALID_CODE_CODE);
}elseif(WeChatReasonOfFailureConstant.ERRMSG_CODE_BEEN_USED_CODE.equals(errcode)){
ExceptionUtil.setFailureMsgAndThrow(ReasonOfFailure.INVALID_CODE_CODE);
return;
}
String openId=jsonObject.getString("openid");
String sessionKey=jsonObject.getString("session_key");
//StringexpiresIn=jsonObject.getString("expires_in");//暂时未用到
System.out.println("-----openId--------"+openId);
ResponseData responseData=dataCenterService.getData("responseData");
ResponseDataUtil.setHeadOfResponseDataWithSuccessInfo(responseData);
ResponseDataUtil.putValueToData(responseData,"openId",openId);
}
- yml
wechat:
app_id:
app_secret:
get_session_key_api:
- 如何向第三方发送get请求:
https://blog.csdn.net/weixin_44308214/article/details/109724173