The solution of applet onLaunch and onload asynchronous request

  1. //app.js  
  2. var http = require('service/http.js')  
  3. App({  
  4.   onLaunch: function() {  
  5.     //Call API to get data from local cache  
  6.     // var that = this;  
  7.   },  
  8.   getAuthKey: function () {  
  9.     var that = this;  
  10.     returnnew Promise(function (resolve, reject) {   
  11.         // call the login interface  
  12.         wx.login({  
  13.           success: function (res) {  
  14.             if (res.code) {  
  15.               that.globalData.code = res.code;  
  16.               //call the login interface  
  17.               wx.getUserInfo({  
  18.                 withCredentials: true,  
  19.                 success: function (res) {  
  20.                   that.globalData.UserRes = res;  
  21.                   that.globalData.userInfo = res.userInfo;  
  22.                   that.func.postReq('/api/v1/image/oauth', {  
  23.                     code: that.globalData.code,  
  24.                     signature: that.globalData.UserRes.signature,  
  25.                     encryptedData: that.globalData.UserRes.encryptedData,  
  26.                     rawData: that.globalData.UserRes.rawData,  
  27.                     iv: that.globalData.UserRes.iv  
  28.                   }, function (res) {  
  29.                     wx.setStorage({  
  30.                       key: "auth_key",  
  31.                       data: res.data.auth_key  
  32.                     })  
  33.                     var  res = {  
  34.                       status: 200,  
  35.                       data: res.data.auth_key  
  36.                     }  
  37.                     resolve(res);  
  38.                       
  39.                   })  
  40.                 }  
  41.               })  
  42.             } else {  
  43.               console.log( 'Failed to get user login status!'  + res.errMsg);  
  44.               var  res = {  
  45.                 status: 300,  
  46.                 data:  'error'  
  47.               }  
  48.               reject('error');  
  49.             }    
  50.           }  
  51.         })  
  52.     });  
  53.   },  
  54. })



    1. //index.js  
    2.   onLoad: function () {  
    3.     app.getAuthKey().then(function (res) {  
    4.       console.log(res);  
    5.       if (res.status == 200){  
    6.         var  auth_key = res.data;  
    7.         app.func.req('/api/v1/image/theme-list', {  
    8.           page: 1,  
    9.           auth_key: auth_key  
    10.         }, function (res) {  
    11.           var page = that.data.pageValue + 1;  
    12.           that.setData({  
    13.             images: res.data,  
    14.             pageValue: page  
    15.           });  
    16.         });  
    17.       }else{  
    18.         console.log(res.data);  
    19.       }  
    20.     });  
      

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979794&siteId=291194637