微信小程序 app onLaunch异步请求,在没有请求执行完就加载首页了的问题

程序加载需要先获取用户信息,然后保存到Storage中,然后首页去

Storage取信息,根据用户信息去查本地服务器数据列表,可是发现第一次进入的时候,数据总是加载不出来,只有再次进入才有数据。调试之后发现app.js onLaunch并没有先于onLoad 执行完再执行,而是onLoad先执行完,所以第一次进入的时候根本没有Storage,追了下原因是因为wx.login是需要用户授权登录,同时异步加载首页,所以就出现这问题,解决办法

1、加一个启动页,获取成功后,然后再跳转首页;

2、使用promise,判断进程状态,在index中去判断进程状态,再去执行页面的加载。

解释下Promise:

var promise = new Promise(function(resolve, reject) {
 if (/* 异步操作成功 */){
 resolve(value);
 } else {
 reject(error);
 }
});

promise.then(function(value) {
 // success
}, function(value) {
 // failure
});

       Promise 构造函数接受一个函数作为参数,该函数的两个参数分别是 resolve 方法和 reject 方法。

       如果异步操作成功,则用 resolve 方法将 Promise 对象的状态,从「未完成」变为「成功」(即从 pending 变为 resolved);

       如果异步操作失败,则用 reject 方法将 Promise 对象的状态,从「未完成」变为「失败」(即从 pending 变为 rejected)。

贴一下APP.js代码:

 
  1. //app.js

  2. var http = require('service/http.js')

  3. App({

  4.   onLaunch: function() {

  5.     //调用API从本地缓存中获取数据

  6.     // var that = this;

  7.   },

  8.   getAuthKey: function () {

  9.     var that = this;

  10.     return new Promise(function (resolve, reject) {

  11.         // 调用登录接口

  12.         wx.login({

  13.           success: function (res) {

  14.             if (res.code) {

  15.               that.globalData.code = res.code;

  16.               //调用登录接口

  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('获取用户登录态失败!' + res.errMsg);

  44.               var res = {

  45.                 status: 300,

  46.                 data: '错误'

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

猜你喜欢

转载自blog.csdn.net/qq_35860064/article/details/82590573