(function () { const TOKEN_KEY = 'pouchlab.token.v1'; function token() { try { return localStorage.getItem(TOKEN_KEY) || ''; } catch (e) { return ''; } } function setToken(v) { try { if (v) localStorage.setItem(TOKEN_KEY, v); else localStorage.removeItem(TOKEN_KEY); } catch (e) {} } function request(method, path, body, opts) { opts = opts || {}; const headers = {}; if (body !== undefined) headers['Content-Type'] = 'application/json'; const t = token(); if (t) headers['Authorization'] = 'Bearer ' + t; const controller = typeof AbortController !== 'undefined' ? new AbortController() : null; const timer = controller ? setTimeout(function () { controller.abort(); }, opts.timeout || 30000) : null; return fetch('/api' + path, { method: method, headers: headers, body: body !== undefined ? JSON.stringify(body) : undefined, signal: controller ? controller.signal : undefined }).then(function (res) { if (timer) clearTimeout(timer); if (res.status === 401) { setToken(''); document.dispatchEvent(new CustomEvent('pl:auth-expired')); } return res.json().catch(function () { return {}; }).then(function (data) { if (!res.ok) { const err = new Error(data.detail || ('请求失败(' + res.status + ')')); err.status = res.status; throw err; } return data; }); }, function (e) { if (timer) clearTimeout(timer); if (e && e.name === 'AbortError') { const err = new Error('请求超时,请重试'); err.network = true; throw err; } const err = new Error('无法连接服务器'); err.network = true; throw err; }); } // 带鉴权的文件下载(DXF/预览图/设计稿),转为浏览器下载 function download(path, filename) { const t = token(); return fetch('/api' + path, { headers: t ? { Authorization: 'Bearer ' + t } : {} }).then(function (res) { if (!res.ok) { return res.json().catch(function () { return {}; }).then(function (d) { throw new Error(d.detail || '下载失败(' + res.status + ')'); }); } return res.blob(); }).then(function (blob) { const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = filename || ''; document.body.appendChild(a); a.click(); a.remove(); setTimeout(function () { URL.revokeObjectURL(url); }, 4000); }); } // SVG 字符串 → PNG dataURL(下单时生成本地预览,服务端与管理后台直接展示) function svgToPng(svg, maxW) { return new Promise(function (resolve, reject) { const blob = new Blob([svg], { type: 'image/svg+xml;charset=utf-8' }); const url = URL.createObjectURL(blob); const img = new Image(); img.onload = function () { const scale = Math.min(1, (maxW || 1000) / (img.width || 1)); const canvas = document.createElement('canvas'); canvas.width = Math.max(1, Math.round((img.width || 1) * scale)); canvas.height = Math.max(1, Math.round((img.height || 1) * scale)); const ctx = canvas.getContext('2d'); ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.drawImage(img, 0, 0, canvas.width, canvas.height); URL.revokeObjectURL(url); resolve(canvas.toDataURL('image/png')); }; img.onerror = function () { URL.revokeObjectURL(url); reject(new Error('预览图生成失败')); }; img.src = url; }); } window.PL = window.PL || {}; window.PL.api = { token: token, setToken: setToken, request: request, download: download, svgToPng: svgToPng, get: function (p, o) { return request('GET', p, undefined, o); }, post: function (p, b, o) { return request('POST', p, b, o); }, patch: function (p, b, o) { return request('PATCH', p, b, o); } }; })();