使用 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. 数据序列化导读(3)[JSON v.s. YAML]

    前面两节介绍了JSON和YAML,本文则对下面的文章做一个中英文对照翻译. Comparison between JSON and YAML for data serialization用于数据序列化 ...

  2. 【IT笔试面试题整理】二叉树中和为某一值的路径--所有可能路径

    [试题描述] You are given a binary tree in which each node contains a value. Design an algorithm to print ...

  3. 海量数据处理之Tire树(字典树)

    参考博文:http://blog.csdn.net/v_july_v/article/details/6897097 第一部分.Trie树 1.1.什么是Trie树 Trie树,即字典树,又称单词查找 ...

  4. Java 使用pipeline对redis进行批量读写

    code import redis.clients.jedis.Jedis; import redis.clients.jedis.Pipeline; import java.util.List; p ...

  5. zoj 2724 Windows Message Queue(使用priority_queue容器模拟消息队列)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 题目描述: Message queue is the b ...

  6. 以ORM的思路来从Excel文件中读取JSON数据列表

    1.一个常见的问题就是如何读取excel. 这里面有几个分支的问题,一个是如何使用poi读取excel,网上例子很多,但是这只解决了第一步.如何将excel读取入一定的数据结构这是第二个问题,还有就是 ...

  7. 用MVC5+EF6+WebApi 做一个小功能(二) 项目需求整理

    在一个项目开始前,需求整理大概要占到整个项目周期15%甚至30%的比重,可以说需求理得越清楚,后续开发中返工几率越小.在一个项目中,开发新功能的花费的精力要远远小于修改功能的精力,这基本是一个共识.老 ...

  8. org.hibernate.HibernateException: Wrong column type

    这个问题一般出现在我们使用定长的字符串作为主键(其它字段也可能)的时候,如数据库中的ID为char(16).虽然很多资料上都说不推荐这样做,但实际上我们在做很多小case的时候自己为了方便也顾不得那么 ...

  9. [javaSE] 看知乎学习工厂模式

    factory的“本质”就是根据不同的输入创建出不同类型的对象. 引入factory的原因就是你需要根据不同的输入创建不同类型的对象. 简单工厂模式相当于是一个工厂中有各种产品,创建在一个类中,客户无 ...

  10. zookeeper 知识点汇总

    目录 Zookeeper 是什么 Zookeeper 树状模型 Zookeeper 集群结构 如何使用 ZooKeeper 运行 Zookeeper 步骤1 修改 ZooKeeper 配置文件 步骤 ...