import store from '@/store' import config from '@/config' import { getToken } from '@/utils/auth' import errorCode from '@/utils/errorCode' import { toast, showConfirm, tansParams } from '@/utils/common' import { encryptCBC, decryptCBC } from '@/utils/aescbc' let timeout = 10000 const baseUrl = config.baseUrl // console.log('🚀 ~ baseUrl:', baseUrl) const request = config => { // 是否需要设置 token const isToken = (config.headers || {}).isToken === false // console.log('🚀 ~ request ~ isToken:', isToken) config.header = config.headers || {} if (getToken() && !isToken) { config.header['Authorization'] = 'Bearer ' + getToken() } // get请求映射params参数 if (config.method == 'get' && config.params) { let param = tansParams(config.params) if (param) { param = param.slice(0, -1) param = encryptCBC(param) } let url = config.url + '?' + param config.params = {} config.url = url } // post请求 if (config.method === 'post') { config.header = { ...config.header } config.data = encryptCBC(JSON.stringify(config.data)) } // put请求 if (config.method === 'put' && config.data) { config.header = { ...config.header } config.data = encryptCBC(JSON.stringify(config.data)) } else if (config.method === 'put' && config.params) { let params = tansParams(config.params) params = encryptCBC(params) config.url = config.url + '?' + params } // delete请求 if (config.method === 'delete') { config.header = { ...config.header } config.data = encryptCBC(JSON.stringify(config.data)) } if (config.headers && config.headers['Content-Type'] == 'application/json') { if (typeof config.data == 'object') { config.data = encryptCBC(JSON.stringify(config.data)) } } //对下载请求进行数据参数拦截加密 if (config.headers && config.headers['Content-Type'] == 'application/x-www-form-urlencoded') { if (typeof config.data == 'object') { // console.log(config.data) let formData = tansParams(config.data) if (formData) { formData = formData.slice(0, -1) let formdata = {} formdata.formData = encryptCBC(formData) config.data = formdata } } else { config.data = 'formData=' + encryptCBC(JSON.stringify(config.data)) } } return new Promise((resolve, reject) => { // console.log('🚀 ~ request-config', config) uni .request({ method: config.method || 'get', timeout: config.timeout || timeout, url: config.baseUrl || baseUrl + config.url, data: config.data, header: { ...config.header, tokenType: 'APP' }, // tokenType: 'APP' 用于区分APP和后台管理系统 dataType: 'json' }) .then(response => { console.log('🚀 ~ request ~ response:-------------', response) if (!response[1]) { toast('系统异常, 请联系管理员') reject('系统异常, 请联系管理员') return } let res = null if (typeof response[1].data.decrypt != 'undefined' && response[1].data.decrypt) { res = JSON.parse(decryptCBC(response[1].data.data)) } else { res = decryptCBC(response[1].data) } console.log('处理之后的res', res) // let res = JSON.parse(decryptCBC(response[1].data.data)) // console.log('🚀 ~ request ~ 返回数据-data:', res) if (res.code === 200) { resolve(res) } else if (res.code == 401) { uni.showToast({ title: res.msg, icon: 'none', duration: 1500 }) // 提示登录过期, 直接跳转到登录页面 store.dispatch('LogOut').then(res => { uni.reLaunch({ url: '/pages/login' }) }) reject('无效的会话,或者会话已过期,请重新登录。') } else if (res.code == 500) { uni.showToast({ title: res.msg, icon: 'none' }) reject('500') } else { uni.showToast({ title: '系统异常, 请联系管理员', icon: 'none' }) reject(res.msg) } // let [error, res] = response // if (error) { // toast('后端接口连接异常') // reject('后端接口连接异常') // return // } // const code = res.data.code || 200 // const msg = errorCode[code] || res.data.msg || errorCode['default'] // if (code === 401) { // showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => { // if (res.confirm) { // store.dispatch('LogOut').then(res => { // uni.reLaunch({ url: '/pages/login' }) // }) // } // }) // reject('无效的会话,或者会话已过期,请重新登录。') // } else if (code === 500) { // toast(msg) // reject('500') // } else if (code !== 200) { // toast(msg) // reject(code) // } // resolve(res.data) }) .catch(error => { console.log('🚀 ~ returnnewPromise ~ error:', error) let message = '系统异常, 请联系管理员' // let { message } = error // if (message === 'Network Error') { // message = '系统异常, 请联系管理员' // } else if (message.includes('timeout')) { // message = '系统接口请求超时' // } else if (message.includes('Request failed with status code')) { // message = '系统异常, 请联系管理员' // } toast(message) reject(error) }) }) } export default request