import msg from './message'; import BaseTool from '@/utils/tool'; import { loginQuickly } from '@/api/upms/user'; // 根据开发环境或生产环境破设置服务器根域名 let ajaxTimes = 0; export default ({ url, method, data }) => { let closeLoading = true; return new Promise((resolve, reject) => { ajaxTimes++; msg.loadingMustClose(); uni.request({ url: BaseTool.UserUtil.getBaseUrl() + url, header: { 'Content-Type': 'application/json;charset=UTF-8', Authorization: BaseTool.UserUtil.getToken(url) }, data: data, method: method || 'GET', success (res) { // todo future 登录过期,需要单独处理 if (res.data.code === 401) { // msg.hideLoading(); // closeLoading = false; uni.clearStorageSync(); // #ifdef MP-WEIXIN if (url.indexOf('/workplace/backlogs/user/page') >= 0) { return reject(res.data) } // #endif uni.showModal({ content: res.data.message, showCancel: true, cancelText: '账号登录', confirmText: '快捷登录', success: res => { if (res.confirm) { loginQuickly().then(res => { const url = res.data if (url !== '' && url !== null && url != undefined) { // 请求用户授权 window.location.href = url } }) } else if (res.cancel) { uni.reLaunch({ url: '/pages/login/login' }) } } }) return reject(res.data) } if (res.data.code === 403) { // uni.showModal({ content: res.data.message, showCancel: false }) // msg.hideLoading(); // closeLoading = false; uni.showToast({ icon: 'none', title: res.data.message }); return reject(res); } // 重定向的专门处理 if (res.data.code === 301) { // uni.showModal({ content: res.data.message, showCancel: false }) // msg.hideLoading(); // closeLoading = false; window.location.href = res.data } if (res.data.code !== 0) { // uni.showModal({ content: res.data.message, showCancel: false }) // msg.hideLoading(); // closeLoading = false; setTimeout(() => { uni.showToast({ icon: 'none', title: res.data.message }); }, 50) return reject(res.data); } resolve(res.data); }, fail (err) { // msg.hideLoading(); // closeLoading = false; uni.showToast({ icon: 'none', title: '系统异常,请联系客服!' }); reject(err); }, complete: () => { ajaxTimes--; if (ajaxTimes === 0) { // 关闭正在等待的图标 closeLoading = false; msg.hideLoading(); } } }); }).finally(() => { // if (closeLoading) { // msg.hideLoading(); // } }); };