94 lines
4.4 KiB
JavaScript
94 lines
4.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,
|
|
};
|
|
}
|
|
}
|
|
|
|
this.app.redis.del(key); // 删除旧的打印机信息
|
|
this.app.redis.set(key, JSON.stringify(data), 'EX', 300); // 设置过期时间为 5分钟
|
|
return this.successResponse('setting.setInfo', data, 'Token 验证成功');
|
|
} catch (error) {
|
|
console.log(error);
|
|
return this.errorResponse('setting.refreshToken', error.message || 'Token 验证失败');
|
|
}
|
|
}
|
|
|
|
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;
|