Files
autoAiWorkSys/api/controller_front/user.js
张成 7858459118 1
2025-11-26 12:39:21 +08:00

105 lines
3.2 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 { pla_account,device_status} = await Framework.getModels();
// 获取用户信息
const user = await pla_account.findOne({ where: { sn_code } });
if (!user) {
return ctx.fail('用户不存在');
}
// 更新设备状态
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
});
return ctx.success({ token, user: user.toJSON() });
}
}