printServer/views/index.html
2026-03-29 13:26:23 +08:00

539 lines
20 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;
line-height: 1.6;
margin: 0;
padding: 0;
background-color: #f4f4f9;
color: #333;
overflow: hidden;
height: 100vh;
}
#app {
display: flex;
flex-direction: column;
max-width: 800px;
margin: 0px auto;
padding: 20px;
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
height: 100%;
box-sizing: border-box;
}
h3,
h2 {
color: #4CAF50;
font-weight: bold;
}
h3 {
font-size: 20px;
}
h2 {
border-bottom: 2px solid #4CAF50;
padding-bottom: 8px;
margin-bottom: 16px;
font-size: 18px;
margin: 0;
}
#qrcode {
width: 256px;
height: 256px;
margin: 0 auto;
flex: 1;
height: 100px;
text-align: center;
}
table {
width: 100%;
margin-top: 16px;
border-collapse: collapse;
text-align: center;
background-color: #f9f9f9;
}
th,
td {
padding: 12px;
border: 1px solid #ddd;
}
th {
background-color: #4CAF50;
color: white;
}
button {
padding: 10px 20px;
margin: 10px 0;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
.error {
color: red;
font-weight: bold;
}
.qrcode-box {
margin-top: 12px;
text-align: center;
display: flex;
flex-direction: column;
height: 100px;
flex: 1;
}
.qrcode-box .error {
margin-top: 10px;
}
img {
width: auto;
height: 100%;
display: inline-block !important;
}
.printers {
display: flex;
gap: 16px;
justify-content: space-between;
}
.header {
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
gap: 20px;
height: 300px;
}
.left,
.right {
width: 50%;
height: 300px;
border: 1px solid;
padding: 20px;
box-sizing: border-box;
display: flex;
flex-direction: column;
}
.printers {
height: 100px;
flex: 1;
}
.list-box {
width: 50%;
border: 1px solid;
padding: 20px;
box-sizing: border-box;
display: flex;
flex-direction: column;
}
.title {
font-size: 16px;
font-weight: bold;
height: 40px;
line-height: 40px;
}
.list {
flex: 1;
margin-top: 12px;
padding: 12px;
background-color: #f9f9f9;
border-radius: 5px;
overflow-y: auto;
}
</style>
</head>
<body>
<div id="app">
<h1 class="title" style="text-align: center;font-size: 30px;font-weight: bold;">天豹云仓打印服务台<template>({{ name
}})</template></h1>
<div class="header">
<div class="left">
<h3>本地服务状态:<span :style="{color: wsStatus[wsClientStatus].color}">{{ wsStatus[wsClientStatus].title
}}</span></h3>
<h3>
服务注册状态:
<span :style="{color: isServices ? 'green' : 'red'}" @click="isServices ? unregister() : register()"
style="cursor: pointer;">
{{ isServices ? '已注册' : '未注册' }}
</span>
</h3>
<h3>服务器连接状态:<span :style="{color: wsStatus[wsServerStatus].color}">{{ wsStatus[wsServerStatus].title
}}</span></h3>
<h3>打印服务绑定状态:<span>
<span :style="{color: isBind ? 'green' : 'red'}">{{ isBind ? '已绑定' : '未绑定' }}</span>
</span></h3>
</div>
<div class="right" v-if="!isBind">
<h2>绑定二维码,微信扫描绑定工作台</h2>
<div class="qrcode-box">
<div id="qrcode" ref="qrcode" style="cursor:pointer;" @click="jumpToAuth"></div>
<div class="error">未绑定打印台,请扫描二维码绑定!</div>
</div>
</div>
</div>
<h2 style="margin-bottom: 20px;margin-top: 10px;">打印组件及打印机</h2>
<div class="printers">
<div class="list-box">
<div class="title">打印机名称
<button @click="loadPrinters"
style="margin-top: 16px; padding: 8px 16px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; cursor: pointer;">刷新打印机状态</button>
</div>
<div class="list">
<div class="item" v-if="printers.length === 0">无打印机</div>
<div class="item" v-for="p in printers" :key="p.name">{{ p.name }}</div>
</div>
</div>
<div class="list-box">
<div class="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;
}
let ws;
let wsUrl = 'ws://127.0.0.1:3001';
let reconnectTimer = null;
// 全局事件监听器注册表
let wsMethodListeners = {};
// 只定义一次 wsOn
function wsOn(method, callback) {
if (!wsMethodListeners[method]) wsMethodListeners[method] = [];
wsMethodListeners[method].push(callback);
}
function connectWS() {
ws = new WebSocket(wsUrl);
ws.onopen = function () {
console.log('WebSocket 已连接');
if (reconnectTimer) {
clearTimeout(reconnectTimer);
reconnectTimer = null;
}
// Update Vue data for WebSocket status
const app = document.getElementById('app').__vue__;
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 () {
console.log('WebSocket 断开,3秒后重连');
reconnectTimer = setTimeout(connectWS, 3000);
// Update Vue data for WebSocket status
const app = document.getElementById('app').__vue__;
if (app) app.wsClientStatus = 'disconnected';
};
ws.onerror = function (err) {
console.log('WebSocket 错误:', err);
ws.close();
// Update Vue data for WebSocket status
const app = document.getElementById('app').__vue__;
if (app) app.wsClientStatus = 'error';
};
}
connectWS();
// 封装 ws 发送并监听回调
function wsSendWithCallback(method, data, callback) {
if (!ws || ws.readyState !== WebSocket.OPEN) {
// Directly update the status instead of showing an alert
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 }));
}
// 按钮事件绑定需在 DOM 渲染后执行
window.onload = function () {
var refreshBtn = document.getElementById('refreshPrinters');
if (refreshBtn) {
}
};
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' },
},
isServices: false
},
methods: {
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 : '未知';
},
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) {
this.isServices = !!(data.result && data.result.isServices);
} else {
alert(data.message || '获取绑定状态失败!');
}
});
},
unregister() {
wsSendWithCallback('setting.unregister', {}, (data) => {
if (data.success) {
this.isServices = !!(data.result && data.result.isServices);
} else {
alert(data.message || '获取绑定状态失败!');
}
});
},
refreshBindStatus() {
wsSendWithCallback('setting.info', {}, (data) => {
if (data.success) {
this.isBind = data.result.isBind || false;
this.isServices = data.result.isService || false;
} else {
alert(data.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(`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 || '';
this.isServices = data.result.isService || false;
}
});
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 || '获取绑定码失败!');
}
});
}
},
watch: {
code(val) {
if (val) {
console.log('新的绑定码:', val);
this.renderQrcode();
}
}
},
mounted() {
setInterval(() => {
this.refreshBindStatus();
}, 10000);
this.installWsListener();
this.refreshBindStatus();
}
});
})();
</script>
</body>
</html>