105 lines
3.2 KiB
JavaScript
105 lines
3.2 KiB
JavaScript
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() });
|
||
|
||
}
|
||
} |