diff --git a/controllers/index.js b/controllers/index.js new file mode 100644 index 0000000..eda4921 --- /dev/null +++ b/controllers/index.js @@ -0,0 +1,27 @@ +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; +import BaseController from '../core/BaseController.js'; + +class IndexController extends BaseController { + constructor() { + super(); + } + index() { + const {ctx} = this; + console.log("访问首页"); + const indexPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '../views/index.html'); + fs.readFile(indexPath, (err, data) => { + if (err) { + console.log('读取首页文件错误:', err.message); + ctx.response.writeHead(500, { 'Content-Type': 'text/plain' }); + ctx.response.end('500 Internal Server Error'); + } else { + ctx.response.writeHead(200, { 'Content-Type': 'text/html' }); + ctx.response.end(data); + } + }); + } +} + +export default new IndexController(); diff --git a/package.json b/package.json index 5b2fc9c..c008f14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "printserver", - "version": "1.0.9", + "version": "1.0.10", "main": "electron/main.cjs", "scripts": { "start": "node index.js", diff --git a/services/setting.js b/services/setting.js index 74503d4..9d668a2 100644 --- a/services/setting.js +++ b/services/setting.js @@ -1,6 +1,6 @@ import service from '../core/BaseService.js'; import configUtil from '../utils/config.js'; -import { ensureServiceInstalled, uninstallService } from '../utils/windowsService.js'; +import { startService, uninstallService } from '../utils/windowsService.js'; function isServiceRunnerProcess() { return process.argv.includes('--service-runner'); @@ -121,7 +121,7 @@ class SettingService extends service { const previousIsService = !!config.isService; try { - await ensureServiceInstalled(); + await startService(); } catch (error) { config.isService = previousIsService; configUtil.write(config); diff --git a/utils/windowsService.js b/utils/windowsService.js index 1403208..7422d80 100644 --- a/utils/windowsService.js +++ b/utils/windowsService.js @@ -110,6 +110,22 @@ function isAccessDeniedErrorMessage(message) { return /AccessDenied|拒绝访问|\b0x5\b|\berror\s*5\b/i.test(String(message)); } +function isServiceAlreadyExistsErrorMessage(message) { + if (!message) { + return false; + } + + return /already exists|已存在|Service with id\s*'?.+?'?\s*already exists/i.test(String(message)); +} + +function isServiceAlreadyRunningErrorMessage(message) { + if (!message) { + return false; + } + + return /already started|already running|已启动|正在运行/i.test(String(message)); +} + async function runWrapperCommandElevated(command, metadata) { const escapedWrapperPath = metadata.wrapperPath.replace(/'/g, "''"); const escapedCommand = String(command).replace(/'/g, "''"); @@ -200,13 +216,35 @@ export async function getServiceStatus(options = {}) { } export async function ensureServiceInstalled(options = {}) { - const status = await getServiceStatus(options); - if (status.installed) { + let status = null; + try { + status = await getServiceStatus(options); + } catch (error) { + status = null; + } + + if (status && status.installed) { return status; } - await runWrapperCommand('install', options); - return getServiceStatus(options); + try { + await runWrapperCommand('install', options); + } catch (error) { + if (!isServiceAlreadyExistsErrorMessage(error.message)) { + throw error; + } + } + + try { + return await getServiceStatus(options); + } catch (error) { + // 某些机器上 status 读取异常,但 install 已提示“已存在”。此时按已安装处理。 + return { + installed: true, + running: false, + rawStatus: 'Existing' + }; + } } export async function startService(options = {}) { @@ -215,7 +253,14 @@ export async function startService(options = {}) { return status; } - await runWrapperCommand('start', options); + try { + await runWrapperCommand('start', options); + } catch (error) { + if (!isServiceAlreadyRunningErrorMessage(error.message)) { + throw error; + } + } + return getServiceStatus(options); } diff --git a/views/index.html b/views/index.html index f92e5e3..af13f9c 100644 --- a/views/index.html +++ b/views/index.html @@ -269,6 +269,40 @@ border-bottom: 1px dashed #e5ebf3; } + .printer-item { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + } + + .printer-name { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + } + + .printer-state { + font-size: 12px; + font-weight: 600; + border-radius: 999px; + padding: 2px 8px; + border: 1px solid transparent; + white-space: nowrap; + } + + .printer-state.online { + color: #107c10; + background: #e9f6ea; + border-color: #c8e6ca; + } + + .printer-state.offline { + color: #c42b1c; + background: #fdeeee; + border-color: #f5c7c2; + } + .item:last-child { border-bottom: none; } @@ -518,7 +552,10 @@
无打印机
-
{{ p.name }}
+
+ {{ p.name }} + {{ isPrinterOffline(p) ? '离线' : '在线' }} +
@@ -815,6 +852,15 @@ const s = this.statusList.find(x => x.name === name); return s ? s.status : '未知'; }, + isPrinterOffline(printer) { + const raw = printer && printer.workOffline; + if (typeof raw === 'boolean') { + return raw; + } + + const normalized = String(raw || '').trim().toLowerCase(); + return normalized === 'true' || normalized === '1' || normalized === 'yes'; + }, loadPrinters() { wsSendWithCallback('printer.getWindowsPrinters', {}, (data) => { if (data.success) { diff --git a/views/index_new.html b/views/index_new.html index 238dc1a..7849cce 100644 --- a/views/index_new.html +++ b/views/index_new.html @@ -168,6 +168,35 @@ .item { margin-bottom: 6px; } + .printer-item { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + } + .printer-name { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + } + .printer-state { + font-size: 12px; + font-weight: bold; + border-radius: 999px; + padding: 2px 8px; + border: 1px solid transparent; + white-space: nowrap; + } + .printer-state.online { + color: #2e7d32; + background: #e8f5e9; + border-color: #c8e6c9; + } + .printer-state.offline { + color: #c62828; + background: #ffebee; + border-color: #ffcdd2; + } button { padding: 7px 18px; background: #4CAF50; @@ -225,7 +254,10 @@
无打印机
-
{{ p.name }}
+
+ {{ p.name }} + {{ isPrinterOffline(p) ? '离线' : '在线' }} +
@@ -334,6 +366,15 @@ new Vue({ const s = this.statusList.find(x => x.name === name); return s ? s.status : '未知'; }, + isPrinterOffline(printer) { + const raw = printer && printer.workOffline; + if (typeof raw === 'boolean') { + return raw; + } + + const normalized = String(raw || '').trim().toLowerCase(); + return normalized === 'true' || normalized === '1' || normalized === 'yes'; + }, loadPrinters() { wsSendWithCallback('printer.getWindowsPrinters', {}, (data) => { if (data.success) {