diff --git a/app/controller/auth.js b/app/controller/auth.js index 5fc8829..0f3d168 100644 --- a/app/controller/auth.js +++ b/app/controller/auth.js @@ -64,6 +64,173 @@ class AuthController extends Controller { }; } } + + // 外部通知注销电脑(强制下线) + async logout() { + const { ctx, app } = this; + const { warehouse, computerId } = ctx.request.body; + + if (!warehouse || !computerId) { + ctx.body = { + status: 'fail', + message: '缺少必要参数: warehouse, computerId', + }; + return; + } + + try { + // 1. 核实数据库:确认电脑存在 + const computerInfo = await app.mysql.get('wms').get('wms_print_computer', { + warehouse_id: warehouse, + id: computerId, + }); + + if (!computerInfo) { + ctx.body = { + status: 'fail', + message: '未在数据库中找到该电脑信息', + }; + return; + } + + console.log(`注销电脑: warehouse=${warehouse}, computerId=${computerId}, deleted_at=${computerInfo.deleted_at}`); + + // 2. 从 Redis 查找电脑是否在线 + const computerInfoKey = `warehouse:${warehouse}:printer:${computerId}`; + const cachedInfo = await app.redis.get(computerInfoKey); + + if (!cachedInfo) { + // 电脑不在线,但数据库验证已通过,返回成功(已离线无需操作) + ctx.body = { + status: 'success', + message: '电脑当前离线,无需注销', + }; + return; + } + + const { uniqueId } = JSON.parse(cachedInfo); + + // 3. 向客户端发送解绑注销命令 + const res = await app.ws.sendTo(uniqueId, JSON.stringify({ + method: 'setting.unbind', + data: { reason: '管理员强制注销' }, + })); + + // 4. 清理 Redis 中的在线状态 + await app.redis.del(computerInfoKey); + await app.redis.del(uniqueId); + console.log(`已清理 Redis: ${computerInfoKey}, ${uniqueId}`); + + if (res !== 1) { + ctx.body = { + status: 'fail', + message: '发送注销命令失败,电脑可能已断开连接', + }; + return; + } + + ctx.body = { + status: 'success', + message: '注销命令已发送', + }; + } catch (error) { + console.error('注销电脑时发生错误:', error); + ctx.body = { + status: 'fail', + message: `注销失败: ${error.message}`, + }; + } + } + + // 外部通知电脑信息变更(拉取最新数据并推送给客户端) + async refreshInfo() { + const { ctx, app } = this; + const { warehouse, computerId } = ctx.request.body; + + if (!warehouse || !computerId) { + ctx.body = { + status: 'fail', + message: '缺少必要参数: warehouse, computerId', + }; + return; + } + + try { + // 1. 从数据库拉取最新电脑信息 + const computerInfo = await app.mysql.get('wms').get('wms_print_computer', { + warehouse_id: warehouse, + id: computerId, + }); + + if (!computerInfo) { + ctx.body = { + status: 'fail', + message: '未在数据库中找到该电脑信息', + }; + return; + } + + console.log(`刷新电脑信息: warehouse=${warehouse}, computerId=${computerId}`); + + // 2. 组装下发的 info 数据 + const infoData = { + name: computerInfo.name || '', + status: computerInfo.status, + is_recording: computerInfo.is_recording || 0, + recording_size_value: computerInfo.recording_size_value || 0, + recording_size_unit: computerInfo.recording_size_unit || '', + remote_play_url: computerInfo.remote_play_url || '', + recording_directory: computerInfo.recording_directory || '', + }; + + // 3. 查找电脑是否在线 + const computerInfoKey = `warehouse:${warehouse}:printer:${computerId}`; + const cachedInfo = await app.redis.get(computerInfoKey); + + if (!cachedInfo) { + // 电脑不在线,无需推送 + ctx.body = { + status: 'success', + message: '电脑当前离线,信息已保存,上线后将自动获取最新信息', + }; + return; + } + + const cached = JSON.parse(cachedInfo); + const { uniqueId } = cached; + + // 4. 合并完整数据并更新 Redis 缓存 + const updatedData = { ...cached, ...infoData }; + await app.redis.set(computerInfoKey, JSON.stringify(updatedData), 'EX', 300); + + // 5. 向客户端推送最新信息 + const res = await app.ws.sendTo(uniqueId, JSON.stringify({ + method: 'setting.setInfo', + data: { ...cached, ...infoData }, + message: '电脑信息已更新', + })); + + if (res !== 1) { + ctx.body = { + status: 'fail', + message: '推送信息失败,电脑可能已断开连接', + }; + return; + } + + ctx.body = { + status: 'success', + message: '信息已推送', + data: infoData, + }; + } catch (error) { + console.error('刷新电脑信息时发生错误:', error); + ctx.body = { + status: 'fail', + message: `刷新失败: ${error.message}`, + }; + } + } } module.exports = AuthController; \ No newline at end of file diff --git a/app/router.js b/app/router.js index ea028cd..7e58468 100644 --- a/app/router.js +++ b/app/router.js @@ -5,6 +5,8 @@ module.exports = app => { const { router, controller } = app; router.get('/', controller.home.index); router.post('/auth/bind', controller.auth.bind); + router.post('/auth/logout', controller.auth.logout); + router.post('/auth/refreshInfo', controller.auth.refreshInfo); router.post('/sendToRoom', controller.home.sendToRoom); app.ws.route('/ws', controller.print.ws); }; \ No newline at end of file diff --git a/app/service/auth.js b/app/service/auth.js index 4c6eb92..7af0637 100644 --- a/app/service/auth.js +++ b/app/service/auth.js @@ -17,11 +17,34 @@ class AuthService extends BaseService { 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 || ''; + } + this.app.redis.del(key); // 删除旧的打印机信息 this.app.redis.set(key, JSON.stringify(data), 'EX', 300); // 设置过期时间为 5分钟 return this.successResponse('setting.setInfo', data, 'Token 验证成功'); diff --git a/config/config.default.js b/config/config.default.js index 5b3b5ff..951f714 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -58,6 +58,9 @@ module.exports = appInfo => { port: 6379, // Redis端口 password: 'qwe2019@', // Redis密码(如果有) db: 11, // 使用的数据库索引 + retryStrategy: (times) => Math.min(times * 200, 5000), // 断线重连策略 + maxRetriesPerRequest: 3, + reconnectOnError: (err) => err.message.includes('READONLY'), }, }; config.websocket = {