小程序Promise函数封装

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()
        }
      })
    }
  },

发布了151 篇原创文章 · 获赞 1 · 访问量 2794

猜你喜欢

转载自blog.csdn.net/hql1024/article/details/103537098
今日推荐