1
This commit is contained in:
@@ -6,7 +6,7 @@ module.exports = {
|
||||
* /api/user/login:
|
||||
* post:
|
||||
* summary: 用户登录
|
||||
* description: 通过设备SN码登录,返回token和用户信息
|
||||
* description: 通过手机号和密码登录,返回token、device_id和用户信息
|
||||
* tags: [前端-用户管理]
|
||||
* requestBody:
|
||||
* required: true
|
||||
@@ -15,17 +15,17 @@ module.exports = {
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - sn_code
|
||||
* - device_id
|
||||
* - phone
|
||||
* - password
|
||||
* properties:
|
||||
* sn_code:
|
||||
* phone:
|
||||
* type: string
|
||||
* description: 设备SN码
|
||||
* example: 'GHJU'
|
||||
* device_id:
|
||||
* description: 手机号(登录名)
|
||||
* example: '13800138000'
|
||||
* password:
|
||||
* type: string
|
||||
* description: 设备ID
|
||||
* example: 'device_123456'
|
||||
* description: 密码
|
||||
* example: 'password123'
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 登录成功
|
||||
@@ -49,6 +49,10 @@ module.exports = {
|
||||
* type: string
|
||||
* description: 认证token
|
||||
* example: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
|
||||
* device_id:
|
||||
* type: string
|
||||
* description: 设备ID
|
||||
* example: 'device_123456'
|
||||
* user:
|
||||
* type: object
|
||||
* description: 用户信息
|
||||
@@ -64,20 +68,35 @@ module.exports = {
|
||||
* example: 400
|
||||
* message:
|
||||
* type: string
|
||||
* example: '用户不存在'
|
||||
* example: '用户不存在或密码错误'
|
||||
*/
|
||||
"POST /user/login": async (ctx) => {
|
||||
|
||||
const { sn_code, device_id } = ctx.getBody();
|
||||
const { phone, password } = ctx.getBody();
|
||||
const dayjs = require('dayjs');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const { pla_account,device_status} = await Framework.getModels();
|
||||
// 验证参数
|
||||
if (!phone || !password) {
|
||||
return ctx.fail('手机号和密码不能为空');
|
||||
}
|
||||
|
||||
const { pla_account } = await Framework.getModels();
|
||||
|
||||
// 根据手机号(login_name)和密码查找用户
|
||||
const user = await pla_account.findOne({
|
||||
where: {
|
||||
login_name: phone,
|
||||
pwd: password
|
||||
}
|
||||
});
|
||||
|
||||
// 获取用户信息
|
||||
|
||||
const user = await pla_account.findOne({ where: { sn_code } });
|
||||
if (!user) {
|
||||
return ctx.fail('用户不存在');
|
||||
return ctx.fail('手机号或密码错误');
|
||||
}
|
||||
|
||||
// 检查账号是否启用
|
||||
if (!user.is_enabled) {
|
||||
return ctx.fail('账号已被禁用');
|
||||
}
|
||||
|
||||
// 检查授权状态
|
||||
@@ -95,32 +114,30 @@ module.exports = {
|
||||
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
|
||||
});
|
||||
// 生成设备ID(如果不存在,基于手机号和机器特征生成)
|
||||
let device_id = user.device_id;
|
||||
if (!device_id) {
|
||||
// 生成唯一设备ID
|
||||
const machineInfo = `${phone}_${Date.now()}_${Math.random()}`;
|
||||
device_id = crypto.createHash('sha256').update(machineInfo).digest('hex').substring(0, 32);
|
||||
|
||||
// 保存设备ID到账号表
|
||||
await pla_account.update(
|
||||
{ device_id: device_id },
|
||||
{ where: { id: user.id } }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// 创建token
|
||||
const token = Framework.getServices().tokenService.create({
|
||||
sn_code: user.sn_code,
|
||||
device_id: user.device_id
|
||||
device_id: device_id,
|
||||
user_id: user.id
|
||||
});
|
||||
|
||||
// 计算剩余天数并返回
|
||||
// 计算剩余天数
|
||||
let remaining_days = 0;
|
||||
if (authDate && authDays > 0) {
|
||||
const startDate = dayjs(authDate);
|
||||
@@ -132,8 +149,13 @@ module.exports = {
|
||||
|
||||
const userInfo = user.toJSON();
|
||||
userInfo.remaining_days = remaining_days;
|
||||
// 不返回密码
|
||||
delete userInfo.pwd;
|
||||
|
||||
return ctx.success({ token, user: userInfo });
|
||||
|
||||
return ctx.success({
|
||||
token,
|
||||
device_id,
|
||||
user: userInfo
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user