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

app.js onLaunch 的代码

 "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const http = require('./utils/http.js');
const api = require('./config.js');
const updateManager = wx.getUpdateManager(); App({
onLaunch: function() {
updateManager.onCheckForUpdate(function(res) {
// 请求完新版本信息的回调
console.log('请求完新版本信息的回调');
console.log(res.hasUpdate)
}) updateManager.onUpdateReady(function() {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
}) updateManager.onUpdateFailed(function() {
// 新版本下载失败
wx.showModal({
title: '提示',
content: '新版本下载失败',
showCancel: false
})
})
var _this = this;
_this.GetSystemInfo();
// _this.toLogin(); },
globalData: {
screenWidth: 0, //屏幕宽度
screenHeight: 0, //屏幕高度
fontSize: 14, //字体大小
openid: '',
phone: '',
shopid: '', //没有openid 退出
logined: false, //是否已经获取了手机号
nickname: '', //昵称
photo: '', //头像
editJobStorageKey: 'edit-job-storage', //编辑职能(角色)的缓存
isbindrole: false, //是否已经绑定了职能(角色)
rolenumber: '', //职能编号
rolename: '', //职能名称
shopname: '',
func_modular: '', //功能模块权限
branchListStorageKey: 'branch-list-storage', //门店列表
branchCityListStorageKey: 'branch-city-list-storage', //门店城市列表
auth_num: 0, //可授权使用人数
productname: '', //线下产品名称
adminid: 0, //管理员或者店员的id
},
GetSystemInfo: function() {
var _this = this;
const info = wx.getSystemInfoSync();
_this.globalData.screenWidth = info.screenWidth;
_this.globalData.screenHeight = info.screenHeight;
_this.globalData.fontSize = info.fontSizeSetting;
}, //首次登录 不存在shopid
toLogin: function() {
console.log('启动页');
var _this = this;
return new Promise(function(resolve, reject) {
wx.login({
success: function(res) {
var code = res.code;
console.log(res);
var postData = {
code: code,
shopid: _this.globalData.shopid
};
wx.showLoading({
title: '登录中...',
})
http.httpPost(api.Login, postData, function(result) {
console.log(result);
wx.hideLoading();
if (result.success) {
if (result.result.success) {
_this.globalData.openid = result.result.data.openid;
if (result.result.data.phone) {
_this.globalData.phone = result.result.data.phone;
}
if (result.result.data.photo) {
_this.globalData.photo = result.result.data.photo;
}
if (result.result.data.nickname) {
_this.globalData.nickname = result.result.data.nickname;
} //存在多个商户号时
if (result.result.data.shopidlist && result.result.data.shopidlist.length > 0) {
//只返回一个时,查询当前用户绑定角色职能关系
if (result.result.data.shopidlist.length == 1) {
_this.globalData.shopid = result.result.data.shopidlist[0];
_this.toGetBranchList();
_this.toGetUserRole();
_this.toGetShopInfo();
} else {
// 存在多个商户号时,跳转到选择商户页面
wx.redirectTo({
url: '../../pages/shoplist/shoplist',
})
}
} else {
_this.globalData.isbindrole = false;
_this.globalData.rolenumber = '';
_this.globalData.rolename = '';
} resolve(result); } else {
wx.showModal({
title: '提示',
content: result.success.message,
showCancel: false
})
reject('error');
}
} else {
wx.showModal({
title: '提示',
content: result.error.message,
showCancel: false
})
reject('error');
}
})
}
}); }); },
//获取门店列表
toGetBranchList: function() {
var _this = this;
var postData = {
'shopid': _this.globalData.shopid,
'from': 'wxapp',
'openid': _this.globalData.openid
};
http.httpPost(api.ObtainBranchList, postData, (res) => { if (res.success) {
var _result = res.result;
if (_result.success) {
var branchList = _result.data;
var branchNameList = ['全部门店'];
for (var i = 0; i < branchList.length; i++) {
branchNameList.push(branchList[i].branch_name);
} wx.setStorageSync(_this.globalData.branchListStorageKey, branchList);
wx.setStorageSync(_this.globalData.branchCityListStorageKey, branchNameList) } else {
wx.showModal({
title: '提示',
content: _result.message,
showCancel: false
})
}
} else {
wx.showModal({
title: '提示',
content: res.error.message,
showCancel: false
})
}
});
},
toGetShopInfo: function() {
var _this = this;
var postData = {
'shopid': _this.globalData.shopid
};
http.httpPost(api.GetShopInfo, postData, (res) => { if (res.success) {
var _result = res.result;
if (_result.success) {
_this.globalData.shopname = _result.data.shopname;
_this.globalData.productname = _result.data.product;
_this.globalData.auth_num = _result.data.auth_num; } else {
wx.showModal({
title: '提示',
content: _result.message,
showCancel: false
})
}
} else {
wx.showModal({
title: '提示',
content: res.error.message,
showCancel: false
})
} });
}, //获取用户的绑定职能角色
toGetUserRole: function() {
var _this = this;
var postData = {
'shopid': _this.globalData.shopid,
'openid': _this.globalData.openid
};
http.httpPost(api.ObtainUserRole, postData, function(res) {
console.log(res);
if (res.success) {
var _result = res.result;
if (_result.success) {
_this.globalData.isbindrole = true;
_this.globalData.rolenumber = _result.data.rolenumber;
_this.globalData.rolename = _result.data.rolename;
_this.globalData.adminid = _result.data.adminid;
if (_result.data.rolenumber != 'admin') {
_this.globalData.func_modular = _result.data.func_list
}
} else {
_this.globalData.isbindrole = false;
_this.globalData.rolenumber = '';
_this.globalData.rolename = '';
wx.showModal({
title: '提示',
content: _result.message,
showCancel: false
})
}
} else {
wx.showModal({
title: '提示',
content: res.error.message,
showCancel: false
})
}
});
}
});

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

  onLoad: function() {

     var _this = this;
app.toLogin().then(function(res) {
console.log('登录后');
console.log(res);
if (app.globalData.shopid) {
_this.toGetKanBanData();
_this.toGetSaleDetail();
_this.toGuestOrderDetail();
}
}); },

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

  1. 微信小程序wx.login先执行onLaunch与onLoad加载顺序问题

    @ 目录 遇到问题 请求api返回需要先登录,实际上登录已成功 问题分析 解决问题 自定义回调函数 app.js index.js 扩展提问 学习交流 随机数字随机幸运数+ My Blog 技术交流 ...

  2. springboot+layui实现PC端用户的增删改查 & 整合mui实现app端的自动登录和用户的上拉加载 & HBuilder打包app并在手机端下载安装

    springboot整合web开发的各个组件在前面已经有详细的介绍,下面是用springboot整合layui实现了基本的增删改查. 同时在学习mui开发app,也就用mui实现了一个简单的自动登录和 ...

  3. Ext JS学习第十天 Ext基础之动态加载JS文件(补充)

    此文用来记录学习笔记: •Ext4.x版本提供的一大亮点就是Ext.Loader这个类的动态加载机制!只要遵循路径规范,即可动态加载js文件,方便把自己扩展组件动态加载进来,并且减轻浏览器的压力. • ...

  4. 记一次解决cmd中执行java提示"找不到或无法加载主类"的问题

    今天遇到一个问题:在cmd命令行中,用javac编译java文件可以成功,但是用java执行却提示“找不到或无法加载主类”.现将该问题的原因以及解决办法记录一下. 先理解一下系统变量path和clas ...

  5. win8和win7下解决php5.3和5.4、5.5等不能加载php_curl.dll的终极解决办法 收藏

    win8和win7下解决php5.3和5.4.5.5等不能加载php_curl.dll的终极解决办法 收藏2015年01月11日 最近分别在WIN7和Windows8 上分别安装php 高版本!都遇到 ...

  6. H5页面基于iScroll.js插件实现下拉刷新,上拉加载更多

    前言 在我之前的项目中,页面总是干巴巴的,用户的体验不是特别完美,我也是一直觉得把设计师给到的psd做出来就好,很少考虑用户的感受.我喜欢看不同的App,操作每个步骤,观赏每个能和我互动的交互设计效果 ...

  7. js同步、异步、延时、无阻塞加载

    一.同步加载 平常默认用的都是同步加载.如:<script src="http://yourdomain.com/script.js"></script> ...

  8. 使用js主函数的原因是等文档加载完了才给里面的元素添加东西 如果不使用主函数则文档加载时候无法找到元素则不能成功给元素添加事件

    使用js主函数的原因是等文档加载完了才给里面的元素添加东西 如果不使用主函数则文档加载时候无法找到元素则不能成功给元素添加事件

  9. js不需要知道图片宽高的懒加载方法(经过实际测试,不加宽高仍然是无法正常加载的,设置height:auto,height:100%,仍然显示高度为0)

    js不需要知道图片宽高的懒加载方法 懒加载是如何实现的? - 简书https://www.jianshu.com/p/e86c61468285找到一个不需要知道图片宽高的懒加载方法了(经过实际测试,不 ...

随机推荐

  1. sql的存储过程实例--循环动态创建表

    创建一个存储过程,动态添加100张track表表名track_0 ~~ track_99注:sql的拼接只能用 CONCAT()函数 -- 创建一个存储过程 CREATE PROCEDURE crea ...

  2. [Golang] GOROOT、GOPATH和Project目录说明

    go env环境查看 用go env 可查看当前go环境变量. $ go env GOARCH="amd64" GOBIN="" GOEXE="&qu ...

  3. Spring IOC 源码之ResourceLoader

    转载自http://www.blogjava.net/DLevin/archive/2012/12/01/392337.html 在<深入Spring IOC源码之Resource>中已经 ...

  4. elasticSearch6源码分析(9)ActionModule

    1.ActionModule概述 /** * Builds and binds the generic action map, all {@link TransportAction}s, and {@ ...

  5. 基于vue实现一个简单的MVVM框架(源码分析)

    不知不觉接触前端的时间已经过去半年了,越来越发觉对知识的学习不应该只停留在会用的层面,这在我学jQuery的一段时间后便有这样的体会. 虽然jQuery只是一个JS的代码库,只要会一些JS的基本操作学 ...

  6. ConcurrentHashMap 扩容分析拾遗

    前言 这是一篇对 transfer 方法的拾遗,关于之前那篇文章的一些一笔带过,或者当时不知道的地方进行回顾. 疑点 1. 为什么将链表拆成两份的时候,0 在低位,1 在高位? 回顾一下 transf ...

  7. 并发编程之 Semaphore 源码分析

    前言 并发 JUC 包提供了很多工具类,比如之前说的 CountDownLatch,CyclicBarrier ,今天说说这个 Semaphore--信号量,关于他的使用请查看往期文章并发编程之 线程 ...

  8. webpack3新特性简介

    6月20号webpack推出了3.0版本,官方也发布了公告.根据公告介绍,webpack团队将未来版本的改动聚焦在社区提出的功能需求,同时将保持一个快速.稳定的发布节奏.本文主要依据公告内容,简单介绍 ...

  9. hive 中的正则表达式

    背景: 前几天拿来apache日志,用hive的正则进行匹配,发现匹配出来的字段算是NULL,但是我用RegexBuddy工具显示能够匹配的到啊!例子如下(我拿正常的apache日志来比较,我的apa ...

  10. sqlserver--install/uninstall

    2017 express版本 安装: https://jingyan.baidu.com/article/76a7e409077997fc3a6e1559.html https://www.cnblo ...