api.js
4.85 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import tt from '../utils/tt';
import config from '../config';
import udid from './udid';
import {HmacSHA256, MD5} from '../vendors/crypto';
import {stringify} from '../vendors/query-stringify';
export const verify = {
verifyMethod: 'resources.simple.pice',
update() {
return api.get({ // eslint-disable-line
data: {
method: this.verifyMethod
}
}).then(result => {
if (result && result.code === 200 && result.data && result.data.sk) {
tt.setStorageSync('verifyKey', result.data.sk);
}
return result;
});
},
gen() {
const self = this;
let maxTryTimes = 3;
function update() {
return self.update().then(result => {
if (result && result.code === 200 && result.data && result.data.sk) {
return result.data.sk;
} else if (maxTryTimes) {
maxTryTimes--;
return update();
} else {
return '';
}
});
}
return update();
},
get() {
const verifyKey = tt.getStorageSync('verifyKey');
if (!verifyKey) {
this.gen(); // 异步更新
}
return verifyKey || '';
},
sign(data, encode = false) {
const verifyKey = this.get();
if (!verifyKey) {
return '';
}
data = stringify(data, {
encode: encode
});
return HmacSHA256(data, verifyKey).toString();
},
computeSecret(data, encode = false) {
const newData = {
private_key: config.apiParams.private_key
};
Object.keys(data).sort().forEach(key => {
newData[key] = String(data[key]).trim();
});
newData.client_secret = MD5(stringify(newData, {
encode: encode
})).toString();
delete newData.private_key;
return newData;
}
};
const api = {
request(params) {
const app = getApp();
// 公有参数
params.data = this.addParams(params.data);
params = this.addSessionKey(params);
// client_secret
params.data = verify.computeSecret(params.data);
// 接口验签
if (!params.header['x-yoho-verify'] && params.data.method !== verify.verifyMethod) {
params.header['x-yoho-verify'] = verify.sign(params.data);
}
return tt.request(params).then(result => {
if (result.statusCode === 200) {
const resultData = result && result.data || {};
delete result.data;
resultData.result = result;
if (resultData.code === 508) {
verify.gen();
}
if (resultData.code === params.code && params.code) {
return resultData.data || {};
}
return resultData;
} else if (result.statusCode === 401) {
app.clearUserSession();
} else {
console.error('api statusCode:', result.statusCode);
return result && result.data || {};
}
}).catch(err => {
console.error(err);
return {};
});
},
addSessionKey(params) {
let app = getApp();
let uid = app && app.getUid();
let sessionKey = app && app.getSessionkey();
params = Object.assign({}, params);
if (uid && sessionKey) {
params.data.session_key = sessionKey;
params.data.uid = uid;
params.header.Cookies = `JSESSIONID=${sessionKey}`;
}
return params;
},
addParams(params) {
return Object.assign({
client_type: config.apiParams.client_type,
app_version: config.apiParams.app_version,
miniapp_version: config.apiParams.miniapp_version,
udid: udid.get(),
business_line: 'miniapp'
}, params);
},
get(params) {
// 默认值
params = Object.assign({
data: {},
header: {},
dataType: 'json'
}, params, {
url: config.domains[params.api || 'api'] + (params.url || ''),
});
// 构造好参数再请求
return this.request(params);
},
post(params) {
// 默认值
params = Object.assign({
method: 'post',
data: {},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
dataType: 'json'
}, params, {
url: config.domains[params.api || 'api'] + (params.url || ''),
});
// 构造好参数再请求
return this.request(params);
}
};
export default api;