first commit
This commit is contained in:
1
node+vue/shop-bend/utils/.pydio
Normal file
1
node+vue/shop-bend/utils/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
83d864a2-4667-4ed1-8e48-e4663cb53c47
|
||||
73
node+vue/shop-bend/utils/index.js
Executable file
73
node+vue/shop-bend/utils/index.js
Executable file
@@ -0,0 +1,73 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const config = require("../config");
|
||||
const NodeRSA = require('node-rsa');
|
||||
const md5 = require('md5');
|
||||
|
||||
class Utils {
|
||||
constructor() {
|
||||
// 私钥
|
||||
this.rsa_private_key = this.ReadFile(this.GetRsaPath(config.dev.rsa.privateKey))
|
||||
// 公钥
|
||||
this.rsa_public_key = this.ReadFile(this.GetRsaPath(config.dev.rsa.publicKey))
|
||||
}
|
||||
|
||||
/**
|
||||
* 前端 :使用RSA的私钥进行加密 rsa_private_key
|
||||
*/
|
||||
encrypt(text) {
|
||||
return (new NodeRSA(this.rsa_private_key)).encryptPrivate(text, 'base64')
|
||||
}
|
||||
|
||||
/**
|
||||
* 前端 : 使用私钥解密json数据给前端
|
||||
*/
|
||||
Private_decrypted(text) {
|
||||
return (new NodeRSA(this.rsa_private_key)).decrypt(text, 'utf8')
|
||||
}
|
||||
|
||||
/**
|
||||
* 后端 : 使用RSA的公钥进行解密 rsa_public_key
|
||||
*/
|
||||
decrypted(text) {
|
||||
return (new NodeRSA(this.rsa_public_key)).decryptPublic(text, 'utf8')
|
||||
}
|
||||
|
||||
/**
|
||||
* 后端 :使用公钥加密json数据给前端
|
||||
*/
|
||||
public_encrypt(text) {
|
||||
return (new NodeRSA(this.rsa_public_key)).encrypt(text, 'base64')
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步读取文件
|
||||
* @param {*string} path 图片的地址
|
||||
*/
|
||||
ReadFile(path) {
|
||||
return fs.readFileSync(path).toString("utf-8")
|
||||
}
|
||||
/**
|
||||
* 获取RSA文件地址
|
||||
* @param {*string} url 获取公私钥文件地址
|
||||
*/
|
||||
GetRsaPath(url) {
|
||||
return path.join(__dirname, url)
|
||||
}
|
||||
/**
|
||||
* md5加密
|
||||
* @param {*string} text 需要被加密的字符串
|
||||
*/
|
||||
md5Text(text) {
|
||||
return md5(text)
|
||||
}
|
||||
|
||||
Timemap() {
|
||||
return Date.parse(new Date())
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = new Utils()
|
||||
29
node+vue/shop-bend/utils/ver.js
Executable file
29
node+vue/shop-bend/utils/ver.js
Executable file
@@ -0,0 +1,29 @@
|
||||
const utils = require("./index")
|
||||
const cache = require("../cache")
|
||||
const config = require("../config")
|
||||
module.exports = {
|
||||
/**
|
||||
* 路由拦截器,拦截路由验证是否有token参数,redis中是否已经有数据缓存
|
||||
*/
|
||||
async Verification(req, res, next) {
|
||||
// 验证请求头是否包含token
|
||||
if (req.headers.hasOwnProperty("token")) {
|
||||
// 验证redis是否有缓存,没有则去获取mysq然后缓存,有直接读取缓存
|
||||
if (await cache.Get(req.url) == null) {
|
||||
req.unparams = utils.decrypted(req.headers.token)
|
||||
next()
|
||||
} else {
|
||||
res.json({
|
||||
status: config.dev.api.success,
|
||||
cache: true,
|
||||
data: await cache.Get(req.url)
|
||||
})
|
||||
}
|
||||
} else {
|
||||
res.json({
|
||||
status: config.dev.api.CodeError,
|
||||
data: "缺少必填参数,请求头token"
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user