- 新增 POST /camera/setOSD 接口,下发OSD叠加信息到录制客户端 - 将 Redis 在线状态维护从 setInfo 迁移到 ping 心跳机制 - WebSocket ping 处理中增加 Redis 在线状态刷新 - 移除 fetchTasks 中 pack_mode=1 的限制条件 - 更新 frp auth_token
117 lines
5.4 KiB
JavaScript
117 lines
5.4 KiB
JavaScript
const BaseService = require('./base');
|
||
|
||
class AuthService extends BaseService {
|
||
async getInfo(token) {
|
||
console.log('AuthService.getInfo called with token:', token);
|
||
const { app } = this;
|
||
try {
|
||
// 验证 JWT Token
|
||
const decoded = app.jwt.verify(token, app.config.jwt.secret);
|
||
const { computerId, warehouse } = decoded;
|
||
const uniqueId = this.ctx.uniqueId;
|
||
const data = decoded;
|
||
data.uniqueId = uniqueId;
|
||
this.ctx.print_computer_info = data;
|
||
const key = `warehouse:${warehouse}:printer:${computerId}`;
|
||
const computerInfo = await this.app.mysql.get("wms").get('wms_print_computer', {
|
||
warehouse_id: warehouse,
|
||
id: computerId
|
||
});
|
||
|
||
// 检查电脑是否已被软删除,若是则通知客户端注销
|
||
if (computerInfo && computerInfo.deleted_at) {
|
||
console.log(`电脑 ${computerId} 已被标记删除(deleted_at: ${computerInfo.deleted_at}),通知客户端注销`);
|
||
// 清理 Redis 中的电脑在线信息
|
||
await this.app.redis.del(key);
|
||
await this.app.redis.del(uniqueId);
|
||
// 通知客户端执行解绑注销
|
||
return this.successResponse('setting.unbind', { reason: '电脑已被管理员删除' }, '电脑已注销');
|
||
}
|
||
|
||
if(computerInfo.status==0) {
|
||
//更新数据库为激活状态
|
||
await this.app.mysql.get("wms").update('wms_print_computer', { status: 1 }, { where: { warehouse_id: warehouse, id: computerId } });
|
||
}
|
||
console.log('查询打印机信息:', computerInfo);
|
||
|
||
// 将数据库中的电脑信息补充到下发数据中
|
||
if (computerInfo) {
|
||
data.name = computerInfo.name || '';
|
||
data.status = computerInfo.status;
|
||
data.is_recording = computerInfo.is_recording || 0;
|
||
data.recording_size_value = computerInfo.recording_size_value || 0;
|
||
data.recording_size_unit = computerInfo.recording_size_unit || '';
|
||
data.remote_play_url = computerInfo.remote_play_url || '';
|
||
data.recording_directory = computerInfo.recording_directory || '';
|
||
|
||
// 如果是录制电脑,生成 frp 连接配置
|
||
if (data.is_recording === 1) {
|
||
const frpToken = app.jwt.sign(
|
||
{ warehouse, computerId, purpose: 'frp' },
|
||
app.config.jwt.secret,
|
||
{ expiresIn: '24h' }
|
||
);
|
||
data.frp = {
|
||
subdomain: `${app.config.frp.subdomain_prefix || 'w'}${warehouse}-c${computerId}`,
|
||
server_addr: app.config.frp.server_addr,
|
||
server_port: app.config.frp.server_port,
|
||
auth_token: app.config.frp.auth_token,
|
||
local_port: app.config.frp.local_port,
|
||
token: frpToken,
|
||
};
|
||
}
|
||
}
|
||
|
||
data._source = 'getInfo'; // 标记来源
|
||
console.log(`[getInfo] 认证成功, computer=${computerId}, warehouse=${warehouse} (在线状态由 ping 维护)`);
|
||
return this.successResponse('setting.setInfo', data, 'Token 验证成功');
|
||
} catch (error) {
|
||
console.log(error);
|
||
return this.errorResponse('setting.refreshToken', error.message || 'Token 验证失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 心跳 ping:客户端定期调用,维护 Redis 在线状态
|
||
* 写入/刷新 warehouse:{warehouse}:printer:{computerId},TTL 300 秒
|
||
*/
|
||
async ping() {
|
||
const { ctx, app } = this;
|
||
const info = ctx.print_computer_info;
|
||
if (!info) {
|
||
// 未登录,仅回 pong 不写 Redis
|
||
return this.successResponse('auth.ping', { timestamp: Date.now() }, 'pong');
|
||
}
|
||
const { warehouse, computerId, uniqueId } = info;
|
||
const key = `warehouse:${warehouse}:printer:${computerId}`;
|
||
|
||
// 写入/刷新电脑在线状态(含完整数据),TTL 5 分钟
|
||
await app.redis.set(key, JSON.stringify(info), 'EX', 300);
|
||
// 同时刷新 uniqueId 房间 key
|
||
await app.redis.expire(uniqueId, 300);
|
||
|
||
console.log(`[ping] 在线状态已刷新, computer=${computerId}, warehouse=${warehouse}`);
|
||
return this.successResponse('auth.ping', { timestamp: Date.now() }, 'pong');
|
||
}
|
||
|
||
async refreshToken(data) {
|
||
const { token } = data;
|
||
console.log('AuthService.refreshToken called with oldToken:', token);
|
||
const { app } = this;
|
||
try {
|
||
// 验证旧 Token
|
||
const decoded = app.jwt.verify(token, app.config.jwt.secret);
|
||
const { code, printer, warehouse, computerId } = decoded;
|
||
const jwtToken = app.jwt.sign({ code, printer, warehouse, computerId }, app.config.jwt.secret, {
|
||
expiresIn: '1h',
|
||
});
|
||
return this.successResponse('setting.setToken', { token: jwtToken, tokenExpireTime: Date.now() + 3600 * 1000 }, 'Token 刷新成功');
|
||
} catch (error) {
|
||
console.log('Token 刷新失败:', error);
|
||
return this.errorResponse('setting.refreshToken', error.message || 'Token 刷新失败');
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = AuthService;
|