| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import './wxPromise.js'; // loading配置,请求次数统计
- function startLoading() {
- uni.showLoading({
- title: '',
- mask: true
- });
- }
- function endLoading() {
- uni.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) {
- uni.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报错,跳转到登录页面
- uni.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;
|