鍍金池/ 問答/HTML/ 小程序login 異步的問題

小程序login 異步的問題

現(xiàn)在是這樣的 app.js 執(zhí)行登錄 登錄的時(shí)候請(qǐng)求用code請(qǐng)求后端 返回的自定義token添加到封裝的request請(qǐng)求上

但是現(xiàn)在因?yàn)楫惒?不知道怎么確定獲取到返回的自定義tocken才執(zhí)行封裝的request

app.js

wx.login({
                  success: function(res) {
                        if (res.code) {
                              const params = {
                                    appid: 'wx66e4ec7b580c2658',
                                    grant_type: 'authorization_code',
                                    js_code: res.code,
                                    secret: '8028563818513852479da62c3004afc2'
                              }
                              handleLogin.loginServer(params).then((res) => {
                                    console.log(res)
                                    wx.setStorageSync('access_token', res.data)
                                    wx.setStorageSync('login', 0)
                                    // wx.setStorageSync('access_token', 'aaa')
                              })
                        }
                        else{
                              showToast()
                        }
                  },
                  fail:function(err){
                        showToast()
                  }
            })

handleLogin.js

// 登錄
const app = getApp()

function loginServer(params) {
      let promise = new Promise(function(resolve, reject) {
            wx.request({
                  url: `http://3b58503d.ngrok.io/20180507/net-cosrun-project/web/v1/activity/login`,
                  data: params,
                  method: 'POST',
                  success: function(res) {
                        resolve(res);
                  },
                  fail:function(err){
                        reject(err);
                        wx.setStorageSync('login', 1)
                  }
            })
      })
      return promise
}

function showToast(content = '登錄失敗,請(qǐng)稍后再試') {
      wx.showToast({
            title: content,
            icon: 'none'
      })
}

module.exports = {
      loginServer: loginServer,
      showToast: showToast
}

封裝的請(qǐng)求promise.js

const app = getApp()
const handleLogin = require('./handleLogin.js');

let token = ''

function request(url, params, method) {
      const appid = 'miinno.com'
      const secret = '123456'
      const version = 'api/v1'
      const timestamp = new Date().getTime()
      const a = appid + 'APPID' + secret + 'SECRET' + timestamp
      const sign = sha1(appid + 'APPID' + secret + 'SECRET' + timestamp)
      token = sign + '.' + timestamp + '.' + version
      // console.log(wx.getStorageSync('access_token'))
      let promise = new Promise(function(resolve, reject) {
            if(wx.getStorageSync('login')=='0'){
                  wx.request({
                        url: url,
                        data: params,
                        header: {
                              'X-Token-With': token,
                              'Authorization': `Miinno ${wx.getStorageSync('access_token')}`
                        },
                        method: method || 'GET',
                        success: function (res) {
                              //     app.globalData.netWorkData = res.data
                              resolve(res);
                              if (res.data.hasOwnProperty('error')) {
                                    console.log(url)
                                    if (url != 'http://f63aca00.ngrok.io/20180507/net-cosrun-project/web/v1/vote/vote') {
                                          wx.showModal({
                                                title: '提示',
                                                content: res.data.error.message,
                                                success: function (res) {
                                                      if (res.confirm) {

                                                      } else if (res.cancel) { }
                                                }
                                          })
                                    }
                              }
                        }
                  })
            }
            else{
                  console.log('aa')
                  // handleLogin.login(() => {
                  // })
            }
      });
      return promise
}

求大神指教

回答
編輯回答
九年囚

小程序文檔檔中有說過吧,可以在 app.js 里設(shè)置一個(gè)狀態(tài)和一個(gè)全局回調(diào),別的頁面的初始化在此狀態(tài)完成之前都等待。

  • app.js 里包含 isReady 和回調(diào)。
  • index.js 初始化時(shí)檢查 app.isReady 的狀態(tài),看是現(xiàn)在立刻啟動(dòng)(.start())還是在回調(diào)之后再啟動(dòng)。
2018年5月29日 23:21