api.js
3.93 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
179
180
181
182
183
184
185
186
187
188
189
190
import Taro from '@tarojs/taro';
import config from '../config';
import udid from './udid';
import { HmacSHA256, MD5 } from './crypto';
import { stringify } from 'querystringify';
import user from './user';
export const verify = {
verifyMethod: 'ufo.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) {
Taro.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 = Taro.getStorageSync('verifyKey');
if (!verifyKey) {
this.gen(); // 异步更新
}
return verifyKey || '';
},
sortData(data) {
let newData = {};
Object.keys(data).sort().forEach(key => {
newData[key] = String(data[key]).trim();
});
return newData;
},
stringify(data) {
let str = '';
Object.keys(data).forEach((key, index) => {
str += index !== 0 ? `&${key}=${data[key]}` : `${key}=${data[key]}`;
});
return str;
},
sign(data) {
const verifyKey = this.get();
if (!verifyKey) {
return '';
}
data = this.stringify(data);
return HmacSHA256(data, verifyKey).toString();
},
computeSecret(data, encode = false) {
let newData = {};
data.private_key = config.apiParams.private_key;
newData = this.sortData(data);
newData.client_secret = MD5(stringify(newData, encode)).toString();
delete newData.private_key;
newData = this.sortData(newData)
return newData;
}
};
const api = {
request(params) {
// 公有参数
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 Taro.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) {
return resultData.data || {};
}
return resultData;
} else {
return result && result.data || {};
}
}).catch(err => {
return {};
});
},
addSessionKey(params) {
params = Object.assign({}, params);
let sessionKey = user.getYHStorageSync('session_key', 'app');
let userInfo = user.getYHStorageSync('userInfo', 'app') || {};
if (sessionKey && params.data.method != "ufo.resource.get") {
params.data.session_key = sessionKey;
if (!params.data.uid) {
params.data.uid = userInfo.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,
udid: udid.get(),
business_line: config.business_line,
source_type: 'wechat',
user_source: 'wechat'
}, 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;