You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
5.1 KiB
149 lines
5.1 KiB
import config from '@/config'
|
|
import { getToken } from '@/utils/auth'
|
|
import errorCode from '@/utils/errorCode'
|
|
import { useUserStore } from '@/store/modules/user'
|
|
import { toast, showConfirm, tansParams } from '@/utils/common'
|
|
import {
|
|
RsaEncryption,
|
|
AesEncryption,
|
|
randomStr,
|
|
encodeBase64,
|
|
decodeBase64
|
|
} from '@/utils/crypto'
|
|
|
|
// 使用配置中的超时时间或默认值
|
|
let timeout = config.harmony?.networkTimeout || 15000
|
|
const baseUrl = config.baseUrl
|
|
const { clientId, enableEncrypt, rsaPublicKey, rsaPrivateKey } = config
|
|
|
|
// 初始化加密工具
|
|
const asymmetricEncryption = new RsaEncryption({
|
|
publicKey: rsaPublicKey,
|
|
privateKey: rsaPrivateKey
|
|
})
|
|
const symmetricEncryption = new AesEncryption()
|
|
|
|
const request = config => {
|
|
// 是否需要设置 token
|
|
const isToken = (config.headers || {}).isToken === false
|
|
// 是否需要加密 (默认不加密,需要手动传入 encrypt: true)
|
|
const needEncrypt = config.encrypt === true
|
|
|
|
config.header = config.header || {}
|
|
if (getToken() && !isToken) {
|
|
config.header['Authorization'] = 'Bearer ' + getToken()
|
|
}
|
|
|
|
// 添加客户端ID
|
|
config.header['ClientID'] = clientId
|
|
|
|
// get请求映射params参数
|
|
if (config.params) {
|
|
let url = config.url + '?' + tansParams(config.params)
|
|
url = url.slice(0, -1)
|
|
config.url = url
|
|
}
|
|
|
|
// 请求加密处理 (仅对 POST/PUT 请求)
|
|
if (
|
|
enableEncrypt &&
|
|
needEncrypt &&
|
|
['POST', 'PUT'].includes((config.method || 'GET').toUpperCase())
|
|
) {
|
|
// 生成随机 AES 密钥
|
|
const aesKey = randomStr(32)
|
|
const keyWithBase64 = encodeBase64(aesKey)
|
|
// 使用 RSA 加密 AES 密钥
|
|
config.header['encrypt-key'] = asymmetricEncryption.encrypt(keyWithBase64)
|
|
// 使用 AES 加密请求数据
|
|
const dataStr = typeof config.data === 'object'
|
|
? JSON.stringify(config.data)
|
|
: config.data
|
|
config.data = symmetricEncryption.encrypt(dataStr, aesKey)
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
// #ifdef HARMONY
|
|
// 鸿蒙平台特定处理
|
|
console.log('发起鸿蒙平台网络请求:', config.url)
|
|
// #endif
|
|
|
|
uni.request({
|
|
method: config.method || 'get',
|
|
timeout: config.timeout || timeout,
|
|
url: config.baseUrl || baseUrl + config.url,
|
|
data: config.data,
|
|
header: config.header,
|
|
dataType: 'json'
|
|
}).then(response => {
|
|
let res = response
|
|
|
|
// 响应解密处理
|
|
const encryptKey = (res.header || res.headers || {})['encrypt-key']
|
|
if (encryptKey && res.data) {
|
|
try {
|
|
// RSA 私钥解密 AES 密钥
|
|
const base64Str = asymmetricEncryption.decrypt(encryptKey)
|
|
// Base64 解码得到 AES 密钥
|
|
const aesKey = decodeBase64(base64Str)
|
|
// 使用 AES 密钥解密响应数据
|
|
const decryptData = symmetricEncryption.decrypt(
|
|
typeof res.data === 'string' ? res.data : JSON.stringify(res.data),
|
|
aesKey
|
|
)
|
|
// 解析为对象
|
|
res.data = JSON.parse(decryptData)
|
|
} catch (error) {
|
|
console.error('解密失败:', 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) {
|
|
useUserStore().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 => {
|
|
let { message } = error || {}
|
|
// 确保 message 是字符串类型
|
|
message = message || '请求失败'
|
|
|
|
// 添加平台信息以便调试
|
|
// #ifdef HARMONY
|
|
console.error('鸿蒙平台请求失败:', error)
|
|
// #endif
|
|
|
|
if (message === 'Network Error') {
|
|
message = '后端接口连接异常'
|
|
} else if (message.includes && message.includes('timeout')) {
|
|
message = '系统接口请求超时'
|
|
} else if (message.includes && message.includes('Request failed with status code')) {
|
|
message = '系统接口' + message.substr(message.length - 3) + '异常'
|
|
} else if (message.includes && message.includes('Connection refused')) {
|
|
message = '连接被拒绝,请检查服务器是否启动以及网络配置是否正确'
|
|
}
|
|
toast(message)
|
|
reject(error)
|
|
})
|
|
})
|
|
}
|
|
|
|
export default request |