| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import "./wxPromise.js";
- // loading配置,请求次数统计
- function startLoading(){
- wx.showLoading({
- title: '',
- mask: true
- });
- };
- function endLoading(){
- wx.hideLoading();
- };
- function showFullScreenLoading(){
- if(getApp().globalData.loadingCount == 0){
- startLoading();
- };
- getApp().globalData.loadingCount++;
- };
- function tryHideFullScreenLoading(){
- if(getApp().globalData.loadingCount<=0){
- return;
- };
- getApp().globalData.loadingCount--;
- if(getApp().globalData.loadingCount == 0){
- // console.log('结束..')
- endLoading();
- };
- };
- // 请求
- function wxRequest(url, type, data,nextInvoke) {
- var res = {};
- return new Promise(function (resolve, reject) {
- data.token = getApp().globalData.token;
- realRequest(url, type, data,nextInvoke).then(
- (res) => {
- // console.log(res);
- resolve(res);
- },
- (e) => {
- reject(e);
- }
- );
- });
- };
- function realRequest(url, type, data,nextInvoke) {
- showFullScreenLoading();
- if(nextInvoke){
- getApp().globalData.loadingCount = 2;
- };
- // console.log('loadingCount',getApp().globalData.loadingCount)
- return new Promise(function (resolve, reject) {
- wx.pro
- .request({
- url: getApp().globalData.api + url,
- method: type,
- data: data,
- })
- .then((res) => {
- tryHideFullScreenLoading();
- var res = res.data;
- // console.log(res);
- if (res.code == 200) {
- // console.log(res.result)
- resolve(res);
- }
- // 逻辑报错
- else if (res.code == 204) {
- reject(res.msg);
- }
- // token失效
- else if (res.code == 401) {
- getApp().globalData.userInfo = null;
- getApp().globalData.token = null;
- // token报错,跳转到登录页面
- wx.pro.redirectTo({
- url: '/pages/login/index/index'
- });
- reject(res.msg);
- }
- reject(res.msg);
- })
- .catch((err) => {
- // 接口连续调用时报错,需主动关闭
- if(nextInvoke){
- getApp().globalData.loadingCount = 1;
- tryHideFullScreenLoading();
- }else{
- // 正常 非连续调用
- tryHideFullScreenLoading();
- }
- console.log(err);
- reject("网络异常,请稍后再试");
- });
- });
- };
- exports.wxRequest = wxRequest;
|