1250 lines
47 KiB
HTML
1250 lines
47 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-cn">
|
||
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>打印电脑设置</title>
|
||
<script>
|
||
(function loadExternalDeps() {
|
||
function loadScript(candidates) {
|
||
return new Promise(function (resolve, reject) {
|
||
var idx = 0;
|
||
function tryNext() {
|
||
if (idx >= candidates.length) {
|
||
reject(new Error('All CDN sources failed: ' + candidates.join(', ')));
|
||
return;
|
||
}
|
||
var src = candidates[idx++];
|
||
var script = document.createElement('script');
|
||
script.src = src;
|
||
script.async = false;
|
||
script.onload = function () { resolve(src); };
|
||
script.onerror = tryNext;
|
||
document.head.appendChild(script);
|
||
}
|
||
tryNext();
|
||
});
|
||
}
|
||
|
||
Promise.resolve()
|
||
.then(function () {
|
||
return loadScript([
|
||
'https://cdn.jsdelivr.net/npm/vue@2/dist/vue.min.js',
|
||
'https://unpkg.com/vue@2/dist/vue.min.js',
|
||
'https://cdn.bootcdn.net/ajax/libs/vue/2.7.16/vue.min.js'
|
||
]);
|
||
})
|
||
.then(function () {
|
||
return loadScript([
|
||
'https://cdn.jsdelivr.net/npm/qrcodejs2@0.0.2/qrcode.min.js',
|
||
'https://unpkg.com/qrcodejs2@0.0.2/qrcode.min.js',
|
||
'https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.min.js'
|
||
]);
|
||
})
|
||
.then(function () {
|
||
window.__depsReady = true;
|
||
window.dispatchEvent(new Event('deps-ready'));
|
||
})
|
||
.catch(function (err) {
|
||
console.error('依赖脚本加载失败:', err);
|
||
window.__depsReady = false;
|
||
window.dispatchEvent(new Event('deps-failed'));
|
||
});
|
||
})();
|
||
</script>
|
||
<style>
|
||
:root {
|
||
--bg: #edf1f6;
|
||
--panel: #ffffff;
|
||
--panel-soft: #f7f9fc;
|
||
--line: #d3dce8;
|
||
--text: #1d2a39;
|
||
--subtext: #5d6b7c;
|
||
--accent: #0b69c7;
|
||
--accent-hover: #0858a7;
|
||
--danger: #c42b1c;
|
||
}
|
||
|
||
* {
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
html,
|
||
body {
|
||
margin: 0;
|
||
width: 100%;
|
||
height: 100%;
|
||
}
|
||
|
||
body {
|
||
font-family: "Segoe UI", "Microsoft YaHei UI", sans-serif;
|
||
color: var(--text);
|
||
background: linear-gradient(150deg, #f8fbff 0%, var(--bg) 72%);
|
||
padding: 12px;
|
||
}
|
||
|
||
#app {
|
||
width: 100%;
|
||
height: calc(100vh - 24px);
|
||
background: var(--panel);
|
||
border: 1px solid var(--line);
|
||
border-radius: 12px;
|
||
box-shadow: 0 14px 34px rgba(34, 50, 74, 0.12);
|
||
display: flex;
|
||
flex-direction: column;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.topbar {
|
||
flex: 0 0 auto;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
padding: 12px 14px;
|
||
border-bottom: 1px solid var(--line);
|
||
background: linear-gradient(180deg, #ffffff 0%, #f5f8fc 100%);
|
||
}
|
||
|
||
.title {
|
||
margin: 0;
|
||
font-size: 24px;
|
||
font-weight: 700;
|
||
letter-spacing: 0.2px;
|
||
}
|
||
|
||
.name-badge {
|
||
font-size: 13px;
|
||
color: var(--subtext);
|
||
background: #fff;
|
||
border: 1px solid var(--line);
|
||
border-radius: 999px;
|
||
padding: 5px 10px;
|
||
white-space: nowrap;
|
||
}
|
||
|
||
.tabbar {
|
||
flex: 0 0 auto;
|
||
display: flex;
|
||
gap: 8px;
|
||
padding: 10px 14px 0;
|
||
border-bottom: 1px solid var(--line);
|
||
background: #f8fbff;
|
||
}
|
||
|
||
.tab {
|
||
appearance: none;
|
||
border: 1px solid #b7c4d5;
|
||
border-bottom: none;
|
||
background: linear-gradient(180deg, #ffffff 0%, #edf2f8 100%);
|
||
color: #2a3b52;
|
||
border-radius: 8px 8px 0 0;
|
||
padding: 9px 14px;
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.tab:hover {
|
||
border-color: #8ca0b8;
|
||
}
|
||
|
||
.tab.active {
|
||
background: #fff;
|
||
color: #0c2f57;
|
||
border-color: #8ea6c2;
|
||
box-shadow: inset 0 2px 0 #0b69c7;
|
||
}
|
||
|
||
.content {
|
||
flex: 1 1 auto;
|
||
min-height: 0;
|
||
padding: 14px;
|
||
overflow: auto;
|
||
background: #fff;
|
||
}
|
||
|
||
.tab-panel {
|
||
height: 100%;
|
||
}
|
||
|
||
.main-grid {
|
||
height: 100%;
|
||
display: flex;
|
||
gap: 14px;
|
||
min-height: 480px;
|
||
}
|
||
|
||
.left {
|
||
flex: 1 1 64%;
|
||
min-width: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 14px;
|
||
}
|
||
|
||
.right {
|
||
flex: 1 1 36%;
|
||
min-width: 320px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
|
||
.panel {
|
||
border: 1px solid var(--line);
|
||
border-radius: 10px;
|
||
background: var(--panel-soft);
|
||
padding: 12px;
|
||
min-height: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
flex: 1;
|
||
}
|
||
|
||
.panel-title {
|
||
margin: 0;
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
color: #26384d;
|
||
padding-bottom: 8px;
|
||
border-bottom: 1px solid #e2e8f0;
|
||
}
|
||
|
||
.list-title-row {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 10px;
|
||
margin-top: 10px;
|
||
}
|
||
|
||
.list-title {
|
||
font-size: 14px;
|
||
color: var(--subtext);
|
||
font-weight: 600;
|
||
}
|
||
|
||
button {
|
||
appearance: none;
|
||
border: 1px solid #a9b6c7;
|
||
background: linear-gradient(180deg, #ffffff 0%, #eaf0f8 100%);
|
||
color: #243446;
|
||
border-radius: 6px;
|
||
padding: 7px 13px;
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
}
|
||
|
||
button:hover {
|
||
border-color: #8698af;
|
||
background: linear-gradient(180deg, #ffffff 0%, #e1e9f3 100%);
|
||
}
|
||
|
||
.refresh-btn {
|
||
border-color: var(--accent);
|
||
background: var(--accent);
|
||
color: #fff;
|
||
}
|
||
|
||
.refresh-btn:hover {
|
||
border-color: var(--accent-hover);
|
||
background: var(--accent-hover);
|
||
}
|
||
|
||
.list {
|
||
margin-top: 8px;
|
||
border: 1px solid #dde5ef;
|
||
border-radius: 7px;
|
||
background: #fff;
|
||
padding: 8px 10px;
|
||
overflow-y: auto;
|
||
min-height: 140px;
|
||
flex: 1;
|
||
}
|
||
|
||
.item {
|
||
font-size: 14px;
|
||
color: #243446;
|
||
padding: 7px 3px;
|
||
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;
|
||
}
|
||
|
||
.qrcode-box {
|
||
margin-top: 10px;
|
||
text-align: center;
|
||
display: flex;
|
||
flex-direction: column;
|
||
flex: 1;
|
||
justify-content: center;
|
||
gap: 8px;
|
||
}
|
||
|
||
#qrcode {
|
||
width: 256px;
|
||
height: 256px;
|
||
margin: 0 auto;
|
||
text-align: center;
|
||
border: 1px solid #dce4ee;
|
||
border-radius: 8px;
|
||
background: #fff;
|
||
padding: 10px;
|
||
}
|
||
|
||
img {
|
||
width: auto;
|
||
height: 100%;
|
||
display: inline-block !important;
|
||
}
|
||
|
||
.error {
|
||
color: var(--danger);
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
}
|
||
|
||
.placeholder {
|
||
height: 100%;
|
||
border: 1px dashed #c5d2e3;
|
||
border-radius: 10px;
|
||
background: #fbfdff;
|
||
color: #607289;
|
||
font-size: 14px;
|
||
padding: 16px;
|
||
}
|
||
|
||
.form-row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
margin-bottom: 12px;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.form-row label {
|
||
min-width: 120px;
|
||
color: #344a62;
|
||
font-weight: 600;
|
||
}
|
||
|
||
.form-row input {
|
||
width: 180px;
|
||
border: 1px solid #b8c7d9;
|
||
border-radius: 6px;
|
||
padding: 7px 10px;
|
||
font-size: 14px;
|
||
color: #203246;
|
||
background: #fff;
|
||
}
|
||
|
||
.hint {
|
||
margin-top: 8px;
|
||
color: #556a83;
|
||
font-size: 13px;
|
||
line-height: 1.5;
|
||
}
|
||
|
||
.inline-ok {
|
||
color: #107c10;
|
||
font-size: 13px;
|
||
margin-top: 8px;
|
||
font-weight: 600;
|
||
}
|
||
|
||
.update-card {
|
||
margin-top: 18px;
|
||
border: 1px solid #c8d8ec;
|
||
border-radius: 10px;
|
||
background: #ffffff;
|
||
padding: 12px;
|
||
}
|
||
|
||
.update-title {
|
||
margin: 0 0 10px 0;
|
||
color: #2c425d;
|
||
font-size: 15px;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.update-meta {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 12px;
|
||
color: #4d627a;
|
||
font-size: 13px;
|
||
margin-bottom: 10px;
|
||
}
|
||
|
||
.update-progress-track {
|
||
width: 100%;
|
||
height: 12px;
|
||
border-radius: 999px;
|
||
background: #e7edf6;
|
||
overflow: hidden;
|
||
border: 1px solid #d7e1ee;
|
||
}
|
||
|
||
.update-progress-fill {
|
||
height: 100%;
|
||
background: linear-gradient(90deg, #0b69c7 0%, #2f87de 100%);
|
||
transition: width 0.25s ease;
|
||
}
|
||
|
||
.update-progress-text {
|
||
margin-top: 8px;
|
||
font-size: 13px;
|
||
color: #38506a;
|
||
}
|
||
|
||
.update-actions {
|
||
margin-top: 10px;
|
||
display: flex;
|
||
gap: 8px;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.statusbar {
|
||
flex: 0 0 auto;
|
||
border-top: 1px solid var(--line);
|
||
background: #f8fbff;
|
||
padding: 9px 12px;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 16px;
|
||
overflow-x: auto;
|
||
}
|
||
|
||
.status-item {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 7px;
|
||
white-space: nowrap;
|
||
color: #3a4d64;
|
||
font-size: 13px;
|
||
}
|
||
|
||
.status-action {
|
||
border-color: #bf8a1d;
|
||
background: linear-gradient(180deg, #ffe5aa 0%, #ffd27a 100%);
|
||
color: #6d4a03;
|
||
}
|
||
|
||
.status-action.restart {
|
||
margin-left: auto;
|
||
}
|
||
|
||
.status-action:hover {
|
||
border-color: #9f6e0d;
|
||
background: linear-gradient(180deg, #ffdc94 0%, #ffc862 100%);
|
||
}
|
||
|
||
.status-dot {
|
||
width: 10px;
|
||
height: 10px;
|
||
border-radius: 50%;
|
||
border: 1px solid rgba(0, 0, 0, 0.12);
|
||
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8);
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.status-link {
|
||
cursor: pointer;
|
||
text-decoration: underline;
|
||
text-underline-offset: 2px;
|
||
}
|
||
|
||
@media (max-width: 980px) {
|
||
.main-grid {
|
||
flex-direction: column;
|
||
min-height: 0;
|
||
}
|
||
|
||
.right {
|
||
min-width: 0;
|
||
}
|
||
}
|
||
|
||
@media (max-width: 640px) {
|
||
body {
|
||
padding: 8px;
|
||
}
|
||
|
||
#app {
|
||
height: calc(100vh - 16px);
|
||
}
|
||
|
||
.topbar {
|
||
flex-direction: column;
|
||
align-items: flex-start;
|
||
gap: 6px;
|
||
}
|
||
|
||
.title {
|
||
font-size: 22px;
|
||
}
|
||
|
||
#qrcode {
|
||
width: 220px;
|
||
height: 220px;
|
||
}
|
||
}
|
||
</style>
|
||
</head>
|
||
|
||
<body>
|
||
<div id="app">
|
||
<div class="topbar">
|
||
<h1 class="title">天豹云仓打印服务台</h1>
|
||
<div class="name-badge">工作台:{{ name || '未命名' }}</div>
|
||
</div>
|
||
|
||
<div class="tabbar">
|
||
<button class="tab" :class="{ active: activeTab === 'overview' }" @click="activeTab = 'overview'">运行总览</button>
|
||
<button class="tab" :class="{ active: activeTab === 'extension' }" @click="activeTab = 'extension'">端口设置</button>
|
||
</div>
|
||
|
||
<div class="content">
|
||
<div class="tab-panel" v-show="activeTab === 'overview'">
|
||
<div class="main-grid">
|
||
<div class="left">
|
||
<div class="panel">
|
||
<h2 class="panel-title">打印机列表</h2>
|
||
<div class="list-title-row">
|
||
<div class="list-title">当前可用打印机</div>
|
||
<button @click="loadPrinters" class="refresh-btn">刷新打印机状态</button>
|
||
</div>
|
||
<div class="list">
|
||
<div class="item" v-if="printers.length === 0">无打印机</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 class="panel">
|
||
<h2 class="panel-title">打印组件状态</h2>
|
||
<div class="list-title-row">
|
||
<div class="list-title">组件连接状态总览</div>
|
||
</div>
|
||
<div class="list">
|
||
<div class="item" v-if="components.length === 0">无打印组件</div>
|
||
<div class="item" v-for="c in components" :key="c.name">{{ c.name }}
|
||
<i :style="{color: wsStatus[getStatus(c.name)].color}">{{wsStatus[getStatus(c.name)].title}}</i>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="right" v-if="!isBind">
|
||
<div class="panel">
|
||
<h2 class="panel-title">绑定工作台</h2>
|
||
<div class="qrcode-box">
|
||
<div id="qrcode" ref="qrcode"></div>
|
||
<div class="error">未绑定打印台,请使用微信扫码二维码完成绑定。</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="tab-panel" v-show="activeTab === 'extension'">
|
||
<div class="placeholder">
|
||
<h3 style="margin:0 0 12px 0;color:#2c425d;">端口设置</h3>
|
||
<div class="form-row">
|
||
<label for="wsPort">本地 WS 端口</label>
|
||
<input id="wsPort" type="number" min="1" max="65535" v-model.number="portForm.wsPort">
|
||
<button class="refresh-btn" @click="savePorts" :disabled="portSaving || restartingBackend">保存并重连</button>
|
||
</div>
|
||
<div class="hint">保存后会自动重启后端,并让页面重新连接到新的 WS 端口。</div>
|
||
<div class="inline-ok" v-if="saveMessage">{{ saveMessage }}</div>
|
||
|
||
<div class="update-card">
|
||
<h4 class="update-title">应用更新</h4>
|
||
<div class="update-meta">
|
||
<span>当前版本:{{ updateState.currentVersion || '-' }}</span>
|
||
<span>最新版本:{{ updateState.latestVersion || '-' }}</span>
|
||
<span>状态:{{ updateStatusText }}</span>
|
||
</div>
|
||
<div class="update-progress-track">
|
||
<div class="update-progress-fill" :style="{ width: updateProgressPercent + '%' }"></div>
|
||
</div>
|
||
<div class="update-progress-text">{{ updateState.message || '未开始下载' }}({{ updateProgressPercent }}%)</div>
|
||
<div class="update-progress-text" v-if="updateState.downloadedBytes > 0">
|
||
{{ formatBytes(updateState.downloadedBytes) }} / {{ updateState.totalBytes > 0 ? formatBytes(updateState.totalBytes) : '未知大小' }}
|
||
</div>
|
||
<div class="update-actions">
|
||
<button class="refresh-btn" @click="checkUpdate(true)" :disabled="updateChecking || updatingInApp">{{ updateChecking ? '检查中...' : '检查更新' }}</button>
|
||
<button @click="startInAppUpdate()" :disabled="!updateState.hasUpdate || updatingInApp || !updateState.downloadUrl">
|
||
{{ updatingInApp ? '下载中...' : '应用内下载并安装' }}
|
||
</button>
|
||
<button @click="openDownloadedInstaller()" :disabled="!updateState.filePath || updatingInApp">再次打开安装包</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="statusbar">
|
||
<div class="status-item">
|
||
<span class="status-dot" :style="{ backgroundColor: wsStatus[wsClientStatus].color }"></span>
|
||
<span>本地服务:<strong :style="{color: wsStatus[wsClientStatus].color}">{{ wsStatus[wsClientStatus].title }}</strong></span>
|
||
</div>
|
||
<div class="status-item">
|
||
<span class="status-dot" :style="{ backgroundColor: isServices ? '#107c10' : '#c42b1c' }"></span>
|
||
<span>服务注册:
|
||
<strong class="status-link" :style="{ color: isServices ? '#107c10' : '#c42b1c' }" @click="isServices ? unregister() : register()">{{ isServices ? '已注册' : '未注册' }}</strong>
|
||
</span>
|
||
</div>
|
||
<div class="status-item">
|
||
<span class="status-dot" :style="{ backgroundColor: wsStatus[wsServerStatus].color }"></span>
|
||
<span>服务器连接:<strong :style="{color: wsStatus[wsServerStatus].color}">{{ wsStatus[wsServerStatus].title }}</strong></span>
|
||
</div>
|
||
<div class="status-item">
|
||
<span class="status-dot" :style="{ backgroundColor: isBind ? '#107c10' : '#c42b1c' }"></span>
|
||
<span>打印服务绑定:<strong :style="{ color: isBind ? '#107c10' : '#c42b1c' }">{{ isBind ? '已绑定' : '未绑定' }}</strong></span>
|
||
</div>
|
||
<button class="status-action" @click="checkUpdate(true)" :disabled="updateChecking">{{ updateChecking ? '检查中...' : '检查更新' }}</button>
|
||
<button class="status-action restart" @click="confirmRestartBackend" :disabled="restartingBackend">{{ restartingBackend ? '重启中...' : '重启服务' }}</button>
|
||
</div>
|
||
</div>
|
||
<script>
|
||
(function initApp() {
|
||
if (!window.__depsReady) {
|
||
window.addEventListener('deps-ready', initApp, { once: true });
|
||
window.addEventListener('deps-failed', function () {
|
||
var root = document.getElementById('app');
|
||
if (root) {
|
||
root.innerHTML = '<h2 style="color:#c62828;">页面依赖加载失败,请检查网络或更换可用 CDN。</h2>';
|
||
}
|
||
}, { once: true });
|
||
return;
|
||
}
|
||
|
||
let ws;
|
||
const runtimeParams = new URLSearchParams(window.location.search || '');
|
||
const runtimeWsPort = Number(runtimeParams.get('wsPort')) || 3001;
|
||
let wsUrl = `ws://127.0.0.1:${runtimeWsPort}`;
|
||
let reconnectTimer = null;
|
||
function setWsClientStatus(status) {
|
||
const app = document.getElementById('app').__vue__;
|
||
if (!app) return;
|
||
if (typeof app.onLocalWsStatusChange === 'function') {
|
||
app.onLocalWsStatusChange(status);
|
||
return;
|
||
}
|
||
app.wsClientStatus = status;
|
||
}
|
||
// 全局事件监听器注册表
|
||
let wsMethodListeners = {};
|
||
// 只定义一次 wsOn
|
||
function wsOn(method, callback) {
|
||
if (!wsMethodListeners[method]) wsMethodListeners[method] = [];
|
||
wsMethodListeners[method].push(callback);
|
||
}
|
||
|
||
function connectWS() {
|
||
setWsClientStatus(reconnectTimer ? 'reconnecting' : 'connecting');
|
||
ws = new WebSocket(wsUrl);
|
||
ws.onopen = function () {
|
||
console.log('WebSocket 已连接');
|
||
if (reconnectTimer) {
|
||
clearTimeout(reconnectTimer);
|
||
reconnectTimer = null;
|
||
}
|
||
setWsClientStatus('connected');
|
||
};
|
||
ws.onmessage = function (event) {
|
||
try {
|
||
const resp = JSON.parse(event.data);
|
||
if (resp.method && wsMethodListeners[resp.method]) {
|
||
wsMethodListeners[resp.method].forEach(cb => cb(resp));
|
||
}
|
||
} catch (e) { }
|
||
};
|
||
ws.onclose = function () {
|
||
console.log('WebSocket 断开,3秒后重连');
|
||
setWsClientStatus('reconnecting');
|
||
reconnectTimer = setTimeout(connectWS, 3000);
|
||
};
|
||
ws.onerror = function (err) {
|
||
console.log('WebSocket 错误:', err);
|
||
// 统一由 onclose 处理重连与状态,避免 error/disconnected 来回抖动。
|
||
if (ws && ws.readyState !== WebSocket.CLOSED) {
|
||
ws.close();
|
||
}
|
||
};
|
||
}
|
||
|
||
function reconnectWSWithPort(nextWsPort) {
|
||
const portNum = Number(nextWsPort) || runtimeWsPort;
|
||
wsUrl = `ws://127.0.0.1:${portNum}`;
|
||
|
||
if (reconnectTimer) {
|
||
clearTimeout(reconnectTimer);
|
||
reconnectTimer = null;
|
||
}
|
||
|
||
if (ws) {
|
||
try {
|
||
ws.onclose = null;
|
||
ws.close();
|
||
} catch (e) { }
|
||
}
|
||
|
||
connectWS();
|
||
}
|
||
connectWS();
|
||
|
||
// 封装 ws 发送并监听回调
|
||
function wsSendWithCallback(method, data, callback) {
|
||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||
// Directly update the status instead of showing an alert
|
||
setWsClientStatus('reconnecting');
|
||
return;
|
||
}
|
||
function handler(event) {
|
||
try {
|
||
const resp = JSON.parse(event.data);
|
||
if (resp.method === method) {
|
||
ws.removeEventListener('message', handler);
|
||
callback(resp);
|
||
}
|
||
} catch (e) { }
|
||
}
|
||
ws.addEventListener('message', handler);
|
||
ws.send(JSON.stringify({ method, data }));
|
||
}
|
||
// 按钮事件绑定需在 DOM 渲染后执行
|
||
window.onload = function () {
|
||
var refreshBtn = document.getElementById('refreshPrinters');
|
||
if (refreshBtn) {
|
||
}
|
||
};
|
||
|
||
new Vue({
|
||
el: '#app',
|
||
data: {
|
||
activeTab: 'overview',
|
||
printers: [],
|
||
components: [],
|
||
statusList: [],
|
||
isBind: false,
|
||
wsClientStatus: 'disconnected',
|
||
wsServerStatus: 'disconnected',
|
||
name: '',
|
||
saveMessage: '',
|
||
portSaving: false,
|
||
restartingBackend: false,
|
||
updateChecking: false,
|
||
updatingInApp: false,
|
||
portForm: {
|
||
wsPort: runtimeWsPort
|
||
},
|
||
updateState: {
|
||
hasUpdate: false,
|
||
currentVersion: '',
|
||
latestVersion: '',
|
||
downloadUrl: '',
|
||
status: 'idle',
|
||
percent: 0,
|
||
downloadedBytes: 0,
|
||
totalBytes: 0,
|
||
message: ''
|
||
},
|
||
code: '',
|
||
qrcode: new QRCode("qrcode"),
|
||
wsStatus: {
|
||
'disconnected': { title: '未连接', color: 'red' },
|
||
'connecting': { title: '连接中', color: '#f59f00' },
|
||
'reconnecting': { title: '重连中', color: '#f59f00' },
|
||
'connected': { title: '已连接', color: 'green' },
|
||
'error': { title: '连接错误', color: 'red' },
|
||
},
|
||
isServices: false
|
||
},
|
||
computed: {
|
||
updateProgressPercent() {
|
||
const value = Number(this.updateState.percent || 0);
|
||
if (!Number.isFinite(value)) {
|
||
return 0;
|
||
}
|
||
return Math.max(0, Math.min(100, Math.round(value)));
|
||
},
|
||
updateStatusText() {
|
||
const map = {
|
||
idle: '空闲',
|
||
starting: '准备下载',
|
||
downloading: '下载中',
|
||
downloaded: '下载完成',
|
||
installing: '安装中',
|
||
failed: '失败'
|
||
};
|
||
return map[this.updateState.status] || '空闲';
|
||
}
|
||
},
|
||
methods: {
|
||
formatBytes(bytes) {
|
||
const value = Number(bytes || 0);
|
||
if (!Number.isFinite(value) || value <= 0) {
|
||
return '0 B';
|
||
}
|
||
|
||
const units = ['B', 'KB', 'MB', 'GB'];
|
||
let size = value;
|
||
let idx = 0;
|
||
while (size >= 1024 && idx < units.length - 1) {
|
||
size /= 1024;
|
||
idx += 1;
|
||
}
|
||
|
||
return `${size.toFixed(idx === 0 ? 0 : 2)} ${units[idx]}`;
|
||
},
|
||
loadComponents() {
|
||
wsSendWithCallback('printerComponent.getAll', {}, (data) => {
|
||
console.log('获取组件列表:', data);
|
||
if (data.success) {
|
||
this.components = data.result.components || [];
|
||
this.statusList = data.result.status || [];
|
||
} else {
|
||
alert(data.message || '获取打印组件列表失败!');
|
||
}
|
||
});
|
||
},
|
||
getStatus(name) {
|
||
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) {
|
||
this.printers = data.result || [];
|
||
if (this.printers.length === 0) {
|
||
this.selectedPrinter = '';
|
||
} else if (!this.selectedPrinter) {
|
||
this.selectedPrinter = this.printers[0].name;
|
||
}
|
||
} else {
|
||
alert(data.message || '获取打印机列表失败!');
|
||
}
|
||
});
|
||
},
|
||
register() {
|
||
wsSendWithCallback('setting.register', {}, (data) => {
|
||
if (data.success && data.result && data.result.ok) {
|
||
this.isServices = !!(data.result && (data.result.isService ?? data.result.isServices));
|
||
} else {
|
||
alert((data.result && data.result.message) || data.message || '系统服务注册失败!');
|
||
this.refreshBindStatus();
|
||
}
|
||
});
|
||
},
|
||
unregister() {
|
||
wsSendWithCallback('setting.unregister', {}, (data) => {
|
||
if (data.success && data.result && data.result.ok) {
|
||
this.isServices = !!(data.result && (data.result.isService ?? data.result.isServices));
|
||
} else {
|
||
alert((data.result && data.result.message) || data.message || '系统服务注销失败!');
|
||
this.refreshBindStatus();
|
||
}
|
||
});
|
||
},
|
||
refreshBindStatus() {
|
||
wsSendWithCallback('setting.info', {}, (data) => {
|
||
if (data.success) {
|
||
this.isBind = data.result.isBind || false;
|
||
this.isServices = data.result.isService || false;
|
||
this.portForm.wsPort = Number(data.result.wsPort) || this.portForm.wsPort;
|
||
} else {
|
||
alert(data.message || '获取绑定状态失败!');
|
||
}
|
||
});
|
||
},
|
||
savePorts() {
|
||
if (!this.portForm.wsPort) {
|
||
alert('端口不能为空');
|
||
return;
|
||
}
|
||
|
||
if (this.portForm.wsPort < 1 || this.portForm.wsPort > 65535) {
|
||
alert('端口范围应为 1-65535');
|
||
return;
|
||
}
|
||
|
||
this.portSaving = true;
|
||
this.saveMessage = '';
|
||
|
||
wsSendWithCallback('setting.updatePorts', {
|
||
wsPort: Number(this.portForm.wsPort)
|
||
}, (data) => {
|
||
this.portSaving = false;
|
||
if (!data.success || !data.result || !data.result.ok) {
|
||
alert((data.result && data.result.message) || data.message || '端口保存失败');
|
||
return;
|
||
}
|
||
|
||
this.saveMessage = data.result.message || '端口已更新';
|
||
this.portForm.wsPort = Number(data.result.wsPort) || this.portForm.wsPort;
|
||
if (data.result.requiresBackendRestart) {
|
||
this.requestBackendRestart({
|
||
wsPort: Number(data.result.wsPort) || this.portForm.wsPort,
|
||
successMessage: '端口已保存,后端重启中...'
|
||
});
|
||
}
|
||
});
|
||
},
|
||
startRestartWatch(timeoutMs, errorMessage) {
|
||
if (this._restartWatchTimer) {
|
||
clearTimeout(this._restartWatchTimer);
|
||
this._restartWatchTimer = null;
|
||
}
|
||
|
||
this._restartWatchTimer = setTimeout(() => {
|
||
if (!this.restartingBackend) {
|
||
return;
|
||
}
|
||
|
||
this.restartingBackend = false;
|
||
alert(errorMessage || '后端重启失败,请检查端口是否被占用后重试');
|
||
}, timeoutMs || 15000);
|
||
},
|
||
onLocalWsStatusChange(status) {
|
||
this.wsClientStatus = status;
|
||
if (status === 'connected' && this.restartingBackend) {
|
||
this.restartingBackend = false;
|
||
if (this._restartWatchTimer) {
|
||
clearTimeout(this._restartWatchTimer);
|
||
this._restartWatchTimer = null;
|
||
}
|
||
this.saveMessage = '后端已重启并连接成功';
|
||
}
|
||
},
|
||
requestBackendRestart(options = {}) {
|
||
if (this.restartingBackend) {
|
||
return;
|
||
}
|
||
|
||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||
alert('当前本地服务未连接,无法发送重启指令,请稍后重试');
|
||
return;
|
||
}
|
||
|
||
const targetWsPort = Number(options.wsPort) || Number(this.portForm.wsPort) || runtimeWsPort;
|
||
this.restartingBackend = true;
|
||
|
||
wsSendWithCallback('setting.restartBackend', {
|
||
wsPort: targetWsPort
|
||
}, (data) => {
|
||
if (!data.success || !data.result || !data.result.ok) {
|
||
this.restartingBackend = false;
|
||
alert((data.result && data.result.message) || data.message || '后端重启失败');
|
||
return;
|
||
}
|
||
|
||
this.saveMessage = options.successMessage || data.result.message || '后端重启中...';
|
||
reconnectWSWithPort(Number(data.result.wsPort) || targetWsPort);
|
||
this.startRestartWatch(15000, '后端重启失败,15 秒内未恢复连接,请检查端口占用后重试');
|
||
});
|
||
},
|
||
confirmRestartBackend() {
|
||
const confirmed = window.confirm('确认重启后端服务吗?重启过程中可能导致正在执行的打印任务失败。');
|
||
if (!confirmed) {
|
||
return;
|
||
}
|
||
|
||
this.requestBackendRestart({
|
||
wsPort: Number(this.portForm.wsPort) || runtimeWsPort,
|
||
successMessage: '后端重启中...'
|
||
});
|
||
},
|
||
checkUpdate(manual = false) {
|
||
if (this.updateChecking) {
|
||
return;
|
||
}
|
||
|
||
this.updateChecking = true;
|
||
wsSendWithCallback('update.check', {}, (data) => {
|
||
this.updateChecking = false;
|
||
|
||
if (!data.success || !data.result || !data.result.ok) {
|
||
if (manual) {
|
||
alert((data.result && data.result.message) || data.message || '检查更新失败');
|
||
}
|
||
return;
|
||
}
|
||
|
||
const result = data.result;
|
||
this.updateState = {
|
||
...this.updateState,
|
||
hasUpdate: !!result.hasUpdate,
|
||
currentVersion: result.currentVersion || this.updateState.currentVersion,
|
||
latestVersion: result.latestVersion || this.updateState.latestVersion,
|
||
downloadUrl: result.downloadUrl || '',
|
||
message: result.hasUpdate
|
||
? `发现新版本 ${result.latestVersion}`
|
||
: `当前已是最新版本(${result.currentVersion})`
|
||
};
|
||
|
||
if (!result.hasUpdate) {
|
||
if (manual) {
|
||
alert(`当前已是最新版本(${result.currentVersion})`);
|
||
}
|
||
return;
|
||
}
|
||
|
||
const prompt = `检测到新版本 ${result.latestVersion}(当前 ${result.currentVersion}),是否在应用内下载并安装?`;
|
||
if (window.confirm(prompt)) {
|
||
this.activeTab = 'extension';
|
||
this.startInAppUpdate();
|
||
}
|
||
});
|
||
},
|
||
startInAppUpdate() {
|
||
if (this.updatingInApp) {
|
||
return;
|
||
}
|
||
|
||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||
alert('当前本地服务未连接,无法开始更新,请稍后重试');
|
||
return;
|
||
}
|
||
|
||
if (!this.updateState.hasUpdate) {
|
||
alert('当前没有可用更新');
|
||
return;
|
||
}
|
||
|
||
if (!this.updateState.downloadUrl) {
|
||
alert('未获取到更新下载地址,请先重新检查更新');
|
||
return;
|
||
}
|
||
|
||
this.activeTab = 'extension';
|
||
|
||
this.updatingInApp = true;
|
||
this.updateState = {
|
||
...this.updateState,
|
||
status: 'starting',
|
||
percent: 0,
|
||
message: '开始下载更新包...'
|
||
};
|
||
|
||
wsSendWithCallback('update.downloadAndInstall', {
|
||
downloadUrl: this.updateState.downloadUrl,
|
||
version: this.updateState.latestVersion
|
||
}, (data) => {
|
||
if (!data.success || !data.result || !data.result.ok) {
|
||
this.updatingInApp = false;
|
||
alert((data.result && data.result.message) || data.message || '启动更新失败');
|
||
return;
|
||
}
|
||
|
||
this.updateState = {
|
||
...this.updateState,
|
||
message: data.result.message || '正在下载更新包...'
|
||
};
|
||
});
|
||
},
|
||
openDownloadedInstaller() {
|
||
if (!this.updateState.filePath) {
|
||
alert('当前没有已下载的安装包');
|
||
return;
|
||
}
|
||
|
||
wsSendWithCallback('update.openDownloadedInstaller', {
|
||
filePath: this.updateState.filePath
|
||
}, (data) => {
|
||
if (!data.success || !data.result || !data.result.ok) {
|
||
alert((data.result && data.result.message) || data.message || '启动安装向导失败');
|
||
return;
|
||
}
|
||
|
||
alert(data.result.message || '安装程序已启动,请在安装向导中完成更新。');
|
||
});
|
||
},
|
||
unbind() {
|
||
wsSendWithCallback('setting.unbind', {}, (data) => {
|
||
if (data.success) {
|
||
this.isBind = false;
|
||
} else {
|
||
alert(data.message || '解绑失败!');
|
||
}
|
||
});
|
||
},
|
||
renderQrcode() {
|
||
this.$nextTick(() => {
|
||
const el = this.$refs.qrcode;
|
||
if (!el) return;
|
||
// 清空容器,防止多次渲染叠加
|
||
el.innerHTML = '';
|
||
// 重新生成二维码实例
|
||
this.qrcode = new QRCode(el, { width: 256, height: 256 });
|
||
if (this.code) {
|
||
this.qrcode.makeCode(`https://ye4s.wms.gztbyc.com/mini/?spm=0.5.${this.code}.1.4`);
|
||
}
|
||
});
|
||
},
|
||
installWsListener() {
|
||
wsOn('printer.getWindowsPrinters', (data) => {
|
||
if (data.success) {
|
||
this.printers = data.result || [];
|
||
if (this.printers.length === 0) {
|
||
this.selectedPrinter = '';
|
||
} else if (!this.selectedPrinter) {
|
||
this.selectedPrinter = this.printers[0].name;
|
||
}
|
||
}
|
||
});
|
||
wsOn('setting.info', (data) => {
|
||
if (data.success) {
|
||
this.isBind = data.result.isBind || false;
|
||
this.name = data.result.name || '';
|
||
this.isServices = data.result.isService || false;
|
||
this.portForm.wsPort = Number(data.result.wsPort) || this.portForm.wsPort;
|
||
}
|
||
});
|
||
wsOn('printerComponent.getAll', (data) => {
|
||
if (data.success) {
|
||
this.components = data.result.components || [];
|
||
this.statusList = data.result.status || [];
|
||
} else {
|
||
alert(data.message || '获取打印组件列表失败!');
|
||
}
|
||
});
|
||
wsOn('printerComponent.statusChange', (data) => {
|
||
if (data.success) {
|
||
this.statusList = [...this.statusList.filter(s => s.name !== data.result.name), { name: data.result.name, status: data.result.status }];
|
||
} else {
|
||
alert(data.message || '获取打印组件列表失败!');
|
||
}
|
||
});
|
||
wsOn('wsClient.statusChange', (data) => {
|
||
if (data.success) {
|
||
this.wsServerStatus = data.result.status;
|
||
} else {
|
||
alert(data.message || '获取在线状态失败!');
|
||
}
|
||
});
|
||
wsOn('bind.generateCode', (data) => {
|
||
if (data.success) {
|
||
this.code = data.result || '';
|
||
} else {
|
||
alert(data.message || '获取绑定码失败!');
|
||
}
|
||
});
|
||
wsOn('update.downloadProgress', (data) => {
|
||
if (!data.success || !data.result) {
|
||
return;
|
||
}
|
||
|
||
this.updateState = {
|
||
...this.updateState,
|
||
...data.result,
|
||
hasUpdate: true,
|
||
latestVersion: data.result.latestVersion || this.updateState.latestVersion,
|
||
currentVersion: data.result.currentVersion || this.updateState.currentVersion,
|
||
downloadUrl: data.result.downloadUrl || this.updateState.downloadUrl
|
||
};
|
||
|
||
if (this.updatingInApp || data.result.status === 'downloading' || data.result.status === 'starting') {
|
||
this.activeTab = 'extension';
|
||
}
|
||
|
||
if (data.result.status === 'installing') {
|
||
this.updatingInApp = false;
|
||
alert('安装程序已启动,请在安装向导中完成更新。');
|
||
}
|
||
|
||
if (data.result.status === 'failed') {
|
||
this.updatingInApp = false;
|
||
alert(data.result.message || '更新失败');
|
||
}
|
||
});
|
||
}
|
||
},
|
||
watch: {
|
||
code(val) {
|
||
if (val) {
|
||
console.log('新的绑定码:', val);
|
||
this.renderQrcode();
|
||
}
|
||
}
|
||
},
|
||
mounted() {
|
||
this._restartWatchTimer = null;
|
||
setInterval(() => {
|
||
this.refreshBindStatus();
|
||
}, 10000);
|
||
this.installWsListener();
|
||
this.refreshBindStatus();
|
||
wsSendWithCallback('update.getDownloadState', {}, (data) => {
|
||
if (data.success && data.result && data.result.ok) {
|
||
this.updateState = {
|
||
...this.updateState,
|
||
...data.result,
|
||
hasUpdate: !!(data.result.latestVersion && data.result.currentVersion && data.result.latestVersion !== data.result.currentVersion)
|
||
};
|
||
}
|
||
});
|
||
setTimeout(() => {
|
||
this.checkUpdate(false);
|
||
}, 3000);
|
||
},
|
||
beforeDestroy() {
|
||
if (this._restartWatchTimer) {
|
||
clearTimeout(this._restartWatchTimer);
|
||
this._restartWatchTimer = null;
|
||
}
|
||
}
|
||
});
|
||
})();
|
||
</script>
|
||
</body>
|
||
|
||
</html> |