1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import axios from 'axios'
import md5 from "js-md5"
// let BASE_API = "https://gd.chfatech.com/guangdian"
// let BASE_API = "/api/"
let BASE_API = "/api"
const zlog = console.log.bind(console)
// URL 编码与拼接
const createSign = (inPostData, inAppSecret) => {
let appUser = inPostData.appUser
let appCode = inPostData.appCode
let ts = inPostData.ts
let args = JSON.parse(JSON.stringify(inPostData.args));
let appSecret = inAppSecret
let res = "";
// URL 编码
for (let key in args) {
res += encodeURIComponent(key) + "%3D" + encodeURIComponent(args[key]) + "%26";
}
res = res.slice(0, -3);
// URL 拼接
res += `&appUser=${appUser}&appCode=${appCode}&${ts}${appSecret}`
return res;
};
// Basic Info
let basicInfo = {
appUser: "YBF001",
ver: "v2.17",
appSecret: "t04yYm6gjsuHeehxOxojtmiwlYfXY8Zkdowf"
}
// 创建 axios 实例
const service = axios.create({
baseURL: BASE_API,
timeout: 20000
})
// request 拦截器
service.interceptors.request.use(
req => {
// zlog('--->axios: req.params: start:', req.params)
let apiMethod = req.method
if (apiMethod === 'post') {
// zlog('--->axios: req.data:', req.data)
let oldPostData = JSON.parse(JSON.stringify(req.data))
let newPostData = {
appUser: basicInfo.appUser,
appCode: oldPostData.args.appCode,
// ts: String(Date.parse(new Date())),
// ts: String(parseInt(new Date().getTime()/1000)),
ts: '1563355520',
ver: basicInfo.ver,
args: oldPostData.args
}
delete newPostData.args.appCode
let postData = {
appUser: newPostData.appUser,
appCode: newPostData.appCode,
ts: newPostData.ts,
sign: md5(createSign(newPostData, basicInfo.appSecret)),
ver: newPostData.ver,
args: newPostData.args
}
req.data = postData
}
if (apiMethod === 'get') {
// log('--->axios: req.data:', req.data)
let oldPostData = JSON.parse(JSON.stringify(req.params))
let newPostData = {
appUser: basicInfo.appUser,
appCode: oldPostData.args.appCode,
ts: String(Date.parse(new Date())),
ver: basicInfo.ver,
args: oldPostData.args
}
delete newPostData.args.appCode
let postData = {
appUser: newPostData.appUser,
appCode: newPostData.appCode,
ts: newPostData.ts,
sign: md5(createSign(newPostData, basicInfo.appSecret)),
ver: newPostData.ver,
args: newPostData.args
}
req.params = postData
}
// zlog('--->axios: req.params: end:', req.params)
return req
},
error => {
Promise.reject(error)
}
)
// response 拦截器
service.interceptors.response.use(
response => {
const res = response.data
return res
},
error => {
return Promise.reject(error)
}
)
export default service