import axios from 'axios'
axios.defaults.timeout = 10000
//排序等封装方法
import myUtil from '../../lib/myutils.js';
//引入rsa私钥
import rsakey from '../../lib/encrypt/rsakey.js'
//引入aes/rsa加密配置文件
import Utils from "../../lib/encrypt/utils.js";
class HttpClient {
constructor(baseUrl = process.env.VUE_APP_BASE_API) {
this.baseUrl ='http://192.168.2.666/uc/'
}
interceptors(instance) {
// 请求拦截
instance.interceptors.request.use(
(config) => {
if (config.data) {
// 参数非空过滤,排序,拼接成字符串
var backData = myUtil.ruleParams(config.data)
config.data = backData.fliterDta
// AES密钥(aesKey)随机生成字符串
var myAesKey = myUtil.randomWord(false, 32); //32位
// aesParams 使用AES加密请求参数
var aesParams = Utils.encrypt(JSON.stringify(config.data), myAesKey); //
config.data = {}
config.data['requestData'] = aesParams
//AES加密后的AES密钥(aesKey),使用rsa私钥加密aes密钥
let rsaAesKey = Utils.JSEncryptCommon(rsakey.rsakey,
myAesKey);
//Vue.$encryptedData(rsakey.rsakey, myAesKey); //myAesKey解密前使用base64转字符串// ----- ok
var baseRsaAesKey = rsaAesKey; // 编码
config.data['sign'] = baseRsaAesKey
return config
}
return config
},
(error) => Promise.reject(error.response)
)
// response拦截器
instance.interceptors.response.use(
(response) => {
// 解密
if (response.data.responseData && response.data.sign) {
// 1.res解出aes秘钥
var aesKey = Utils.JSDecryptCommon(response.data.sign)
// 2.aes解出返回数据
var parseData = Utils.decrypt(response.data.responseData, aesKey)
parseData = JSON.parse(parseData)
var myres = {
data: parseData
}
const { data } = myres
const { code } = data
if (data && (code === 0 || code === 4000)) return data
return Promise.reject(myres)
} else {
const { data } = response
const { code } = data
if (data && (code === 0 || code === 4000)) return data
return Promise.reject(response)
}
},
(error) => Promise.reject(error)
)
}
getDefaultOptions() {
const config = {
baseURL: this.baseUrl,
timeout: 10 * 1000, // 请求超时时间
withCredentials: false,
headers: {}
}
return config
}
request(options) {
const instance = axios.create()
options = Object.assign(this.getDefaultOptions(), options)
this.interceptors(instance, options.url)
return instance(options)
}
// 下载接口
getDefaultOptionsD() {
const config = {
baseURL: ' https://api-test.baidu.com',
timeout: 10 * 1000, // 请求超时时间
withCredentials: false,
headers: {}
}
return config
}
requestD(options) {
const instance = axios.create()
options = Object.assign(this.getDefaultOptionsD(), options)
this.interceptors(instance, options.url)
return instance(options)
}
}
export default new HttpClient()
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。