App({
onLaunch: function () {
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
var that = this
this.getOpenid().then(()=>{
return that.setAdmin()
//为什么这里没有任何console啊,没有执行
})
},
getOpenid: function () {
var that = this
return new Promise(function (resolve, reject) {
console.log(resolve)
wx.getStorage({
key: 'openid',
success: function (res) {
that.globalData.openId = res.data
console.log(that.globalData.openId)
},
fail: function () {
wx.login({
success: res => {
var code = res.code; //返回code
var appId = '';
var secret = '';
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
data: {},
header: {
'content-type': 'json'
},
success: function (res) {
wx.setStorage({
key: "openid",
data: res.data.openid
})
that.globalData.openId = res.data.openid
}
})
}
})
}
})
})
},
setAdmin:function () {
var that = this
return new Promise(function (resolve, reject) {
console.log(that.globalData.openId)
wx.request({
url: 'http://127.0.0.1:8889/api/club/adminComfirm',
method:'post',
data:{
name:that.globalData.openId
},
header:{
"content-type":'application/json'
},
success:function(res){
console.log(that.globalData.openId)
}
})
})
},
globalData: {
userInfo: null,
openId:null,
adminOn:true
}
})
目的是要先 设置openid到data后,再传给服务器验证,但是wxrequst异步要promise来规定执行顺序啊,但是这里为什么then后不执行呢,求大大解答
这个地方少写了resolve( xxx ),
这个地方同样少写了resolve(xxx)
不调用reslove函数,then是不会执行的