km-card-layout-component-miniprogram 0.1.14 → 0.1.16

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.
@@ -1,6 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasCompanyDutyKey = exports.normalizeMoreCompany = void 0;
3
4
  const index_1 = require("../../vendor/km-card-layout-core/index");
5
+ const EMPTY_COMPANY_DUTY = {
6
+ company: '',
7
+ duty: '',
8
+ };
9
+ /**
10
+ * 规范化 moreCardInfo.company,保证长度始终为 2
11
+ */
12
+ function normalizeMoreCompany(data) {
13
+ var _a, _b;
14
+ const origin = (_b = (_a = data.user) === null || _a === void 0 ? void 0 : _a.moreCardInfo) === null || _b === void 0 ? void 0 : _b.company;
15
+ // 不存在 / 不是数组 → 不处理
16
+ if (!Array.isArray(origin)) {
17
+ return data;
18
+ }
19
+ // 长度 === 0 → 不处理
20
+ if (origin.length === 0) {
21
+ return data;
22
+ }
23
+ const normalized = origin.slice(0, 2);
24
+ while (normalized.length < 2) {
25
+ normalized.push({ ...EMPTY_COMPANY_DUTY });
26
+ }
27
+ return {
28
+ ...data,
29
+ user: {
30
+ ...data.user,
31
+ moreCardInfo: {
32
+ ...data.user.moreCardInfo,
33
+ company: normalized,
34
+ },
35
+ },
36
+ };
37
+ }
38
+ exports.normalizeMoreCompany = normalizeMoreCompany;
4
39
  const handleSpecialFields = (data) => {
5
40
  var _a;
6
41
  const user = (_a = data.user) !== null && _a !== void 0 ? _a : {};
@@ -53,6 +88,29 @@ const buildCards = (layouts) => {
53
88
  elements: decorateElements(layout.children || []),
54
89
  }));
55
90
  };
91
+ const hasCompanyDutyKey = (schemas) => {
92
+ const TARGET_KEYS = new Set([
93
+ 'company_duty_custom',
94
+ 'company_duty_1',
95
+ 'company_duty_2',
96
+ ]);
97
+ const traverse = (elements) => {
98
+ return elements.some(el => {
99
+ var _a;
100
+ // 命中 key
101
+ if (el.key && TARGET_KEYS.has(el.key)) {
102
+ return true;
103
+ }
104
+ // 递归 layout-panel
105
+ if (el.type === 'layout-panel' && ((_a = el.children) === null || _a === void 0 ? void 0 : _a.length)) {
106
+ return traverse(el.children);
107
+ }
108
+ return false;
109
+ });
110
+ };
111
+ return schemas.some(schema => traverse(schema.children));
112
+ };
113
+ exports.hasCompanyDutyKey = hasCompanyDutyKey;
56
114
  Component({
57
115
  options: {
58
116
  styleIsolation: 'apply-shared',
@@ -89,8 +147,9 @@ Component({
89
147
  },
90
148
  methods: {
91
149
  rebuild() {
92
- const layoutInput = (0, index_1.processCardLayout)(this.data.layout, this.data.data);
93
- const rootData = handleSpecialFields(this.data.data);
150
+ const data = normalizeMoreCompany(this.data.data);
151
+ const layoutInput = (0, exports.hasCompanyDutyKey)(this.data.layout) ? (0, index_1.processCardLayout)(this.data.layout, data) : this.data.layout;
152
+ const rootData = handleSpecialFields(data);
94
153
  if (!layoutInput.length) {
95
154
  this.setData({ cards: [], rootData });
96
155
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "km-card-layout-component-miniprogram",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "",
5
5
  "main": "miniprogram_dist/index.js",
6
6
  "miniprogram": "miniprogram_dist",
@@ -19,6 +19,48 @@ type CompanyDuty = {
19
19
  duty: string;
20
20
  };
21
21
 
22
+
23
+
24
+ const EMPTY_COMPANY_DUTY: CompanyDuty = {
25
+ company: '',
26
+ duty: '',
27
+ };
28
+
29
+ /**
30
+ * 规范化 moreCardInfo.company,保证长度始终为 2
31
+ */
32
+ export function normalizeMoreCompany(data: AnyObject): AnyObject {
33
+ const origin = data.user?.moreCardInfo?.company;
34
+
35
+ // 不存在 / 不是数组 → 不处理
36
+ if (!Array.isArray(origin)) {
37
+ return data;
38
+ }
39
+
40
+ // 长度 === 0 → 不处理
41
+ if (origin.length === 0) {
42
+ return data;
43
+ }
44
+
45
+ const normalized = origin.slice(0, 2);
46
+
47
+ while (normalized.length < 2) {
48
+ normalized.push({ ...EMPTY_COMPANY_DUTY });
49
+ }
50
+
51
+ return {
52
+ ...data,
53
+ user: {
54
+ ...data.user,
55
+ moreCardInfo: {
56
+ ...data.user.moreCardInfo,
57
+ company: normalized,
58
+ },
59
+ },
60
+ };
61
+ }
62
+
63
+
22
64
  const handleSpecialFields = (data: { user: Record<string, any> }) => {
23
65
  const user = data.user ?? {};
24
66
  return {
@@ -93,6 +135,36 @@ const buildCards = (layouts: CardLayoutSchema[]) => {
93
135
  }));
94
136
  };
95
137
 
138
+
139
+ export const hasCompanyDutyKey = (
140
+ schemas: CardLayoutSchema[]
141
+ ): boolean => {
142
+ const TARGET_KEYS = new Set([
143
+ 'company_duty_custom',
144
+ 'company_duty_1',
145
+ 'company_duty_2',
146
+ ]);
147
+
148
+ const traverse = (elements: CardElement[]): boolean => {
149
+ return elements.some(el => {
150
+ // 命中 key
151
+ if (el.key && TARGET_KEYS.has(el.key)) {
152
+ return true;
153
+ }
154
+
155
+ // 递归 layout-panel
156
+ if (el.type === 'layout-panel' && el.children?.length) {
157
+ return traverse(el.children);
158
+ }
159
+
160
+ return false;
161
+ });
162
+ };
163
+
164
+ return schemas.some(schema => traverse(schema.children));
165
+ };
166
+
167
+
96
168
  Component({
97
169
  options: {
98
170
  styleIsolation: 'apply-shared',
@@ -129,8 +201,9 @@ Component({
129
201
  },
130
202
  methods: {
131
203
  rebuild() {
132
- const layoutInput = processCardLayout(this.data.layout, this.data.data);
133
- const rootData = handleSpecialFields(this.data.data);
204
+ const data = normalizeMoreCompany(this.data.data)
205
+ const layoutInput = hasCompanyDutyKey(this.data.layout)?processCardLayout(this.data.layout, data as any): this.data.layout;
206
+ const rootData = handleSpecialFields(data as any);
134
207
  if (!layoutInput.length) {
135
208
  this.setData({ cards: [], rootData });
136
209
  return;