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

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

1 "use strict"; 2 Object.defineProperty(exports, "__esModule", { 3value: true 4 }); 5 const http = require('./utils/http.js'); 6 const api = require('./config.js'); 7 const updateManager = wx.getUpdateManager(); 8 9 App({ 10onLaunch: function() { 11updateManager.onCheckForUpdate(function(res) { 12// 请求完新版本信息的回调 13console.log('请求完新版本信息的回调'); 14console.log(res.hasUpdate) 15}) 16 17updateManager.onUpdateReady(function() { 18wx.showModal({ 19title: '更新提示', 20content: '新版本已经准备好,是否重启应用?', 21success(res) { 22if (res.confirm) { 23// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 24updateManager.applyUpdate() 25} 26} 27}) 28}) 29 30updateManager.onUpdateFailed(function() { 31// 新版本下载失败 32wx.showModal({ 33title: '提示', 34content: '新版本下载失败', 35showCancel: false 36}) 37}) 38var _this = this; 39_this.GetSystemInfo(); 40// _this.toLogin(); 41 42 43}, 44globalData: { 45screenWidth: 0, //屏幕宽度 46screenHeight: 0, //屏幕高度 47fontSize: 14, //字体大小 48openid: '', 49phone: '', 50shopid: '', //没有openid 退出 51logined: false, //是否已经获取了手机号 52nickname: '', //昵称 53photo: '', //头像 54editJobStorageKey: 'edit-job-storage', //编辑职能(角色)的缓存 55isbindrole: false, //是否已经绑定了职能(角色) 56rolenumber: '', //职能编号 57rolename: '', //职能名称 58shopname: '', 59func_modular: '', //功能模块权限 60branchListStorageKey: 'branch-list-storage', //门店列表 61branchCityListStorageKey: 'branch-city-list-storage', //门店城市列表 62auth_num: 0, //可授权使用人数 63productname: '', //线下产品名称 64adminid: 0, //管理员或者店员的id 65}, 66GetSystemInfo: function() { 67var _this = this; 68const info = wx.getSystemInfoSync(); 69_this.globalData.screenWidth = info.screenWidth; 70_this.globalData.screenHeight = info.screenHeight; 71_this.globalData.fontSize = info.fontSizeSetting; 72}, 73 74 75 76//首次登录不存在shopid 77toLogin: function() { 78console.log('启动页'); 79var _this = this; 80return new Promise(function(resolve, reject) { 81wx.login({ 82success: function(res) { 83var code = res.code; 84console.log(res); 85var postData = https://www.it610.com/article/{ 86code: code, 87shopid: _this.globalData.shopid 88}; 89wx.showLoading({ 90title:'登录中...', 91}) 92http.httpPost(api.Login, postData, function(result) { 93console.log(result); 94wx.hideLoading(); 95if (result.success) { 96if (result.result.success) { 97_this.globalData.openid = result.result.data.openid; 98if (result.result.data.phone) { 99_this.globalData.phone = result.result.data.phone; 100} 101if (result.result.data.photo) { 102_this.globalData.photo = result.result.data.photo; 103} 104if (result.result.data.nickname) { 105_this.globalData.nickname = result.result.data.nickname; 106} 107 108//存在多个商户号时 109if (result.result.data.shopidlist && result.result.data.shopidlist.length > 0) { 110//只返回一个时,查询当前用户绑定角色职能关系 111if (result.result.data.shopidlist.length == 1) { 112_this.globalData.shopid = result.result.data.shopidlist[0]; 113_this.toGetBranchList(); 114_this.toGetUserRole(); 115_this.toGetShopInfo(); 116} else { 117// 存在多个商户号时,跳转到选择商户页面 118wx.redirectTo({ 119url: '../../pages/shoplist/shoplist', 120}) 121} 122} else { 123_this.globalData.isbindrole = false; 124_this.globalData.rolenumber = ''; 125_this.globalData.rolename = ''; 126} 127 128resolve(result); 129 130} else { 131wx.showModal({ 132title: '提示', 133content: result.success.message, 134showCancel: false 135}) 136reject('error'); 137} 138} else { 139wx.showModal({ 140title: '提示', 141content: result.error.message, 142showCancel: false 143}) 144reject('error'); 145} 146}) 147} 148}); 149 150}); 151 152}, 153//获取门店列表 154toGetBranchList: function() { 155var _this = this; 156var postData = https://www.it610.com/article/{ 157'shopid': _this.globalData.shopid, 158'from': 'wxapp', 159'openid': _this.globalData.openid 160}; 161http.httpPost(api.ObtainBranchList, postData, (res) => { 162 163if (res.success) { 164var _result = res.result; 165if (_result.success) { 166var branchList = _result.data; 167var branchNameList = ['全部门店']; 168for (var i = 0; i < branchList.length; i++) { 169branchNameList.push(branchList[i].branch_name); 170} 171 172wx.setStorageSync(_this.globalData.branchListStorageKey, branchList); 173wx.setStorageSync(_this.globalData.branchCityListStorageKey, branchNameList) 174 175} else { 176wx.showModal({ 177title: '提示', 178content: _result.message, 179showCancel: false 180}) 181} 182} else { 183wx.showModal({ 184title: '提示', 185content: res.error.message, 186showCancel: false 187}) 188} 189}); 190}, 191toGetShopInfo: function() { 192var _this = this; 193var postData = https://www.it610.com/article/{ 194'shopid': _this.globalData.shopid 195}; 196http.httpPost(api.GetShopInfo, postData, (res) => { 197 198if (res.success) { 199var _result = res.result; 200if (_result.success) { 201_this.globalData.shopname = _result.data.shopname; 202_this.globalData.productname = _result.data.product; 203_this.globalData.auth_num = _result.data.auth_num; 204 205} else { 206wx.showModal({ 207title: '提示', 208content: _result.message, 209showCancel: false 210}) 211} 212} else { 213wx.showModal({ 214title: '提示', 215content: res.error.message, 216showCancel: false 217}) 218} 219 220}); 221}, 222 223//获取用户的绑定职能角色 224toGetUserRole: function() { 225var _this = this; 226var postData = https://www.it610.com/article/{ 227'shopid': _this.globalData.shopid, 228'openid': _this.globalData.openid 229}; 230http.httpPost(api.ObtainUserRole, postData, function(res) { 231console.log(res); 232if (res.success) { 233var _result = res.result; 234if (_result.success) { 235_this.globalData.isbindrole = true; 236_this.globalData.rolenumber = _result.data.rolenumber; 237_this.globalData.rolename = _result.data.rolename; 238_this.globalData.adminid = _result.data.adminid; 239if (_result.data.rolenumber != 'admin') { 240_this.globalData.func_modular = _result.data.func_list 241} 242} else { 243_this.globalData.isbindrole = false; 244_this.globalData.rolenumber = ''; 245_this.globalData.rolename = ''; 246wx.showModal({ 247title: '提示', 248content: _result.message, 249showCancel: false 250}) 251} 252} else { 253wx.showModal({ 254title: '提示', 255content: res.error.message, 256showCancel: false 257}) 258} 259}); 260} 261 });


小程序页面的代码 onlaod的事件得写在 Promise 的then方法里
微信小程序里解决app.js|微信小程序里解决app.js onLaunch事件与小程序页面的onLoad加载前后异常问题
文章图片
微信小程序里解决app.js|微信小程序里解决app.js onLaunch事件与小程序页面的onLoad加载前后异常问题
文章图片
1onLoad: function() { 2 3var _this = this; 4app.toLogin().then(function(res) { 5console.log('登录后'); 6console.log(res); 7if (app.globalData.shopid) { 8_this.toGetKanBanData(); 9_this.toGetSaleDetail(); 10_this.toGuestOrderDetail(); 11} 12}); 13 14},

View Code
【微信小程序里解决app.js|微信小程序里解决app.js onLaunch事件与小程序页面的onLoad加载前后异常问题】转载于:https://www.cnblogs.com/WQ1992/p/10438325.html

    推荐阅读