http.js
import {
loginRedirect
} from './index'
export let baseURL = 'https://xxx/prod-api'
const CryptoJS = require("crypto-js")
const JSEncrypt = require("jsencrypt")
function getUUID() {
var d = new Date().getTime();
var uuid = "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
});
return uuid;
}
let AESKey = getUUID()
function aesEncrypt(data) {
const _key = CryptoJS.enc.Utf8.parse(AESKey)
const encrypt = CryptoJS.AES.encrypt(JSON.stringify(data), _key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
return encrypt.toString()
}
function rsaEncrypt(publicKey) {
var encryptor = new JSEncrypt.JSEncrypt();
encryptor.setPublicKey(publicKey);
return encryptor.encrypt(AESKey);
}
module.exports = (url, data, method = "GET", type) => {
return new Promise((resolve, reject) => {
console.log('request11', url, data)
let data1 = data
if (url == "/login") {
let PublicKey = ''
if (!wx.getStorageSync("PublicKey")) {
setTimeout(function () {
wx.showToast({
title: '未获取到公钥,请退出重进',
icon: 'none'
})
}, 0)
return
} else {
PublicKey = wx.getStorageSync("PublicKey")
}
data1 = {
data: aesEncrypt(data),
aesKey: rsaEncrypt(PublicKey),
publicKey: PublicKey
};
console.log(data1, 66666)
} else {
wx.showLoading({
title: '加载中...'
});
}
wx.request({
url: baseURL + url,
data: data1,
method: method,
header: {
'content-type': type || '',
'Authorization': 'Bearer ' + wx.getStorageSync('token'),
},
success(res) {
if (res.statusCode == 200) {
wx.hideLoading();
if (res.data.code == 200) {
resolve(res.data);
} else if (res.data.code == 401) {
loginRedirect()
} else {
wx.showLoading();
wx.hideLoading();
setTimeout(function () {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
})
}, 0)
reject(res.data);
}
} else if (res.statusCode == 401) {
loginRedirect()
} else {
wx.showToast({
title: '接口请求失败,接口状态' + res.statusCode,
icon: 'none',
duration: 2000
})
}
},
fail() {
wx.showToast({
title: '接口调取失败,请检查网络连接',
icon: 'none',
duration: 2000
})
},
complete() {
wx.hideLoading();
}
})
})
}