| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- (function () {
- const TOKEN_KEY = 'pouchlab.token.v1';
- function langHeader() {
- const lang = window.PL && window.PL.i18n ? window.PL.i18n.getLang() : 'zh';
- return lang === 'en' ? 'en' : 'zh-CN';
- }
- function tr(s) {
- return window.PL && window.PL.t ? window.PL.t(s) : s;
- }
- 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';
- headers['Accept-Language'] = langHeader();
- 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 || tr('请求失败(' + 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(tr('请求超时,请重试'));
- err.network = true;
- throw err;
- }
- const err = new Error(tr('无法连接服务器'));
- err.network = true;
- throw err;
- });
- }
- // 带鉴权的文件下载(DXF/预览图/设计稿),转为浏览器下载
- function download(path, filename) {
- const t = token();
- return fetch('/api' + path, { headers: Object.assign({ 'Accept-Language': langHeader() }, t ? { Authorization: 'Bearer ' + t } : {}) }).then(function (res) {
- if (!res.ok) {
- return res.json().catch(function () { return {}; }).then(function (d) {
- throw new Error(d.detail || tr('下载失败(' + 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(tr('预览图生成失败')));
- };
- 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); }
- };
- })();
|