1.通过 wx.login获取 登录凭证(code)
wx.login({ success: function (res) { console.log(res.code); } })
2.在此处获得
appid 和 secret :https://developers.weixin.qq.com/sandbox
如图
3.小程序端
http://127.0.0.1:8080/jeecg-boot 这一段是自己的访问路径
//app.js
App({
globalData: {
appid: '',
appsecret: '',//
openid: ''
}
onLaunch: function () {
var that =this;
// 登录
wx.login({
success: function (res) {
console.log(res.code)
wx.request({
url: 'http://127.0.0.1:8080/jeecg-boot/hwork/hworkLog/GetOpenIdServlet',
data: {
appid: that.globalData.appid,
secret: that.globalData.appsecret,
js_code: res.code,
grant_type: 'authorization_code'
},
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function (res) {
console.log(res)
//转json
var j= JSON.parse(res.data.result)
//获取到openid
that.globalData.openid = j.openid;