增加公共请求方法配置
This commit is contained in:
64
utils/encryptUtil.js
Normal file
64
utils/encryptUtil.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import { SM4 } from 'gm-crypto'
|
||||
import {encryptKey} from '@/commons/config.js'
|
||||
|
||||
let HEX_KEY = null
|
||||
|
||||
// 字符串转16进制
|
||||
function str2hex(str) {
|
||||
var val = ''
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
if (val == '')
|
||||
val = str.charCodeAt(i).toString(16)
|
||||
else
|
||||
val += str.charCodeAt(i).toString(16)
|
||||
}
|
||||
val += ''
|
||||
return val
|
||||
}
|
||||
|
||||
// 获取hex秘钥
|
||||
function getHexKey(){
|
||||
|
||||
if(!HEX_KEY){
|
||||
|
||||
HEX_KEY = str2hex(encryptKey)
|
||||
|
||||
}
|
||||
return HEX_KEY
|
||||
}
|
||||
|
||||
|
||||
// 解密 (http响应数据, 做通用处理)
|
||||
export function sm4DecryptByResData(data){
|
||||
|
||||
if(!data){
|
||||
return data
|
||||
}
|
||||
|
||||
let res = SM4.decrypt(data, getHexKey(), {
|
||||
inputEncoding: 'base64',
|
||||
outputEncoding: 'utf8'
|
||||
})
|
||||
|
||||
if(!res){
|
||||
return res
|
||||
}
|
||||
|
||||
return JSON.parse(res)['originData']
|
||||
}
|
||||
|
||||
// 加密 (http响应数据, 做通用处理)
|
||||
export function sm4EncryptByReqData(data){
|
||||
if(!data){
|
||||
return data
|
||||
}
|
||||
|
||||
// 加密处理
|
||||
let encryptData = SM4.encrypt(JSON.stringify(data), getHexKey(), {
|
||||
inputEncoding: 'utf8',
|
||||
outputEncoding: 'base64'
|
||||
})
|
||||
|
||||
return {encryptData : encryptData}
|
||||
}
|
||||
|
||||
58
utils/infoBox.js
Normal file
58
utils/infoBox.js
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* 提示信息公共文件
|
||||
*/
|
||||
|
||||
const model = {
|
||||
|
||||
// uni.showToast的封装
|
||||
// 参数: 标题、 显示时长(单位: 秒), 扩展参数
|
||||
// 返回: promise对象, 当提示消失后调用 resolve()
|
||||
showToast: (title, duration = 1.5, extObject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.showToast(Object.assign({ title: title, icon: 'none', mask: false, duration: (duration * 1000) }, extObject))
|
||||
setTimeout(resolve, (duration * 1000));
|
||||
})
|
||||
},
|
||||
|
||||
// success类型的提示
|
||||
showSuccessToast: (title, duration) => {
|
||||
return model.showToast(title, duration, {icon: 'success'})
|
||||
},
|
||||
|
||||
// error类型的提示
|
||||
showErrorToast: (title, duration) => {
|
||||
return model.showToast(title, duration, {icon: 'error'})
|
||||
},
|
||||
|
||||
showLoading: (title = '请稍后' ) => {
|
||||
return uni.showLoading({ title: title, mask: true })
|
||||
},
|
||||
|
||||
hideLoading: () => {
|
||||
return uni.hideLoading()
|
||||
},
|
||||
|
||||
// 返回 Promise 点击确定和取消
|
||||
// APP安卓原生提示(比较丑), APP 不推荐使用该函数 。
|
||||
showModal: (title, confirmText = '确定', cancalText = '取消', extObject) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.showModal( Object.assign({
|
||||
title: title,
|
||||
confirmText: confirmText,
|
||||
showCancel: cancalText ? true : false,
|
||||
cancelText: cancalText,
|
||||
success: function(r) {
|
||||
if (r.confirm) {
|
||||
resolve()
|
||||
}else if (r.cancel) {
|
||||
reject()
|
||||
}
|
||||
}
|
||||
}, extObject ));
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
}
|
||||
|
||||
export default model
|
||||
Reference in New Issue
Block a user