在app.js添加此函数,使用promise获取
getOpenid: function () {
var that = this;
return new Promise(function (resolve, reject) {
var appid = ‘wx717f0845e785ba46′; //填写微信小程序appid
var secret = ’98ea5800ed753296f88e7bd0f25a2391’; //填写微信小程序secret
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
console.log(res)
that.globalData.code = res.code;
wx.request({
url: ‘https://api.weixin.qq.com/sns/jscode2session?appid=’ + appid + ‘&secret=’ + secret + ‘&grant_type=authorization_code&js_code=’ + res.code,
header: {
‘content-type’: ‘application/json’
},
success: function (data) {
that.globalData.opendid = data.data.openid;
console.log(data.data.openid) /