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

鍍金池/ 問答/HTML/ 小程序在login請求成功后請求其他的接口怎么實(shí)現(xiàn)

小程序在login請求成功后請求其他的接口怎么實(shí)現(xiàn)

handleLogin.js

// 登錄
function login(callback) {
      let promise = new Promise(function(resolve, reject) {
            wx.login({
                  success: function(res) {
                        if (res.code) {
                              const params = {
                                    appid: 'wx66e4ec7b580c2658',
                                    grant_type: 'authorization_code',
                                    js_code: res.code,
                                    secret: '8028563818513852479da62c3004afc2'
                              }
                              loginServer(params).then((res) => {
                                    console.log(res)
                                    wx.setStorageSync('access_token', res.data)
                                    console.log(res.data)
                                    wx.setStorageSync('login', 0)
                                    resolve(res)
                                    // wx.setStorageSync('access_token', 'aaa')
                              })
                        } else {
                              showToast()
                        }
                  },
                  fail() {
                        showToast()
                  }
            });
            return promise
      });
}

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

                  }
            })
      })
      return promise
}

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

module.exports = {
      login: login,
}

封裝的請求是這樣的

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

handleLogin.login().then({
      request()
})

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) {
         
                  wx.request({
                        url: url,
                        data: params,
                        header: {
                              'X-Token-With': token,
                              'Authorization': 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) { }
                                                }
                                          })
                                    }
                              }
                        }
                  })
          
      });
      return promise
}

怎么寫才能實(shí)現(xiàn)login 完成之后才能請求其他接口

回答
編輯回答
憶當(dāng)年

看你這用的是promise,這是可以鏈?zhǔn)秸{(diào)用的

handleLogin.login().then({
   return request(參數(shù))
}).then((res) => {
    console.log(res,'拿到請求的值')
})
2017年9月25日 12:54