提交 | 用户 | age
|
6da3c1
|
1 |
/* |
J |
2 |
* @Author: xio |
|
3 |
* @Date: 2023-12-06 12:06:00 |
|
4 |
* @Last Modified by: xio |
|
5 |
* @Last Modified time: 2023-12-06 12:06:00 |
|
6 |
*/ |
|
7 |
// 通用function,通过全局安装,或import引用调用 |
|
8 |
import config from '../config' // 域名配置 |
|
9 |
import md5 from 'js-md5' |
1d1095
|
10 |
// import axios from 'axios' |
6da3c1
|
11 |
// 基础配置 - 获取当前环境 |
J |
12 |
var ismock = config.ismock |
|
13 |
var isTestView = config.isTestView |
|
14 |
var fn_obj = {}// fn.js对象 |
|
15 |
var APPID, SECRET |
|
16 |
if (isTestView) { |
|
17 |
APPID = 'phi123' |
|
18 |
SECRET = 'test123' |
|
19 |
} else { |
|
20 |
APPID = 'phiwyrnujZPOBRaGs' |
|
21 |
SECRET = 'TmUlZWaBLQdbdpiDwMKUAjHwKdnkXMyQ' |
|
22 |
} |
|
23 |
/** |
|
24 |
* 生成唯一id |
|
25 |
*/ |
|
26 |
function uuid() { |
|
27 |
var s = [] |
|
28 |
var hexDigits = '0123456789abcdef' |
|
29 |
for (var i = 0; i < 36; i++) { |
|
30 |
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1) |
|
31 |
} |
|
32 |
s[14] = '4' |
|
33 |
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) |
|
34 |
|
|
35 |
s[8] = s[13] = s[18] = s[23] = '-' |
|
36 |
var uuid = s.join('') |
|
37 |
return uuid |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* 请求参数md5加密处理 |
|
42 |
* @param {Object} option 请求对象 |
|
43 |
*/ |
|
44 |
function httpOptionMd5Handle(option) { |
cf3e3e
|
45 |
// 当前时间戳 |
6da3c1
|
46 |
var timestamp = new Date().getTime() |
J |
47 |
var sign, params |
cf3e3e
|
48 |
// data参数字段排序(26字母A到Z排序) |
J |
49 |
var newData = {} |
|
50 |
Object.keys(option.data).sort().map(key => { |
|
51 |
newData[key] = option.data[key] |
|
52 |
}) |
6da3c1
|
53 |
// 参数处理 |
cf3e3e
|
54 |
params = JSON.stringify(newData) |
6da3c1
|
55 |
// if (params === '{}') { |
J |
56 |
// params = '' |
|
57 |
// } |
|
58 |
// 非mock模式 |
cf3e3e
|
59 |
if (option) { |
6da3c1
|
60 |
option.header[`appId`] = APPID |
J |
61 |
option.header[`timestamp`] = timestamp |
|
62 |
// 签名大写 |
|
63 |
sign = md5(`${APPID}${timestamp}${params}${SECRET}`).toUpperCase() |
|
64 |
option.header[`sign`] = sign |
|
65 |
} |
|
66 |
console.log('==============================>', option, params, sign) |
|
67 |
return option |
|
68 |
} |
|
69 |
|
|
70 |
var fn = { |
1d1095
|
71 |
fn_obj, |
6da3c1
|
72 |
uuid, |
J |
73 |
httpOptionMd5Handle, |
|
74 |
/** |
|
75 |
* 深拷贝 |
|
76 |
* @param {object} obj 被复制的对象 |
|
77 |
* @return {object} 复制完成的对象 |
|
78 |
*/ |
|
79 |
deepCopyFN(obj) { |
|
80 |
if (typeof obj !== 'object') { |
|
81 |
return obj |
|
82 |
} |
|
83 |
|
|
84 |
let cloneObj = {} |
|
85 |
switch (obj.constructor) { |
|
86 |
case Array: |
|
87 |
cloneObj = [] |
|
88 |
// eslint-disable-next-line no-fallthrough |
|
89 |
case Object: |
|
90 |
for (var property in obj) { |
|
91 |
cloneObj[property] = typeof obj[property] === 'object' ? this.deepCopyFN(obj[property]) : obj[property] |
|
92 |
} |
|
93 |
break |
|
94 |
case Map: |
|
95 |
cloneObj = new Map() |
|
96 |
obj.forEach((value, key) => { |
|
97 |
cloneObj.set(key, typeof value === 'object' ? this.deepCopyFN(value) : value) |
|
98 |
}) |
|
99 |
break |
|
100 |
case Set: |
|
101 |
cloneObj = new Set() |
|
102 |
obj.forEach(value => { |
|
103 |
cloneObj.add(typeof value === 'object' ? this.deepCopyFN(value) : value) |
|
104 |
}) |
|
105 |
break |
|
106 |
} |
|
107 |
return cloneObj |
|
108 |
} |
|
109 |
} |
|
110 |
export default fn |