team-tree.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <view class="container">
  3. <!-- 顶部统计卡 -->
  4. <view class="stats-card">
  5. <view class="stat-item">
  6. <text class="stat-value orange">{{ fenToYuan(consumption.totalConsumption) }}</text>
  7. <text class="stat-label">团队总消费</text>
  8. </view>
  9. <view class="stat-divider"></view>
  10. <view class="stat-item">
  11. <text class="stat-value blue">{{ consumption.totalMembers }}</text>
  12. <text class="stat-label">团队人数</text>
  13. </view>
  14. <view class="stat-divider"></view>
  15. <view class="stat-item">
  16. <text class="stat-value green">{{ currentMaxLevel }}</text>
  17. <text class="stat-label">统计层级</text>
  18. </view>
  19. </view>
  20. <!-- 层级切换 tabs -->
  21. <view class="level-tabs">
  22. <view class="level-tab" :class="{ active: currentMaxLevel === 1 }" @click="switchLevel(1)">1级</view>
  23. <view class="level-tab" :class="{ active: currentMaxLevel === 2 }" @click="switchLevel(2)">2级</view>
  24. <view class="level-tab" :class="{ active: currentMaxLevel === 3 }" @click="switchLevel(3)">3级</view>
  25. <view class="level-tab" :class="{ active: currentMaxLevel === 4 }" @click="switchLevel(4)">4级</view>
  26. <view class="level-tab" :class="{ active: currentMaxLevel === 5 }" @click="switchLevel(5)">5级</view>
  27. </view>
  28. <!-- 树形区域(从下往上生长:column-reverse,L1 在底部) -->
  29. <view class="tree-wrapper">
  30. <view class="tree-container">
  31. <TreeNode
  32. v-for="root in level1Roots"
  33. :key="root.userId"
  34. :node="root"
  35. :level="1"
  36. :maxLevel="currentMaxLevel"
  37. />
  38. </view>
  39. <view class="empty-state" v-if="!loading && level1Roots.length === 0">
  40. <text>暂无团队成员</text>
  41. </view>
  42. </view>
  43. <view class="loading-overlay" v-if="loading">
  44. <text>加载中...</text>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import { getTeamTree, getTeamConsumption } from '@/utils/api.js'
  50. export default {
  51. components: {
  52. TreeNode: {
  53. name: 'TreeNode',
  54. props: { node: Object, level: Number, maxLevel: Number },
  55. data() {
  56. return { expanded: false }
  57. },
  58. methods: {
  59. toggle() {
  60. this.expanded = !this.expanded
  61. }
  62. },
  63. template: `
  64. <view class="tree-node" :style="{ paddingLeft: (level > 1 ? (level - 1) * 24 : 0) + 'rpx' }">
  65. <view class="node-main" @click="toggle">
  66. <image v-if="node.avatar" class="node-avatar" :src="node.avatar" mode="aspectFill" />
  67. <text v-else class="node-avatar-text">{{ node.nickname ? node.nickname.charAt(0) : '?' }}</text>
  68. <text class="node-name">{{ node.nickname }}</text>
  69. <text class="node-tag" v-if="level > 1">L{{ level }}</text>
  70. <view class="node-stats">
  71. <text class="node-stat consumption">消费 ¥{{ (node.memberConsumption / 100).toFixed(2) }}</text>
  72. <text class="node-stat my-cf">得CF ¥{{ (node.myCfEarnedFromMember / 100).toFixed(2) }}</text>
  73. </view>
  74. <text class="node-arrow">{{ expanded ? '▲' : '▼' }}</text>
  75. </view>
  76. <view v-if="expanded && node.children && node.children.length > 0" class="children">
  77. <TreeNode
  78. v-for="child in node.children"
  79. :key="child.userId"
  80. :node="child"
  81. :level="level + 1"
  82. :maxLevel="maxLevel"
  83. />
  84. </view>
  85. </view>
  86. `
  87. }
  88. },
  89. data() {
  90. return {
  91. nodes: [],
  92. level1Roots: [],
  93. consumption: { totalConsumption: 0, totalMembers: 0, maxLevel: 3 },
  94. currentMaxLevel: 3,
  95. loading: false
  96. }
  97. },
  98. onLoad() {
  99. this.loadData()
  100. this._onLoadFired = true
  101. },
  102. onShow() {
  103. if (this._onLoadFired) {
  104. this._onLoadFired = false
  105. } else {
  106. this.loadData()
  107. }
  108. },
  109. methods: {
  110. async loadData() {
  111. this.loading = true
  112. try {
  113. var self = this
  114. var treeRes = await getTeamTree(self.currentMaxLevel)
  115. var consumptionRes = await getTeamConsumption(self.currentMaxLevel)
  116. if (treeRes && treeRes.data && treeRes.data.nodes) {
  117. self.nodes = treeRes.data.nodes
  118. self.buildLevel1Roots()
  119. }
  120. if (consumptionRes && consumptionRes.data) {
  121. self.consumption = consumptionRes.data
  122. self.consumption.maxLevel = self.consumption.maxLevel || self.currentMaxLevel
  123. }
  124. } catch (e) {
  125. console.error('加载团队树失败', e)
  126. }
  127. this.loading = false
  128. },
  129. switchLevel(level) {
  130. if (this.currentMaxLevel === level) return
  131. this.currentMaxLevel = level
  132. this.loadData()
  133. },
  134. buildLevel1Roots() {
  135. var self = this
  136. // 将扁平 nodes 构建成以 L1 为根的子树
  137. var nodeMap = {}
  138. var l1Ids = []
  139. self.nodes.forEach(function(n) {
  140. nodeMap[n.userId] = { userId: n.userId, children: [] }
  141. if (n.level === 1) l1Ids.push(n.userId)
  142. })
  143. self.nodes.forEach(function(n) {
  144. if (!nodeMap[n.userId]) return
  145. nodeMap[n.userId] = Object.assign({}, nodeMap[n.userId], n)
  146. if (n.parentId && nodeMap[n.parentId]) {
  147. nodeMap[n.parentId].children.push(nodeMap[n.userId])
  148. }
  149. })
  150. // 按 userId 排序
  151. var sortNodes = function(arr) {
  152. arr.sort(function(a, b) { return (a.userId || 0) - (b.userId || 0) })
  153. arr.forEach(function(c) { sortNodes(c.children) })
  154. }
  155. sortNodes(l1Ids.map(function(id) { return nodeMap[id] }))
  156. self.level1Roots = l1Ids.map(function(id) { return nodeMap[id] })
  157. },
  158. fenToYuan(fen) {
  159. if (fen === null || fen === undefined) return '0.00'
  160. var num = Number(fen)
  161. if (isNaN(num)) return '0.00'
  162. return (num / 100).toFixed(2)
  163. }
  164. }
  165. }
  166. </script>
  167. <style scoped>
  168. .container { min-height: 100vh; background: #FFF7ED; padding-bottom: 40rpx; }
  169. .stats-card {
  170. background: #fff; border-radius: 20rpx; padding: 30rpx; margin: 20rpx 30rpx 16rpx;
  171. display: flex; align-items: center; box-shadow: 0 2rpx 12rpx rgba(249,115,22,0.08);
  172. }
  173. .stat-item { flex: 1; text-align: center; }
  174. .stat-value { font-size: 36rpx; font-weight: bold; display: block; }
  175. .stat-value.orange { color: #F97316; }
  176. .stat-value.blue { color: #0EA5E9; }
  177. .stat-value.green { color: #10B981; }
  178. .stat-label { font-size: 22rpx; color: #94A3B8; margin-top: 4rpx; display: block; }
  179. .stat-divider { width: 1rpx; height: 48rpx; background: #FED7AA; }
  180. .level-tabs {
  181. display: flex; padding: 0 30rpx; margin-bottom: 16rpx; overflow-x: auto; gap: 12rpx;
  182. white-space: nowrap;
  183. }
  184. .level-tab {
  185. flex-shrink: 0; padding: 12rpx 24rpx; border-radius: 20rpx;
  186. font-size: 24rpx; color: #64748B; background: #F1F5F9;
  187. }
  188. .level-tab.active { background: #F97316; color: #fff; font-weight: 600; }
  189. .tree-wrapper { padding: 0 30rpx; }
  190. .tree-container { min-height: 300rpx; }
  191. .tree-node { margin-bottom: 16rpx; }
  192. .node-main {
  193. display: flex; align-items: center; gap: 16rpx;
  194. background: #fff; border-radius: 16rpx; padding: 20rpx;
  195. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  196. }
  197. .node-avatar { width: 60rpx; height: 60rpx; border-radius: 50%; background: #FEF3C7; flex-shrink: 0; }
  198. .node-avatar-text {
  199. width: 60rpx; height: 60rpx; border-radius: 50%; background: #FEF3C7;
  200. display: flex; align-items: center; justify-content: center;
  201. font-size: 24rpx; color: #F97316; flex-shrink: 0;
  202. }
  203. .node-name { flex: 1; min-width: 0; font-size: 26rpx; color: #1E293B; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  204. .node-tag { font-size: 20rpx; background: #FFF7ED; color: #F97316; padding: 2rpx 8rpx; border-radius: 8rpx; flex-shrink: 0; }
  205. .node-stats { flex-shrink: 0; text-align: right; margin-right: 8rpx; }
  206. .node-stat { font-size: 20rpx; display: block; line-height: 1.5; }
  207. .node-stat.consumption { color: #F97316; }
  208. .node-stat.my-cf { color: #0EA5E9; }
  209. .node-arrow { font-size: 20rpx; color: #94A3B8; flex-shrink: 0; }
  210. .children { margin-top: 12rpx; padding-left: 32rpx; border-left: 1rpx dashed #FED7AA; }
  211. .empty-state { text-align: center; padding: 80rpx; color: #94A3B8; font-size: 28rpx; }
  212. .loading-overlay {
  213. position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  214. background: rgba(255,255,255,0.85); display: flex; align-items: center; justify-content: center;
  215. z-index: 999; font-size: 28rpx; color: #94A3B8;
  216. }
  217. </style>