在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  HTML/ 小程序 Promise執(zhí)行問(wèn)題

小程序 Promise執(zhí)行問(wèn)題

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()
        //為什么這里沒(méi)有任何console啊,沒(méi)有執(zhí)行
    })
  },
  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
  }
})

目的是要先 設(shè)置openid到data后,再傳給服務(wù)器驗(yàn)證,但是wxrequst異步要promise來(lái)規(guī)定執(zhí)行順序啊,但是這里為什么then后不執(zhí)行呢,求大大解答

回答
編輯回答
青黛色
 success: function (res) {
                 that.globalData.openId = res.data
                 console.log(that.globalData.openId)
              },

這個(gè)地方少寫(xiě)了resolve( xxx ),

success: function (res) {
                                  wx.setStorage({
                                      key: "openid",
                                      data: res.data.openid
                                  })
                                  that.globalData.openId = res.data.openid
                              }

這個(gè)地方同樣少寫(xiě)了resolve(xxx)

不調(diào)用reslove函數(shù),then是不會(huì)執(zhí)行的

2017年10月27日 23:25