1
This commit is contained in:
100
api/controller_front/account.js
Normal file
100
api/controller_front/account.js
Normal file
@@ -0,0 +1,100 @@
|
||||
const Framework = require("../../framework/node-core-framework.js");
|
||||
const authorizationService = require('../services/authorization_service.js');
|
||||
|
||||
/**
|
||||
* 账号管理控制器(客户端接口)
|
||||
* 提供客户端调用的账号相关接口
|
||||
*/
|
||||
module.exports = {
|
||||
/**
|
||||
* @swagger
|
||||
* /api/account/check-authorization:
|
||||
* post:
|
||||
* summary: 检查账号授权状态
|
||||
* description: 根据设备SN码检查账号的授权状态(剩余天数、是否过期等)
|
||||
* tags: [前端-账号管理]
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - sn_code
|
||||
* properties:
|
||||
* sn_code:
|
||||
* type: string
|
||||
* description: 设备SN码
|
||||
* example: 'GHJU'
|
||||
* 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:
|
||||
* is_authorized:
|
||||
* type: boolean
|
||||
* description: 是否已授权
|
||||
* example: true
|
||||
* remaining_days:
|
||||
* type: integer
|
||||
* description: 剩余天数
|
||||
* example: 30
|
||||
* message:
|
||||
* type: string
|
||||
* description: 授权状态消息
|
||||
* example: '授权有效,剩余 30 天'
|
||||
* 400:
|
||||
* description: 参数错误
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* code:
|
||||
* type: integer
|
||||
* example: 400
|
||||
* message:
|
||||
* type: string
|
||||
* example: '请提供设备SN码'
|
||||
* 500:
|
||||
* description: 服务器错误
|
||||
*/
|
||||
'POST /account/check-authorization': async (ctx) => {
|
||||
try {
|
||||
const { sn_code } = ctx.getBody();
|
||||
|
||||
// 参数验证
|
||||
if (!sn_code) {
|
||||
return ctx.fail('请提供设备SN码');
|
||||
}
|
||||
|
||||
// 调用授权服务检查授权状态
|
||||
const result = await authorizationService.checkAuthorization(sn_code, 'sn_code');
|
||||
|
||||
// 返回授权检查结果
|
||||
return ctx.success({
|
||||
is_authorized: result.is_authorized,
|
||||
remaining_days: result.remaining_days,
|
||||
message: result.message
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[账号管理] 检查授权状态失败:', error);
|
||||
return ctx.fail('检查授权状态失败: ' + (error.message || '未知错误'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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