label-editor.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>TSC 标签可视化编辑器</title>
  7. <!-- QR Code 生成库 -->
  8. <script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. body {
  16. font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  17. background: #f5f5f5;
  18. padding: 20px;
  19. }
  20. .container {
  21. max-width: 1400px;
  22. margin: 0 auto;
  23. background: white;
  24. border-radius: 8px;
  25. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  26. overflow: hidden;
  27. }
  28. .header {
  29. background: #2c3e50;
  30. color: white;
  31. padding: 20px 30px;
  32. display: flex;
  33. justify-content: space-between;
  34. align-items: center;
  35. }
  36. .header h1 {
  37. font-size: 24px;
  38. font-weight: 500;
  39. }
  40. .header-actions {
  41. display: flex;
  42. gap: 10px;
  43. }
  44. .btn {
  45. padding: 10px 20px;
  46. border: none;
  47. border-radius: 4px;
  48. cursor: pointer;
  49. font-size: 14px;
  50. transition: all 0.3s;
  51. }
  52. .btn-primary {
  53. background: #3498db;
  54. color: white;
  55. }
  56. .btn-primary:hover {
  57. background: #2980b9;
  58. }
  59. .btn-success {
  60. background: #27ae60;
  61. color: white;
  62. }
  63. .btn-success:hover {
  64. background: #229954;
  65. }
  66. .main-content {
  67. display: flex;
  68. height: calc(100vh - 140px);
  69. }
  70. .sidebar {
  71. width: 300px;
  72. border-right: 1px solid #ddd;
  73. overflow-y: auto;
  74. padding: 20px;
  75. }
  76. .sidebar h3 {
  77. margin-bottom: 15px;
  78. color: #2c3e50;
  79. font-size: 16px;
  80. }
  81. .element-list {
  82. list-style: none;
  83. }
  84. .element-item {
  85. padding: 12px;
  86. margin-bottom: 8px;
  87. background: #f8f9fa;
  88. border-radius: 4px;
  89. cursor: pointer;
  90. transition: all 0.2s;
  91. border-left: 3px solid #3498db;
  92. }
  93. .element-item:hover {
  94. background: #e9ecef;
  95. }
  96. .element-item.selected {
  97. background: #d4e6f1;
  98. border-left-color: #2980b9;
  99. }
  100. .element-item .label {
  101. font-weight: 500;
  102. color: #2c3e50;
  103. display: block;
  104. margin-bottom: 4px;
  105. }
  106. .element-item .type {
  107. font-size: 12px;
  108. color: #7f8c8d;
  109. }
  110. .canvas-area {
  111. flex: 1;
  112. padding: 30px;
  113. display: flex;
  114. flex-direction: column;
  115. align-items: center;
  116. background: #ecf0f1;
  117. }
  118. .canvas-wrapper {
  119. background: white;
  120. box-shadow: 0 4px 15px rgba(0,0,0,0.15);
  121. border-radius: 4px;
  122. padding: 20px;
  123. position: relative;
  124. }
  125. .canvas-container {
  126. position: relative;
  127. border: 2px solid #bdc3c7;
  128. background: white;
  129. cursor: crosshair;
  130. }
  131. .canvas-element {
  132. position: absolute;
  133. cursor: move;
  134. border: 2px solid transparent;
  135. transition: border-color 0.2s;
  136. }
  137. .canvas-element:hover {
  138. border-color: #3498db;
  139. }
  140. .canvas-element.selected {
  141. border-color: #e74c3c;
  142. box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.3);
  143. }
  144. .canvas-element.text-element {
  145. white-space: nowrap;
  146. font-family: Arial, sans-serif;
  147. padding: 2px 4px;
  148. }
  149. .canvas-element.qrcode-element {
  150. background: #ecf0f1;
  151. display: flex;
  152. align-items: center;
  153. justify-content: center;
  154. font-size: 10px;
  155. color: #7f8c8d;
  156. }
  157. .properties-panel {
  158. width: 320px;
  159. border-left: 1px solid #ddd;
  160. overflow-y: auto;
  161. padding: 20px;
  162. }
  163. .properties-panel h3 {
  164. margin-bottom: 15px;
  165. color: #2c3e50;
  166. font-size: 16px;
  167. }
  168. .property-group {
  169. margin-bottom: 20px;
  170. }
  171. .property-group label {
  172. display: block;
  173. margin-bottom: 5px;
  174. color: #34495e;
  175. font-size: 13px;
  176. font-weight: 500;
  177. }
  178. .property-group input,
  179. .property-group select {
  180. width: 100%;
  181. padding: 8px;
  182. border: 1px solid #ddd;
  183. border-radius: 4px;
  184. font-size: 13px;
  185. }
  186. .property-group input:focus,
  187. .property-group select:focus {
  188. outline: none;
  189. border-color: #3498db;
  190. }
  191. .info-bar {
  192. padding: 10px 20px;
  193. background: #34495e;
  194. color: white;
  195. font-size: 13px;
  196. display: flex;
  197. justify-content: space-between;
  198. }
  199. .zoom-controls {
  200. display: flex;
  201. align-items: center;
  202. gap: 10px;
  203. margin-bottom: 15px;
  204. }
  205. .zoom-btn {
  206. padding: 5px 12px;
  207. background: #7f8c8d;
  208. color: white;
  209. border: none;
  210. border-radius: 3px;
  211. cursor: pointer;
  212. font-size: 12px;
  213. }
  214. .zoom-btn:hover {
  215. background: #6c7a7d;
  216. }
  217. .zoom-level {
  218. font-size: 13px;
  219. color: #2c3e50;
  220. min-width: 60px;
  221. text-align: center;
  222. }
  223. .property-row {
  224. display: grid;
  225. grid-template-columns: 1fr 1fr;
  226. gap: 10px;
  227. }
  228. </style>
  229. </head>
  230. <body>
  231. <div class="container">
  232. <div class="header">
  233. <h1>TSC 标签可视化编辑器</h1>
  234. <div class="header-actions">
  235. <button class="btn btn-primary" onclick="loadConfig()">加载配置</button>
  236. <button class="btn btn-success" onclick="saveConfig()">保存配置</button>
  237. </div>
  238. </div>
  239. <div class="main-content">
  240. <!-- 左侧元素列表 -->
  241. <div class="sidebar">
  242. <h3>📋 标签元素</h3>
  243. <ul class="element-list" id="elementList"></ul>
  244. </div>
  245. <!-- 中间画布区域 -->
  246. <div class="canvas-area">
  247. <div class="zoom-controls">
  248. <button class="zoom-btn" onclick="zoomOut()">缩小 -</button>
  249. <span class="zoom-level" id="zoomLevel">100%</span>
  250. <button class="zoom-btn" onclick="zoomIn()">放大 +</button>
  251. <button class="zoom-btn" onclick="resetZoom()">重置</button>
  252. </div>
  253. <div class="canvas-wrapper">
  254. <div class="canvas-container" id="canvas"></div>
  255. </div>
  256. </div>
  257. <!-- 右侧属性面板 -->
  258. <div class="properties-panel">
  259. <h3>⚙️ 属性设置</h3>
  260. <div id="propertiesContent">
  261. <p style="color: #7f8c8d; font-size: 13px;">请选择一个元素</p>
  262. </div>
  263. </div>
  264. </div>
  265. <div class="info-bar">
  266. <span>标签尺寸: <span id="labelSize">76.2mm × 38.1mm</span></span>
  267. <span>选中元素: <span id="selectedElement">无</span></span>
  268. </div>
  269. </div>
  270. <script>
  271. // 全局变量
  272. let config = null;
  273. let selectedElementId = null;
  274. let zoom = 1.0;
  275. const DPI = 203 / 25.4; // 203 DPI,1mm = 8 dots
  276. let isDragging = false;
  277. let dragStartX = 0;
  278. let dragStartY = 0;
  279. let dragElement = null;
  280. // 加载配置
  281. async function loadConfig() {
  282. try {
  283. const response = await fetch('../config/label-config.json');
  284. config = await response.json();
  285. renderCanvas();
  286. renderElementList();
  287. showMessage('配置加载成功', 'success');
  288. } catch (error) {
  289. showMessage('配置加载失败: ' + error.message, 'error');
  290. }
  291. }
  292. // 保存配置
  293. function saveConfig() {
  294. if (!config) {
  295. showMessage('没有配置可保存', 'error');
  296. return;
  297. }
  298. const json = JSON.stringify(config, null, 2);
  299. const blob = new Blob([json], { type: 'application/json' });
  300. const url = URL.createObjectURL(blob);
  301. const a = document.createElement('a');
  302. a.href = url;
  303. a.download = 'label-config.json';
  304. a.click();
  305. URL.revokeObjectURL(url);
  306. showMessage('配置已下载', 'success');
  307. }
  308. // 渲染画布
  309. function renderCanvas() {
  310. if (!config) return;
  311. const canvas = document.getElementById('canvas');
  312. const widthPx = config.labelSize.width * DPI * zoom;
  313. const heightPx = config.labelSize.height * DPI * zoom;
  314. canvas.style.width = widthPx + 'px';
  315. canvas.style.height = heightPx + 'px';
  316. canvas.innerHTML = '';
  317. config.elements.forEach(element => {
  318. const div = document.createElement('div');
  319. div.className = 'canvas-element';
  320. div.id = 'canvas-' + element.id;
  321. div.dataset.id = element.id;
  322. const x = element.x * zoom;
  323. const y = element.y * zoom;
  324. div.style.left = x + 'px';
  325. div.style.top = y + 'px';
  326. if (element.type === 'text') {
  327. div.className += ' text-element';
  328. div.textContent = element.text;
  329. div.style.fontSize = (element.fontSize * zoom) + 'px';
  330. div.style.fontFamily = element.fontFamily;
  331. div.style.fontWeight = element.fontStyle === 1 ? 'bold' : 'normal';
  332. } else if (element.type === 'qrcode') {
  333. div.className += ' qrcode-element';
  334. div.style.width = (element.width * zoom) + 'px';
  335. div.style.height = (element.height * zoom) + 'px';
  336. div.style.background = 'white';
  337. div.style.padding = '0';
  338. // 生成真实二维码
  339. const qrData = element.text || '+KB60IS3031T811001201'; // 默认示例数据
  340. try {
  341. new QRCode(div, {
  342. text: qrData,
  343. width: element.width * zoom,
  344. height: element.height * zoom,
  345. colorDark: '#000000',
  346. colorLight: '#ffffff',
  347. correctLevel: QRCode.CorrectLevel.L
  348. });
  349. } catch (e) {
  350. div.textContent = 'QR';
  351. div.style.background = '#ecf0f1';
  352. }
  353. }
  354. if (element.id === selectedElementId) {
  355. div.className += ' selected';
  356. }
  357. div.addEventListener('click', (e) => {
  358. e.stopPropagation();
  359. selectElement(element.id);
  360. });
  361. div.addEventListener('mousedown', startDrag);
  362. canvas.appendChild(div);
  363. });
  364. document.getElementById('labelSize').textContent =
  365. `${config.labelSize.width}mm × ${config.labelSize.height}mm`;
  366. }
  367. // 拖拽开始
  368. function startDrag(e) {
  369. isDragging = true;
  370. dragElement = e.target;
  371. dragStartX = e.clientX;
  372. dragStartY = e.clientY;
  373. const elementId = dragElement.dataset.id;
  374. selectElement(elementId);
  375. document.addEventListener('mousemove', onDrag);
  376. document.addEventListener('mouseup', stopDrag);
  377. e.preventDefault();
  378. }
  379. // 拖拽中
  380. function onDrag(e) {
  381. if (!isDragging || !dragElement) return;
  382. const dx = (e.clientX - dragStartX) / zoom;
  383. const dy = (e.clientY - dragStartY) / zoom;
  384. const elementId = dragElement.dataset.id;
  385. const element = config.elements.find(el => el.id === elementId);
  386. if (element) {
  387. element.x = Math.max(0, element.x + dx);
  388. element.y = Math.max(0, element.y + dy);
  389. renderCanvas();
  390. updateProperties();
  391. }
  392. dragStartX = e.clientX;
  393. dragStartY = e.clientY;
  394. }
  395. // 拖拽结束
  396. function stopDrag() {
  397. isDragging = false;
  398. dragElement = null;
  399. document.removeEventListener('mousemove', onDrag);
  400. document.removeEventListener('mouseup', stopDrag);
  401. }
  402. // 渲染元素列表
  403. function renderElementList() {
  404. const list = document.getElementById('elementList');
  405. list.innerHTML = '';
  406. config.elements.forEach(element => {
  407. const li = document.createElement('li');
  408. li.className = 'element-item';
  409. li.dataset.id = element.id;
  410. li.innerHTML = `
  411. <span class="label">${element.label}</span>
  412. <span class="type">${element.type === 'text' ? '文本' : '二维码'} - (${Math.round(element.x)}, ${Math.round(element.y)})</span>
  413. `;
  414. li.addEventListener('click', () => selectElement(element.id));
  415. list.appendChild(li);
  416. });
  417. }
  418. // 选择元素
  419. function selectElement(elementId) {
  420. selectedElementId = elementId;
  421. // 更新列表选中状态
  422. document.querySelectorAll('.element-item').forEach(item => {
  423. item.classList.toggle('selected', item.dataset.id === elementId);
  424. });
  425. // 更新画布选中状态
  426. document.querySelectorAll('.canvas-element').forEach(el => {
  427. el.classList.toggle('selected', el.dataset.id === elementId);
  428. });
  429. const element = config.elements.find(el => el.id === elementId);
  430. document.getElementById('selectedElement').textContent = element ? element.label : '无';
  431. updateProperties();
  432. }
  433. // 更新属性面板
  434. function updateProperties() {
  435. if (!selectedElementId) {
  436. document.getElementById('propertiesContent').innerHTML =
  437. '<p style="color: #7f8c8d; font-size: 13px;">请选择一个元素</p>';
  438. return;
  439. }
  440. const element = config.elements.find(el => el.id === selectedElementId);
  441. if (!element) return;
  442. let html = '';
  443. // 位置属性
  444. html += `
  445. <div class="property-group">
  446. <label>ID</label>
  447. <input type="text" value="${element.id}" disabled>
  448. </div>
  449. <div class="property-group">
  450. <div class="property-row">
  451. <div>
  452. <label>X 坐标 (dots)</label>
  453. <input type="number" value="${element.x}" onchange="updateElementProperty('x', parseFloat(this.value))">
  454. </div>
  455. <div>
  456. <label>Y 坐标 (dots)</label>
  457. <input type="number" value="${element.y}" onchange="updateElementProperty('y', parseFloat(this.value))">
  458. </div>
  459. </div>
  460. </div>
  461. `;
  462. if (element.type === 'text') {
  463. html += `
  464. <div class="property-group">
  465. <label>文本内容</label>
  466. <input type="text" value="${element.text}" onchange="updateElementProperty('text', this.value)">
  467. </div>
  468. <div class="property-group">
  469. <label>字体大小 (pt)</label>
  470. <input type="number" value="${element.fontSize}" onchange="updateElementProperty('fontSize', parseInt(this.value))">
  471. </div>
  472. <div class="property-group">
  473. <label>字体</label>
  474. <select value="${element.fontFamily}" onchange="updateElementProperty('fontFamily', this.value)">
  475. <option value="Arial" ${element.fontFamily === 'Arial' ? 'selected' : ''}>Arial</option>
  476. <option value="SimSun" ${element.fontFamily === 'SimSun' ? 'selected' : ''}>SimSun (宋体)</option>
  477. <option value="SimHei" ${element.fontFamily === 'SimHei' ? 'selected' : ''}>SimHei (黑体)</option>
  478. </select>
  479. </div>
  480. <div class="property-group">
  481. <label>字体样式</label>
  482. <select value="${element.fontStyle}" onchange="updateElementProperty('fontStyle', parseInt(this.value))">
  483. <option value="0" ${element.fontStyle === 0 ? 'selected' : ''}>正常</option>
  484. <option value="1" ${element.fontStyle === 1 ? 'selected' : ''}>加粗</option>
  485. </select>
  486. </div>
  487. `;
  488. } else if (element.type === 'qrcode') {
  489. html += `
  490. <div class="property-group">
  491. <div class="property-row">
  492. <div>
  493. <label>宽度 (dots)</label>
  494. <input type="number" value="${element.width}" onchange="updateElementProperty('width', parseInt(this.value))">
  495. </div>
  496. <div>
  497. <label>高度 (dots)</label>
  498. <input type="number" value="${element.height}" onchange="updateElementProperty('height', parseInt(this.value))">
  499. </div>
  500. </div>
  501. </div>
  502. <div class="property-group">
  503. <label>纠错等级</label>
  504. <select value="${element.eccLevel}" onchange="updateElementProperty('eccLevel', this.value)">
  505. <option value="L" ${element.eccLevel === 'L' ? 'selected' : ''}>L (7%)</option>
  506. <option value="M" ${element.eccLevel === 'M' ? 'selected' : ''}>M (15%)</option>
  507. <option value="Q" ${element.eccLevel === 'Q' ? 'selected' : ''}>Q (25%)</option>
  508. <option value="H" ${element.eccLevel === 'H' ? 'selected' : ''}>H (30%)</option>
  509. </select>
  510. </div>
  511. <div class="property-group">
  512. <label>单元宽度</label>
  513. <input type="number" value="${element.cellWidth}" onchange="updateElementProperty('cellWidth', this.value)">
  514. </div>
  515. `;
  516. }
  517. document.getElementById('propertiesContent').innerHTML = html;
  518. }
  519. // 更新元素属性
  520. function updateElementProperty(property, value) {
  521. if (!selectedElementId) return;
  522. const element = config.elements.find(el => el.id === selectedElementId);
  523. if (element) {
  524. element[property] = value;
  525. renderCanvas();
  526. renderElementList();
  527. }
  528. }
  529. // 缩放控制
  530. function zoomIn() {
  531. zoom = Math.min(zoom + 0.1, 2.0);
  532. document.getElementById('zoomLevel').textContent = Math.round(zoom * 100) + '%';
  533. renderCanvas();
  534. }
  535. function zoomOut() {
  536. zoom = Math.max(zoom - 0.1, 0.5);
  537. document.getElementById('zoomLevel').textContent = Math.round(zoom * 100) + '%';
  538. renderCanvas();
  539. }
  540. function resetZoom() {
  541. zoom = 1.0;
  542. document.getElementById('zoomLevel').textContent = '100%';
  543. renderCanvas();
  544. }
  545. // 显示消息
  546. function showMessage(message, type) {
  547. const color = type === 'success' ? '#27ae60' : '#e74c3c';
  548. const div = document.createElement('div');
  549. div.style.cssText = `
  550. position: fixed;
  551. top: 20px;
  552. right: 20px;
  553. background: ${color};
  554. color: white;
  555. padding: 15px 25px;
  556. border-radius: 4px;
  557. box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  558. z-index: 1000;
  559. animation: slideIn 0.3s ease-out;
  560. `;
  561. div.textContent = message;
  562. document.body.appendChild(div);
  563. setTimeout(() => {
  564. div.style.animation = 'slideOut 0.3s ease-out';
  565. setTimeout(() => div.remove(), 300);
  566. }, 2000);
  567. }
  568. // 初始化
  569. window.addEventListener('load', loadConfig);
  570. </script>
  571. <style>
  572. @keyframes slideIn {
  573. from {
  574. transform: translateX(400px);
  575. opacity: 0;
  576. }
  577. to {
  578. transform: translateX(0);
  579. opacity: 1;
  580. }
  581. }
  582. @keyframes slideOut {
  583. from {
  584. transform: translateX(0);
  585. opacity: 1;
  586. }
  587. to {
  588. transform: translateX(400px);
  589. opacity: 0;
  590. }
  591. }
  592. </style>
  593. </body>
  594. </html>