小程序 使用promise 操作 異步api


npm install --save miniprogram-api-promise

  getPhoneNumber: function (e) {
    console.log(e)
    var ivObj = e.detail.iv
    var telObj = e.detail.encryptedData
    var codeObj = "";
    var that = this;
    //執行login
    wx.login({
      success: res => {
        console.log('獲取code成功');
        wx.request({
          url: 'https://xxxxxxxx.com/xcx',
          data: {
            code: res.code,
            iv: ivObj,
            encryptedData: telObj
          },
          // 成功的回調
          success: res => {
            console.log(res);
            // 把信息存儲到變量
            this.setData({
              userPhoneNum: res.data,
              isShow: true
            })
          }
        })
      }
    });
  }

  用了之后

  getPhoneNumber1: function (e) {
    // console.log(e)
    var ivObj = e.detail.iv
    var telObj = e.detail.encryptedData
    var codeObj = "";
    var that = this;

    //第一步login請求
    wxp.login()
      //第二步得到code之后 請求后台接口
      .then(res => {
      //返回一個promise
       return wxp.request({
          url: 'https://xxxxxxx.com/xcx',
          data: {
            code: res.code,
            iv: ivObj,
            encryptedData: telObj
          }
        })
      })
      //第三步 得出結果
      .then(res => {
        //獲取結果 賦值
        this.setData({
          userPhoneNum: res.data.data,
          isShow: true
        })
      });
  }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM