@pisell/pisellos 0.10.20 → 0.10.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import { BaseModule } from '../BaseModule';
3
3
  import type { ScheduleModule } from '../Schedule';
4
4
  import type { OpenDataConfig, OpenDataFetchParams, OpenDataAvailabilityResult, InvoiceLayoutConfig, InvoiceTemplateSettings } from './types';
5
5
  export * from './types';
6
- export { computeAvailability, getDefaultInvoiceLayoutConfig, getDefaultInvoiceTemplateSettings, normalizeInvoiceLayoutConfig, normalizeInvoiceTemplateSettings, normalizeRawInvoiceTemplateSettings, resolveInvoiceReceiptLocales, } from './utils';
6
+ export { computeAvailability, getDefaultInvoiceLayoutConfig, getDefaultInvoiceTemplateSettings, normalizeInvoiceLayoutConfig, normalizeInvoiceLayoutFromTemplateSettings, normalizeInvoiceTemplateSettings, normalizeRawInvoiceTemplateSettings, resolveInvoiceReceiptLocales, } from './utils';
7
7
  export declare class OpenDataModule extends BaseModule implements Module {
8
8
  protected defaultName: string;
9
9
  protected defaultVersion: string;
@@ -16,6 +16,9 @@ export declare class OpenDataModule extends BaseModule implements Module {
16
16
  private fatherModule;
17
17
  private otherParams;
18
18
  private fetchRevision;
19
+ private invoiceTemplateCacheRevision;
20
+ private invoiceTemplateDocumentCache;
21
+ private invoiceTemplateDocumentInFlight;
19
22
  constructor(name?: string, version?: string);
20
23
  initialize(core: PisellCore, options: ModuleOptions): Promise<void>;
21
24
  /**
@@ -29,6 +32,12 @@ export declare class OpenDataModule extends BaseModule implements Module {
29
32
  getOpenData(): OpenDataConfig | null;
30
33
  getInvoiceLayoutConfig(): InvoiceLayoutConfig;
31
34
  getInvoiceTemplateSettings(): InvoiceTemplateSettings;
35
+ ensureInvoiceTemplateSettings(target: string): Promise<InvoiceTemplateSettings>;
36
+ private commitInvoiceTemplateSettings;
37
+ private resolveInvoiceTemplateSettings;
38
+ private buildInvoiceTemplateCacheKey;
39
+ private fetchInvoiceTemplateDocument;
40
+ private logInvoiceTemplateDocumentFallback;
32
41
  getLastFetchedAt(): number | null;
33
42
  /** 清除内存与 sessionStorage 镜像中的 OpenData,供宿主切店时调用。 */
34
43
  clearCache(): void;
@@ -1,4 +1,3 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
1
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
2
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
3
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
@@ -15,10 +14,30 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
15
14
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
16
15
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
17
16
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
17
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
18
18
  import { BaseModule } from "../BaseModule";
19
- import { computeAvailability, normalizeInvoiceLayoutConfig, normalizeInvoiceTemplateSettings } from "./utils";
19
+ import { computeAvailability, getDefaultInvoiceTemplateSettings, normalizeInvoiceLayoutFromTemplateSettings, normalizeInvoiceTemplateSettings, normalizeRawInvoiceTemplateSettings } from "./utils";
20
20
  export * from "./types";
21
- export { computeAvailability, getDefaultInvoiceLayoutConfig, getDefaultInvoiceTemplateSettings, normalizeInvoiceLayoutConfig, normalizeInvoiceTemplateSettings, normalizeRawInvoiceTemplateSettings, resolveInvoiceReceiptLocales } from "./utils";
21
+ export { computeAvailability, getDefaultInvoiceLayoutConfig, getDefaultInvoiceTemplateSettings, normalizeInvoiceLayoutConfig, normalizeInvoiceLayoutFromTemplateSettings, normalizeInvoiceTemplateSettings, normalizeRawInvoiceTemplateSettings, resolveInvoiceReceiptLocales } from "./utils";
22
+ function isRecord(value) {
23
+ return !!value && _typeof(value) === 'object' && !Array.isArray(value);
24
+ }
25
+ function resolveInvoiceDocumentId(value) {
26
+ if (typeof value === 'number') {
27
+ return Number.isSafeInteger(value) && value > 0 ? String(value) : null;
28
+ }
29
+ if (typeof value !== 'string') return null;
30
+ var normalized = value.trim();
31
+ if (!/^\d+$/.test(normalized)) return null;
32
+ var numericId = Number(normalized);
33
+ return Number.isSafeInteger(numericId) && numericId > 0 ? String(numericId) : null;
34
+ }
35
+ function isLegacyInvoiceTemplate(value) {
36
+ if (typeof value !== 'string' || value.trim().length === 0) return false;
37
+ var normalized = value.trim();
38
+ var numericLike = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i;
39
+ return !numericLike.test(normalized);
40
+ }
22
41
  export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
23
42
  _inherits(OpenDataModule, _BaseModule);
24
43
  var _super = _createSuper(OpenDataModule);
@@ -37,6 +56,9 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
37
56
  _defineProperty(_assertThisInitialized(_this), "fatherModule", void 0);
38
57
  _defineProperty(_assertThisInitialized(_this), "otherParams", {});
39
58
  _defineProperty(_assertThisInitialized(_this), "fetchRevision", 0);
59
+ _defineProperty(_assertThisInitialized(_this), "invoiceTemplateCacheRevision", 0);
60
+ _defineProperty(_assertThisInitialized(_this), "invoiceTemplateDocumentCache", new Map());
61
+ _defineProperty(_assertThisInitialized(_this), "invoiceTemplateDocumentInFlight", new Map());
40
62
  return _this;
41
63
  }
42
64
  _createClass(OpenDataModule, [{
@@ -52,6 +74,12 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
52
74
  this.store = options.store;
53
75
  this.store.data = (_options$initialState = (_options$initialState2 = options.initialState) === null || _options$initialState2 === void 0 ? void 0 : _options$initialState2.data) !== null && _options$initialState !== void 0 ? _options$initialState : null;
54
76
  this.store.lastFetchedAt = (_options$initialState3 = (_options$initialState4 = options.initialState) === null || _options$initialState4 === void 0 ? void 0 : _options$initialState4.lastFetchedAt) !== null && _options$initialState3 !== void 0 ? _options$initialState3 : null;
77
+ // Document 解析结果只在当前模块实例内复用。页面刷新后即使恢复了原始
78
+ // OpenData,也必须重新查询一次 NocoBase,以取得当前模板内容。
79
+ this.store.invoiceTemplateSettings = null;
80
+ this.store.invoiceTemplateTarget = null;
81
+ this.store.invoiceTemplateDocumentId = null;
82
+ this.store.invoiceTemplateCacheKey = null;
55
83
  this.otherParams = options.otherParams || {};
56
84
  if (this.otherParams.cacheId) {
57
85
  this.openCache = (_this$otherParams$ope = this.otherParams.openCache) !== null && _this$otherParams$ope !== void 0 ? _this$otherParams$ope : false;
@@ -64,17 +92,17 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
64
92
  this.window = core.getPlugin('window');
65
93
  this.appPlugin = core.getPlugin('app');
66
94
  if (this.request) {
67
- _context.next = 12;
95
+ _context.next = 16;
68
96
  break;
69
97
  }
70
98
  throw new Error('OpenDataModule 需要 request 插件支持');
71
- case 12:
99
+ case 16:
72
100
  if (this.window) {
73
- _context.next = 14;
101
+ _context.next = 18;
74
102
  break;
75
103
  }
76
104
  throw new Error('OpenDataModule 需要 window 插件支持');
77
- case 14:
105
+ case 18:
78
106
  case "end":
79
107
  return _context.stop();
80
108
  }
@@ -105,7 +133,7 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
105
133
  value: function () {
106
134
  var _fetchOpenData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(params) {
107
135
  var _this$appPlugin, _this$appPlugin$getDa;
108
- var fetchRevision, getData, shopId, tenantId, headers, response, data;
136
+ var fetchRevision, getData, shopId, tenantId, headers, response, data, resolvedInvoiceTemplate;
109
137
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
110
138
  while (1) switch (_context2.prev = _context2.next) {
111
139
  case 0:
@@ -140,13 +168,19 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
140
168
  }
141
169
  throw new Error((response === null || response === void 0 ? void 0 : response.message) || '获取 OpenData 配置失败');
142
170
  case 13:
143
- data = (response === null || response === void 0 ? void 0 : response.data) || {}; // clearCache() 或后续请求发生后,旧响应不得复活已失效的 target 配置。
171
+ data = (response === null || response === void 0 ? void 0 : response.data) || {};
172
+ _context2.next = 16;
173
+ return this.resolveInvoiceTemplateSettings(data, params.target);
174
+ case 16:
175
+ resolvedInvoiceTemplate = _context2.sent;
176
+ // clearCache() 或后续请求发生后,旧响应不得复活已失效的 target 配置。
144
177
  if (fetchRevision === this.fetchRevision) {
145
178
  this.store.data = data;
146
179
  this.store.lastFetchedAt = Date.now();
180
+ this.commitInvoiceTemplateSettings(params.target, resolvedInvoiceTemplate);
147
181
  }
148
182
  return _context2.abrupt("return", data);
149
- case 16:
183
+ case 19:
150
184
  case "end":
151
185
  return _context2.stop();
152
186
  }
@@ -165,12 +199,225 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
165
199
  }, {
166
200
  key: "getInvoiceLayoutConfig",
167
201
  value: function getInvoiceLayoutConfig() {
168
- return normalizeInvoiceLayoutConfig(this.store.data);
202
+ return normalizeInvoiceLayoutFromTemplateSettings(this.getInvoiceTemplateSettings());
169
203
  }
170
204
  }, {
171
205
  key: "getInvoiceTemplateSettings",
172
206
  value: function getInvoiceTemplateSettings() {
173
- return normalizeInvoiceTemplateSettings(this.store.data);
207
+ var _this$store$data2;
208
+ if (isRecord(this.store.invoiceTemplateSettings)) {
209
+ var _this$store$data;
210
+ var documentId = resolveInvoiceDocumentId((_this$store$data = this.store.data) === null || _this$store$data === void 0 ? void 0 : _this$store$data['documents.invoice_template']);
211
+ if (documentId) {
212
+ var target = this.store.invoiceTemplateTarget;
213
+ if (!target || this.store.invoiceTemplateCacheKey !== this.buildInvoiceTemplateCacheKey(target, documentId)) {
214
+ return getDefaultInvoiceTemplateSettings();
215
+ }
216
+ }
217
+ return normalizeRawInvoiceTemplateSettings(this.store.invoiceTemplateSettings);
218
+ }
219
+ if (isLegacyInvoiceTemplate((_this$store$data2 = this.store.data) === null || _this$store$data2 === void 0 ? void 0 : _this$store$data2['documents.invoice_template'])) {
220
+ return normalizeInvoiceTemplateSettings(this.store.data);
221
+ }
222
+ return getDefaultInvoiceTemplateSettings();
223
+ }
224
+ }, {
225
+ key: "ensureInvoiceTemplateSettings",
226
+ value: function () {
227
+ var _ensureInvoiceTemplateSettings = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(target) {
228
+ var normalizedTarget, config, fetchRevision, resolved;
229
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
230
+ while (1) switch (_context3.prev = _context3.next) {
231
+ case 0:
232
+ normalizedTarget = String(target || '').trim();
233
+ config = this.store.data;
234
+ if (!(!normalizedTarget || !config)) {
235
+ _context3.next = 4;
236
+ break;
237
+ }
238
+ return _context3.abrupt("return", getDefaultInvoiceTemplateSettings());
239
+ case 4:
240
+ fetchRevision = this.fetchRevision;
241
+ _context3.next = 7;
242
+ return this.resolveInvoiceTemplateSettings(config, normalizedTarget);
243
+ case 7:
244
+ resolved = _context3.sent;
245
+ if (!(fetchRevision !== this.fetchRevision || this.store.data !== config)) {
246
+ _context3.next = 10;
247
+ break;
248
+ }
249
+ return _context3.abrupt("return", getDefaultInvoiceTemplateSettings());
250
+ case 10:
251
+ this.commitInvoiceTemplateSettings(normalizedTarget, resolved);
252
+ return _context3.abrupt("return", normalizeRawInvoiceTemplateSettings(resolved.settings));
253
+ case 12:
254
+ case "end":
255
+ return _context3.stop();
256
+ }
257
+ }, _callee3, this);
258
+ }));
259
+ function ensureInvoiceTemplateSettings(_x4) {
260
+ return _ensureInvoiceTemplateSettings.apply(this, arguments);
261
+ }
262
+ return ensureInvoiceTemplateSettings;
263
+ }()
264
+ }, {
265
+ key: "commitInvoiceTemplateSettings",
266
+ value: function commitInvoiceTemplateSettings(target, resolved) {
267
+ this.store.invoiceTemplateSettings = normalizeRawInvoiceTemplateSettings(resolved.settings);
268
+ this.store.invoiceTemplateTarget = target;
269
+ this.store.invoiceTemplateDocumentId = resolved.documentId;
270
+ this.store.invoiceTemplateCacheKey = resolved.cacheKey;
271
+ }
272
+ }, {
273
+ key: "resolveInvoiceTemplateSettings",
274
+ value: function () {
275
+ var _resolveInvoiceTemplateSettings = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(config, target) {
276
+ var _this2 = this;
277
+ var rawTemplate, documentId, cacheKey, cachedSettings, request, cacheRevision;
278
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
279
+ while (1) switch (_context4.prev = _context4.next) {
280
+ case 0:
281
+ rawTemplate = config['documents.invoice_template'];
282
+ documentId = resolveInvoiceDocumentId(rawTemplate);
283
+ if (documentId) {
284
+ _context4.next = 4;
285
+ break;
286
+ }
287
+ return _context4.abrupt("return", {
288
+ settings: isLegacyInvoiceTemplate(rawTemplate) ? normalizeInvoiceTemplateSettings(config) : getDefaultInvoiceTemplateSettings(),
289
+ documentId: null,
290
+ cacheKey: null
291
+ });
292
+ case 4:
293
+ cacheKey = this.buildInvoiceTemplateCacheKey(target, documentId);
294
+ if (!(this.store.invoiceTemplateTarget === target && this.store.invoiceTemplateDocumentId === documentId && this.store.invoiceTemplateCacheKey === cacheKey && isRecord(this.store.invoiceTemplateSettings))) {
295
+ _context4.next = 7;
296
+ break;
297
+ }
298
+ return _context4.abrupt("return", {
299
+ settings: normalizeRawInvoiceTemplateSettings(this.store.invoiceTemplateSettings),
300
+ documentId: documentId,
301
+ cacheKey: cacheKey
302
+ });
303
+ case 7:
304
+ cachedSettings = this.invoiceTemplateDocumentCache.get(cacheKey);
305
+ if (!cachedSettings) {
306
+ _context4.next = 10;
307
+ break;
308
+ }
309
+ return _context4.abrupt("return", {
310
+ settings: normalizeRawInvoiceTemplateSettings(cachedSettings),
311
+ documentId: documentId,
312
+ cacheKey: cacheKey
313
+ });
314
+ case 10:
315
+ request = this.invoiceTemplateDocumentInFlight.get(cacheKey);
316
+ if (!request) {
317
+ cacheRevision = this.invoiceTemplateCacheRevision;
318
+ request = this.fetchInvoiceTemplateDocument(documentId).catch(function (error) {
319
+ _this2.logInvoiceTemplateDocumentFallback(error, target, documentId);
320
+ return getDefaultInvoiceTemplateSettings();
321
+ }).then(function (settings) {
322
+ var normalized = normalizeRawInvoiceTemplateSettings(settings);
323
+ if (cacheRevision === _this2.invoiceTemplateCacheRevision) {
324
+ _this2.invoiceTemplateDocumentCache.set(cacheKey, normalized);
325
+ }
326
+ return normalized;
327
+ }).finally(function () {
328
+ if (_this2.invoiceTemplateDocumentInFlight.get(cacheKey) === request) {
329
+ _this2.invoiceTemplateDocumentInFlight.delete(cacheKey);
330
+ }
331
+ });
332
+ this.invoiceTemplateDocumentInFlight.set(cacheKey, request);
333
+ }
334
+ _context4.next = 14;
335
+ return request;
336
+ case 14:
337
+ _context4.t0 = _context4.sent;
338
+ _context4.t1 = documentId;
339
+ _context4.t2 = cacheKey;
340
+ return _context4.abrupt("return", {
341
+ settings: _context4.t0,
342
+ documentId: _context4.t1,
343
+ cacheKey: _context4.t2
344
+ });
345
+ case 18:
346
+ case "end":
347
+ return _context4.stop();
348
+ }
349
+ }, _callee4, this);
350
+ }));
351
+ function resolveInvoiceTemplateSettings(_x5, _x6) {
352
+ return _resolveInvoiceTemplateSettings.apply(this, arguments);
353
+ }
354
+ return resolveInvoiceTemplateSettings;
355
+ }()
356
+ }, {
357
+ key: "buildInvoiceTemplateCacheKey",
358
+ value: function buildInvoiceTemplateCacheKey(target, documentId) {
359
+ var _this$appPlugin2, _this$appPlugin2$getD, _getData, _getData2;
360
+ var getData = (_this$appPlugin2 = this.appPlugin) === null || _this$appPlugin2 === void 0 || (_this$appPlugin2$getD = _this$appPlugin2.getData) === null || _this$appPlugin2$getD === void 0 ? void 0 : _this$appPlugin2$getD.call(_this$appPlugin2);
361
+ return JSON.stringify([(_getData = getData === null || getData === void 0 ? void 0 : getData('tenant_id')) !== null && _getData !== void 0 ? _getData : null, (_getData2 = getData === null || getData === void 0 ? void 0 : getData('shop_id')) !== null && _getData2 !== void 0 ? _getData2 : null, target, documentId]);
362
+ }
363
+ }, {
364
+ key: "fetchInvoiceTemplateDocument",
365
+ value: function () {
366
+ var _fetchInvoiceTemplateDocument = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(documentId) {
367
+ var response, document;
368
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
369
+ while (1) switch (_context5.prev = _context5.next) {
370
+ case 0:
371
+ _context5.next = 2;
372
+ return this.request.get("/document:get?filterByTk=".concat(encodeURIComponent(documentId)), {}, {
373
+ isNocobase: true,
374
+ prefix: false,
375
+ headers: {
376
+ 'x-data-source': 'pisell2'
377
+ },
378
+ customToast: function customToast() {}
379
+ });
380
+ case 2:
381
+ response = _context5.sent;
382
+ document = response === null || response === void 0 ? void 0 : response.data;
383
+ if (!(!document || resolveInvoiceDocumentId(document.id) !== documentId)) {
384
+ _context5.next = 6;
385
+ break;
386
+ }
387
+ throw new Error("Invoice Document ".concat(documentId, " \u8FD4\u56DE\u7684 ID \u4E0D\u5339\u914D"));
388
+ case 6:
389
+ if (!(document.deleted_at !== null && document.deleted_at !== undefined)) {
390
+ _context5.next = 8;
391
+ break;
392
+ }
393
+ throw new Error("Invoice Document ".concat(documentId, " \u5DF2\u88AB\u5220\u9664"));
394
+ case 8:
395
+ if (!(!isRecord(document.data) || !isRecord(document.data.template_settings))) {
396
+ _context5.next = 10;
397
+ break;
398
+ }
399
+ throw new Error("Invoice Document ".concat(documentId, " \u7F3A\u5C11 template_settings"));
400
+ case 10:
401
+ return _context5.abrupt("return", normalizeRawInvoiceTemplateSettings(document.data.template_settings));
402
+ case 11:
403
+ case "end":
404
+ return _context5.stop();
405
+ }
406
+ }, _callee5, this);
407
+ }));
408
+ function fetchInvoiceTemplateDocument(_x7) {
409
+ return _fetchInvoiceTemplateDocument.apply(this, arguments);
410
+ }
411
+ return fetchInvoiceTemplateDocument;
412
+ }()
413
+ }, {
414
+ key: "logInvoiceTemplateDocumentFallback",
415
+ value: function logInvoiceTemplateDocumentFallback(error, target, documentId) {
416
+ console.warn('[OpenDataModule] Invoice Document 加载失败,使用 DEFAULT', {
417
+ target: target,
418
+ documentId: documentId,
419
+ message: error instanceof Error ? error.message : String(error)
420
+ });
174
421
  }
175
422
  }, {
176
423
  key: "getLastFetchedAt",
@@ -184,14 +431,21 @@ export var OpenDataModule = /*#__PURE__*/function (_BaseModule) {
184
431
  key: "clearCache",
185
432
  value: function clearCache() {
186
433
  this.fetchRevision += 1;
434
+ this.invoiceTemplateCacheRevision += 1;
435
+ this.invoiceTemplateDocumentCache.clear();
436
+ this.invoiceTemplateDocumentInFlight.clear();
187
437
  this.store.data = null;
188
438
  this.store.lastFetchedAt = null;
439
+ this.store.invoiceTemplateSettings = null;
440
+ this.store.invoiceTemplateTarget = null;
441
+ this.store.invoiceTemplateDocumentId = null;
442
+ this.store.invoiceTemplateCacheKey = null;
189
443
  }
190
444
  }, {
191
445
  key: "checkAvailability",
192
446
  value: function checkAvailability(scheduleModule) {
193
- var _this$store$data;
194
- var scheduleList = scheduleModule ? scheduleModule === null || scheduleModule === void 0 ? void 0 : scheduleModule.getScheduleListByIds(((_this$store$data = this.store.data) === null || _this$store$data === void 0 ? void 0 : _this$store$data['availability.operating_hours']) || []) : undefined;
447
+ var _this$store$data3;
448
+ var scheduleList = scheduleModule ? scheduleModule === null || scheduleModule === void 0 ? void 0 : scheduleModule.getScheduleListByIds(((_this$store$data3 = this.store.data) === null || _this$store$data3 === void 0 ? void 0 : _this$store$data3['availability.operating_hours']) || []) : undefined;
195
449
  return computeAvailability({
196
450
  config: this.store.data,
197
451
  scheduleList: scheduleList
@@ -32,6 +32,15 @@ export interface InvoiceTemplateSettings {
32
32
  modules: InvoiceTemplateModuleConfig[];
33
33
  [key: string]: unknown;
34
34
  }
35
+ export interface InvoiceDocumentResponse {
36
+ data?: {
37
+ id?: number | string;
38
+ deleted_at?: string | null;
39
+ data?: {
40
+ template_settings?: unknown;
41
+ };
42
+ };
43
+ }
35
44
  export type RawInvoiceEditConfig = string | {
36
45
  language?: unknown;
37
46
  modules?: unknown;
@@ -79,7 +88,7 @@ export interface OpenDataConfig {
79
88
  label: string;
80
89
  value: number;
81
90
  }>;
82
- 'documents.invoice_template'?: string;
91
+ 'documents.invoice_template'?: string | number;
83
92
  'documents.invoice_edit'?: RawInvoiceEditConfig;
84
93
  /** @deprecated 仅保留旧调用方的类型兼容,Invoice 读取逻辑不再消费该字段。 */
85
94
  'documents.invoice'?: RawInvoiceLayoutConfig;
@@ -92,6 +101,10 @@ export interface OpenDataConfig {
92
101
  export interface OpenDataState {
93
102
  data: OpenDataConfig | null;
94
103
  lastFetchedAt: number | null;
104
+ invoiceTemplateSettings?: InvoiceTemplateSettings | null;
105
+ invoiceTemplateTarget?: string | null;
106
+ invoiceTemplateDocumentId?: string | null;
107
+ invoiceTemplateCacheKey?: string | null;
95
108
  }
96
109
  export interface OpenDataFetchParams {
97
110
  scope: string;
@@ -14,6 +14,7 @@ export declare function normalizeRawInvoiceTemplateSettings(value: unknown): Inv
14
14
  * 从 OpenData Documents section 的两个扁平字段中构造 Native Invoice 配置。
15
15
  */
16
16
  export declare function normalizeInvoiceTemplateSettings(config: OpenDataConfig | null | undefined): InvoiceTemplateSettings;
17
+ export declare function normalizeInvoiceLayoutFromTemplateSettings(settings: InvoiceTemplateSettings | null | undefined): InvoiceLayoutConfig;
17
18
  /**
18
19
  * 将 invoice_edit.modules 原样投影为 Web Invoice 使用的 module 字段。
19
20
  */
@@ -125,16 +125,19 @@ export function normalizeInvoiceTemplateSettings(config) {
125
125
  template: normalizeInvoiceTemplate(config === null || config === void 0 ? void 0 : config['documents.invoice_template'])
126
126
  }));
127
127
  }
128
+ export function normalizeInvoiceLayoutFromTemplateSettings(settings) {
129
+ var modules = normalizeRawInvoiceTemplateSettings(settings).modules;
130
+ if (modules.length === 0) return getDefaultInvoiceLayoutConfig();
131
+ return {
132
+ module: modules
133
+ };
134
+ }
128
135
 
129
136
  /**
130
137
  * 将 invoice_edit.modules 原样投影为 Web Invoice 使用的 module 字段。
131
138
  */
132
139
  export function normalizeInvoiceLayoutConfig(config) {
133
- var modules = normalizeInvoiceTemplateSettings(config).modules;
134
- if (modules.length === 0) return getDefaultInvoiceLayoutConfig();
135
- return {
136
- module: modules
137
- };
140
+ return normalizeInvoiceLayoutFromTemplateSettings(normalizeInvoiceTemplateSettings(config));
138
141
  }
139
142
  function toBoolean(value) {
140
143
  if (typeof value === 'boolean') return value;
@@ -528,7 +528,7 @@ export declare class OrderModule extends BaseModule implements Module, OrderModu
528
528
  generateIdempotencyToken(): string;
529
529
  syncPaymentsToOrder<T = any>(params: SyncPaymentsToOrderParams): Promise<SyncPaymentsToOrderResult<T>>;
530
530
  createOrder(params: CommitOrderParams['query']): {
531
- type: "virtual" | "appointment_booking";
531
+ type: "appointment_booking" | "virtual";
532
532
  platform: string;
533
533
  sales_channel: string;
534
534
  order_sales_channel: string;
@@ -69,6 +69,7 @@ export interface RequestOptions {
69
69
  customToast?: () => void;
70
70
  cache?: CacheProps;
71
71
  osServer?: boolean;
72
+ isNocobase?: boolean;
72
73
  prefix?: boolean;
73
74
  callback?: (res: any) => void;
74
75
  subscriberId?: string;
@@ -124,7 +125,7 @@ declare class RequestPluginImpl implements RequestPlugin {
124
125
  /**
125
126
  * GET 请求
126
127
  */
127
- get<T = any>(url: string, options?: RequestOptions): Promise<T>;
128
+ get<T = any>(url: string, dataOrOptions?: any, options?: RequestOptions): Promise<T>;
128
129
  /**
129
130
  * POST 请求
130
131
  */
@@ -147,8 +147,10 @@ var RequestPluginImpl = /*#__PURE__*/function () {
147
147
  }, {
148
148
  key: "get",
149
149
  value: function get(url) {
150
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
151
- return this.request(_objectSpread(_objectSpread({}, options), {}, {
150
+ var dataOrOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
151
+ var options = arguments.length > 2 ? arguments[2] : undefined;
152
+ var requestOptions = options !== null && options !== void 0 ? options : dataOrOptions;
153
+ return this.request(_objectSpread(_objectSpread({}, requestOptions), {}, {
152
154
  url: url,
153
155
  method: 'GET'
154
156
  }));
@@ -7027,7 +7027,7 @@ var Server = /*#__PURE__*/function () {
7027
7027
  key: "withLocalSmallTicketData",
7028
7028
  value: function () {
7029
7029
  var _withLocalSmallTicketData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee70(order, options) {
7030
- var _options$productMap, enrichedOrder, productMap, smallTicketData;
7030
+ var _options$productMap, normalizedCollections, normalizedOrder, collapsedDuplicateCounts, _ref75, _ref76, _order_id, enrichedOrder, productMap, smallTicketData;
7031
7031
  return _regeneratorRuntime().wrap(function _callee70$(_context70) {
7032
7032
  while (1) switch (_context70.prev = _context70.next) {
7033
7033
  case 0:
@@ -7044,23 +7044,40 @@ var Server = /*#__PURE__*/function () {
7044
7044
  return _context70.abrupt("return", order);
7045
7045
  case 4:
7046
7046
  _context70.prev = 4;
7047
- _context70.next = 7;
7048
- return this.enrichPaymentNamesByCode(order);
7049
- case 7:
7047
+ normalizedCollections = normalizeOrderCollections(order, {
7048
+ ensureUid: false
7049
+ });
7050
+ normalizedOrder = normalizedCollections.order;
7051
+ collapsedDuplicateCounts = {
7052
+ products: normalizedCollections.stats.product.collapsedDuplicateCount,
7053
+ bookings: normalizedCollections.stats.booking.collapsedDuplicateCount,
7054
+ payments: normalizedCollections.stats.payment.collapsedDuplicateCount
7055
+ };
7056
+ if (Object.values(collapsedDuplicateCounts).some(function (count) {
7057
+ return count > 0;
7058
+ })) {
7059
+ this.logWarning('withLocalSmallTicketData: 折叠重复订单子项', {
7060
+ order_identifier: (_ref75 = (_ref76 = (_order_id = normalizedOrder.order_id) !== null && _order_id !== void 0 ? _order_id : normalizedOrder.external_sale_number) !== null && _ref76 !== void 0 ? _ref76 : normalizedOrder.shop_order_number) !== null && _ref75 !== void 0 ? _ref75 : null,
7061
+ collapsed_duplicate_counts: collapsedDuplicateCounts
7062
+ });
7063
+ }
7064
+ _context70.next = 11;
7065
+ return this.enrichPaymentNamesByCode(normalizedOrder);
7066
+ case 11:
7050
7067
  enrichedOrder = _context70.sent;
7051
7068
  if (!((_options$productMap = options === null || options === void 0 ? void 0 : options.productMap) !== null && _options$productMap !== void 0)) {
7052
- _context70.next = 12;
7069
+ _context70.next = 16;
7053
7070
  break;
7054
7071
  }
7055
7072
  _context70.t0 = _options$productMap;
7056
- _context70.next = 15;
7073
+ _context70.next = 19;
7057
7074
  break;
7058
- case 12:
7059
- _context70.next = 14;
7075
+ case 16:
7076
+ _context70.next = 18;
7060
7077
  return this.buildSmallTicketProductMap(enrichedOrder);
7061
- case 14:
7078
+ case 18:
7062
7079
  _context70.t0 = _context70.sent;
7063
- case 15:
7080
+ case 19:
7064
7081
  productMap = _context70.t0;
7065
7082
  smallTicketData = buildSmallTicketData({
7066
7083
  order: enrichedOrder,
@@ -7073,18 +7090,18 @@ var Server = /*#__PURE__*/function () {
7073
7090
  small_ticket_data: smallTicketData
7074
7091
  })
7075
7092
  }));
7076
- case 20:
7077
- _context70.prev = 20;
7093
+ case 24:
7094
+ _context70.prev = 24;
7078
7095
  _context70.t1 = _context70["catch"](4);
7079
7096
  this.logError('withLocalSmallTicketData: 生成本地小票数据失败', {
7080
7097
  error: _context70.t1 instanceof Error ? _context70.t1.message : String(_context70.t1)
7081
7098
  });
7082
7099
  return _context70.abrupt("return", order);
7083
- case 24:
7100
+ case 28:
7084
7101
  case "end":
7085
7102
  return _context70.stop();
7086
7103
  }
7087
- }, _callee70, this, [[4, 20]]);
7104
+ }, _callee70, this, [[4, 24]]);
7088
7105
  }));
7089
7106
  function withLocalSmallTicketData(_x78, _x79) {
7090
7107
  return _withLocalSmallTicketData.apply(this, arguments);
@@ -7103,7 +7120,7 @@ var Server = /*#__PURE__*/function () {
7103
7120
  value: function () {
7104
7121
  var _fetchAndPersistSalesOrderByLookup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee71(lookup, data, title) {
7105
7122
  var _this$order4, _this$order5, _this$app17;
7106
- var backendPath, response, _response$code3, responseStatusCode, code, message, fresh, savedOrder, _errorRecord$response, backendError, errorRecord, _responseStatusCode, _ref75, _backendError$code2, backendStatusCode, _code, _message2, _message3;
7123
+ var backendPath, response, _response$code3, responseStatusCode, code, message, fresh, savedOrder, _errorRecord$response, backendError, errorRecord, _responseStatusCode, _ref77, _backendError$code2, backendStatusCode, _code, _message2, _message3;
7107
7124
  return _regeneratorRuntime().wrap(function _callee71$(_context71) {
7108
7125
  while (1) switch (_context71.prev = _context71.next) {
7109
7126
  case 0:
@@ -7224,7 +7241,7 @@ var Server = /*#__PURE__*/function () {
7224
7241
  break;
7225
7242
  }
7226
7243
  backendStatusCode = typeof (backendError === null || backendError === void 0 ? void 0 : backendError.status) === 'number' ? backendError.status : undefined;
7227
- _code = this.normalizeRemoteSalesOrderErrorCode((_ref75 = (_backendError$code2 = backendError === null || backendError === void 0 ? void 0 : backendError.code) !== null && _backendError$code2 !== void 0 ? _backendError$code2 : backendStatusCode) !== null && _ref75 !== void 0 ? _ref75 : _responseStatusCode);
7244
+ _code = this.normalizeRemoteSalesOrderErrorCode((_ref77 = (_backendError$code2 = backendError === null || backendError === void 0 ? void 0 : backendError.code) !== null && _backendError$code2 !== void 0 ? _backendError$code2 : backendStatusCode) !== null && _ref77 !== void 0 ? _ref77 : _responseStatusCode);
7228
7245
  _message2 = (backendError === null || backendError === void 0 ? void 0 : backendError.message) || this.getUnknownErrorMessage(_context71.t0);
7229
7246
  this.logInfo("".concat(title, ": \u540E\u7AEF\u8BA2\u5355\u67E5\u8BE2\u5931\u8D25"), {
7230
7247
  lookup: lookup,