zoukankan      html  css  js  c++  java
  • 微信小程序里解决app.js onLaunch事件与小程序页面的onLoad加载前后异常问题

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

    小程序页面的代码 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
  • 相关阅读:
    Linux 下编译Android-VLC开源播放器详解(附源码下载)
    VC/Wince 实现仿Win8 Metro风格界面2——页面滑动切换(附效果图)
    Android Launcher分析和修改3——Launcher启动和初始化
    VC/Wince 实现仿Win8 Metro风格界面1——设计概述和自绘Button(附效果图)
    Android Launcher分析和修改2——Icon修改、界面布局调整、壁纸设置
    Android Launcher分析和修改1——Launcher默认界面配置(default_workspace)
    WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 8
    编程工具系列之二------使用GDB的源代码查看功能
    编程工具系列之一------使用GDB的堆栈跟踪功能
    unable to open sync connection
  • 原文地址:https://www.cnblogs.com/WQ1992/p/10438325.html
Copyright © 2011-2022 走看看