OA/utils/encrypt.js

66 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2023-11-05 00:11:08 +08:00
import Base64 from "@/utils/base64.js"
import cryptoJS from "@/node_modules/crypto-js"
const key_code = 'gxzhptxiaochengxu_1';
// 加密
const encode = (key, data='')=>{
try{
let str;
typeof data == 'object'? str = JSON.stringify(data) : str = data;
str = cryptoJS.AES.encrypt(str, key_code).toString();
uni.setStorageSync(key, str)
return true;
}catch(e){
2023-11-20 18:25:05 +08:00
//console.log(e);
2023-11-05 00:11:08 +08:00
return false;
}
}
// 解密
const decode = (key)=>{
try{
let str = uni.getStorageSync(key);
str = cryptoJS.AES.decrypt(str, key_code).toString(cryptoJS.enc.Utf8);
isJSON(str) ? str = JSON.parse(str) : null;
return str;
}catch(e){
2023-11-20 18:25:05 +08:00
//console.log(e);
2023-11-05 00:11:08 +08:00
return undefined;
}
}
const isJSON = (str)=>{
try{
JSON.parse(str)
return true;
}catch(e){
return false;
}
}
const LOGIN = ()=>{
return new Promise((reslove, reject) => {
uni.request({
url: Url + '/api' + url,
method: 'POST',
data: data,
success: (res) => {
},
fail: (message) => {
uni.showToast({
title: '网络错误',
icon: 'none'
})
reject('请求失败');
}
})
});
}
export default {
encode,
decode
}