小程序Promise函数封装

烈酒焚心 提交于 2019-12-15 01:06:09

app.js里获取pages需要拿的参,使用promise处理

  getlogin() {
    var that = this
    return new Promise(function (resolve, reject) {
      wx.login({
        success: (res) => {
          wx.request({
            url: url,
            data: {},
            method: 'POST',
            success: (res) => {
              resolve(res)
            },
          });
        }
      })
    })
  },

page.js里执行,因为每次都会去跑一遍promise导致接口又请求了一次,
所以判断让它没有的这个参的时候再执行

const app = getApp()
  onLoad: function (options) {
    if (wx.getStorageSync('sign') != '') {
      this.getexpert()
      this.getrecom()
    } else {
      app.getlogin().then(res => {
        if (res.data.status == 1) {
          this.getexpert()
          this.getrecom()
        }
      })
    }
  },

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!