import $C from './config.js' import $U from './util.js' import { jsonFilterEmpty } from './tools.js' export default { // 全局配置 application/json; common: { baseUrl: $C.baseUrl, header: { // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' 'Content-Type': 'application/json;charset=utf-8' }, data: {}, method: 'GET', dataType: 'json', token: true }, // 请求 返回promise request(options = {}) { let _that = this // 组织参数 if (options.commonBaseUrl) { options.url = options.commonBaseUrl + options.url } else { options.url = this.common.baseUrl + options.url } options.header = options.header || this.common.header options.data = options.data || this.common.data options.method = options.method || this.common.method options.dataType = options.dataType || this.common.dataType options.header['api-type'] = 'wxapp' if (uni.getStorageSync('token')) { options.header.Authorization = uni.getStorageSync('token') options.header['api-token'] = uni.getStorageSync('token') } options.timeout = 10000 uni.showLoading() // 请求 return new Promise((res, rej) => { // 请求之前检测有没有网络 uni.request({ ...options, success: result => { uni.hideLoading() let { statusCode, data } = result // 返回原始数据 if (options.native) { return res(result) } // 成功 if (statusCode == 200) { if (data.code === 200) { res(data) } else if (data.code === 401) { uni.removeStorageSync('userInfo') uni.removeStorage({ key: 'token', success: () => { setTimeout(function() { uni.reLaunch({ url: '/pages/home/dashboard/index', }) }, 500) } }) return rej(data) } else if (data.code === 403) { uni.removeStorageSync('userInfo') uni.removeStorage({ key: 'token', success: () => { setTimeout(function() { uni.reLaunch({ url: '/pages/home/dashboard/index', }) }, 500) } }) return rej(data) } else if (data.code === 500) { uni.showToast({ title: data.msg || '', icon: 'none', duration: 2000 }) return rej(data) } else { uni.hideLoading() uni.showToast({ title: data.msg || '返回失败', icon: 'none', duration: 3000 }) return rej(data) } } else if (statusCode == 401 || statusCode == 403 || statusCode == 204) { // uni.removeStorageSync(`userInfo`) // uni.removeStorage({ // key: `token`, // success: () => { // setTimeout(function() { // uni.reLaunch({ // url: `/pages/home/dashboard/index`, // }); // }, 500); // } // }) return rej(data) } else { if (!options.msgFlag) { uni.showToast({ title: data.msg || '返回失败', icon: 'none', duration: 3000 }) } return rej(data) } }, fail: error => { uni.hideLoading() if (error.errMsg && error.errMsg.indexOf('fail timeout') != -1 || error .errMsg && error.errMsg.indexOf('请求超时') != -1) { uni.showToast({ title: error.errMsg, icon: 'none', duration: 2500 }) } else { // uni.showToast({ // title: '服务维护中,请稍后尝试', // icon: 'none', // duration: 2500 // }); } return rej(error) } }) }) }, // get请求 get(url, data = {}, options = {}, needtoken = true) { options.url = url // 参数是否去空 if (data.notFliter) { delete data.notFliter options.data = data } else { options.data = jsonFilterEmpty(data) } options.method = 'GET' options.token = needtoken return this.request(options) }, // post请求 post(url, data = {}, options = {}, needtoken = true) { options.url = url // 参数是否去空 if (data.notFliter) { delete data.notFliter options.data = data } else { options.data = jsonFilterEmpty(data) } options.method = 'POST' options.token = needtoken return this.request(options) }, // put请求 put(url, data = {}, options = {}, needtoken = true) { options.url = url options.data = data options.method = 'PUT' options.token = needtoken return this.request(options) }, // delete请求 del(url, data = {}, options = {}, needtoken = true) { options.url = url options.data = data options.method = 'DELETE' options.token = needtoken return this.request(options) }, // 上传文件 upload(url, data, onProgress = false) { return new Promise((result, reject) => { // 上传 let token = uni.getStorageSync('token') if (!token) { uni.showToast({ title: '请先登录', icon: 'none' }) // token不存在时跳转 return uni.reLaunch({ url: '/pages/home/dashboard/index', }) } // uni.showLoading({ // title: '上传中...', // mask: true // }); const uploadTask = uni.uploadFile({ url: $C.baseUrl + url, filePath: data.filePath, name: data.name || 'files', header: { 'api-token': token, 'api-name': 'wxapp', }, success: res => { uni.hideLoading() let message = JSON.parse(res.data) if (res.statusCode === 200) { result(message.data) } else if (res.statusCode === 401) { uni.showToast({ title: message.msg, icon: 'none' }) uni.reLaunch({ url: '/pages/home/dashboard/index', }) reject(message) } else { uni.showToast({ title: message.msg, icon: 'none' }) result(false) } }, fail: err => { uni.hideLoading() reject(err) } }) uploadTask.onProgressUpdate(res => { if (typeof onProgress === 'function') { onProgress(res.progress) } }) }) } }