yoho.js
1.21 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
const url = require('url');
const _ = require('lodash');
const md5 = require('yoho-md5');
const yoho = {
getSign(params, clientSecret) {
let secretParams = {},
secretStr = '';
for (const k of Object.keys(params).sort()) {
if (k === 'yh_sign') {
continue;
}
secretParams[k] = params[k];
}
secretStr = _.join(_.map(secretParams, (v, k) => {
return `${k}=${v}`;
}), '&');
return md5(secretStr + clientSecret);
},
/**
* refer限制
* @param referer string
* 1. array: ['/login', '/signin'] 如果referer 在array中,将返回 /home
* 2. function: 如果返回true, 返回/home
* @return referer
*/
refererLimit(referer) { // eslint-disable-line
let result;
try {
result = decodeURIComponent(referer || '/home');
} catch (e) {
result = referer || '/home';
}
let urlObj = url.parse(result, false, true);
if (urlObj.hostname && !/(?:yohobuy\.com$)|(?:yoho\.cn$)/.test(urlObj.hostname)) {
result = '/home';
}
return result;
}
};
module.exports = yoho;