115 lines
3.3 KiB
JavaScript
115 lines
3.3 KiB
JavaScript
//success成功之前拦截器 在这里 自定义异常拦截
|
|
$.ajaxSetup({
|
|
beforeSend: function(xhr, options) {
|
|
var originalSuccess = options.success
|
|
options.success = function(data, textStatus, jqXhr) {
|
|
data = modifyResponseData(data);
|
|
success(data,textStatus, jqXhr);
|
|
originalSuccess.apply(this, arguments)
|
|
}
|
|
}
|
|
})
|
|
// 请求发送之前的拦截器
|
|
$(document).ajaxSend(function (event, xhr, settings) {
|
|
//修改请求参数
|
|
xhr.setRequestHeader('Authorization', localStorage.getItem("tokens"));
|
|
|
|
var modifiedRequestData = modifyRequestData(settings);
|
|
settings.data = modifiedRequestData;
|
|
});
|
|
// 响应成功后的拦截器
|
|
$(document).ajaxSuccess(function (event, xhr, settings, data) {
|
|
data = modifyResponseData(data);
|
|
let modifiedResponseData=data;
|
|
settings.data = modifiedResponseData;
|
|
return data;
|
|
});
|
|
|
|
// 请求失败后的拦截器
|
|
$(document).ajaxError(function (event, xhr, settings, er) {
|
|
error(event, xhr, settings, er);
|
|
});
|
|
|
|
// 修改请求数据的函数
|
|
function modifyRequestData(settings) {
|
|
var requestData = settings.data;
|
|
var modifiedData = requestData;
|
|
if(settings.headers){
|
|
if(settings.headers['decrypt']=='decrypt'){
|
|
return modifiedData;
|
|
}
|
|
}
|
|
if(settings.type=='POST'){
|
|
if(settings.contentType.indexOf('multipart/form-data')!=-1){
|
|
return modifiedData;
|
|
}else if(settings.contentType.indexOf('application/x-www-form-urlencoded')!=-1){
|
|
var requestData = settings.data;
|
|
var modifiedData = requestData;
|
|
modifiedData=decodeURIComponent(modifiedData);
|
|
if(!modifiedData){
|
|
modifiedData="123";
|
|
}else{
|
|
modifiedData=modifiedData.replaceAll("+-+"," - ");
|
|
modifiedData=modifiedData.replaceAll("+~+"," ~ ");
|
|
}
|
|
modifiedData=sm2Encrypt(modifiedData);
|
|
return "formData="+modifiedData;
|
|
}
|
|
}
|
|
//默认加密
|
|
let url=getUrlParam(settings.url);
|
|
settings.url=url;
|
|
if(modifiedData){
|
|
modifiedData=modifiedData.replaceAll("+"," ");
|
|
}
|
|
modifiedData=decodeURIComponent(modifiedData);
|
|
modifiedData= sm2Encrypt(modifiedData);
|
|
return modifiedData;
|
|
}
|
|
// 修改响应数据的函数
|
|
function modifyResponseData(responseData) {
|
|
// 在这里对响应数据进行修改
|
|
if(responseData.decrypt){
|
|
responseData = sm2Decrypt(responseData.data);
|
|
}else{
|
|
if(typeof(responseData.data)=='undefined'){
|
|
return responseData;
|
|
}
|
|
responseData=responseData.data;
|
|
}
|
|
if(typeof(responseData)=='object'){
|
|
return responseData;
|
|
}
|
|
if(typeof(responseData)=='string'){
|
|
var responseData = JSON.parse(responseData);
|
|
var modifiedData = responseData;
|
|
return modifiedData;
|
|
}
|
|
return responseData;
|
|
}
|
|
|
|
// url后面路径解析加密
|
|
function getUrlParam (url) {
|
|
var arrObj = url.split("?");
|
|
if (arrObj.length > 1) {
|
|
if(arrObj[1]){
|
|
arrObj[1]=arrObj[1].replaceAll("+"," ");
|
|
}
|
|
|
|
return arrObj[0]+"?"+ sm2Encrypt(arrObj[1]);
|
|
} else {
|
|
return url;
|
|
}
|
|
}
|
|
function removePadding2(decrypted) {
|
|
const lastByte = decrypted.charCodeAt(decrypted.length - 1);
|
|
if (lastByte <= 16) {
|
|
for (let i = decrypted.length - lastByte; i < decrypted.length; i++) {
|
|
if (decrypted.charCodeAt(i) !== lastByte) {
|
|
return decrypted; // 如果填充不正确,返回原始字符串
|
|
}
|
|
}
|
|
return decrypted.slice(0, -lastByte);
|
|
}
|
|
return decrypted;
|
|
} |