import axios from 'axios'
import { matchApiPath, newMatchApiPath } from '../utils/utils'
const http = (baseURL: any, { system = null, requestUrlList = [], responseUrlList = [] }) => {
const service = axios.create({
baseURL,
})
let tokenFlag = true;
// 添加请求拦截器
service.interceptors.request.use(function (config: any) {
config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
// const urlList = requestUrlList
return config;
}, function (error) {
// close()
// 对请求错误做些什么
return Promise.reject(error);
});
//添加响应拦截器
service.interceptors.response.use(function (response: any) {
// close()
// 对响应数据做点什么
const urlList = responseUrlList
if (
response.data.state != 200 &&
!newMatchApiPath(
response.config.url,
urlList
)
) {
if (
(response.data.state == "401" ||
response.data.state == "403") &&
tokenFlag
) {
//token过期,返回用户中心登录页面
tokenFlag = false;
let token = window.name;
let url = (<any>window).global_url.user_center_web_url;
window.open(url + "#/login", token || "_self");
} else {
}
}
return response;
}, function (error) {
// close()
// 对响应错误做点什么
return Promise.reject(error);
});
return service
}
export default http
import service from "./http"
import signature from '../utils/signature';
const request = (baseUrl: string, object: any) => {
const http = service(baseUrl, object)
const get = (url: string, params?: any) => {
params.Sign = signature.getSign(params)
params.appid = "test"
return new Promise((resolve, reject) => {
http.get(
url, params
).then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
})
}
const post = (url: string, params?: any, responseType?: any) => {
params.Sign = signature.getSign(params)
params.appid = "test"
return new Promise((resolve, reject) => {
http.post(
url,
params,
responseType
).then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
})
}
const put = (url: string, params?: any, responseType?: any) => {
params.Sign = signature.getSign(params)
params.appid = "test"
return new Promise((resolve, reject) => {
http.put(
url,
params,
responseType
).then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
})
}
const del = (url: string, params?: any) => {
params.Sign = signature.getSign(params)
params.appid = "test"
return new Promise((resolve, reject) => {
http.delete(
url,
params,
).then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
})
}
return {
get,
post,
put,
del
}
}
export default request
import request from "./request.js";
const requestUrlList: never[] = [];
const responseUrlList: never[] = [];
const baseURL = "https://test.com";
const { get, post, put, del } = request(baseURL, {
requestUrlList,
responseUrlList,
});
export function getUserInfo(data: any) {
return post(`/user/get_userinfo.htm`, data);
}
function Sha1Hex(sIn:any) {
var _this = this;
var x = _this.align(sIn);
var w = new Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j < 80; j++) {
if (j < 16) w[j] = x[i + j];
else w[j] = _this.rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
var t = _this.add(_this.add(_this.rol(a, 5), _this.ft(j, b, c, d)), _this.add(_this.add(e, w[j]), _this.kt(j)));
e = d;
d = c;
c = _this.rol(b, 30);
b = a;
a = t;
}
a = _this.add(a, olda);
b = _this.add(b, oldb);
c = _this.add(c, oldc);
d = _this.add(d, oldd);
e = _this.add(e, olde);
}
_this.sha1Value = _this.hex(a) + _this.hex(b) + _this.hex(c) + _this.hex(d) + _this.hex(e);
//return SHA1Value/*.toUpperCase()*/;
}
Sha1Hex.prototype.add = function (x, y) {
return ((x & 0x7FFFFFFF) + (y & 0x7FFFFFFF)) ^ (x & 0x80000000) ^ (y & 0x80000000);
};
Sha1Hex.prototype.hex = function (num) {
var sHEXChars = "0123456789abcdef";
var str = "";
for (var j = 7; j >= 0; j--)
str += sHEXChars.charAt((num >> (j * 4)) & 0x0F);
return str;
};
Sha1Hex.prototype.align = function (sIn) {
var nblk = ((sIn.length + 8) >> 6) + 1,
blks = new Array(nblk * 16);
for (var i = 0; i < nblk * 16; i++) blks[i] = 0;
for (i = 0; i < sIn.length; i++)
blks[i >> 2] |= sIn.charCodeAt(i) << (24 - (i & 3) * 8);
blks[i >> 2] |= 0x80 << (24 - (i & 3) * 8);
blks[nblk * 16 - 1] = sIn.length * 8;
return blks;
};
Sha1Hex.prototype.rol = function (num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
};
Sha1Hex.prototype.ft = function (t, b, c, d) {
if (t < 20) return (b & c) | ((~b) & d);
if (t < 40) return b ^ c ^ d;
if (t < 60) return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
};
Sha1Hex.prototype.kt = function (t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514;
};
function utf16to8(str) {
var out, i, len, c;
out = "";
len = str.length;
for (i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i)
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F))
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F))
}
}
return out
}
var uTF8Encode = function (string) {
string = string.replace(/\x0d\x0a/g, "\x0a");
var output = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
output += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
output += String.fromCharCode((c >> 6) | 192);
output += String.fromCharCode((c & 63) | 128);
} else {
output += String.fromCharCode((c >> 12) | 224);
output += String.fromCharCode(((c >> 6) & 63) | 128);
output += String.fromCharCode((c & 63) | 128);
}
}
return output;
};
function sha1(sIn) {
// console.log(sIn);
// sIn = uTF8Encode(sIn);
var hex = new Sha1Hex(sIn);
// console.log(hex.sha1Value);
return hex.sha1Value;
}
function htly_shasign(app_id: any, sign_str: any, app_secret: any) {
var for_sign = app_id + sign_str + app_secret;
for_sign = unescape(decodeURIComponent(for_sign));
return sha1(for_sign);
}
export default htly_shasign;
/**
* 签名插件
*/
import htly_shasign from '../utils/sha1.ts';
export default {
// 参数集合
assemble(param: any) {
if (typeof (param) !== 'object' || param === null) return '';
var key = Object.keys(param).sort();
var str = '';
for (var n = 0; n < key.length; n++) {
if (typeof (param[key[n]]) != "undefined") {
str += key[n] + '=' + ((typeof (param[key[n]]) == 'object' ? this.assemble(param[key[n]]) : encodeURIComponent(param[key[n]]))) + '&';
}
}
str = str.substring(0, str.length - 1);
return str;
},
// 获取签名
getSign(param: any) {
var app_id = 'test', //签名id
app_secret = 'test', //签名密钥
sign_str = this.assemble(param);
// console.log(sign_str)
return (htly_shasign(app_id, sign_str, app_secret))
}
}