北斗启航科技教育平台登录系统
This commit is contained in:
95
app/core/base_controller.js
Normal file
95
app/core/base_controller.js
Normal file
@@ -0,0 +1,95 @@
|
||||
const { Controller } = require('egg');
|
||||
|
||||
const sha256 = require("crypto-js/sha256");
|
||||
|
||||
const CryptoJS = require("crypto-js");
|
||||
|
||||
const KEY = CryptoJS.enc.Utf8.parse('BDQH');
|
||||
|
||||
class BaseController extends Controller {
|
||||
shaForm = (body, salt) => {
|
||||
return sha256(sha256(body).toString() + salt).toString();
|
||||
}
|
||||
|
||||
encryptRequest = (body) => {
|
||||
return CryptoJS.AES.encrypt(body, KEY, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
}).toString();
|
||||
}
|
||||
|
||||
decryptRequest = (body) => {
|
||||
return CryptoJS.AES.decrypt(body, KEY, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
}).toString(CryptoJS.enc.Utf8);
|
||||
}
|
||||
|
||||
// 用户uid生成函数
|
||||
generateNumberString = (side) => {
|
||||
// 获取当前日期(年月日)
|
||||
const now = new Date();
|
||||
const year = String(now.getFullYear()).padStart(4, '0');
|
||||
const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始,所以加1
|
||||
const date = String(now.getDate()).padStart(2, '0');
|
||||
const dateString = year + month + date;
|
||||
|
||||
// 获取当前时间戳的后三位
|
||||
const timestamp = String(Date.now()).slice(-3);
|
||||
|
||||
// 生成三位随机数字
|
||||
const randomDigits = Math.floor(Math.random() * 100).toString().padStart(3, '0');
|
||||
|
||||
// 组合成最终的15位字符串
|
||||
const fullNumberString = dateString + timestamp + randomDigits + side;
|
||||
|
||||
return fullNumberString;
|
||||
}
|
||||
|
||||
// 随机盐生成函数
|
||||
generateRandomString = () => {
|
||||
const lowerCaseLetters = 'abcdefghijklmnopqrstuvwxyz';
|
||||
const upperCaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
const numbers = '0123456789';
|
||||
|
||||
let result = '';
|
||||
|
||||
// 生成5位随机小写字母
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * lowerCaseLetters.length);
|
||||
result += lowerCaseLetters[randomIndex];
|
||||
}
|
||||
|
||||
// 生成5位随机大写字母
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * upperCaseLetters.length);
|
||||
result += upperCaseLetters[randomIndex];
|
||||
}
|
||||
|
||||
// 生成2位随机数字
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * numbers.length);
|
||||
result += numbers[randomIndex];
|
||||
}
|
||||
|
||||
// 打乱整个字符串
|
||||
const characters = result.split('');
|
||||
for (let i = characters.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[characters[i], characters[j]] = [characters[j], characters[i]];
|
||||
}
|
||||
|
||||
return characters.join('');
|
||||
}
|
||||
|
||||
setResponse = (ctx, code, msg, model, data = {}) => {
|
||||
const modelType = {
|
||||
"none": { code, msg },
|
||||
"deconstruction": { code, msg, ...data },
|
||||
"polymerization": { code, msg, data }
|
||||
}
|
||||
ctx.body = modelType[model];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BaseController;
|
||||
Reference in New Issue
Block a user