// 全局请求路径,也就是后端的请求基准路径 export const BASE_URL = //'https://hzl.willalp.com:3005' //'http://192.168.188.2:8080' 'http://139.9.104.214:3013' // 'http://192.168.0.188:3013' // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理 let ajaxTimes = 0; // 封装请求方法,并向外暴露该方法 export const httpRequest = (options) => { // 解构请求头参数 // 解构请求头参数 let header = { ...options.header }; // 当前请求不是登录和获取所有机构时的请求,在header中加上后端返回的token if (!options.isNotToken) { header["Authorization"] = "Bearer " + uni.getStorageSync('token'); // 显示加载中 效果 uni.showLoading({ title: "加载中", mask: true, }); } header['Content-Type'] = 'application/json'; header['Allow-Control-Allow-Origin'] = '*'; ajaxTimes++; return new Promise((resolve, reject) => { uni.request({ url: BASE_URL + options.url, method: options.method || 'POST', data: options.data || {}, header, success: (res) => { const data = res.data; if (data.code === 401) { uni.showToast({ duration: 2500, title: '账号在别处登录', icon: 'error' }); uni.clearStorageSync() goto('../../pages/login/login') } else if (data.code != 200) { if (!options.isNotErrorMsg) { uni.showToast({ duration: 3000, title: data.msg ? data.msg : '请求异常', icon: 'none' }); } } resolve(res) }, fail: (err) => { uni.showToast({ duration: 3500, title: '网络异常', icon: 'error' }); reject(err) }, // 完成之后关闭加载效果 complete: () => { ajaxTimes--; if (ajaxTimes === 0) { // 关闭正在等待的图标 uni.hideLoading(); } } }) }) } export const goto = (url) => { uni.navigateTo({ url: url, animationType: 'pop-in', animationDuration: 200 }); } export const goTab = (url) => { uni.switchTab({ url: url }); } export const goBack = () => { uni.navigateBack({ delta: 1, animationType: 'pop-out', animationDuration: 200 }); } export const formatter = (type, value) => { if (type === 'year') { return `${value}年` } if (type === 'month') { return `${value}月` } if (type === 'day') { return `${value}日` } return value } export const dateResult = (time, mode) => { const timeFormat = uni.$u.timeFormat switch (mode) { case 'datetime': return timeFormat(time, 'yyyy-mm-dd hh:MM:ss') case 'date': return timeFormat(time, 'yyyy-mm-dd') case 'year-month': return timeFormat(time, 'yyyy-mm') case 'time': return time default: return '' } }