使用 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. HashMap:JDK7 与 JDK8 的实现

    JDK7中的HashMap HashMap底层维护一个数组,数组中的每一项都是一个Entry: transient Entry<K,V>[] table; 我们向在HashMap 中存放的 ...

  2. ELK批量删除索引 及 相关操作命令 - 运维小结

    线上部署了ELK+Redis日志分析平台环境, 随着各类日志数据源源不断的收集, 发现过了一段时间之后, ELK查看会原来越慢, 重启elasticsearch服务器节点之前同步时间也会很长,  这是 ...

  3. gitlab之gitlab-ci和gitlab-runner<一>

    一.概述 gitlab是开源代码托管软件,有ce和ee两种版本,一般情况下ce完全能满足企业使用,如果不差钱可以使用ee版本,这里使用的是ce版.之前也一直在做CI/CD,最开始采用gitlab+we ...

  4. JVM学习记录-类加载器

    前言 JVM设计团队把类加载阶段中的“通过一个类的全限定名来获取描述此类的二进制字节流”这个动作放到Java虚拟机外面去实现,以便让应用程序自己决定如何去获取所需要的类.实现这个动作的代码模块称为“类 ...

  5. Redis发布订阅机制

    1. 什么是Redis Redis是一个开源的内存数据库,它以键值对的形式存储数据.由于数据存储在内存中,因此Redis的速度很快,但是每次重启Redis服务时,其中的数据也会丢失,因此,Redis也 ...

  6. spring@value取不到值的几种情况

    一,spring组件重写构造方法,在构造方法中引用@value为null 由于spring实例化顺序为先执行构造方法,再注入成员变量,所以序为先执行构造方法,再注入成员变量,所以ing实例化顺取值为n ...

  7. HTML 【表单】

    表单在网页中主要负责数据采集功能. 一个表单有三个基本组成部分: 表单标签. 表单域.表单按钮. 表单标签 < form  action = " "  method = &q ...

  8. 关于 luv_letters

    luv_letters是一枚现高二文化课菜鸡,是一枚临汾一中联赛oier(我太菜了),也是一名Cher. (好像没有好的地方 真名不提跟某个当红女明星一样(正经汉子不女装 话说当初选择竞赛科目的时候( ...

  9. BestCoder Round #27

    Jump and Jump... Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. 悟空模式-java-原型模式

    [却说那妖精与大圣斗经半日,不分胜败.行者把棒丢起,叫一声“变!”就以一变十,以十变百,以百变千,半天里,好似蛇游蟒搅,乱打妖邪.妖邪慌了手脚,将身一闪,化道清风,即奔碧空之上逃走.行者念声咒语,将铁 ...