|
|
@@ -0,0 +1,56 @@
|
|
|
+import dayjs from 'dayjs';
|
|
|
+import { Audit } from '../model/index.js';
|
|
|
+import { generateCaptcha, ipLocResFu } from '../util/index.js';
|
|
|
+import resSend from '../util/resSend.js';
|
|
|
+import { isEnv } from '../config/config.default.js';
|
|
|
+
|
|
|
+// 需要做定时器处理,防止短时间多次发送
|
|
|
+let loginFlag: any = {};
|
|
|
+
|
|
|
+export const clearAuditCode = () => {
|
|
|
+ loginFlag = {};
|
|
|
+};
|
|
|
+
|
|
|
+const audit = {
|
|
|
+ getCode: async (req: any, res: any) => {
|
|
|
+ req.apiDescription = '展示端-获取验证码';
|
|
|
+ const clientIp = ipLocResFu(req);
|
|
|
+
|
|
|
+ const captcha = generateCaptcha();
|
|
|
+ // 将验证码文本存入session(小写以便不区分大小写校验)
|
|
|
+ const captchaTxt = captcha.text.toLowerCase();
|
|
|
+ if (loginFlag[clientIp]) loginFlag[clientIp].loginFlagCode = captchaTxt;
|
|
|
+ else loginFlag[clientIp] = { loginFlagCode: captchaTxt };
|
|
|
+
|
|
|
+ // console.log('生成的验证码(开发调试用):', captchaTxt); // 调试时可查看
|
|
|
+
|
|
|
+ // 设置响应头,告诉浏览器这是SVG图片
|
|
|
+ res.type('svg');
|
|
|
+ res.send(captcha.data);
|
|
|
+ },
|
|
|
+ saveAudit: async (req: any, res: any) => {
|
|
|
+ const clientIp = ipLocResFu(req);
|
|
|
+ if (!loginFlag[clientIp]) loginFlag[clientIp] = { loginFlagCode: '' };
|
|
|
+
|
|
|
+ const codeTxt = req.body.code.toLowerCase();
|
|
|
+
|
|
|
+ if (loginFlag[clientIp].loginFlagCode === codeTxt || (isEnv && codeTxt === '1111')) {
|
|
|
+ const fileIds: string[] = req.body.fileIds || [];
|
|
|
+
|
|
|
+ if (typeof fileIds !== 'object' || fileIds.length < 1)
|
|
|
+ return resSend(res, 404, '至少上传一个文件');
|
|
|
+
|
|
|
+ const infoModel = new Audit(req.body);
|
|
|
+ infoModel.createTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ infoModel.updateTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ // 保存数据到数据库
|
|
|
+ const dbBack = await infoModel.save();
|
|
|
+ // 将文档转换为普通对象
|
|
|
+ const findObj = dbBack.toObject();
|
|
|
+ req.apiDescription = `展示端-素材上传-${findObj.title}`;
|
|
|
+ return resSend(res, 0, '素材上传成功', findObj);
|
|
|
+ } else return resSend(res, 400, '验证码错误');
|
|
|
+ },
|
|
|
+};
|
|
|
+
|
|
|
+export default audit;
|