team-tree.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. <view class="level-counts" v-if="!loading && levelCounts.length > 0">
  21. <view class="level-count-chip" v-for="lc in levelCounts" :key="lc.level">
  22. <text class="level-count-num">L{{ lc.level }}</text>
  23. <text class="level-count-people">{{ lc.count }}人</text>
  24. </view>
  25. </view>
  26. <!-- 层级切换 tabs -->
  27. <view class="level-tabs">
  28. <view class="level-tab" :class="{ active: currentMaxLevel === 1 }" @click="switchLevel(1)">1级</view>
  29. <view class="level-tab" :class="{ active: currentMaxLevel === 2 }" @click="switchLevel(2)">2级</view>
  30. <view class="level-tab" :class="{ active: currentMaxLevel === 3 }" @click="switchLevel(3)">3级</view>
  31. <view class="level-tab" :class="{ active: currentMaxLevel === 4 }" @click="switchLevel(4)">4级</view>
  32. <view class="level-tab" :class="{ active: currentMaxLevel === 5 }" @click="switchLevel(5)">5级</view>
  33. </view>
  34. <!-- 树形区域:扁平渲染,depth 控制缩进 -->
  35. <view class="tree-wrapper">
  36. <view class="tree-container" v-if="displayNodes.length > 0">
  37. <view v-for="item in displayNodes" :key="item.userId" class="tree-node"
  38. :style="{ paddingLeft: (item.depth * 32) + 'rpx' }">
  39. <view class="node-main" @click="item.hasChildren && toggleExpand(item.userId)">
  40. <image v-if="item.avatar" class="node-avatar" :src="item.avatar" mode="aspectFill" />
  41. <text v-else class="node-avatar-text">{{ item.nickname ? item.nickname.charAt(0) : '?' }}</text>
  42. <text class="node-name">{{ item.nickname }}</text>
  43. <text class="node-tag" v-if="item.level > 1">L{{ item.level }}</text>
  44. <view class="node-stats">
  45. <text class="node-stat consumption">消费 ¥{{ fenToYuan(item.memberConsumption) }}</text>
  46. <text class="node-stat my-cf">得CF ¥{{ fenToYuan(item.myCfEarnedFromMember) }}</text>
  47. </view>
  48. <text class="node-arrow" v-if="item.hasChildren">{{ expandedMap[item.userId] ? '▲' : '▼' }}</text>
  49. </view>
  50. </view>
  51. </view>
  52. <view class="empty-state" v-if="!loading && displayNodes.length === 0">
  53. <text>暂无团队成员</text>
  54. </view>
  55. </view>
  56. <view class="loading-overlay" v-if="loading">
  57. <text>加载中...</text>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import { getTeamTree, getTeamConsumption } from '@/utils/api.js'
  63. export default {
  64. data() {
  65. return {
  66. nodes: [],
  67. levelCounts: [],
  68. consumption: { totalConsumption: 0, totalMembers: 0, maxLevel: 3 },
  69. currentMaxLevel: 3,
  70. loading: false,
  71. expandedMap: {}
  72. }
  73. },
  74. computed: {
  75. displayNodes() {
  76. var self = this
  77. if (this.nodes.length === 0) return []
  78. var nodeMap = {}
  79. var i, n, roots = [], result = []
  80. for (i = 0; i < this.nodes.length; i++) {
  81. n = this.nodes[i]
  82. nodeMap[n.userId] = {
  83. userId: n.userId, nickname: n.nickname, avatar: n.avatar,
  84. memberConsumption: n.memberConsumption, myCfEarnedFromMember: n.myCfEarnedFromMember,
  85. level: n.level, _children: []
  86. }
  87. }
  88. for (i = 0; i < this.nodes.length; i++) {
  89. n = this.nodes[i]
  90. if (n.parentId && nodeMap[n.parentId] && nodeMap[n.userId]) {
  91. nodeMap[n.parentId]._children.push(nodeMap[n.userId])
  92. }
  93. }
  94. for (i = 0; i < this.nodes.length; i++) {
  95. if (this.nodes[i].level === 1 && nodeMap[this.nodes[i].userId]) {
  96. roots.push(nodeMap[this.nodes[i].userId])
  97. }
  98. }
  99. function flatten(list, depth) {
  100. for (var j = 0; j < list.length; j++) {
  101. var node = list[j]
  102. var hasChildren = node._children.length > 0
  103. result.push({
  104. userId: node.userId, nickname: node.nickname, avatar: node.avatar,
  105. memberConsumption: node.memberConsumption, myCfEarnedFromMember: node.myCfEarnedFromMember,
  106. level: node.level, depth: depth, hasChildren: hasChildren, childCount: node._children.length
  107. })
  108. if (hasChildren && self.expandedMap[node.userId]) {
  109. flatten(node._children, depth + 1)
  110. }
  111. }
  112. }
  113. flatten(roots, 0)
  114. return result
  115. }
  116. },
  117. onLoad() {
  118. this.loadData()
  119. this._onLoadFired = true
  120. },
  121. onShow() {
  122. if (this._onLoadFired) {
  123. this._onLoadFired = false
  124. } else {
  125. this.loadData()
  126. }
  127. },
  128. methods: {
  129. async loadData() {
  130. this.loading = true
  131. try {
  132. var self = this
  133. var treeRes = await getTeamTree(self.currentMaxLevel)
  134. var consumptionRes = await getTeamConsumption(self.currentMaxLevel)
  135. if (treeRes && treeRes.data && treeRes.data.nodes) {
  136. self.nodes = treeRes.data.nodes
  137. }
  138. if (consumptionRes && consumptionRes.data) {
  139. self.consumption = consumptionRes.data
  140. self.consumption.maxLevel = self.consumption.maxLevel || self.currentMaxLevel
  141. }
  142. self.buildLevelCounts()
  143. } catch (e) {
  144. console.error('加载团队树失败', e)
  145. }
  146. this.loading = false
  147. },
  148. buildLevelCounts() {
  149. var max = this.consumption.maxLevel || this.currentMaxLevel || 3
  150. var counts = []
  151. var i
  152. for (i = 1; i <= max; i++) counts.push({ level: i, count: 0 })
  153. this.nodes.forEach(function(n) {
  154. if (n && n.level && n.level >= 1 && n.level <= max) {
  155. counts[n.level - 1].count++
  156. }
  157. })
  158. this.levelCounts = counts
  159. },
  160. switchLevel(level) {
  161. if (this.currentMaxLevel === level) return
  162. this.currentMaxLevel = level
  163. this.loadData()
  164. },
  165. toggleExpand(userId) {
  166. this.$set(this.expandedMap, userId, !this.expandedMap[userId])
  167. },
  168. fenToYuan(fen) {
  169. if (fen === null || fen === undefined) return '0.00'
  170. var num = Number(fen)
  171. if (isNaN(num)) return '0.00'
  172. return (num / 100).toFixed(2)
  173. }
  174. }
  175. }
  176. </script>
  177. <style scoped>
  178. .container { min-height: 100vh; background: #FFF7ED; padding-bottom: 40rpx; }
  179. .stats-card {
  180. background: #fff; border-radius: 20rpx; padding: 30rpx; margin: 20rpx 30rpx 16rpx;
  181. display: flex; align-items: center; box-shadow: 0 2rpx 12rpx rgba(249,115,22,0.08);
  182. }
  183. .stat-item { flex: 1; text-align: center; }
  184. .stat-value { font-size: 36rpx; font-weight: bold; display: block; }
  185. .stat-value.orange { color: #F97316; }
  186. .stat-value.blue { color: #0EA5E9; }
  187. .stat-value.green { color: #10B981; }
  188. .stat-label { font-size: 22rpx; color: #94A3B8; margin-top: 4rpx; display: block; }
  189. .stat-divider { width: 1rpx; height: 48rpx; background: #FED7AA; }
  190. .level-tabs {
  191. display: flex; padding: 0 30rpx; margin-bottom: 16rpx; overflow-x: auto; gap: 12rpx;
  192. white-space: nowrap;
  193. }
  194. .level-tab {
  195. flex-shrink: 0; padding: 12rpx 24rpx; border-radius: 20rpx;
  196. font-size: 24rpx; color: #64748B; background: #F1F5F9;
  197. }
  198. .level-tab.active { background: #F97316; color: #fff; font-weight: 600; }
  199. .tree-wrapper { padding: 0 30rpx; }
  200. .tree-container { min-height: 300rpx; }
  201. .tree-node { margin-bottom: 16rpx; }
  202. .node-main {
  203. display: flex; align-items: center; gap: 16rpx;
  204. background: #fff; border-radius: 16rpx; padding: 20rpx;
  205. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  206. }
  207. .node-avatar { width: 60rpx; height: 60rpx; border-radius: 50%; background: #FEF3C7; flex-shrink: 0; }
  208. .node-avatar-text {
  209. width: 60rpx; height: 60rpx; border-radius: 50%; background: #FEF3C7;
  210. display: flex; align-items: center; justify-content: center;
  211. font-size: 24rpx; color: #F97316; flex-shrink: 0;
  212. }
  213. .node-name { flex: 1; min-width: 0; font-size: 26rpx; color: #1E293B; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  214. .node-tag { font-size: 20rpx; background: #FFF7ED; color: #F97316; padding: 2rpx 8rpx; border-radius: 8rpx; flex-shrink: 0; }
  215. .node-stats { flex-shrink: 0; text-align: right; margin-right: 8rpx; }
  216. .node-stat { font-size: 20rpx; display: block; line-height: 1.5; }
  217. .node-stat.consumption { color: #F97316; }
  218. .node-stat.my-cf { color: #0EA5E9; }
  219. .node-arrow { font-size: 20rpx; color: #94A3B8; flex-shrink: 0; }
  220. .empty-state { text-align: center; padding: 80rpx; color: #94A3B8; font-size: 28rpx; }
  221. .loading-overlay {
  222. position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  223. background: rgba(255,255,255,0.85); display: flex; align-items: center; justify-content: center;
  224. z-index: 999; font-size: 28rpx; color: #94A3B8;
  225. }
  226. .level-counts {
  227. display: flex; padding: 0 30rpx; margin-bottom: 16rpx; gap: 12rpx;
  228. flex-wrap: wrap; align-items: center;
  229. }
  230. .level-count-chip {
  231. flex-shrink: 0; display: flex; align-items: center; gap: 6rpx;
  232. background: #fff; border-radius: 16rpx; padding: 10rpx 20rpx;
  233. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05);
  234. }
  235. .level-count-num { font-size: 24rpx; font-weight: 600; color: #F97316; }
  236. .level-count-people { font-size: 24rpx; color: #64748B; }
  237. </style>