使用 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. GOROOT、GOPATH和project目录说明

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

  2. python服务器环境搭建Flask,uwsgi和nginx

    python服务器环境搭建Flask,uwsgi和nginx 环境配置 服务器配置  [部署系统环境Ubuntu] 使用python的Flask框架搭建好网页后台后,便要开始将网站部署到服务器平台了. ...

  3. ActiveMQ P2P版的HelloWorld

    1.2 JMS应用程序接口 ConnectionFactory: 用户用来创建到JMS提供者的连接的被管对象.JMS客户通过可移植的接口访问连接,这样当下层的实现改变时,代码不需要进行修改. 管理员 ...

  4. Git Windows客户端保存用户名和密码

    解决Git Windows客户端保存用户名和密码的方法,至于为什么,就不想说了. 1. 添加一个HOME环境变量,值为%USERPROFILE% 2. 开始菜单中,点击“运行”,输入“%Home%”并 ...

  5. 并发编程之 AQS 源码剖析

    前言 JDK 1.5 的 java.util.concurrent.locks 包中都是锁,其中有一个抽象类 AbstractQueuedSynchronizer (抽象队列同步器),也就是 AQS, ...

  6. C# 两个独立exe程序直接通信

    从别的地方转载过来,转载地址不详细,需要知道的话,可以自动去搜索,我不是原作者. 我之前主要是用工序内存做过两个进程的通信. 两个独立的exe程序之间如何完成通信呢?首先想到的办法是利用生成文件的方法 ...

  7. 了解java虚拟机—串行回收器(6)

    串行回收器 串行回收器只有一个工作线程,串行回收器可以在新生代和老年代使用,根据作用于不同的堆和空间,分为新生代串行回收器和老年代串行回收器. 1.新生代串行回收器 串行收集器是所有垃圾回收器中最古老 ...

  8. Java static和final

    java提高篇(七)-----关键字static static 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中static表示“全局”或者“静 ...

  9. python学习之老男孩python全栈第九期_day015作业_老男孩Python全9期练习题(面试真题模拟)

    一. 选择题(32分) 1. python不支持的数据类型有:AA. charB. intC. floatD. list 2. Ex = ‘foo’y = 2print(x + y)A. fooB. ...

  10. 从零开始学习html(十四)单位和值

    一.颜色值 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <tit ...