/** * 接口请求封装 * * 1、需要成功提示的,带 additional.successMessage ,如果值为'msg',提示内容为返回的msg * 2、默认loading,不需要的传 additional.needLoading = false。 */ import { URL } from "./requestUrl"; import { stringify } from 'qs'; const BASE_URL = `${URL}/imed/pfm/`; let AllRequestNum = 0; // 存放请求数,以保证有请求未返回就持续loading /** * * @param obj 接口配置数据{url,method,successMessage,needLoading} * @param data 接口请求数据 */ function creatRequest(obj, data) { if (obj) { let additional = {}, url = obj.url; if (obj.successMessage !== undefined) additional.successMessage = obj.successMessage; if (obj.needLoading !== undefined) additional.needLoading = obj.needLoading; // DELETE 参数以问号形式拼接到url if (obj.method === 'DELETE') { url = data ? `${url}?${stringify(data)}` : url; data = null; } return request(url, { method: obj.method, data }, additional); } else { uni.showModal({ title: '错误提示', content: '请求的接口未定义!', showCancel: false }); } } /** * url: 请求的url * options: uni.request的一系列参数,常用的method和data * additional: { successMessage、needLoading } */ function request(url, options, additional) { if (additional.needLoading === undefined) additional.needLoading = true; // 加载中提示打开 openCloseLoading(additional, true); // 模拟token,做登录的同学将这行代码移到登录成功函数中. // uni.setStorageSync('token', 'MjU2OzE2MTE2MjIxODYwNjM7OGJjMTA2ZjNhMzMyNGUxOTRhMmFlMGExOThjMzA5OGE='); const token = uni.getStorageSync('token'); return uni.request({ url: `${BASE_URL}${url}`, ...options, header: { token: token ? token : null, 'Content-Type': 'application/json', } }).then((res) => notifyException(res, additional)) .catch((error) => { if ('stack' in error && 'message' in error) { uni.showModal({ title: '错误提示', content: '服务器异常,请稍后重试!', showCancel: false }); console.error(`请求错误: ${url} ${error.message}`); } return error; }); } // 加载中提示打开/关闭 function openCloseLoading(additional, isOpen) { if (additional.needLoading) { if (isOpen) { // 打开(如果已经打开不必重复打开) if (AllRequestNum === 0) uni.showLoading(); AllRequestNum++; } else { // 关闭 if (AllRequestNum > 0) { // 防止数值错乱 AllRequestNum--; if (AllRequestNum === 0) uni.hideLoading(); } else { AllRequestNum = 0; } } } } // 登录过期,重置请求计数,强制关闭loading function LoginExpired() { AllRequestNum = 0; uni.navigateTo({ url: '/pages/login/login' }); } function notifyException(resArr, additional) { openCloseLoading(additional, false); const [error, res] = resArr; if (error) { uni.showModal({ title: '错误提示', content: JSON.stringify(error), showCancel: false }); console.error(`请求错误: ${error.errMsg}`); return null; } else if (res.data.code !== 'SUCCESS') { const { code, msg } = res.data; if (code === '410' || code === '401') { LoginExpired(); } else if (code === '403') { uni.showModal({ title: '提示', content: '登录过期,请重新登录!', success: function(_res) { if (_res.confirm) { LoginExpired(); } else if (_res.cancel) { console.log('用户点击取消'); } } }); } else { uni.showModal({ title: '错误提示', content: msg, showCancel: false }); } console.error(`错误信息: ${msg}`); return null; } else if (additional.successMessage) { let msg = additional.successMessage; uni.showModal({ title: '提示', content: msg === 'msg' ? res.data.msg : msg, showCancel: false }); } return res.data.data; } export { request, creatRequest }