添加分页功能、打包优化、ts编译优化

This commit is contained in:
周炽键
2021-07-23 21:34:47 +08:00
parent fd85fcef74
commit 6a1f9a3f6b
15 changed files with 3340 additions and 141 deletions

57
utils/auth.js Normal file
View File

@@ -0,0 +1,57 @@
const isJSON = require('is-json')
const CryptoJS = require('crypto-js')
const keyHex = process.env.AES
function encode (payload, timestamp = 0) {
const data = {
payload,
expired_timestamp: timestamp ? timestamp + Date.now() : 0,
}
const messageHex = CryptoJS.enc.Utf8.parse(JSON.stringify(data))
var encrypted = CryptoJS.AES.encrypt(messageHex, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
})
return encrypted.toString()
}
function decode (encrypted) {
const errmsg = '登录凭证无效'
try{
var decrypt = CryptoJS.enc.Utf8.stringify(
CryptoJS.AES.decrypt(encrypted, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
}),
)
}catch(err) {
throw new Error(errmsg)
}
if(!isJSON(decrypt)) {
throw new Error(errmsg)
}
var payload = JSON.parse(decrypt)
if (!payload.payload) {
throw new Error(errmsg)
}
if (Date.now() >= payload.expired_timestamp) {
throw new Error('登录凭证已过期')
}
return payload.payload
}
module.exports = {
encode,
decode
}

6
utils/id.ts Normal file
View File

@@ -0,0 +1,6 @@
import { v4 as uuid } from 'uuid'
import md5 from 'md5'
export function md5_uuid ():string {
return md5(uuid())
}