feat: improve service startup and printer status UI

This commit is contained in:
liufumingren 2026-03-31 00:55:42 +08:00
parent 5bc0e6637e
commit 4912056fbf
6 changed files with 169 additions and 10 deletions

27
controllers/index.js Normal file
View File

@ -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();

View File

@ -1,6 +1,6 @@
{ {
"name": "printserver", "name": "printserver",
"version": "1.0.9", "version": "1.0.10",
"main": "electron/main.cjs", "main": "electron/main.cjs",
"scripts": { "scripts": {
"start": "node index.js", "start": "node index.js",

View File

@ -1,6 +1,6 @@
import service from '../core/BaseService.js'; import service from '../core/BaseService.js';
import configUtil from '../utils/config.js'; import configUtil from '../utils/config.js';
import { ensureServiceInstalled, uninstallService } from '../utils/windowsService.js'; import { startService, uninstallService } from '../utils/windowsService.js';
function isServiceRunnerProcess() { function isServiceRunnerProcess() {
return process.argv.includes('--service-runner'); return process.argv.includes('--service-runner');
@ -121,7 +121,7 @@ class SettingService extends service {
const previousIsService = !!config.isService; const previousIsService = !!config.isService;
try { try {
await ensureServiceInstalled(); await startService();
} catch (error) { } catch (error) {
config.isService = previousIsService; config.isService = previousIsService;
configUtil.write(config); configUtil.write(config);

View File

@ -110,6 +110,22 @@ function isAccessDeniedErrorMessage(message) {
return /AccessDenied|拒绝访问|\b0x5\b|\berror\s*5\b/i.test(String(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) { async function runWrapperCommandElevated(command, metadata) {
const escapedWrapperPath = metadata.wrapperPath.replace(/'/g, "''"); const escapedWrapperPath = metadata.wrapperPath.replace(/'/g, "''");
const escapedCommand = String(command).replace(/'/g, "''"); const escapedCommand = String(command).replace(/'/g, "''");
@ -200,13 +216,35 @@ export async function getServiceStatus(options = {}) {
} }
export async function ensureServiceInstalled(options = {}) { export async function ensureServiceInstalled(options = {}) {
const status = await getServiceStatus(options); let status = null;
if (status.installed) { try {
status = await getServiceStatus(options);
} catch (error) {
status = null;
}
if (status && status.installed) {
return status; return status;
} }
await runWrapperCommand('install', options); try {
return getServiceStatus(options); 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 = {}) { export async function startService(options = {}) {
@ -215,7 +253,14 @@ export async function startService(options = {}) {
return status; return status;
} }
await runWrapperCommand('start', options); try {
await runWrapperCommand('start', options);
} catch (error) {
if (!isServiceAlreadyRunningErrorMessage(error.message)) {
throw error;
}
}
return getServiceStatus(options); return getServiceStatus(options);
} }

View File

@ -269,6 +269,40 @@
border-bottom: 1px dashed #e5ebf3; 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 { .item:last-child {
border-bottom: none; border-bottom: none;
} }
@ -518,7 +552,10 @@
</div> </div>
<div class="list"> <div class="list">
<div class="item" v-if="printers.length === 0">无打印机</div> <div class="item" v-if="printers.length === 0">无打印机</div>
<div class="item" v-for="p in printers" :key="p.name">{{ p.name }}</div> <div class="item printer-item" v-for="p in printers" :key="p.name">
<span class="printer-name">{{ p.name }}</span>
<span class="printer-state" :class="isPrinterOffline(p) ? 'offline' : 'online'">{{ isPrinterOffline(p) ? '离线' : '在线' }}</span>
</div>
</div> </div>
</div> </div>
@ -815,6 +852,15 @@
const s = this.statusList.find(x => x.name === name); const s = this.statusList.find(x => x.name === name);
return s ? s.status : '未知'; 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() { loadPrinters() {
wsSendWithCallback('printer.getWindowsPrinters', {}, (data) => { wsSendWithCallback('printer.getWindowsPrinters', {}, (data) => {
if (data.success) { if (data.success) {

View File

@ -168,6 +168,35 @@
.item { .item {
margin-bottom: 6px; 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 { button {
padding: 7px 18px; padding: 7px 18px;
background: #4CAF50; background: #4CAF50;
@ -225,7 +254,10 @@
</div> </div>
<div class="list"> <div class="list">
<div class="item" v-if="printers.length === 0">无打印机</div> <div class="item" v-if="printers.length === 0">无打印机</div>
<div class="item" v-for="p in printers" :key="p.name">{{ p.name }}</div> <div class="item printer-item" v-for="p in printers" :key="p.name">
<span class="printer-name">{{ p.name }}</span>
<span class="printer-state" :class="isPrinterOffline(p) ? 'offline' : 'online'">{{ isPrinterOffline(p) ? '离线' : '在线' }}</span>
</div>
</div> </div>
</div> </div>
<div class="list-box"> <div class="list-box">
@ -334,6 +366,15 @@ new Vue({
const s = this.statusList.find(x => x.name === name); const s = this.statusList.find(x => x.name === name);
return s ? s.status : '未知'; 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() { loadPrinters() {
wsSendWithCallback('printer.getWindowsPrinters', {}, (data) => { wsSendWithCallback('printer.getWindowsPrinters', {}, (data) => {
if (data.success) { if (data.success) {