189 lines
6.5 KiB
JavaScript
189 lines
6.5 KiB
JavaScript
import {Base64} from "js-base64";
|
||
|
||
|
||
const arr = [
|
||
'mobile',
|
||
'express_mobile',
|
||
'doctor',
|
||
'accept_tel',
|
||
'patient_mobile',
|
||
'id_card',
|
||
'idcard',
|
||
'password'
|
||
]
|
||
|
||
// 敏感数据
|
||
const sensitiveData = [
|
||
'id_card',
|
||
'idcard'
|
||
]
|
||
|
||
function request(url, params, method = 0) {
|
||
// let baseURL = "https://app.xiaokang88.com/member"
|
||
let baseURL = "http://127.0.0.1:18000/member"
|
||
if (url.split('/').indexOf('imApi') !== -1) {
|
||
baseURL = "http://127.0.0.1:12080/api"
|
||
}
|
||
if (url.split('/').indexOf('newApi') !== -1) {
|
||
baseURL = "https://shop.xiaokang88.com/member"
|
||
}
|
||
url = url.replace('/oldApi', '');
|
||
url = url.replace('/newApi', '');
|
||
url = url.replace('/imApi', '');
|
||
|
||
// 加密
|
||
if (params != null) {
|
||
params.data = getRes(params.data, false)
|
||
}
|
||
let num = 0;
|
||
// promise导出
|
||
return new Promise((resolve, reject) => {
|
||
uni.request({
|
||
// 基地址 + 传过来的地址
|
||
url: baseURL + url,
|
||
timeout: 8000, // 8秒超时时间,单位ms
|
||
// 展开传过来的数据 请求方式 所需数据 wxfc279dcf921a38f7
|
||
// store_id: '11001'
|
||
...params,
|
||
// 请求头
|
||
header: {
|
||
"authorization": 'Bearer ' + uni.getStorageSync("token"),
|
||
"Content-Type": method === 0? "application/x-www-form-urlencoded": 'application/json'
|
||
},
|
||
|
||
// 成功钩子
|
||
success(res) {
|
||
if (res.data.errcode === 401) {
|
||
// 获取当前页面,如果不是 /pages/home/index
|
||
if (getCurrentPages()[getCurrentPages().length - 1].route !== 'pages/home/index') {
|
||
uni.removeStorageSync('token')
|
||
uni.removeStorageSync('userinfo')
|
||
uni.removeStorageSync('token_h5')
|
||
uni.removeStorageSync('user_id')
|
||
uni.reLaunch({url: '/pages/home/index'})
|
||
}
|
||
}
|
||
if (res.data?.data != null) {
|
||
res.data.data = getRes(res.data.data)
|
||
}
|
||
resolve(res)
|
||
},
|
||
// 失败钩子
|
||
fail(err) {
|
||
reject(err)
|
||
uni.showToast({
|
||
title: "请求失败",
|
||
duration: 1500,
|
||
mask: false,
|
||
icon: "error"
|
||
})
|
||
},
|
||
// 成功失败都会执行的钩子
|
||
complete() {
|
||
// 成功或者失败停止下拉刷新
|
||
uni.stopPullDownRefresh()
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
|
||
function getRes(obj, isDecode = true) {
|
||
try {
|
||
for (const key in obj) {
|
||
if (Array.isArray(obj[key])) {
|
||
obj[key] = getRes(obj[key])
|
||
} else if (typeof obj[key] === 'object') {
|
||
obj[key] = getRes(obj[key])
|
||
} else {
|
||
// 判断obj[key]是否在arr中
|
||
if (arr.indexOf(key) !== -1) {
|
||
let aseFile = ''
|
||
if (isDecode === true) {
|
||
aseFile = customBase64Decode(obj[key])
|
||
} else {
|
||
aseFile = customBase64Encode(obj[key])
|
||
}
|
||
const isGarbled = /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\xFF]|[\u{E000}-\u{F8FF}]/u.test(
|
||
typeof aseFile === 'string' ? aseFile : ''
|
||
)
|
||
if (isGarbled) {
|
||
aseFile = obj[key]
|
||
}
|
||
// 为了修改,暂时不脱敏
|
||
if (isDecode === true) {
|
||
|
||
if (sensitiveData.indexOf(key) !== -1) {
|
||
// 数据脱敏,自动匹配姓名、手机号、身份证
|
||
switch (key) {
|
||
case 'express_name':
|
||
case 'express_region':
|
||
case 'accept_name':
|
||
case 'patient':
|
||
// 保留前两个字符,其余用星号代替
|
||
aseFile = aseFile.slice(0, 1).padEnd(aseFile.length, '*');
|
||
break;
|
||
case 'mobile':
|
||
case 'express_mobile':
|
||
// 保留前三位和后四位,中间用星号代替
|
||
aseFile = aseFile.slice(0, 3).padEnd(aseFile.length - 4, '*') + aseFile.slice(-4);
|
||
break;
|
||
case 'id_card':
|
||
case 'idcard':
|
||
// 保留前六位和后四位,中间用星号代替
|
||
aseFile = aseFile.slice(0, 6).padEnd(aseFile.length - 4, '*') + aseFile.slice(-4);
|
||
break;
|
||
default:
|
||
// 默认情况下,全部用星号代替
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
obj[key] = aseFile
|
||
}
|
||
}
|
||
}
|
||
return obj
|
||
} catch (error) {
|
||
console.error('错误:', error)
|
||
return obj
|
||
}
|
||
}
|
||
|
||
function customBase64Decode(data) {
|
||
// return data
|
||
try {
|
||
// 第一次Base64解码
|
||
const decodedFirst = Base64.decode(data)
|
||
// 截取从第30个字符开始往后的内容
|
||
const subStr = decodedFirst.slice(30)
|
||
// 第二次Base64解码
|
||
return Base64.decode(subStr) != '' ? Base64.decode(subStr) : data
|
||
} catch (error) {
|
||
console.error(`解码Base64字符串【${data}】时发生错误`, error)
|
||
return data
|
||
}
|
||
}
|
||
|
||
// 加密
|
||
function customBase64Encode(data) {
|
||
if (data == null || data === '') return data
|
||
// return data
|
||
try {
|
||
// 和加密方法差不多但是是要生成30个随机字符
|
||
let randomString = ''
|
||
for (let i = 0; i < 30; i++) {
|
||
const randomChar = String.fromCharCode(Math.floor(Math.random() * (126 - 32 + 1)) + 32)
|
||
randomString += randomChar
|
||
}
|
||
const encoded = Base64.encode(data)
|
||
return Base64.encode(randomString + encoded)
|
||
} catch (error) {
|
||
console.error(`加密字符串【${data}】时发生错误`, error)
|
||
return data
|
||
}
|
||
}
|
||
|
||
// 导出
|
||
export default request
|