1202 lines
46 KiB
HTML
1202 lines
46 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%;
|
||
overflow: hidden;
|
||
}
|
||
|
||
body {
|
||
font-family: "Segoe UI", "Microsoft YaHei UI", sans-serif;
|
||
color: var(--text);
|
||
background: linear-gradient(150deg, #f8fbff 0%, var(--bg) 72%);
|
||
padding: 12px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
#app {
|
||
width: 100%;
|
||
flex: 1;
|
||
min-height: 0;
|
||
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="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: 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" id="btnCheckUpdate">{{ updateChecking ? '检查中...' : '检查更新' }}</button>
|
||
<button class="status-action restart" id="btnRestartBackend">{{ 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;
|
||
}
|
||
|
||
// ─── IPC 通讯层 ───
|
||
// 使用 window.printServerAPI(由 preload.cjs 通过 contextBridge 注入)
|
||
// callService(method, data) → Promise
|
||
// onEvent(method, callback) → unsubscribe function
|
||
|
||
new Vue({
|
||
el: '#app',
|
||
data: {
|
||
activeTab: 'overview',
|
||
printers: [],
|
||
components: [],
|
||
statusList: [],
|
||
isBind: false,
|
||
wsClientStatus: 'connected',
|
||
wsServerStatus: 'disconnected',
|
||
name: '',
|
||
restartingBackend: false,
|
||
updateChecking: false,
|
||
updatingInApp: false,
|
||
updateState: {
|
||
hasUpdate: false,
|
||
currentVersion: '',
|
||
latestVersion: '',
|
||
downloadUrl: '',
|
||
status: 'idle',
|
||
percent: 0,
|
||
downloadedBytes: 0,
|
||
totalBytes: 0,
|
||
message: ''
|
||
},
|
||
code: '',
|
||
qrcode: null,
|
||
wsStatus: {
|
||
'disconnected': { title: '未连接', color: 'red' },
|
||
'connecting': { title: '连接中', color: '#f59f00' },
|
||
'reconnecting': { title: '重连中', color: '#f59f00' },
|
||
'connected': { title: '已连接', color: 'green' },
|
||
'error': { title: '连接错误', color: 'red' },
|
||
},
|
||
},
|
||
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() {
|
||
window.printServerAPI.callService('printerComponent.getAll', {}).then((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() {
|
||
window.printServerAPI.callService('printer.getWindowsPrinters', {}).then((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 || '获取打印机列表失败!');
|
||
}
|
||
});
|
||
},
|
||
|
||
refreshBindStatus() {
|
||
window.printServerAPI.callService('setting.info', {}).then((data) => {
|
||
if (data.success) {
|
||
this.isBind = data.result.isBind || false;
|
||
this.name = data.result.name || '';
|
||
} else {
|
||
alert(data.message || '获取绑定状态失败!');
|
||
}
|
||
});
|
||
},
|
||
onBackendStatusChange(status) {
|
||
this.wsClientStatus = status;
|
||
if (status === 'connected' && this.restartingBackend) {
|
||
this.restartingBackend = false;
|
||
}
|
||
},
|
||
requestBackendRestart() {
|
||
if (this.restartingBackend) {
|
||
return;
|
||
}
|
||
|
||
this.restartingBackend = true;
|
||
|
||
window.printServerAPI.callService('setting.restartBackend', {}).then((data) => {
|
||
if (!data.success || !data.result || !data.result.ok) {
|
||
this.restartingBackend = false;
|
||
alert((data.result && data.result.message) || data.message || '后端重启失败');
|
||
return;
|
||
}
|
||
|
||
this.restartingBackend = false;
|
||
// 重启后刷新数据
|
||
setTimeout(() => {
|
||
this.refreshBindStatus();
|
||
this.loadComponents();
|
||
this.loadPrinters();
|
||
}, 500);
|
||
}).catch(() => {
|
||
this.restartingBackend = false;
|
||
});
|
||
},
|
||
confirmRestartBackend() {
|
||
const confirmed = window.confirm('确认重启后端服务吗?重启过程中可能导致正在执行的打印任务失败。');
|
||
if (!confirmed) {
|
||
return;
|
||
}
|
||
|
||
this.requestBackendRestart();
|
||
},
|
||
checkUpdate(manual = false) {
|
||
if (this.updateChecking) {
|
||
return;
|
||
}
|
||
|
||
this.updateChecking = true;
|
||
window.printServerAPI.callService('update.check', {}).then((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 (!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: '开始下载更新包...'
|
||
};
|
||
|
||
window.printServerAPI.callService('update.downloadAndInstall', {
|
||
downloadUrl: this.updateState.downloadUrl,
|
||
version: this.updateState.latestVersion
|
||
}).then((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;
|
||
}
|
||
|
||
window.printServerAPI.callService('update.openDownloadedInstaller', {
|
||
filePath: this.updateState.filePath
|
||
}).then((data) => {
|
||
if (!data.success || !data.result || !data.result.ok) {
|
||
alert((data.result && data.result.message) || data.message || '启动安装向导失败');
|
||
return;
|
||
}
|
||
|
||
alert(data.result.message || '安装程序已启动,请在安装向导中完成更新。');
|
||
});
|
||
},
|
||
unbind() {
|
||
window.printServerAPI.callService('setting.unbind', {}).then((data) => {
|
||
if (data.success) {
|
||
this.isBind = false;
|
||
} else {
|
||
alert(data.message || '解绑失败!');
|
||
}
|
||
});
|
||
},
|
||
renderQrcode() {
|
||
console.log('[QR] renderQrcode 被调用, code:', this.code);
|
||
try {
|
||
var el = document.getElementById('qrcode');
|
||
console.log('[QR] qrcode DOM 元素:', el);
|
||
if (!el) { console.log('[QR] 容器未找到'); return; }
|
||
el.innerHTML = '';
|
||
console.log('[QR] typeof QRCode:', typeof QRCode);
|
||
if (typeof QRCode === 'undefined') { console.log('[QR] QRCode 库未加载'); return; }
|
||
this.qrcode = new QRCode(el, { width: 256, height: 256 });
|
||
console.log('[QR] QRCode 实例已创建');
|
||
if (this.code) {
|
||
var url = 'https://ye4s.wms.gztbyc.com/mini/?spm=0.5.' + this.code + '.1.4';
|
||
console.log('[QR] 生成二维码, URL:', url);
|
||
this.qrcode.makeCode(url);
|
||
console.log('[QR] makeCode 完成');
|
||
}
|
||
} catch (e) { console.error('[QR] 渲染失败:', e); }
|
||
},
|
||
installWsListener() {
|
||
this._unsubFns.push(
|
||
window.printServerAPI.onEvent('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;
|
||
}
|
||
}
|
||
})
|
||
);
|
||
this._unsubFns.push(
|
||
window.printServerAPI.onEvent('setting.info', (data) => {
|
||
if (data.success) {
|
||
this.isBind = data.result.isBind || false;
|
||
this.name = data.result.name || '';
|
||
}
|
||
})
|
||
);
|
||
this._unsubFns.push(
|
||
window.printServerAPI.onEvent('printerComponent.getAll', (data) => {
|
||
if (data.success) {
|
||
this.components = data.result.components || [];
|
||
this.statusList = data.result.status || [];
|
||
} else {
|
||
alert(data.message || '获取打印组件列表失败!');
|
||
}
|
||
})
|
||
);
|
||
this._unsubFns.push(
|
||
window.printServerAPI.onEvent('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 || '获取打印组件列表失败!');
|
||
}
|
||
})
|
||
);
|
||
this._unsubFns.push(
|
||
window.printServerAPI.onEvent('wsClient.statusChange', (data) => {
|
||
if (data.success) {
|
||
this.wsServerStatus = data.result.status;
|
||
} else {
|
||
alert(data.message || '获取在线状态失败!');
|
||
}
|
||
})
|
||
);
|
||
this._unsubFns.push(
|
||
window.printServerAPI.onEvent('bind.generateCode', (data) => {
|
||
console.log('[QR] 收到 bind.generateCode 事件:', JSON.stringify(data));
|
||
if (data.success) {
|
||
this.code = data.result || '';
|
||
console.log('[QR] code 已设为:', this.code);
|
||
} else {
|
||
alert(data.message || '获取绑定码失败!');
|
||
}
|
||
})
|
||
);
|
||
this._unsubFns.push(
|
||
window.printServerAPI.onEvent('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) {
|
||
console.log('[QR] code watcher 触发, 旧值->新值:', val);
|
||
if (val) {
|
||
console.log('[QR] 新的绑定码:', val);
|
||
this.renderQrcode();
|
||
} else {
|
||
console.log('[QR] code 为空, 跳过二维码渲染');
|
||
}
|
||
}
|
||
},
|
||
mounted() {
|
||
this._unsubFns = [];
|
||
|
||
// 注册 IPC 事件监听
|
||
this.installWsListener();
|
||
|
||
// 注册后端连接状态监听
|
||
this._unsubFns.push(
|
||
window.printServerAPI.onBackendStatusChange((status) => {
|
||
this.onBackendStatusChange(status);
|
||
})
|
||
);
|
||
|
||
// 初始数据加载
|
||
this.refreshBindStatus();
|
||
this.loadComponents();
|
||
this.loadPrinters();
|
||
// 主动拉取远端服务器连接状态(弥补 IPC 事件早于监听注册的时序问题)
|
||
window.printServerAPI.callService('wsClient.getStatus', {}).then((data) => {
|
||
if (data.success && data.result) {
|
||
this.wsServerStatus = data.result.status;
|
||
}
|
||
});
|
||
// 弥补 IPC 事件可能早于监听注册的时序问题:主动获取缓存中的绑定码
|
||
window.printServerAPI.callService('bind.getCurrentCode', {}).then((data) => {
|
||
console.log('[QR] bind.getCurrentCode 返回:', JSON.stringify(data));
|
||
if (data.success && data.result) {
|
||
console.log('[QR] 从缓存获取到 code:', data.result);
|
||
this.code = data.result;
|
||
} else {
|
||
console.log('[QR] 缓存无 code,等待 IPC 事件');
|
||
}
|
||
});
|
||
window.printServerAPI.callService('update.getDownloadState', {}).then((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)
|
||
};
|
||
}
|
||
});
|
||
|
||
// 定期刷新
|
||
this._refreshInterval = setInterval(() => {
|
||
this.refreshBindStatus();
|
||
}, 10000);
|
||
|
||
// 延迟检查更新
|
||
setTimeout(() => {
|
||
this.checkUpdate(false);
|
||
}, 3000);
|
||
|
||
// QRCode 重试:首次加载失败时自动重试
|
||
this._qrTimer = setInterval(function () {
|
||
var self = document.getElementById('app').__vue__;
|
||
if (!self) return;
|
||
var qrEl = document.getElementById('qrcode');
|
||
if (qrEl && qrEl.children.length > 0) { clearInterval(self._qrTimer); self._qrTimer = null; return; }
|
||
if (self.code && typeof QRCode !== 'undefined') {
|
||
self.renderQrcode();
|
||
clearInterval(self._qrTimer);
|
||
self._qrTimer = null;
|
||
}
|
||
}, 2000);
|
||
// 15 秒后停止重试
|
||
setTimeout(function () {
|
||
var self = document.getElementById('app').__vue__;
|
||
if (self && self._qrTimer) { clearInterval(self._qrTimer); self._qrTimer = null; }
|
||
}, 30000);
|
||
},
|
||
beforeDestroy() {
|
||
if (this._refreshInterval) {
|
||
clearInterval(this._refreshInterval);
|
||
this._refreshInterval = null;
|
||
}
|
||
if (this._unsubFns) {
|
||
this._unsubFns.forEach(function (fn) { if (typeof fn === 'function') fn(); });
|
||
this._unsubFns = [];
|
||
}
|
||
if (this._qrTimer) { clearInterval(this._qrTimer); this._qrTimer = null; }
|
||
}
|
||
});
|
||
})();
|
||
|
||
// ─── 状态栏原生 JS 渲染 ───
|
||
(function renderStatusBar() {
|
||
var statusEl = document.querySelector('.statusbar');
|
||
if (!statusEl) return;
|
||
|
||
function updateSB() {
|
||
var mainApp = document.getElementById('app').__vue__;
|
||
if (!mainApp) return;
|
||
|
||
var ws = mainApp.wsStatus;
|
||
var items = statusEl.querySelectorAll('.status-item');
|
||
|
||
if (items[0]) {
|
||
var c = ws[mainApp.wsClientStatus] || ws.connected;
|
||
items[0].innerHTML = '<span class="status-dot" style="background:' + c.color + '"></span>' +
|
||
'<span>本地服务:<strong style="color:' + c.color + '">' + c.title + '</strong></span>';
|
||
}
|
||
if (items[1]) {
|
||
var s = ws[mainApp.wsServerStatus] || ws.disconnected;
|
||
items[1].innerHTML = '<span class="status-dot" style="background:' + s.color + '"></span>' +
|
||
'<span>服务器连接:<strong style="color:' + s.color + '">' + s.title + '</strong></span>';
|
||
}
|
||
if (items[2]) {
|
||
var bindC = mainApp.isBind ? '#107c10' : '#c42b1c';
|
||
var bindT = mainApp.isBind ? '已绑定' : '未绑定';
|
||
var clickable = mainApp.isBind ? 'cursor:pointer;text-decoration:underline' : '';
|
||
items[2].innerHTML = '<span class="status-dot" style="background:' + bindC + '"></span>' +
|
||
'<span>打印服务绑定:<strong style="color:' + bindC + ';' + clickable + '" id="bindStatusText">' + bindT + '</strong></span>';
|
||
}
|
||
|
||
var btns = statusEl.querySelectorAll('button');
|
||
if (btns[0]) btns[0].textContent = mainApp.updateChecking ? '检查中...' : '检查更新';
|
||
if (btns[1]) { btns[1].textContent = mainApp.restartingBackend ? '重启中...' : '重启服务'; btns[1].disabled = mainApp.restartingBackend; }
|
||
}
|
||
|
||
// 绑定按钮事件
|
||
var btnCheck = document.getElementById('btnCheckUpdate');
|
||
if (btnCheck) {
|
||
btnCheck.addEventListener('click', function () {
|
||
var mainApp = document.getElementById('app').__vue__;
|
||
if (mainApp && mainApp.checkUpdate) mainApp.checkUpdate(true);
|
||
});
|
||
}
|
||
var btnRestart = document.getElementById('btnRestartBackend');
|
||
if (btnRestart) {
|
||
btnRestart.addEventListener('click', function () {
|
||
var mainApp = document.getElementById('app').__vue__;
|
||
if (mainApp && mainApp.confirmRestartBackend) mainApp.confirmRestartBackend();
|
||
});
|
||
}
|
||
|
||
updateSB();
|
||
setInterval(updateSB, 500);
|
||
|
||
// 点击"打印服务绑定"解绑
|
||
statusEl.addEventListener('click', function (e) {
|
||
if (e.target && e.target.id === 'bindStatusText') {
|
||
var mainApp = document.getElementById('app').__vue__;
|
||
if (mainApp && mainApp.isBind && mainApp.unbind) {
|
||
if (window.confirm('确认解除绑定吗?解除后需要重新扫码绑定。')) {
|
||
mainApp.unbind();
|
||
}
|
||
}
|
||
}
|
||
});
|
||
})();
|
||
</script>
|
||
</body>
|
||
|
||
</html> |