printServer/views/index_new.html

474 lines
16 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>Print Server</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>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
margin: 0;
padding: 0;
min-height: 100vh;
}
#app {
max-width: 1000px;
margin: 40px auto;
background: #fff;
border-radius: 12px;
box-shadow: 0 6px 24px rgba(0,0,0,0.08);
padding: 32px 40px 40px 40px;
display: flex;
flex-direction: column;
gap: 32px;
}
.main-title {
text-align: center;
font-size: 32px;
font-weight: bold;
color: #4CAF50;
margin-bottom: 0;
}
.status-section {
display: flex;
flex-wrap: wrap;
gap: 32px;
justify-content: space-between;
}
.status-box {
flex: 1 1 320px;
min-width: 320px;
background: #f9f9f9;
border-radius: 8px;
padding: 24px 20px 20px 20px;
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 18px;
}
.status-box h3 {
margin: 0 0 8px 0;
color: #4CAF50;
font-size: 20px;
font-weight: bold;
}
.status-item {
font-size: 16px;
display: flex;
align-items: center;
gap: 8px;
}
.status-label {
min-width: 120px;
color: #666;
}
.status-value {
font-weight: bold;
}
.qrcode-section {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
min-height: 220px;
}
#qrcode {
width: 180px;
height: 180px;
}
.qrcode-tip {
color: #e53935;
font-size: 15px;
font-weight: bold;
}
.printer-section {
display: flex;
gap: 32px;
flex-wrap: wrap;
}
.list-box {
flex: 1 1 340px;
min-width: 340px;
background: #f9f9f9;
border-radius: 8px;
padding: 20px 18px 18px 18px;
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 10px;
}
.list-title {
font-size: 18px;
font-weight: bold;
color: #4CAF50;
margin-bottom: 8px;
display: flex;
align-items: center;
justify-content: space-between;
}
.list {
background: #fff;
border-radius: 5px;
padding: 10px;
min-height: 60px;
max-height: 180px;
overflow-y: auto;
font-size: 15px;
}
.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;
color: #fff;
border: none;
border-radius: 4px;
font-size: 15px;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background: #388e3c;
}
@media (max-width: 900px) {
#app { padding: 16px; }
.status-section, .printer-section { flex-direction: column; gap: 18px; }
.list-box, .status-box { min-width: unset; }
}
</style>
</head>
<body>
<div id="app">
<div class="main-title">天豹云仓打印服务台<template>({{ name }})</template></div>
<div class="status-section">
<div class="status-box">
<h3>服务状态</h3>
<div class="status-item">
<span class="status-label">本地服务:</span>
<span class="status-value" :style="{color: wsStatus[wsClientStatus].color}">{{ wsStatus[wsClientStatus].title }}</span>
</div>
<div class="status-item">
<span class="status-label">服务器连接:</span>
<span class="status-value" :style="{color: wsStatus[wsServerStatus].color}">{{ wsStatus[wsServerStatus].title }}</span>
</div>
<div class="status-item">
<span class="status-label">打印服务绑定:</span>
<span class="status-value" :style="{color: isBind ? 'green' : 'red'}">{{ isBind ? '已绑定' : '未绑定' }}</span>
</div>
</div>
<div class="status-box qrcode-section" v-if="!isBind">
<h3>绑定二维码</h3>
<div id="qrcode" ref="qrcode" style="cursor:pointer;" @click="jumpToAuth"></div>
<div class="qrcode-tip">未绑定打印台,请扫描二维码绑定!</div>
</div>
</div>
<div class="printer-section">
<div class="list-box">
<div class="list-title">
打印机名称
<button @click="loadPrinters">刷新打印机状态</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="list-box">
<div class="list-title">打印组件状态</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>
<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;
}
// ...existing JS 逻辑,直接复制 index.html 的 <script> 内容 ...
let ws;
let wsUrl = 'ws://127.0.0.1:3001';
let reconnectTimer = null;
let wsMethodListeners = {};
function wsOn(method, callback) {
if (!wsMethodListeners[method]) wsMethodListeners[method] = [];
wsMethodListeners[method].push(callback);
}
function connectWS() {
ws = new WebSocket(wsUrl);
ws.onopen = function () {
const app = document.getElementById('app').__vue__;
if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; }
if (app) app.wsClientStatus = '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 () {
reconnectTimer = setTimeout(connectWS, 3000);
const app = document.getElementById('app').__vue__;
if (app) app.wsClientStatus = 'disconnected';
};
ws.onerror = function (err) {
ws.close();
const app = document.getElementById('app').__vue__;
if (app) app.wsClientStatus = 'error';
};
}
connectWS();
function wsSendWithCallback(method, data, callback) {
if (!ws || ws.readyState !== WebSocket.OPEN) {
const app = document.getElementById('app').__vue__;
if (app) app.wsClientStatus = 'disconnected';
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 }));
}
new Vue({
el: '#app',
data: {
printers: [],
components: [],
statusList: [],
isBind: false,
wsClientStatus: 'disconnected',
wsServerStatus: 'disconnected',
code:'',
qrcode:new QRCode("qrcode"),
wsStatus: {
'disconnected': { title: '未连接', color: 'red' },
'connected': { title: '已连接', color: 'green' },
'error': { title: '连接错误', color: 'red' },
}
},
methods: {
loadComponents() {
wsSendWithCallback('printerComponent.getAll', {}, (data) => {
if (data.success) {
this.components = data.result.components || [];
this.statusList = data.result.status || [];
}
});
},
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;
}
}
});
},
isBind() {
wsSendWithCallback('setting.info', {}, (data) => {
if (data.success) {
this.isBind = data.result.isBind || false;
}
});
},
unbind() {
wsSendWithCallback('setting.unbind', {}, (data) => {
if (data.success) {
this.isBind = false;
}
});
},
renderQrcode() {
this.$nextTick(() => {
const el = this.$refs.qrcode;
if (!el) return;
el.innerHTML = '';
this.qrcode = new QRCode(el, { width: 180, height: 180 });
if (this.code) {
this.qrcode.makeCode(`http://127.0.0.1:6060/auth/printSetting/${this.code}`);
}
});
},
jumpToAuth() {
if (this.code) {
window.open(`http://127.0.0.1:6060/auth/printSetting/${this.code}`, '_blank');
}
},
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 || '';
}
});
wsOn('printerComponent.getAll', (data) => {
if (data.success) {
this.components = data.result.components || [];
this.statusList = data.result.status || [];
}
});
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 }];
}
});
wsOn('wsClient.statusChange', (data) => {
if (data.success) {
this.wsServerStatus = data.result.status;
}
});
wsOn('bind.generateCode', (data) => {
if (data.success) {
this.code = data.result || '';
}
});
}
},
watch: {
code(val) {
if (val) {
this.renderQrcode();
}
}
},
mounted() {
setInterval(() => {
this.isBind();
}, 10000);
this.installWsListener();
}
});
}
initApp();
</script>
</body>
</html>