使用 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();
}
}); },