import service from '../core/BaseService.js'; import configUtil from '../utils/config.js'; import { startService, uninstallService } from '../utils/windowsService.js'; function isServiceRunnerProcess() { return process.argv.includes('--service-runner'); } function isManagedBackendRunnerProcess() { return process.argv.includes('--backend-runner') || process.argv.includes('--service-runner'); } function scheduleCurrentProcessExit() { // 给 WS 响应留出时间,再退出当前前台后端以释放端口。 setTimeout(() => { try { process.exit(0); } catch (error) { console.error('[SettingService] 退出前台后端失败:', error); } }, 500); } class SettingService extends service { constructor() { super(); this.info = this.info.bind(this); this.unbind = this.unbind.bind(this); this.register = this.register.bind(this); this.unregister = this.unregister.bind(this); this.updatePorts = this.updatePorts.bind(this); this.restartBackend = this.restartBackend.bind(this); this.setToken = this.setToken.bind(this); this.setInfo = this.setInfo.bind(this); } // 判断是否已绑定 info() { const {config} = this; return { isBind: !!config.token, name: config.name || '', isService: !!config.isService, wsPort: Number(config.wsPort) || 3001 }; } async updatePorts(data = {}) { const { config, app } = this; const nextWsPort = Number(data.wsPort); if (!Number.isInteger(nextWsPort) || nextWsPort < 1 || nextWsPort > 65535) { return { ok: false, message: 'WS 端口无效,范围应为 1-65535' }; } const wsPortChanged = Number(config.wsPort) !== nextWsPort; config.wsPort = nextWsPort; configUtil.write(config); app.config = config; return { ok: true, message: wsPortChanged ? 'WS 端口已更新,重启后端后生效' : 'WS 端口未变化', wsPort: nextWsPort, wsPortChanged, requiresBackendRestart: !!wsPortChanged }; } async restartBackend(data = {}) { const { app } = this; const targetWsPort = Number(data.wsPort) || Number(app.config?.wsPort) || 3001; if (isManagedBackendRunnerProcess()) { scheduleCurrentProcessExit(); return { ok: true, restarting: true, wsPort: targetWsPort, message: '后端正在重启,请稍候重连' }; } if (!app || typeof app.restartBackendRuntime !== 'function') { return { ok: false, restarting: false, message: '当前运行态不支持重启后端' }; } try { const result = await app.restartBackendRuntime({ wsPort: targetWsPort }); return { ok: true, restarting: false, wsPort: result.wsPort, message: '后端已重启' }; } catch (error) { return { ok: false, restarting: false, message: `后端重启失败:${error.message}` }; } } // 解绑(WebSocket/HTTP 通用,无 ctx 依赖) unbind() { const { config, app } = this; config.token = ''; configUtil.write(config); app.isBind = false; // 解绑后主动请求新绑定码 if (app.wsClient?.status === 'connected') { app.wsClient.send({ method: 'bind.generateCode', data: {} }); } return { ok: true, message: '解绑成功' }; } // WebSocket 注册服务,切换为“系统服务管理后端”的状态 async register() { const { config } = this; const previousIsService = !!config.isService; try { await startService(); } catch (error) { config.isService = previousIsService; configUtil.write(config); this.app.isService = previousIsService; return { ok: false, message: `系统服务注册失败:${error.message}`, isService: previousIsService }; } config.isService = true; configUtil.write(config); this.app.isService = true; if (!isServiceRunnerProcess()) { scheduleCurrentProcessExit(); } return { ok: true, message: isServiceRunnerProcess() ? '服务模式已启用' : '服务模式已启用,当前前台后端将退出,由系统服务接管', isService: true }; } // WebSocket 注销服务,切回“桌面壳直接拉起后端”的状态 async unregister() { const { config } = this; const previousIsService = !!config.isService; if (!isServiceRunnerProcess()) { try { await uninstallService(); } catch (error) { config.isService = previousIsService; configUtil.write(config); this.app.isService = previousIsService; return { ok: false, message: `系统服务注销失败:${error.message}`, isService: previousIsService }; } } config.isService = false; configUtil.write(config); this.app.isService = false; return { ok: true, message: isServiceRunnerProcess() ? '服务模式已关闭,桌面壳会切回本地后端启动并清理系统服务' : '服务已注销,后端将恢复为桌面壳直接启动', isService: false }; } async setToken(data) { const { config } = this; config.token = data.token; config.tokenExpireTime = data.tokenExpireTime; configUtil.write(config); this.app.isBind = !!data.token; //重载设置 await this.app.wsClient._checkAuth(); } async setInfo(data) { const { config, app } = this; config.name = data.computerInfo?.name || data.name || ''; console.log("设置打印台信息", data); app.info = data; configUtil.write(config); // 登录成功后主动全量同步一次组件状态,避免服务端只收到后续增量变化 if (app.wsClient && app.printerComponents) { const allStatus = app.printerComponents.getAllStatus(); for (const item of allStatus) { app.wsClient.send({ method: 'printerComponent.statusChange', data: item }); } } return { ok: true }; } } export default new SettingService();