- .wxml
<button bindtap="paytap">授权</button>
- .js
Page({ paytap: function () { var that = this //登陆获取code wx.login({ success: function (res) { that.getOpenId(res.code) } }); }, getOpenId: function (code) { var that = this; wx.request({ url: "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code", data: { appid: '', secret: '', js_code: code, grant_type: 'authorization_code' }, method: 'GET', success: function (res) { console.log(res) wx.setStorageSync("sessionid", res.header["Set-Cookie"]) }, fail: function () { // fail }, complete: function () { // complete } }) }})