微信小程序里解决app.js onLaunch事件与小程序页面的onLoad加载前后异常问题

时间:2019-02-26 17:43:10   收藏:0   阅读:2050

使用 Promise 解决小程序页面因为需要app.js onLaunch 参数导致的请求失败

app.js onLaunch 的代码

技术图片
  1 "use strict";
  2 Object.defineProperty(exports, "__esModule", {
  3   value: true
  4 });
  5 const http = require(‘./utils/http.js‘);
  6 const api = require(‘./config.js‘);
  7 App({
  8   onLaunch: function() {
  9     var _this = this;
 10     _this.GetSystemInfo();
 11     if (!_this.globalData.openid) {
 12       _this.toLogin();
 13     }
 14 
 15 
 16   },
 17   globalData: {
 18     screenWidth: 0, //屏幕宽度
 19     screenHeight: 0, //屏幕高度
 20     fontSize: 14, //字体大小
 21     openid: ‘‘,
 22     phone: ‘‘,
 23     shopid: ‘‘, //没有openid 退出
 24     logined: false, //是否已经获取了手机号
 25     nickname: ‘‘, //昵称
 26     photo: ‘‘, //头像
 27     editJobStorageKey: ‘edit-job-storage‘, //编辑职能(角色)的缓存
 28     isbindrole: false, //是否已经绑定了职能(角色)
 29     rolenumber: ‘‘, //职能编号
 30     rolename: ‘‘, //职能名称
 31     shopname: ‘‘,
 32     branchListStorageKey: ‘branch-list-storage‘, //门店列表
 33     branchCityListStorageKey: ‘branch-city-list-storage‘, //门店城市列表
 34     auth_num: 0, //可授权使用人数
 35     productname: ‘‘, //线下产品名称
 36   },
 37   GetSystemInfo: function() {
 38     var _this = this;
 39     const info = wx.getSystemInfoSync();
 40     _this.globalData.screenWidth = info.screenWidth;
 41     _this.globalData.screenHeight = info.screenHeight;
 42     _this.globalData.fontSize = info.fontSizeSetting;
 43   },
 44 
 45 
 46 
 47   //首次登录  不存在shopid
 48   toLogin: function() {
 49     console.log(‘启动页LOGIN‘);
 50     var _this = this;
 51     return new Promise(function(resolve, reject) {
 52       wx.login({
 53         success: function(res) {
 54           var code = res.code;
 55           console.log(res);
 56           var postData = {
 57             code: code,
 58             shopid: _this.globalData.shopid
 59           };
 60           wx.showLoading({
 61             title: ‘登录中...‘,
 62           })
 63           http.httpPost(api.Login, postData, function(result) {
 64             console.log(result);
 65             wx.hideLoading();
 66             if (result.success) {
 67               if (result.result.success) {
 68                 _this.globalData.openid = result.result.data.openid;
 69                 if (result.result.data.phone) {
 70                   _this.globalData.phone = result.result.data.phone;
 71                 }
 72                 if (result.result.data.photo) {
 73                   _this.globalData.photo = result.result.data.photo;
 74                 }
 75                 if (result.result.data.nickname) {
 76                   _this.globalData.nickname = result.result.data.nickname;
 77                 }
 78 
 79                 //存在多个商户号时
 80                 if (result.result.data.shopidlist && result.result.data.shopidlist.length > 0) {
 81                   //只返回一个时,查询当前用户绑定角色职能关系
 82                   if (result.result.data.shopidlist.length == 1) {
 83                     _this.globalData.shopid = result.result.data.shopidlist[0];
 84                     _this.toGetUserRole();
 85                     _this.toGetShopInfo();
 86                   } else {
 87                     //存在多个商户号时,跳转到选择商户页面
 88                     wx.redirectTo({
 89                       url: ‘../../pages/shoplist/shoplist‘,
 90                     })
 91                   }
 92                 } else {
 93                   _this.globalData.isbindrole = false;
 94                   _this.globalData.rolenumber = ‘‘;
 95                   _this.globalData.rolename = ‘‘;
 96                 }
 97 
 98                 resolve(result);
 99 
100               } else {
101                 wx.showModal({
102                   title: ‘提示‘,
103                   content: result.success.message,
104                 })
105                 reject(‘error‘);
106               }
107             } else {
108               wx.showModal({
109                 title: ‘提示‘,
110                 content: ‘登录失败:‘ + result.error.message,
111               })
112               reject(‘error‘);
113             }
114           })
115         }
116       });
117 
118     });
119 
120   },
121 
122   toGetShopInfo: function() {
123     var _this = this;
124     var postData = {
125       ‘shopid‘: _this.globalData.shopid
126     };
127     http.httpPost(api.GetShopInfo, postData, (res) => {
128       console.log(res);
129       if (res.success) {
130         var _result = res.result;
131         if (_result.success) {
132           _this.globalData.shopname = _result.data.shopname;
133           _this.globalData.productname = _result.data.product;
134           _this.globalData.auth_num = _result.data.auth_num;
135 
136         } else {
137           wx.showModal({
138             title: ‘提示‘,
139             content: ‘‘,
140           })
141         }
142       } else {
143         wx.showModal({
144           title: ‘提示‘,
145           content: res.error,
146           showCancel: false
147         })
148       }
149 
150     });
151   },
152 
153   //获取用户的绑定职能角色
154   toGetUserRole: function() {
155     var _this = this;
156     var postData = {
157       ‘shopid‘: _this.globalData.shopid,
158       ‘openid‘: _this.globalData.openid
159     };
160     http.httpPost(api.ObtainUserRole, postData, function(res) {
161       console.log(res);
162       if (res.success) {
163         var _result = res.result;
164         if (_result.success) {
165           _this.globalData.isbindrole = true;
166           _this.globalData.rolenumber = _result.data.rolenumber;
167           _this.globalData.rolename = _result.data.rolename;
168         } else {
169           _this.globalData.isbindrole = false;
170           _this.globalData.rolenumber = ‘‘;
171           _this.globalData.rolename = ‘‘;
172           wx.showModal({
173             title: ‘提示‘,
174             content: _result.message,
175             showCancel: false
176           })
177         }
178       } else {
179         wx.showModal({
180           title: ‘提示‘,
181           content: res.error,
182           showCancel: false
183         })
184       }
185     });
186   }
187 });
View Code

小程序页面的代码 onlaod的事件得写在 Promise 的then方法里

技术图片
 1  onLoad: function() {
 2 
 3     var _this = this;
 4     app.toLogin().then(function(res) {
 5       console.log(‘登录后‘);
 6       console.log(res);
 7       if (app.globalData.shopid) {
 8         _this.toGetKanBanData();
 9         _this.toGetSaleDetail();
10         _this.toGuestOrderDetail();
11       }
12     });
13 
14   },
View Code

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!