InviteTreeChildRows.vue
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<!--
* @Author: 赵丽婷
* @Date: 2026-01-06
* @LastEditors: 赵丽婷
* @LastEditTime: 2026-01-13 20:29:25
* @FilePath: \LinkMed\linkmed-vue3\src\components\common\InviteTreeChildRows.vue
* @Description: 邀请树行渲染组件(支持父行和子行)
* Copyright (c) 2025 by 北京连心医疗科技有限公司, All Rights Reserved.
-->
<template>
<!-- 渲染单个节点(用于父行) -->
<template v-if="renderSingle">
<tr
:class="{
'child-row': level > 0,
'target-node-row':
item.startUserId !== undefined && item.startUserId === item.userId,
}"
:data-level="level"
>
<td>
<button
v-if="
!targetNodeChildrenUserIds.has(item.userId) &&
item.descendantCount !== undefined &&
item.descendantCount !== null &&
item.descendantCount > 0
"
@click="handleToggleExpand(item.userId)"
class="expand-btn"
:class="{ expanded: expandedRows.has(item.userId) }"
>
<i
class="expand-icon fas"
:class="
expandedRows.has(item.userId)
? 'fa-chevron-down'
: 'fa-chevron-right'
"
></i>
</button>
</td>
<td>
{{ formatDateTime(item.registeredAt || "") }}
</td>
<td>
<span>{{ formatSourceUserName(item.username) || "-" }}</span>
<span
v-if="
showDescendantCount &&
item.descendantCount !== undefined &&
item.descendantCount !== null &&
item.descendantCount > 0
"
class="descendant-count"
>
{{ item.descendantCount }}
</span>
</td>
<td>
{{
(() => {
const parts = [];
if (item.salesLevel !== undefined && item.salesLevel !== null) {
parts.push(`S${item.salesLevel}`);
}
if (item.inviteLevel !== null && item.inviteLevel !== undefined) {
parts.push(`L${item.inviteLevel}`);
}
return parts.length > 0 ? parts.join(", ") : "-";
})()
}}
</td>
<td>
{{ item.maskedPhone || "-" }}
</td>
<td>
{{ item.maskedEmail || "-" }}
</td>
<td
v-if="showTotalEarnings"
class="earnings"
:class="{
'earnings-positive':
item.totalEarnings !== undefined &&
item.totalEarnings !== null &&
item.totalEarnings > 0,
}"
>
{{
item.totalEarnings !== undefined &&
item.totalEarnings !== null &&
item.totalEarnings > 0
? "+"
: ""
}}{{
item.totalEarnings !== undefined &&
item.totalEarnings !== null &&
item.totalEarnings > 0
? `${item.totalEarnings.toFixed(2)}${$t("settingsSidebar.yuan")}`
: "-"
}}
</td>
</tr>
<!-- 递归渲染子行 -->
<template
v-if="
item.children &&
item.children.length > 0 &&
expandedRows.has(item.userId)
"
>
<InviteTreeChildRows
:item="item"
:level="level + 1"
:expanded-rows="expandedRows"
:loaded-children-nodes="loadedChildrenNodes"
:target-node-children-user-ids="targetNodeChildrenUserIds"
:render-single="false"
:show-descendant-count="showDescendantCount"
:show-total-earnings="showTotalEarnings"
@toggle-expand="handleToggleExpand"
/>
</template>
</template>
<!-- 渲染子节点列表(用于子行) -->
<template v-else>
<template
v-for="(child, childIndex) in item.children"
:key="child.userId || childIndex"
>
<tr
:class="{
'child-row': true,
'target-node-row':
child.startUserId !== undefined &&
child.startUserId === child.userId,
}"
:data-level="level"
>
<td :style="{ paddingLeft: `${level * 10}px` }">
<button
v-if="
!targetNodeChildrenUserIds.has(child.userId) &&
child.descendantCount !== undefined &&
child.descendantCount !== null &&
child.descendantCount > 0
"
@click="handleToggleExpand(child.userId)"
class="expand-btn"
:class="{ expanded: expandedRows.has(child.userId) }"
>
<i
class="expand-icon fas"
:class="
expandedRows.has(child.userId)
? 'fa-chevron-down'
: 'fa-chevron-right'
"
></i>
</button>
</td>
<td :style="{ paddingLeft: `${level * 10}px` }">
{{ formatDateTime(child.registeredAt || "") }}
</td>
<td :style="{ paddingLeft: `${level * 10}px` }">
<span>{{ formatSourceUserName(child.username) || "-" }}</span>
<span
v-if="
showDescendantCount &&
child.descendantCount !== undefined &&
child.descendantCount !== null &&
child.descendantCount > 0
"
class="descendant-count"
>
{{ child.descendantCount }}
</span>
</td>
<td :style="{ paddingLeft: `${level * 10}px` }">
{{
(() => {
const parts = [];
if (child.salesLevel !== undefined && child.salesLevel !== null) {
parts.push(`S${child.salesLevel}`);
}
if (
child.inviteLevel !== null &&
child.inviteLevel !== undefined
) {
parts.push(`L${child.inviteLevel}`);
}
return parts.length > 0 ? parts.join(", ") : "-";
})()
}}
</td>
<td :style="{ paddingLeft: `${level * 10}px` }">
{{ child.maskedPhone || "-" }}
</td>
<td :style="{ paddingLeft: `${level * 10}px` }">
{{ child.maskedEmail || "-" }}
</td>
<td
v-if="showTotalEarnings"
class="earnings"
:class="{
'earnings-positive':
child.totalEarnings !== undefined &&
child.totalEarnings !== null &&
child.totalEarnings > 0,
}"
:style="{ paddingLeft: `${level * 10}px` }"
>
{{
child.totalEarnings !== undefined &&
child.totalEarnings !== null &&
child.totalEarnings > 0
? "+"
: ""
}}{{
child.totalEarnings !== undefined &&
child.totalEarnings !== null &&
child.totalEarnings > 0
? `${child.totalEarnings.toFixed(2)}${$t("settingsSidebar.yuan")}`
: "-"
}}
</td>
</tr>
<!-- 递归渲染更深层的子行 -->
<template
v-if="
child.children &&
child.children.length > 0 &&
expandedRows.has(child.userId)
"
>
<InviteTreeChildRows
:item="child"
:level="level + 1"
:expanded-rows="expandedRows"
:loaded-children-nodes="loadedChildrenNodes"
:target-node-children-user-ids="targetNodeChildrenUserIds"
:render-single="false"
:show-descendant-count="showDescendantCount"
@toggle-expand="handleToggleExpand"
/>
</template>
</template>
</template>
</template>
<script setup lang="ts">
import { useI18n } from "vue-i18n";
import type { InviteTreeItem } from "@/api/pay";
interface Props {
item: InviteTreeItem;
level: number;
expandedRows: Set<number>;
loadedChildrenNodes?: Set<number>; // 已加载子节点的节点ID集合
targetNodeChildrenUserIds?: Set<number>; // 目标节点的子节点 userId 集合(用于隐藏展开/收起按钮)
renderSingle?: boolean; // 是否渲染单个节点(用于父行),默认为 false(渲染子节点列表)
showDescendantCount?: boolean; // 是否显示后代数量
showTotalEarnings?: boolean; // 是否显示用户累计收益列
}
interface Emits {
(e: "toggle-expand", userId: number): void;
}
const props = withDefaults(defineProps<Props>(), {
renderSingle: false,
loadedChildrenNodes: () => new Set<number>(),
targetNodeChildrenUserIds: () => new Set<number>(),
showDescendantCount: false,
showTotalEarnings: true,
});
const emit = defineEmits<Emits>();
const { t } = useI18n();
// 格式化日期时间
const formatDateTime = (dateStr: string): string => {
if (!dateStr) return "";
// 如果日期字符串包含时间部分,直接格式化
if (dateStr.includes("T") || dateStr.includes(" ")) {
const date = new Date(dateStr);
if (isNaN(date.getTime())) return dateStr;
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
// 如果只有日期部分,添加默认时间 00:00:00
return `${dateStr} 00:00:00`;
};
// 格式化用户名
const formatSourceUserName = (userName?: string): string => {
if (!userName) return "-";
// 匹配"销售代表"(可能有后缀,也可能没有)
if (userName.startsWith("销售代表")) {
const suffix = userName.substring(4); // "销售代表"长度为4
return suffix
? `${t("settingsSidebar.salesRepresentative")}${suffix}`
: t("settingsSidebar.salesRepresentative");
}
return userName;
};
// 处理展开/收起
const handleToggleExpand = (userId: number) => {
emit("toggle-expand", userId);
};
</script>
<style lang="scss">
// 不使用 scoped,因为 tr 元素是直接渲染在父组件的表格 tbody 中的
.child-row {
background: #fafafa;
&:hover {
background: #f5f5f5;
}
}
// 确保 td 的 padding 为 10px,与资金记录表格一致
// 但保留 paddingLeft 的缩进(通过内联样式设置)
tr td {
padding-top: 12px !important;
padding-right: 10px !important;
padding-bottom: 12px !important;
// padding-left 由内联样式控制,用于缩进
}
.expand-btn {
background: none;
border: none;
cursor: pointer;
padding: 4px 8px;
display: inline-flex;
align-items: center;
justify-content: center;
border-radius: 4px;
transition: all 0.2s;
color: #666;
.expand-icon {
font-size: 12px;
transition: transform 0.2s;
}
}
.earnings-positive {
font-weight: 600;
color: #00c853;
}
.empty-children-row {
background: #fafafa;
.empty-children-cell {
text-align: center;
color: #999;
font-size: 14px;
padding: 12px 10px !important;
}
}
.descendant-count {
margin-left: 8px;
color: var(--color-primary);
font-size: 12px;
font-weight: 500;
}
// 目标节点高亮样式
.target-node-row {
background-color: rgba(30, 112, 255, 0.2) !important;
&:hover {
background-color: rgba(30, 112, 255, 0.3) !important;
}
}
</style>