ajax/index.js
import axiosApi from '../js/fetch';
import { baseUrl, headerParams } from '../js/baseUrl';
// import引用AES源码js
import CryptoJS from 'crypto-js/crypto-js';
console.log(process.env);
// 密钥 4492d531a3a04939
// 生成验签的密钥不做任何处理
let signKey = '4492d531a3a04939';
// 参与加解密的验签
var keyCode = '4492d531a3a04939';
switch (process.env.NODE_ENV) {
case 'development': keyCode = '4492d531a3a04939'; break;
case 'test': keyCode = '4492d531a3a04939'; break;
case 'production': keyCode = '/static/dev/'; break;
default: keyCode = '4492d531a3a04939'; break;
}
// 随机数
function getRandom (length) {
let randomStr = '';
if (!length) {
var length = 5;
}
for (var i = 0; i <= length; i++) {
randomStr += Math.floor(Math.random() * 10);
}
return randomStr;
}
// 得到验签
function getRequestObj () {
var newRequestObj = {}; // 参数对象
let initArray = []; // 延签数组
let nonce = getRandom(); // 随机数
let timestamp = new Date().getTime();
initArray.push(nonce, timestamp, signKey);
let initStr = initArray.sort().join(''); // 延签
console.log(initStr);
// eslint-disable-next-line new-cap
initStr = CryptoJS.SHA1(initStr).toString().toUpperCase();
// console.log('加密后的验签', initStr);
let queryStr = '';
// 拼接参数
newRequestObj.timestamp = timestamp;
newRequestObj.nonce = nonce;
newRequestObj.signature = initStr;
for (var key in newRequestObj) {
queryStr += key + '=' + newRequestObj[key] + '&';
}
queryStr = queryStr.substring(0, queryStr.length - 1);
return queryStr;
}
// 处理key
function getKey (lightKey) {
// eslint-disable-next-line new-cap
var realKey = CryptoJS.SHA1(lightKey);
// eslint-disable-next-line new-cap
realKey = CryptoJS.SHA1(realKey).toString().substring(0, 32); // 真正的key
// console.log('realKey', realKey);
return realKey;
}
// 加密
export function encrypt (data, lightKey) {
lightKey = lightKey || keyCode; // 这一步异常关键!!!!!!
var realKey = getKey(lightKey);
var encrypt = CryptoJS.AES.encrypt(data, CryptoJS.enc.Hex.parse(realKey), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypt.ciphertext.toString(CryptoJS.enc.Base64);
}
// 解密
export function decrypt (data, lightKey) {
lightKey = lightKey || keyCode; // 这一步异常关键!!!!!!
var realKey = getKey(lightKey);
var decrypt = CryptoJS.AES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(data)
}, CryptoJS.enc.Hex.parse(realKey), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decrypt.toString(CryptoJS.enc.Utf8);
}
// 解析响应
function ajax (opts) {
return new Promise((resolve, reject) => {
axiosApi
.request(opts)
.then(data => {
// 对入参解密
data = JSON.parse(decrypt(data));
const { code } = data;
if (Number(code) === 0) return resolve(data);
reject(data);
})
.catch(err => {
reject(err);
});
});
}
// 加解密封装ajax;
export function doAjax (name, params, method) {
var test = getRequestObj();
console.log(test, '===>');
const opts = {
url: baseUrl + name + '?' + test,
method: method, // 请求方式
data: encrypt(JSON.stringify(headerParams(name, params))),
headers: { 'Content-Type': 'application/json' }
};
var data = encrypt(JSON.stringify(headerParams(name, params)));
console.log(data, '====>加密');
console.log(JSON.parse(decrypt(data)), '===>解密');
return ajax(opts);
}
export default {
encrypt,
decrypt
};
export const baseUrl = 'http://10.29.32.19:8088/bmp/payment/';
export const headerParams = (name, data) => {
if (!data) {
data = {};
};
let headerParams = {
charset: 'utf-8',
method: 'com.taiping.cashier.' + name,
signature: '',
timestamp: new Date().getTime(),
transId: '',
version: '2.0',
requestData: data
};
return headerParams;
};