Files
autoAiWorkSys/api/controller_front/user.js
张成 6e5c35f144 1
2025-12-15 18:36:20 +08:00

139 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const Framework = require("../../framework/node-core-framework.js");
module.exports = {
/**
* @swagger
* /api/user/login:
* post:
* summary: 用户登录
* description: 通过设备SN码登录返回token和用户信息
* tags: [前端-用户管理]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - sn_code
* - device_id
* properties:
* sn_code:
* type: string
* description: 设备SN码
* example: 'GHJU'
* device_id:
* type: string
* description: 设备ID
* example: 'device_123456'
* responses:
* 200:
* description: 登录成功
* content:
* application/json:
* schema:
* type: object
* properties:
* code:
* type: integer
* description: 状态码0表示成功
* example: 0
* message:
* type: string
* description: 响应消息
* example: 'success'
* data:
* type: object
* properties:
* token:
* type: string
* description: 认证token
* example: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
* user:
* type: object
* description: 用户信息
* 400:
* description: 参数错误或用户不存在
* content:
* application/json:
* schema:
* type: object
* properties:
* code:
* type: integer
* example: 400
* message:
* type: string
* example: '用户不存在'
*/
"POST /user/login": async (ctx) => {
const { sn_code, device_id } = ctx.getBody();
const dayjs = require('dayjs');
const { pla_account,device_status} = await Framework.getModels();
// 获取用户信息
const user = await pla_account.findOne({ where: { sn_code } });
if (!user) {
return ctx.fail('用户不存在');
}
// 检查授权状态
const userData = user.toJSON();
const authDate = userData.authorization_date;
const authDays = userData.authorization_days || 0;
if (authDate && authDays > 0) {
const startDate = dayjs(authDate);
const endDate = startDate.add(authDays, 'day');
const now = dayjs();
const remaining = endDate.diff(now, 'day', true);
const remaining_days = Math.max(0, Math.ceil(remaining));
if (remaining_days <= 0) {
return ctx.fail('账号授权已过期,请联系管理员续费');
}
} else {
// 如果没有授权信息,检查是否允许登录(可以根据业务需求决定是否允许)
// 这里暂时允许登录,但可以添加配置项控制
}
// 更新设备状态
const device = await device_status.findOne({ where: { sn_code } });
if (device) {
await device_status.update({
device_id: device_id
}, { where: { sn_code } });
} else {
await device_status.create({
sn_code: sn_code,
device_id: device_id
});
}
const token = Framework.getServices().tokenService.create({
sn_code: user.sn_code,
device_id: user.device_id
});
// 计算剩余天数并返回
let remaining_days = 0;
if (authDate && authDays > 0) {
const startDate = dayjs(authDate);
const endDate = startDate.add(authDays, 'day');
const now = dayjs();
const remaining = endDate.diff(now, 'day', true);
remaining_days = Math.max(0, Math.ceil(remaining));
}
const userInfo = user.toJSON();
userInfo.remaining_days = remaining_days;
return ctx.success({ token, user: userInfo });
}
}