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

@@ -0,0 +1,80 @@
/**
* 授权服务
* 提供账号授权检查功能
*/
const db = require('../middleware/dbProxy');
const dayjs = require('dayjs');
class AuthorizationService {
/**
* 检查账号授权状态
* @param {string|number} identifier - 账号标识sn_code 或 id
* @param {string} identifierType - 标识类型:'sn_code' 或 'id'
* @returns {Promise<Object>} 授权检查结果 {is_authorized: boolean, remaining_days: number, message: string}
*/
async checkAuthorization(identifier, identifierType = 'sn_code') {
const pla_account = db.getModel('pla_account');
const where = {};
where[identifierType] = identifier;
const account = await pla_account.findOne({ where });
if (!account) {
return {
is_authorized: false,
remaining_days: 0,
message: '账号不存在'
};
}
const accountData = account.toJSON();
const authDate = accountData.authorization_date;
const authDays = accountData.authorization_days || 0;
// 如果没有授权信息,默认不允许
if (!authDate || authDays <= 0) {
return {
is_authorized: false,
remaining_days: 0,
message: '账号未授权,请联系管理员'
};
}
// 计算剩余天数
const startDate = dayjs(authDate);
const endDate = startDate.add(authDays, 'day');
const now = dayjs();
const remaining = endDate.diff(now, 'day', true);
const remaining_days = Math.max(0, Math.ceil(remaining));
if (remaining_days <= 0) {
return {
is_authorized: false,
remaining_days: 0,
message: '账号授权已过期,请联系管理员续费'
};
}
return {
is_authorized: true,
remaining_days: remaining_days,
message: `授权有效,剩余 ${remaining_days}`
};
}
/**
* 检查账号授权状态(快速检查,只返回是否授权)
* @param {string|number} identifier - 账号标识sn_code 或 id
* @param {string} identifierType - 标识类型:'sn_code' 或 'id'
* @returns {Promise<boolean>} 是否授权
*/
async isAuthorized(identifier, identifierType = 'sn_code') {
const result = await this.checkAuthorization(identifier, identifierType);
return result.is_authorized;
}
}
module.exports = new AuthorizationService();

View File

@@ -6,6 +6,7 @@
const db = require('../middleware/dbProxy');
const scheduleManager = require('../middleware/schedule/index.js');
const locationService = require('./locationService');
const authorizationService = require('./authorization_service');
class PlaAccountService {
/**
@@ -382,6 +383,12 @@ class PlaAccountService {
throw new Error('账号不存在');
}
// 检查授权状态
const authCheck = await authorizationService.checkAuthorization(id, 'id');
if (!authCheck.is_authorized) {
throw new Error(authCheck.message);
}
// 从 device_status 检查账号是否在线
const deviceStatus = await device_status.findByPk(account.sn_code);
if (!deviceStatus || !deviceStatus.isOnline) {