| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
- <title>送货单</title>
- <style>
- * { box-sizing: border-box; }
- html, body {
- margin: 0; min-height: 100%;
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
- background: #eef3f7; color: #222; font-size: 14px;
- }
- .bar {
- position: sticky; top: 0; z-index: 20;
- height: 48px; padding: 0 12px;
- background: #3c8dbc; color: #fff;
- display: flex; align-items: center; justify-content: space-between;
- }
- .bar a, .bar button {
- color: #fff; text-decoration: none; font-size: 14px; padding: 6px 4px;
- background: none; border: 0; font-family: inherit; cursor: pointer;
- }
- .bar h1 { margin: 0; font-size: 16px; font-weight: 600; }
- .bar button:disabled { opacity: .7; }
- #note-toast {
- position: fixed; left: 50%; bottom: 72px; z-index: 40;
- transform: translateX(-50%);
- padding: 8px 16px; border-radius: 6px;
- background: rgba(0,0,0,.78); color: #fff; font-size: 13px;
- pointer-events: none;
- }
- #save-mask {
- display: none; position: fixed; inset: 0; z-index: 30;
- background: rgba(0,0,0,.78); flex-direction: column;
- align-items: center; justify-content: center; padding: 16px 12px 24px;
- }
- #save-mask.show { display: flex; }
- #save-mask .save-tip { color: #fff; font-size: 14px; margin-bottom: 12px; }
- #save-mask img {
- max-width: 88%; max-height: 68vh; background: #fff;
- -webkit-touch-callout: default;
- }
- #save-mask .save-actions { margin-top: 14px; }
- #save-mask .save-actions button {
- min-width: 108px; height: 36px; border: 0; border-radius: 6px;
- font-size: 14px; color: #fff; background: #666;
- -webkit-tap-highlight-color: transparent;
- }
- .sheet {
- position: relative;
- max-width: 420px; margin: 12px auto 24px;
- background: #fff; padding: 16px 16px 22px;
- box-shadow: 0 1px 6px rgba(0,0,0,.06);
- }
- .meta-top { font-size: 12px; color: #666; }
- .brand { text-align: center; margin-top: 6px; }
- .brand .co { font-size: 17px; font-weight: 700; color: #111; line-height: 1.35; }
- .brand .doc { margin-top: 4px; font-size: 20px; font-weight: 700; letter-spacing: .3em; color: #222; }
- .kv { margin-top: 12px; font-size: 13px; line-height: 1.7; color: #333; }
- .kv b { font-weight: 600; color: #555; }
- .qr-wrap { text-align: center; margin: 14px 0 8px; }
- #qrcode {
- display: inline-block; width: 132px; height: 132px;
- padding: 6px; border: 1px solid #ddd; background: #fff;
- }
- #qrcode img, #qrcode canvas { display: block; margin: 0 auto; }
- #qrcode table {
- width: 120px !important; height: 120px !important;
- border-collapse: collapse; table-layout: fixed; margin: 0 auto;
- }
- #qrcode table td { padding: 0 !important; border: 0 !important; }
- .ord-no { text-align: center; font-size: 13px; font-weight: 600; margin: 6px 0 0; letter-spacing: .04em; }
- .block { margin-top: 16px; padding-top: 12px; border-top: 1px dashed #ddd; }
- .block h2 { margin: 0 0 8px; font-size: 13px; color: #888; font-weight: 600; }
- .line { font-size: 13px; line-height: 1.75; color: #333; }
- .line span { color: #888; }
- @media print {
- body { background: #fff; }
- .bar, #save-mask, #note-toast { display: none !important; }
- .sheet { margin: 0; box-shadow: none; max-width: none; }
- }
- </style>
- </head>
- <body>
- <div class="bar">
- <a href="{:url('index/index/index')}?tab=done" id="btn-note-back">返回</a>
- <h1>送货码</h1>
- <button type="button" id="btn-note-save">保存</button>
- </div>
- <div id="save-mask" aria-hidden="true">
- <div class="save-tip">长按图片保存到相册或转发</div>
- <img id="save-preview" alt="送货码">
- <div class="save-actions">
- <button type="button" id="btn-save-close">关闭</button>
- </div>
- </div>
- <div id="note-toast" hidden></div>
- <div class="sheet">
- <div class="meta-top">生成时间:{$note.generated_at|default=''|htmlentities}</div>
- <div class="brand">
- <div class="co">{$note.supplier_name|default=''|htmlentities}</div>
- <div class="doc">送货码</div>
- </div>
- <div class="kv"><b>客户名称:</b>{notempty name="note.customer_name"}{$note.customer_name|htmlentities}{else /}—{/notempty}</div>
- <div class="kv"><b>送货地址:</b>{notempty name="note.address"}{$note.address|htmlentities}{else /}—{/notempty}</div>
- <div class="qr-wrap">
- <div id="qrcode">{notempty name="note.qr_data_uri"}<img src="{$note.qr_data_uri}" width="120" height="120" alt="送货码">{/notempty}</div>
- </div>
- <div class="block">
- <h2>订单信息</h2>
- <div class="line"><span>订单号:</span>{$note.ccydh|default=''|htmlentities}</div>
- <div class="line"><span>印件名称:</span>{$note.cyjmc|default=''|htmlentities}</div>
- <div class="line"><span>工序名称:</span>{$note.cgymc|default=''|htmlentities}</div>
- <div class="line"><span>单位:</span>{$note.cdw|default=''|htmlentities}</div>
- <div class="line"><span>订法:</span>{$note.cdf|default=''|htmlentities}</div>
- <div class="line"><span>备注:</span>{notempty name="note.remark"}{$note.remark|htmlentities}{else /}—{/notempty}</div>
- </div>
- <div class="block">
- <h2>供应商信息</h2>
- <div class="line"><span>供应商名称:</span>{$note.supplier_name|default=''|htmlentities}</div>
- <div class="line"><span>姓名:</span>{$note.contact_name|default=''|htmlentities}</div>
- <div class="line"><span>手机号:</span>{$note.mobile|default=''|htmlentities}</div>
- </div>
- </div>
- <script src="__CDN__/assets/js/jquery.js"></script>
- <script src="__CDN__/assets/js/jquery.qrcode.min.js"></script>
- <script>
- (function () {
- var ccydh = {:json_encode((string)($note['ccydh'] ?? ''), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)};
- var saveBlob = null;
- var saveUrl = '';
- var toastTimer = 0;
- var back = document.getElementById('btn-note-back');
- if (back) {
- back.addEventListener('click', function (e) {
- if (window.history.length > 1) {
- e.preventDefault();
- history.back();
- }
- });
- }
- function toast(msg) {
- var el = document.getElementById('note-toast');
- if (!el) {
- return;
- }
- el.textContent = msg;
- el.hidden = false;
- clearTimeout(toastTimer);
- toastTimer = setTimeout(function () {
- el.hidden = true;
- }, 1800);
- }
- function isMobile() {
- return /Android|iPhone|iPad|iPod|Mobile|MicroMessenger/i.test(navigator.userAgent || '');
- }
- function filename() {
- return '送货单' + (ccydh ? '-' + ccydh : '') + '.png';
- }
- function revokeSaveUrl() {
- if (saveUrl) {
- URL.revokeObjectURL(saveUrl);
- saveUrl = '';
- }
- }
- function closeMask() {
- var mask = document.getElementById('save-mask');
- if (mask) {
- mask.classList.remove('show');
- mask.setAttribute('aria-hidden', 'true');
- }
- document.body.style.overflow = '';
- }
- function showMask(blob) {
- var mask = document.getElementById('save-mask');
- var img = document.getElementById('save-preview');
- if (!mask || !img) {
- return;
- }
- revokeSaveUrl();
- saveBlob = blob;
- saveUrl = URL.createObjectURL(blob);
- img.src = saveUrl;
- mask.classList.add('show');
- mask.setAttribute('aria-hidden', 'false');
- document.body.style.overflow = 'hidden';
- }
- function tryShare(blob) {
- if (!blob || typeof navigator.share !== 'function' || typeof File === 'undefined') {
- return Promise.resolve(false);
- }
- var file = new File([blob], filename(), {type: 'image/png'});
- var payload = {files: [file], title: '送货单'};
- if (typeof navigator.canShare === 'function' && !navigator.canShare(payload)) {
- return Promise.resolve(false);
- }
- return navigator.share(payload).then(function () {
- return true;
- }).catch(function (err) {
- if (err && err.name === 'AbortError') {
- return true;
- }
- return false;
- });
- }
- function tryDownload(blob) {
- if (isMobile()) {
- return false;
- }
- var a = document.createElement('a');
- a.href = URL.createObjectURL(blob);
- a.download = filename();
- a.style.display = 'none';
- document.body.appendChild(a);
- a.click();
- setTimeout(function () {
- URL.revokeObjectURL(a.href);
- a.remove();
- }, 800);
- return true;
- }
- function isDarkColor(color) {
- var c = String(color || '').replace(/\s+/g, '').toLowerCase();
- if (!c || c === 'transparent' || c === 'rgba(0,0,0,0)') {
- return false;
- }
- if (c === '#000' || c === '#000000' || c === 'black' || c === 'rgb(0,0,0)') {
- return true;
- }
- var m = c.match(/^rgba?\((\d+),(\d+),(\d+)/);
- if (m) {
- return (parseInt(m[1], 10) + parseInt(m[2], 10) + parseInt(m[3], 10)) < 380;
- }
- return false;
- }
- function qrTableToDataUrl(table) {
- var n = table.rows.length;
- if (!n) {
- return '';
- }
- var c = document.createElement('canvas');
- c.width = n;
- c.height = n;
- var ctx = c.getContext('2d');
- if (!ctx) {
- return '';
- }
- ctx.fillStyle = '#ffffff';
- ctx.fillRect(0, 0, n, n);
- ctx.fillStyle = '#000000';
- for (var r = 0; r < n; r++) {
- var cells = table.rows[r].cells;
- for (var i = 0; i < cells.length; i++) {
- var bg = cells[i].style.backgroundColor || '';
- if (!bg && window.getComputedStyle) {
- bg = getComputedStyle(cells[i]).backgroundColor;
- }
- if (isDarkColor(bg)) {
- ctx.fillRect(i, r, 1, 1);
- }
- }
- }
- return c.toDataURL('image/png');
- }
- function getQrDataUrl() {
- var box = document.getElementById('qrcode');
- if (!box) {
- return '';
- }
- var img = box.querySelector('img');
- if (img && img.src) {
- return img.src;
- }
- var canvas = box.querySelector('canvas');
- if (canvas) {
- try {
- return canvas.toDataURL('image/png');
- } catch (e) {}
- }
- var table = box.querySelector('table');
- if (table) {
- return qrTableToDataUrl(table);
- }
- return '';
- }
- function loadQrImage(src) {
- return new Promise(function (resolve) {
- if (!src) {
- resolve(null);
- return;
- }
- var exist = document.querySelector('#qrcode img');
- if (exist && exist.src === src && exist.complete && exist.naturalWidth) {
- resolve(exist);
- return;
- }
- var im = new Image();
- im.onload = function () {
- resolve(im);
- };
- im.onerror = function () {
- resolve(null);
- };
- im.src = src;
- });
- }
- function wrapText(ctx, text, maxWidth) {
- var s = String(text || '');
- var lines = [];
- var line = '';
- for (var i = 0; i < s.length; i++) {
- var next = line + s.charAt(i);
- if (line && ctx.measureText(next).width > maxWidth) {
- lines.push(line);
- line = s.charAt(i);
- } else {
- line = next;
- }
- }
- if (line !== '') {
- lines.push(line);
- }
- return lines.length ? lines : [''];
- }
- function drawSpacedCenter(ctx, text, x, y, tracking) {
- var s = String(text || '');
- var extra = tracking * Math.max(0, s.length - 1);
- var w = ctx.measureText(s).width + extra;
- var left = x - w / 2;
- for (var i = 0; i < s.length; i++) {
- ctx.fillText(s.charAt(i), left, y);
- left += ctx.measureText(s.charAt(i)).width + tracking;
- }
- }
- function drawLabeledLine(ctx, text, x, y, maxWidth, fontFamily) {
- var sp = String(text || '').indexOf(':');
- var label = sp >= 0 ? text.slice(0, sp + 1) : '';
- var rest = sp >= 0 ? text.slice(sp + 1) : text;
- ctx.font = '13px ' + fontFamily;
- if (!label) {
- ctx.fillStyle = '#333333';
- wrapText(ctx, text, maxWidth).forEach(function (ln) {
- ctx.fillText(ln, x, y);
- y += 22;
- });
- return y;
- }
- ctx.fillStyle = '#888888';
- ctx.fillText(label, x, y);
- var lw = ctx.measureText(label).width;
- ctx.fillStyle = '#333333';
- var firstMax = Math.max(40, maxWidth - lw);
- var restLines = wrapText(ctx, rest, firstMax);
- if (restLines.length) {
- ctx.fillText(restLines[0], x + lw, y);
- y += 22;
- for (var i = 1; i < restLines.length; i++) {
- ctx.fillText(restLines[i], x, y);
- y += 22;
- }
- } else {
- y += 22;
- }
- return y;
- }
- function drawNoteCanvas(qrImage) {
- var sheet = document.querySelector('.sheet');
- var cssW = sheet && sheet.offsetWidth ? sheet.offsetWidth : 360;
- var pad = 16;
- var inner = cssW - pad * 2;
- var scale = 2;
- var fontFamily = '-apple-system,BlinkMacSystemFont,"PingFang SC","Hiragino Sans GB","Microsoft YaHei",sans-serif';
- var probe = document.createElement('canvas').getContext('2d');
- if (!probe) {
- throw new Error('无法生成图片');
- }
- var meta = ((document.querySelector('.meta-top') || {}).textContent || '').replace(/\s+/g, ' ').trim();
- var co = ((document.querySelector('.brand .co') || {}).textContent || '').replace(/\s+/g, ' ').trim();
- var docTitle = ((document.querySelector('.brand .doc') || {}).textContent || '送货单').replace(/\s+/g, ' ').trim();
- var kvs = [];
- document.querySelectorAll('.sheet > .kv').forEach(function (el) {
- kvs.push((el.textContent || '').replace(/\s+/g, ' ').trim());
- });
- var ordNo = ((document.querySelector('.ord-no') || {}).textContent || '').replace(/\s+/g, ' ').trim();
- var blocks = [];
- document.querySelectorAll('.sheet > .block').forEach(function (block) {
- blocks.push({
- title: ((block.querySelector('h2') || {}).textContent || '').replace(/\s+/g, ' ').trim(),
- lines: Array.prototype.map.call(block.querySelectorAll('.line'), function (line) {
- return (line.textContent || '').replace(/\s+/g, ' ').trim();
- })
- });
- });
- var canvas = document.createElement('canvas');
- canvas.width = Math.round(cssW * scale);
- canvas.height = Math.round(1200 * scale);
- var ctx = canvas.getContext('2d');
- ctx.scale(scale, scale);
- ctx.fillStyle = '#ffffff';
- ctx.fillRect(0, 0, cssW, 1200);
- ctx.textBaseline = 'top';
- var cy = pad;
- ctx.font = '12px ' + fontFamily;
- ctx.fillStyle = '#666666';
- ctx.fillText(meta, pad, cy);
- cy += 24;
- ctx.font = 'bold 17px ' + fontFamily;
- ctx.fillStyle = '#111111';
- wrapText(ctx, co, inner).forEach(function (ln) {
- var tw = ctx.measureText(ln).width;
- ctx.fillText(ln, (cssW - tw) / 2, cy);
- cy += 24;
- });
- cy += 4;
- ctx.font = 'bold 20px ' + fontFamily;
- ctx.fillStyle = '#222222';
- drawSpacedCenter(ctx, docTitle, cssW / 2, cy, 6);
- cy += 34;
- kvs.forEach(function (kv) {
- var sp = kv.indexOf(':');
- var label = sp >= 0 ? kv.slice(0, sp + 1) : '';
- var val = sp >= 0 ? kv.slice(sp + 1) : kv;
- ctx.font = '600 13px ' + fontFamily;
- ctx.fillStyle = '#555555';
- ctx.fillText(label, pad, cy);
- var lw = ctx.measureText(label).width;
- ctx.font = '13px ' + fontFamily;
- ctx.fillStyle = '#333333';
- var valLines = wrapText(ctx, val, Math.max(40, inner - lw));
- valLines.forEach(function (ln, i) {
- ctx.fillText(ln, pad + (i === 0 ? lw : 0), cy);
- cy += 22;
- });
- cy += 4;
- });
- cy += 8;
- var qrBox = 132;
- var qx = (cssW - qrBox) / 2;
- ctx.strokeStyle = '#dddddd';
- ctx.lineWidth = 1;
- ctx.fillStyle = '#ffffff';
- ctx.fillRect(qx, cy, qrBox, qrBox);
- ctx.strokeRect(qx + 0.5, cy + 0.5, qrBox - 1, qrBox - 1);
- if (qrImage) {
- ctx.imageSmoothingEnabled = false;
- ctx.drawImage(qrImage, qx + 6, cy + 6, 120, 120);
- }
- cy += qrBox + 8;
- ctx.font = 'bold 13px ' + fontFamily;
- ctx.fillStyle = '#222222';
- var onw = ctx.measureText(ordNo).width;
- ctx.fillText(ordNo, (cssW - onw) / 2, cy);
- cy += 20;
- blocks.forEach(function (block) {
- cy += 10;
- ctx.strokeStyle = '#dddddd';
- ctx.setLineDash([4, 3]);
- ctx.beginPath();
- ctx.moveTo(pad, cy);
- ctx.lineTo(cssW - pad, cy);
- ctx.stroke();
- ctx.setLineDash([]);
- cy += 12;
- ctx.font = '600 13px ' + fontFamily;
- ctx.fillStyle = '#888888';
- ctx.fillText(block.title, pad, cy);
- cy += 22;
- block.lines.forEach(function (line) {
- cy = drawLabeledLine(ctx, line, pad, cy, inner, fontFamily);
- });
- });
- cy += pad;
- var out = document.createElement('canvas');
- out.width = canvas.width;
- out.height = Math.max(1, Math.round(cy * scale));
- var octx = out.getContext('2d');
- octx.fillStyle = '#ffffff';
- octx.fillRect(0, 0, out.width, out.height);
- octx.drawImage(canvas, 0, 0);
- return out;
- }
- function canvasToBlob(canvas) {
- return new Promise(function (resolve, reject) {
- if (canvas.toBlob) {
- canvas.toBlob(function (blob) {
- if (blob) {
- resolve(blob);
- } else {
- reject(new Error('生成图片失败'));
- }
- }, 'image/png');
- return;
- }
- try {
- var data = canvas.toDataURL('image/png');
- var arr = data.split(',');
- var bin = atob(arr[1] || '');
- var u8 = new Uint8Array(bin.length);
- for (var i = 0; i < bin.length; i++) {
- u8[i] = bin.charCodeAt(i);
- }
- resolve(new Blob([u8], {type: 'image/png'}));
- } catch (e) {
- reject(e);
- }
- });
- }
- function captureSheet() {
- return loadQrImage(getQrDataUrl()).then(function (qrImage) {
- return canvasToBlob(drawNoteCanvas(qrImage));
- });
- }
- var saveBtn = document.getElementById('btn-note-save');
- if (saveBtn) {
- saveBtn.addEventListener('click', function (e) {
- e.preventDefault();
- if (saveBtn.disabled) {
- return;
- }
- saveBtn.disabled = true;
- saveBtn.textContent = '保存中';
- captureSheet().then(function (blob) {
- if (!isMobile()) {
- return tryShare(blob).then(function (ok) {
- if (ok) {
- toast('已保存');
- return;
- }
- if (tryDownload(blob)) {
- toast('图片已下载');
- return;
- }
- showMask(blob);
- });
- }
- showMask(blob);
- }).catch(function () {
- toast('保存失败,请稍后重试');
- }).then(function () {
- saveBtn.disabled = false;
- saveBtn.textContent = '保存';
- });
- });
- }
- var closeBtn = document.getElementById('btn-save-close');
- if (closeBtn) {
- closeBtn.addEventListener('click', function (e) {
- e.preventDefault();
- closeMask();
- });
- }
- function renderQr(text) {
- var box = document.getElementById('qrcode');
- if (!box) {
- return;
- }
- if (box.querySelector('img')) {
- return;
- }
- if (!text || typeof jQuery === 'undefined' || !jQuery.fn.qrcode) {
- return;
- }
- try {
- jQuery(box).empty().qrcode({
- width: 120,
- height: 120,
- text: text,
- render: 'table',
- correctLevel: 2,
- background: '#ffffff',
- foreground: '#000000'
- });
- } catch (err) {
- return;
- }
- var table = box.querySelector('table');
- var dataUrl = table ? qrTableToDataUrl(table) : '';
- if (!dataUrl) {
- return;
- }
- var img = document.createElement('img');
- img.src = dataUrl;
- img.width = 120;
- img.height = 120;
- img.alt = '送货码';
- box.innerHTML = '';
- box.appendChild(img);
- }
- var url = {:json_encode((string)($note['scan_url'] ?? ''), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)};
- renderQr(url);
- })();
- </script>
- </body>
- </html>
|