北斗应用技术培训报名系统题库

This commit is contained in:
2026-03-27 17:55:17 +08:00
commit ab1d364524
42 changed files with 1301 additions and 0 deletions

156
app/controller/home.js Normal file
View File

@@ -0,0 +1,156 @@
const Controller = require('../core/base_controller');
const fileName = "Home";
class HomeController extends Controller {
// 题库登录接口
async login() {
const { ctx } = this;
try {
const body = ctx.request.body;
const result = await ctx.service.home.login(body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
await ctx.app.redis.get('db3').set(result.dataValues.user_uid, result.dataValues.type, 'EX', 21600);
this.setResponse(ctx, 200, '操作成功', "polymerization", this.encryptRequest(result.dataValues.user_uid + '?' + new Date().getTime()));
} else {
this.setResponse(ctx, 404, '操作失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "login";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 获取题库登录状态接口
async loginStatus() {
const { ctx } = this;
try {
const data = ctx.request.body.data;
const topic = await ctx.app.redis.get('db3').get(this.decryptRequest(data).split('?')[0]);
if (topic == null) {
this.setResponse(ctx, 404, '操作失败', "none");
} else {
this.setResponse(ctx, 200, '操作成功', "none");
}
} catch (error) {
const blockName = fileName + "--" + "loginStatus";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 获取单选题接口
async radio() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const body = ctx.request.body;
const result = await ctx.service.home.radio(body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '操作成功', "deconstruction", result);
} else {
this.setResponse(ctx, 404, '操作失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "radio";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 获取多选题接口
async multiple() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const body = ctx.request.body;
const result = await ctx.service.home.multiple(body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '操作成功', "deconstruction", result);
} else {
this.setResponse(ctx, 404, '操作失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "multiple";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 获取判断题接口
async judgment() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const body = ctx.request.body;
const result = await ctx.service.home.judgment(body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '操作成功', "deconstruction", result);
} else {
this.setResponse(ctx, 404, '操作失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "judgment";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 获取问答题接口
async interlocution() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const body = ctx.request.body;
const result = await ctx.service.home.interlocution(body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '操作成功', "deconstruction", result);
} else {
this.setResponse(ctx, 404, '操作失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "interlocution";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = HomeController;

133
app/core/base_controller.js Normal file
View File

@@ -0,0 +1,133 @@
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('');
}
// 随机生成12位客户端id
generateClientId = () => {
const numbers = '0123456789';
let result = '';
// 生成4位随机数字
for (let i = 0; i < 4; 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]];
}
// 获取当前日期(年月日)
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 fullNumberString = dateString + characters.join('');
return fullNumberString;
}
checkUid = async (ctx) => {
const token = ctx.request.header.authorization;
const redis = ctx.app.redis;
const topic = await redis.get('db3').get(this.decryptRequest(token).split('?')[0]);
return topic || false;
};
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;

88
app/core/base_service.js Normal file
View File

@@ -0,0 +1,88 @@
const { Service } = require('egg');
const { Op } = require('sequelize');
const sha256 = require("crypto-js/sha256");
const CryptoJS = require("crypto-js");
const KEY = CryptoJS.enc.Utf8.parse('BDQH');
class BaseService extends Service {
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 = () => {
// 获取当前日期(年月日)
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(-4);
// 生成四位随机数字
const randomDigits = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
// 组合成最终的16位数字字符串
const fullNumberString = dateString + timestamp + randomDigits;
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('');
}
}
module.exports = BaseService;

72
app/middleware/log.js Normal file
View File

@@ -0,0 +1,72 @@
// 错误日志
const fs = require('fs');
const os = require('os');
const path = require('path');
const dirCheck = (dirPath) => {
// 检查文件夹是否存在
fs.access(dirPath, fs.constants.F_OK, (err) => {
if (err) {
// 文件夹不存在
console.log('文件夹不存在,正在创建...');
fs.mkdir(dirPath, { recursive: true }, (err) => {
if (err) throw err;
console.log('文件夹已创建');
});
} else {
// 文件夹存在
console.log('文件夹已存在');
}
});
}
module.exports = (filePath, msg) => {
// 获取时间
let now = new Date(),
times = {
year: now.getFullYear(),
month: now.getMonth() + 1,
day: now.getDate(),
hour: now.getHours(),
minute: now.getMinutes(),
second: now.getSeconds()
};
// 获取操作系统类型
const type = os.type();
console.log(`操作系统类型: ${type}`);
filePath = path.join(__dirname, filePath);
dirCheck(filePath);
const yearPath = filePath + '/' + times.year;
dirCheck(yearPath);
const monthPath = yearPath + '/' + times.month;
dirCheck(monthPath);
const dayPath = monthPath + '/' + times.day + '.txt';
const logTime = '[' + times.year + '-' + times.month + '-' + times.day + '>>>' + times.hour + ':' + times.minute + ':' + times.second + ']' + '>>>';
let crlf;
// 判断操作系统类型,设置换行符
if (type === 'Windows_NT') {
crlf = '\n';
} else {
crlf = '\r\n';
}
// 读取文件内容
fs.readFile(dayPath, 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
// 文件不存在,直接写入新内容(这里实际上还是使用了覆盖的方式)
fs.writeFile(dayPath, logTime + msg + crlf, (err) => {
if (err) throw err;
console.log('文件已创建并写入新内容');
});
} else {
throw err;
}
} else {
// 文件存在,追加新内容
fs.appendFile(dayPath, logTime + msg + crlf, (err) => {
if (err) throw err;
console.log('新内容已追加到文件');
});
}
});
};

31
app/model/Examinee.js Normal file
View File

@@ -0,0 +1,31 @@
'use strict';
module.exports = (app) => {
const { INTEGER, CHAR, STRING, DATE } = app.Sequelize;
const Examinee = app.model.define('examinee', {
id: { type: INTEGER(8), primaryKey: true, autoIncrement: true },
uid: CHAR(15),
salt: CHAR(24),
password: CHAR(64),
user_name: STRING(10),
identity_card: CHAR(18),
real_check: INTEGER(3),
workplace: STRING(60),
address: STRING(120),
phone: CHAR(11),
email: STRING(60),
referrers: STRING(60),
status: INTEGER(3),
created_at: DATE,
updated_at: DATE,
login_status: INTEGER(3),
},{
freezeTableName: true,
timestamp: true,
createdAt: true,
updatedAt: true,
});
return Examinee;
};

19
app/model/Whitelist.js Normal file
View File

@@ -0,0 +1,19 @@
'use strict';
module.exports = (app) => {
const { INTEGER, CHAR, DATE } = app.Sequelize;
const Whitelist = app.model.define('whitelist', {
id: { type: INTEGER(5), primaryKey: true, autoIncrement: true },
user_uid: CHAR(15),
type: INTEGER(3),
expired_at: DATE,
},{
freezeTableName: true,
timestamp: true,
createdAt: false,
updatedAt: false,
});
return Whitelist;
};

View File

@@ -0,0 +1,19 @@
'use strict';
module.exports = (app) => {
const { INTEGER, STRING } = app.Sequelize;
const Interlocution = app.model2.define('interlocution', {
id: { type: INTEGER(5), primaryKey: true, autoIncrement: true },
stem: STRING(400),
answer: STRING(400),
difficulty: INTEGER(3),
},{
freezeTableName: true,
timestamp: false,
createdAt: false,
updatedAt: false,
});
return Interlocution;
};

19
app/model2/Judgment.js Normal file
View File

@@ -0,0 +1,19 @@
'use strict';
module.exports = (app) => {
const { INTEGER, STRING } = app.Sequelize;
const Judgment = app.model2.define('judgment', {
id: { type: INTEGER(5), primaryKey: true, autoIncrement: true },
stem: STRING(400),
answer: INTEGER(1),
difficulty: INTEGER(3),
},{
freezeTableName: true,
timestamp: false,
createdAt: false,
updatedAt: false,
});
return Judgment;
};

24
app/model2/Multiple.js Normal file
View File

@@ -0,0 +1,24 @@
'use strict';
module.exports = (app) => {
const { INTEGER, STRING } = app.Sequelize;
const Multiple = app.model2.define('multiple', {
id: { type: INTEGER(5), primaryKey: true, autoIncrement: true },
stem: STRING(400),
options_a: STRING(400),
options_b: STRING(400),
options_c: STRING(400),
options_d: STRING(400),
options_e: STRING(400),
answer: STRING(5),
difficulty: INTEGER(3),
},{
freezeTableName: true,
timestamp: false,
createdAt: false,
updatedAt: false,
});
return Multiple;
};

23
app/model2/Radio.js Normal file
View File

@@ -0,0 +1,23 @@
'use strict';
module.exports = (app) => {
const { INTEGER, STRING, CHAR } = app.Sequelize;
const Radio = app.model2.define('radio', {
id: { type: INTEGER(5), primaryKey: true, autoIncrement: true },
stem: STRING(400),
options_a: STRING(400),
options_b: STRING(400),
options_c: STRING(400),
options_d: STRING(400),
answer: CHAR(1),
difficulty: INTEGER(3),
},{
freezeTableName: true,
timestamp: false,
createdAt: false,
updatedAt: false,
});
return Radio;
};

19
app/router.js Normal file
View File

@@ -0,0 +1,19 @@
/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller } = app;
// 题库登录接口
router.post("/login", controller.home.login);
// 获取题库登录状态接口
router.post("/loginStatus", controller.home.loginStatus);
// 获取单选题接口
router.post("/radio", controller.home.radio);
// 获取多选题接口
router.post("/multiple", controller.home.multiple);
// 获取判断题接口
router.post("/judgment", controller.home.judgment);
// 获取问答题接口
router.post("/interlocution", controller.home.interlocution);
};

119
app/service/home.js Normal file
View File

@@ -0,0 +1,119 @@
'use strict';
const Service = require('../core/base_service');
const { Op } = require('sequelize');
const fileName = "Home";
class HomeService extends Service {
// 题库登录接口
async login(body) {
const { ctx } = this;
try {
const phone = this.decryptRequest(body.phone);
const password = this.decryptRequest(body.password);
let attributes = ['uid', 'salt', 'password'], where = { phone }, dbOptions;
dbOptions = { attributes, where }
const user = await ctx.model.Examinee.findOne(dbOptions);
if (user == null) return false;
if (user.dataValues.password != this.shaForm(password, this.decryptRequest(user.dataValues.salt))) return false;
const result = await ctx.model.Whitelist.findOne({ where: { user_uid: user.dataValues.uid } });
if (result == null) return false;
if (result.dataValues.type == '0') return result;
console.log(result.dataValues)
console.log(new Date())
if (result.dataValues.type == '1' && result.dataValues.expired_at < new Date()) {
return false;
} else {
return result;
}
} catch (error) {
const blockName = fileName + "--" + "login";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 获取单选题接口
async radio(body) {
const { ctx } = this;
try {
let limit = 1;
let { offset } = body;
offset = (offset - 1) * limit;
let dbOptions = { offset, limit }
const result = await ctx.model2.Radio.findAndCountAll(dbOptions);
await ctx.model2.Radio.min('id').then(min => {
result.min = min;
});
return result.rows.length == 0 ? false : result;
} catch (error) {
const blockName = fileName + "--" + "radio";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 获取多选题接口
async multiple(body) {
const { ctx } = this;
try {
let limit = 1;
let { offset } = body;
offset = (offset - 1) * limit;
let dbOptions = { offset, limit }
const result = await ctx.model2.Multiple.findAndCountAll(dbOptions);
await ctx.model2.Multiple.min('id').then(min => {
result.min = min;
});
return result.rows.length == 0 ? false : result;
} catch (error) {
const blockName = fileName + "--" + "multiple";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 获取判断题接口
async judgment(body) {
const { ctx } = this;
try {
let limit = 1;
let { offset } = body;
offset = (offset - 1) * limit;
let dbOptions = { offset, limit }
const result = await ctx.model2.Judgment.findAndCountAll(dbOptions);
await ctx.model2.Judgment.min('id').then(min => {
result.min = min;
});
return result.rows.length == 0 ? false : result;
} catch (error) {
const blockName = fileName + "--" + "judgment";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 获取问答题接口
async interlocution(body) {
const { ctx } = this;
try {
let limit = 1;
let { offset } = body;
offset = (offset - 1) * limit;
let dbOptions = { offset, limit }
const result = await ctx.model2.Interlocution.findAndCountAll(dbOptions);
await ctx.model2.Interlocution.min('id').then(min => {
result.min = min;
});
return result.rows.length == 0 ? false : result;
} catch (error) {
const blockName = fileName + "--" + "interlocution";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = HomeService;