/*
|
* @Author: xio
|
* @Date: 2023-12-06 12:06:00
|
* @Last Modified by: xio
|
* @Last Modified time: 2023-12-06 12:06:00
|
*/
|
// 通用function,通过全局安装,或import引用调用
|
import config from '../config' // 域名配置
|
import md5 from 'js-md5'
|
// import axios from 'axios'
|
// 基础配置 - 获取当前环境
|
var ismock = config.ismock
|
var isTestView = config.isTestView
|
var fn_obj = {}// fn.js对象
|
var APPID, SECRET
|
if (isTestView) {
|
APPID = 'phi123'
|
SECRET = 'test123'
|
} else {
|
APPID = 'phiwyrnujZPOBRaGs'
|
SECRET = 'TmUlZWaBLQdbdpiDwMKUAjHwKdnkXMyQ'
|
}
|
/**
|
* 生成唯一id
|
*/
|
function uuid() {
|
var s = []
|
var hexDigits = '0123456789abcdef'
|
for (var i = 0; i < 36; i++) {
|
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
|
}
|
s[14] = '4'
|
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
|
|
s[8] = s[13] = s[18] = s[23] = '-'
|
var uuid = s.join('')
|
return uuid
|
}
|
|
/**
|
* 请求参数md5加密处理
|
* @param {Object} option 请求对象
|
*/
|
function httpOptionMd5Handle(option) {
|
// 当前时间戳
|
var timestamp = new Date().getTime()
|
var sign, params
|
// data参数字段排序(26字母A到Z排序)
|
var newData = {}
|
Object.keys(option.data).sort().map(key => {
|
newData[key] = option.data[key]
|
})
|
// 参数处理
|
params = JSON.stringify(newData)
|
// if (params === '{}') {
|
// params = ''
|
// }
|
// 非mock模式
|
if (option) {
|
option.header[`appId`] = APPID
|
option.header[`timestamp`] = timestamp
|
// 签名大写
|
sign = md5(`${APPID}${timestamp}${params}${SECRET}`).toUpperCase()
|
option.header[`sign`] = sign
|
}
|
console.log('==============================>', option, params, sign)
|
return option
|
}
|
|
var fn = {
|
fn_obj,
|
uuid,
|
httpOptionMd5Handle,
|
/**
|
* 深拷贝
|
* @param {object} obj 被复制的对象
|
* @return {object} 复制完成的对象
|
*/
|
deepCopyFN(obj) {
|
if (typeof obj !== 'object') {
|
return obj
|
}
|
|
let cloneObj = {}
|
switch (obj.constructor) {
|
case Array:
|
cloneObj = []
|
// eslint-disable-next-line no-fallthrough
|
case Object:
|
for (var property in obj) {
|
cloneObj[property] = typeof obj[property] === 'object' ? this.deepCopyFN(obj[property]) : obj[property]
|
}
|
break
|
case Map:
|
cloneObj = new Map()
|
obj.forEach((value, key) => {
|
cloneObj.set(key, typeof value === 'object' ? this.deepCopyFN(value) : value)
|
})
|
break
|
case Set:
|
cloneObj = new Set()
|
obj.forEach(value => {
|
cloneObj.add(typeof value === 'object' ? this.deepCopyFN(value) : value)
|
})
|
break
|
}
|
return cloneObj
|
}
|
}
|
export default fn
|