This commit is contained in:
张成
2025-12-15 18:36:20 +08:00
parent c083494fce
commit 6e5c35f144
15 changed files with 867 additions and 8 deletions

View File

@@ -509,6 +509,158 @@ module.exports = {
const { ids } = body;
const result = await plaAccountService.batchParseLocation(ids);
return ctx.success(result);
},
/**
* @swagger
* /admin_api/account/check-authorization:
* post:
* summary: 检查账号授权状态
* description: 根据账号ID或SN码检查账号的授权状态剩余天数、是否过期等
* tags: [后台-账号管理]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* id:
* type: integer
* description: 账号ID可选如果提供id则不需要sn_code
* sn_code:
* type: string
* description: 设备SN码可选如果提供sn_code则不需要id
* responses:
* 200:
* description: 检查成功
*/
'POST /account/check-authorization': async (ctx) => {
const { id, sn_code } = ctx.getBody();
const models = await Framework.getModels();
const { pla_account } = models;
const dayjs = require('dayjs');
if (!id && !sn_code) {
return ctx.fail('请提供账号ID或SN码');
}
const where = id ? { id } : { sn_code };
const account = await pla_account.findOne({ where });
if (!account) {
return ctx.fail('账号不存在');
}
const accountData = account.toJSON();
const authDate = accountData.authorization_date;
const authDays = accountData.authorization_days || 0;
// 计算剩余天数
let remaining_days = 0;
let is_expired = false;
let end_date = null;
if (authDate && authDays > 0) {
const startDate = dayjs(authDate);
end_date = startDate.add(authDays, 'day').toDate();
const now = dayjs();
const remaining = dayjs(end_date).diff(now, 'day', true);
remaining_days = Math.max(0, Math.ceil(remaining));
is_expired = remaining_days <= 0;
}
return ctx.success({
id: accountData.id,
sn_code: accountData.sn_code,
name: accountData.name,
authorization_date: authDate,
authorization_days: authDays,
remaining_days: remaining_days,
end_date: end_date,
is_expired: is_expired,
is_authorized: !is_expired && remaining_days > 0
});
},
/**
* @swagger
* /admin_api/account/update-authorization:
* post:
* summary: 更新账号授权信息
* description: 更新账号的授权日期和授权天数
* tags: [后台-账号管理]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - id
* properties:
* id:
* type: integer
* description: 账号ID
* authorization_date:
* type: string
* format: date-time
* description: 授权日期(可选,不提供则使用当前时间)
* authorization_days:
* type: integer
* description: 授权天数(必填)
* responses:
* 200:
* description: 更新成功
*/
'POST /account/update-authorization': async (ctx) => {
const { id, authorization_date, authorization_days } = ctx.getBody();
const models = await Framework.getModels();
const { pla_account } = models;
const dayjs = require('dayjs');
if (!id) {
return ctx.fail('账号ID不能为空');
}
if (authorization_days === undefined || authorization_days === null) {
return ctx.fail('授权天数不能为空');
}
if (authorization_days < 0) {
return ctx.fail('授权天数不能为负数');
}
const account = await pla_account.findOne({ where: { id } });
if (!account) {
return ctx.fail('账号不存在');
}
// 如果提供了授权日期,使用提供的日期;否则使用当前时间
const authDate = authorization_date ? new Date(authorization_date) : new Date();
await account.update({
authorization_date: authDate,
authorization_days: authorization_days
});
// 计算更新后的剩余天数
const end_date = dayjs(authDate).add(authorization_days, 'day').toDate();
const now = dayjs();
const remaining = dayjs(end_date).diff(now, 'day', true);
const remaining_days = Math.max(0, Math.ceil(remaining));
return ctx.success({
message: '授权信息更新成功',
data: {
id: account.id,
authorization_date: authDate,
authorization_days: authorization_days,
remaining_days: remaining_days,
end_date: end_date
}
});
}
};