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 }) }); }