import { encryptBase64, encryptWithAes, generateAesKey, decryptWithAes, decryptBase64 } from '@/utils/crypto'; import { encrypt, decrypt } from '@/utils/jsencrypt'; import { getGlobalData } from '@/store/index.js' const encryptHeader = getGlobalData('encryptHeader'); const baseURL = 'https://frp-off.com:47543' const getToken = () => { return uni.getStorageSync('token') } export function request(options) { options.headers = options.headers || {} // 加密 const isEncrypt = options.headers && (options.headers.isEncrypt === 'true' || options.headers.isEncrypt === true); if (isEncrypt && (options.method === 'post' || options.method === 'put')) { const aesKey = generateAesKey(); options.headers[encryptHeader] = encrypt(encryptBase64(aesKey)); options.data = typeof options.data === 'object' ? encryptWithAes(JSON.stringify(options.data), aesKey) : encryptWithAes(options.data, aesKey); } // token let isToken = (options.headers || {}).isToken === false if (getToken() && !isToken) { options.headers['Authorization'] = 'Bearer ' + getToken(); } options.headers['clientid'] = getGlobalData('clientId'); return new Promise((resolve, reject) => { uni.request({ url: baseURL + options.url, method: options.method || 'GET', data: options.data || {}, header: options.headers || {}, success: res => { if (res.statusCode === 200) { let code = res.data.code let msg = res.data.msg if (code === 200) { resolve(res.data) } else { if (code === 401) { uni.showToast({ title: '请重新登录', icon: 'error', duration: 2000 }) uni.reLaunch({ url: '/pages/login/index' }) } else { uni.showToast({ title: msg.length < 10 ? msg : '网络错误请重试', icon: 'error', duration: 2000 }) } reject(false) } } else { reject(false) } }, fail: err => { uni.showToast({ title: '网络错误请重试', icon: 'error', duration: 2000 }) reject(false) } }) }) }