OctopusDiagram.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. <template>
  2. <!-- 章鱼图 - 基于 Canvas force-directed 物理引擎 -->
  3. <view class="octopus-graph-wrapper" v-if="tentacles && tentacles.length > 0" :style="{ minHeight: canvasHeight + 'px' }">
  4. <canvas
  5. class="octopus-graph-canvas"
  6. canvas-id="octopusGraphCanvas"
  7. id="octopusGraphCanvas"
  8. type="2d"
  9. :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
  10. @touchstart="onTouchStart"
  11. @touchmove="onTouchMove"
  12. @touchend="onTouchEnd" />
  13. <!-- 点击节点时显示详情弹窗 -->
  14. <view class="octopus-detail-popup" v-if="selectedTentacle" :style="popupPosition">
  15. <view class="popup-content">
  16. <view class="popup-header">
  17. <image class="popup-avatar" :src="selectedTentacle.avatar || defaultAvatar" mode="aspectFill" />
  18. <view class="popup-info">
  19. <text class="popup-name">{{ selectedTentacle.name }}</text>
  20. <text class="popup-relation" v-if="selectedTentacle.relationshipType">{{ selectedTentacle.relationshipType }}</text>
  21. </view>
  22. </view>
  23. <view class="popup-stats">
  24. <view class="stat-item">
  25. <text class="stat-value">{{ selectedTentacle.helpCount }}</text>
  26. <text class="stat-label">次数</text>
  27. </view>
  28. <view class="stat-divider"></view>
  29. <view class="stat-item">
  30. <text class="stat-value">¥{{ formatAmount(selectedTentacle.totalAmount) }}</text>
  31. <text class="stat-label">金额</text>
  32. </view>
  33. <view class="stat-divider"></view>
  34. <view class="stat-item">
  35. <text class="stat-value">{{ selectedTentacle.mainHelpTypeName }}</text>
  36. <text class="stat-label">主导类型</text>
  37. </view>
  38. </view>
  39. <view class="popup-actions">
  40. <text class="action-btn" @tap="viewRecords(selectedTentacle.contactId)">查看明细</text>
  41. </view>
  42. <view class="popup-arrow" :style="arrowPosition"></view>
  43. </view>
  44. </view>
  45. </view>
  46. <!-- 空状态 -->
  47. <view class="octopus-graph-empty" v-else-if="tentacles && tentacles.length === 0">
  48. <text class="empty-icon">🐙</text>
  49. <text class="empty-text">暂无章鱼图数据</text>
  50. <text class="empty-hint">记录谁帮助过你,画出你的触手图</text>
  51. <text class="action-btn" @tap="goAddHelp">+ 记录一次帮助</text>
  52. </view>
  53. <!-- 加载状态 -->
  54. <view class="octopus-loading" v-else>
  55. <text class="loading-text">加载章鱼图...</text>
  56. </view>
  57. </template>
  58. <script>
  59. /**
  60. * OctopusDiagram - 章鱼图 Canvas 组件
  61. *
  62. * 复用 FamilyRelationGraph 的 force-directed 物理引擎:
  63. * - 中心节点 = 当前用户(固定不动)
  64. * - 触手节点 = 帮助过我的人(分布在四周)
  65. * - 节点大小 = 帮助次数
  66. * - 节点颜色 = 主导帮助类型
  67. * - 触手末端标注 = 帮助金额
  68. * - 点击节点 → 显示帮助明细
  69. *
  70. * 数据源:GET /api/octopus/tentacles
  71. * 返回字段:contactId, name, avatar, relationshipType, helpCount, totalAmount,
  72. * lastHelpedAt, mainHelpType, mainHelpTypeName, radius
  73. */
  74. export default {
  75. name: 'OctopusDiagram',
  76. props: {
  77. /** 当前用户ID(可从 storage 获取,也可由父组件传入) */
  78. selfId: {
  79. type: [Number, String],
  80. default: null
  81. },
  82. /** 画布宽度 rpx */
  83. canvasWidth: {
  84. type: Number,
  85. default: 340
  86. },
  87. /** 画布高度 rpx */
  88. canvasHeight: {
  89. type: Number,
  90. default: 280
  91. },
  92. /** 是否允许交互(点击节点查看明细) */
  93. interactive: {
  94. type: Boolean,
  95. default: true
  96. }
  97. },
  98. data: function() {
  99. return {
  100. dpr: 1,
  101. // 物理参数
  102. physics: {
  103. repulsion: 600, // 节点间斥力强度
  104. springLength: 140, // 弹簧自然长度(中心到触手距离)
  105. springStrength: 0.05, // 弹簧劲度系数
  106. centering: 0.02, // 中心引力
  107. damping: 0.90, // 速度阻尼
  108. maxSpeed: 10, // 最大速度
  109. minDistance: 40 // 最小节点距离
  110. },
  111. // 动画状态
  112. dragging: null, // { nodeIndex, offsetX, offsetY }
  113. dragStartPos: null,
  114. animFrameId: null,
  115. isSimulating: false,
  116. stillFrames: 0,
  117. energyThreshold: 1.5,
  118. stillFrameLimit: 30,
  119. // 节点数据
  120. nodeData: [],
  121. // 边数据(中心到每个触手)
  122. edges: [],
  123. // 触手数据(从 API 获取)
  124. tentacles: [],
  125. // 选中的触手(用于显示弹窗)
  126. selectedTentacle: null,
  127. // 默认头像
  128. defaultAvatar: '/static/default-avatar.png',
  129. // 是否销毁
  130. _destroyed: false,
  131. // 类型颜色映射
  132. typeColors: {
  133. MONEY: '#FF8C42', // 橙 - 金钱
  134. ITEM: '#10B981', // 绿 - 物质
  135. EMOTION: '#FF6B9D', // 粉 - 情感
  136. RESOURCE: '#6366F1', // 蓝 - 资源
  137. SKILL: '#F59E0B', // 金 - 技能
  138. TIME: '#8B5CF6', // 紫 - 时间
  139. OTHER: '#94A3B8' // 灰 - 其他
  140. }
  141. }
  142. },
  143. computed: {
  144. // 中心节点(自己)
  145. centerNode: function() {
  146. return this.nodeData.find(function(n) { return n.isCenter })
  147. }
  148. },
  149. watch: {
  150. tentacles: {
  151. handler: function() {
  152. this.buildNodesAndEdges()
  153. this.resetSimulation()
  154. },
  155. immediate: true,
  156. deep: true
  157. },
  158. canvasWidth: function() {
  159. this.resetSimulation()
  160. },
  161. canvasHeight: function() {
  162. this.resetSimulation()
  163. }
  164. },
  165. mounted: function() {
  166. this._destroyed = false
  167. var self = this
  168. try {
  169. this.dpr = uni.getWindowInfo().pixelRatio || 1
  170. } catch (e) {
  171. this.dpr = 1
  172. }
  173. // 延迟初始化 canvas(等待 DOM 布局完成)
  174. var retryCount = 0
  175. var maxRetry = 5
  176. function tryInit() {
  177. if (self._destroyed) return
  178. self.initCanvas(function(success) {
  179. if (self._destroyed) return
  180. if (!success && retryCount < maxRetry) {
  181. retryCount++
  182. setTimeout(tryInit, 60)
  183. } else if (!success) {
  184. console.log('[OctopusDiagram] canvas 初始化失败,重试 ' + maxRetry + ' 次后放弃')
  185. }
  186. })
  187. }
  188. this.$nextTick(function() {
  189. if (self._destroyed) return
  190. self.updateCanvasSize()
  191. self.$nextTick(function() {
  192. if (!self._destroyed) {
  193. tryInit()
  194. }
  195. })
  196. })
  197. },
  198. beforeDestroy: function() {
  199. this._destroyed = true
  200. this.stopSimulation()
  201. },
  202. methods: {
  203. /* ===========================
  204. * 数据构建
  205. * =========================== */
  206. buildNodesAndEdges: function() {
  207. if (!this.tentacles || this.tentacles.length === 0) {
  208. this.nodeData = []
  209. this.edges = []
  210. return
  211. }
  212. var centerX = this.canvasWidth / 2
  213. var centerY = this.canvasHeight / 2
  214. var self = this
  215. // 1. 中心节点(自己)
  216. var centerNode = {
  217. id: 'center',
  218. isCenter: true,
  219. nickname: '我',
  220. displayName: '我',
  221. radius: 32,
  222. x: centerX,
  223. y: centerY,
  224. vx: 0,
  225. vy: 0,
  226. fx: 0,
  227. fy: 0,
  228. fixed: true
  229. }
  230. // 2. 触手节点(帮助过我的人)
  231. var tentacleNodes = this.tentacles.map(function(t, idx) {
  232. // 初始位置:圆形分布
  233. var angle = (2 * Math.PI * idx) / self.tentacles.length
  234. var r = Math.min(self.canvasWidth, self.canvasHeight) * 0.38
  235. var initX = centerX + r * Math.cos(angle)
  236. var initY = centerY + r * Math.sin(angle)
  237. return {
  238. id: 'tentacle_' + t.contactId,
  239. isCenter: false,
  240. contactId: t.contactId,
  241. nickname: t.name,
  242. displayName: t.name ? t.name.charAt(0) : '?',
  243. avatar: t.avatar,
  244. relationshipType: t.relationshipType,
  245. helpCount: t.helpCount,
  246. totalAmount: t.totalAmount,
  247. mainHelpType: t.mainHelpType,
  248. mainHelpTypeName: t.mainHelpTypeName,
  249. radius: t.radius || 28,
  250. color: self.typeColors[t.mainHelpType] || self.typeColors.OTHER,
  251. x: initX,
  252. y: initY,
  253. vx: 0,
  254. vy: 0,
  255. fx: 0,
  256. fy: 0,
  257. fixed: false
  258. }
  259. })
  260. this.nodeData = [centerNode].concat(tentacleNodes)
  261. // 3. 边:中心连接到每个触手
  262. this.edges = tentacleNodes.map(function(n) {
  263. return {
  264. source: centerNode,
  265. target: n,
  266. helpCount: n.helpCount,
  267. totalAmount: n.totalAmount
  268. }
  269. })
  270. },
  271. /* ===========================
  272. * 物理引擎
  273. * =========================== */
  274. resetSimulation: function() {
  275. this.stopSimulation()
  276. // 重新初始化位置
  277. var centerX = this.canvasWidth / 2
  278. var centerY = this.canvasHeight / 2
  279. var self = this
  280. this.nodeData.forEach(function(n, idx) {
  281. if (!n.isCenter) {
  282. var angle = (2 * Math.PI * (idx - 1)) / Math.max(1, self.nodeData.length - 1)
  283. var r = Math.min(self.canvasWidth, self.canvasHeight) * 0.38
  284. n.x = centerX + r * Math.cos(angle)
  285. n.y = centerY + r * Math.sin(angle)
  286. } else {
  287. n.x = centerX
  288. n.y = centerY
  289. }
  290. n.vx = 0
  291. n.vy = 0
  292. n.fx = 0
  293. n.fy = 0
  294. })
  295. this.startSimulation()
  296. },
  297. initSimulation: function() {
  298. if (!this.nodeData || this.nodeData.length === 0) return
  299. this.startSimulation()
  300. },
  301. startSimulation: function() {
  302. if (this.isSimulating) return
  303. this.isSimulating = true
  304. this.stillFrames = 0
  305. this.simulationTick()
  306. },
  307. stopSimulation: function() {
  308. this.isSimulating = false
  309. if (this.animFrameId) {
  310. clearTimeout(this.animFrameId)
  311. this.animFrameId = null
  312. }
  313. },
  314. simulationTick: function() {
  315. if (!this.isSimulating) return
  316. if (this._destroyed) {
  317. this.isSimulating = false
  318. return
  319. }
  320. var nodes = this.nodeData
  321. var edges = this.edges
  322. var p = this.physics
  323. // 1. 清零力
  324. for (var i = 0; i < nodes.length; i++) {
  325. if (!nodes[i].fixed) {
  326. nodes[i].fx = 0
  327. nodes[i].fy = 0
  328. }
  329. }
  330. // 2. 节点间斥力(触手之间互斥)
  331. for (var i = 1; i < nodes.length; i++) { // 跳过中心节点(索引0)
  332. for (var j = i + 1; j < nodes.length; j++) {
  333. var dx = nodes[j].x - nodes[i].x
  334. var dy = nodes[j].y - nodes[i].y
  335. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  336. if (dist < p.minDistance) {
  337. var force = p.repulsion / (dist * dist)
  338. var fx = force * dx / dist
  339. var fy = force * dy / dist
  340. nodes[i].fx -= fx
  341. nodes[i].fy -= fy
  342. nodes[j].fx += fx
  343. nodes[j].fy += fy
  344. }
  345. }
  346. }
  347. // 3. 边弹簧引力(中心 -> 触手)
  348. for (var k = 0; k < edges.length; k++) {
  349. var e = edges[k]
  350. var dx = e.target.x - e.source.x
  351. var dy = e.target.y - e.source.y
  352. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  353. var force = p.springStrength * (dist - p.springLength)
  354. var fx = force * dx / dist
  355. var fy = force * dy / dist
  356. if (!e.target.fixed) {
  357. e.target.fx += fx
  358. e.target.fy += fy
  359. }
  360. }
  361. // 4. 中心引力(将触手轻轻拉向画布中心)
  362. for (var m = 1; m < nodes.length; m++) {
  363. var dx = (this.canvasWidth / 2) - nodes[m].x
  364. var dy = (this.canvasHeight / 2) - nodes[m].y
  365. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  366. var force = p.centering * dist
  367. if (!nodes[m].fixed) {
  368. nodes[m].fx += force * dx / dist
  369. nodes[m].fy += force * dy / dist
  370. }
  371. }
  372. // 5. 更新速度和位置
  373. var totalEnergy = 0
  374. for (var n = 1; n < nodes.length; n++) { // 跳过中心节点
  375. if (nodes[n].fixed) continue
  376. nodes[n].vx = (nodes[n].vx + nodes[n].fx) * p.damping
  377. nodes[n].vy = (nodes[n].vy + nodes[n].fy) * p.damping
  378. var speed = Math.sqrt(nodes[n].vx * nodes[n].vx + nodes[n].vy * nodes[n].vy)
  379. if (speed > p.maxSpeed) {
  380. nodes[n].vx = nodes[n].vx / speed * p.maxSpeed
  381. nodes[n].vy = nodes[n].vy / speed * p.maxSpeed
  382. }
  383. nodes[n].x += nodes[n].vx
  384. nodes[n].y += nodes[n].vy
  385. totalEnergy += speed * speed
  386. }
  387. // 6. 渲染
  388. this._doRender()
  389. // 7. 收敛检测
  390. if (totalEnergy < p.energyThreshold) {
  391. this.stillFrames++
  392. } else {
  393. this.stillFrames = 0
  394. }
  395. if (this.stillFrames >= p.stillFrameLimit) {
  396. this.stopSimulation()
  397. return
  398. }
  399. // 8. 下一帧
  400. var self = this
  401. this.animFrameId = setTimeout(function() {
  402. self.simulationTick()
  403. }, 16)
  404. },
  405. /* ===========================
  406. * 渲染
  407. * =========================== */
  408. _doRender: function() {
  409. if (!this.ctx) { return }
  410. var ctx = this.ctx
  411. var w = this.canvasWidth
  412. var h = this.canvasHeight
  413. ctx.clearRect(0, 0, w, h)
  414. // 1. 绘制边(触手线条)
  415. this._drawEdges(ctx)
  416. // 2. 绘制节点
  417. for (var i = 0; i < this.nodeData.length; i++) {
  418. this._drawNode(ctx, this.nodeData[i])
  419. }
  420. },
  421. _drawEdges: function(ctx) {
  422. var self = this
  423. this.edges.forEach(function(e) {
  424. var source = e.source
  425. var target = e.target
  426. // 贝塞尔曲线模拟触手弯曲
  427. var cx = (source.x + target.x) / 2
  428. var cy = (source.y + target.y) / 2
  429. // 偏移控制点,制造弯曲感
  430. var offsetX = (target.y - source.y) * 0.15
  431. var offsetY = -(target.x - source.x) * 0.15
  432. ctx.beginPath()
  433. ctx.moveTo(source.x, source.y)
  434. ctx.quadraticCurveTo(cx + offsetX, cy + offsetY, target.x, target.y)
  435. // 线条宽度随帮助次数变化
  436. var lineWidth = 2 + Math.min(4, e.helpCount * 0.5)
  437. ctx.lineWidth = lineWidth
  438. // 线条颜色用目标节点颜色,透明度递减
  439. ctx.strokeStyle = self._hexToRgba(target.color, 0.4)
  440. ctx.stroke()
  441. // 金额标注(在触手末端附近)
  442. if (e.totalAmount && e.totalAmount > 0) {
  443. var labelX = (source.x + target.x) / 2
  444. var labelY = (source.y + target.y) / 2 - 20
  445. ctx.font = '12px PingFang SC, sans-serif'
  446. ctx.fillStyle = '#FF8C42'
  447. ctx.textAlign = 'center'
  448. ctx.fillText('¥' + self._formatAmountShort(e.totalAmount), labelX, labelY)
  449. }
  450. })
  451. },
  452. _drawNode: function(ctx, node) {
  453. var r = node.radius
  454. var x = node.x
  455. var y = node.y
  456. // 裁剪到画布范围
  457. if (x - r > this.canvasWidth || x + r < 0 || y - r > this.canvasHeight || y + r < 0) {
  458. return
  459. }
  460. if (node.isCenter) {
  461. // 中心节点:实心圆 + "我" 字
  462. ctx.beginPath()
  463. ctx.arc(x, y, r, 0, Math.PI * 2)
  464. ctx.fillStyle = '#10B981'
  465. ctx.fill()
  466. ctx.strokeStyle = '#FFFFFF'
  467. ctx.lineWidth = 2
  468. ctx.stroke()
  469. ctx.font = 'bold 18px PingFang SC, sans-serif'
  470. ctx.fillStyle = '#FFFFFF'
  471. ctx.textAlign = 'center'
  472. ctx.textBaseline = 'middle'
  473. ctx.fillText('我', x, y + 1)
  474. } else {
  475. // 触手节点:实心圆 + 首字
  476. ctx.beginPath()
  477. ctx.arc(x, y, r, 0, Math.PI * 2)
  478. ctx.fillStyle = node.color
  479. ctx.fill()
  480. // 白边
  481. ctx.strokeStyle = '#FFFFFF'
  482. ctx.lineWidth = 2
  483. ctx.stroke()
  484. // 首字
  485. ctx.font = 'bold ' + Math.max(14, r * 0.7) + 'px PingFang SC, sans-serif'
  486. ctx.fillStyle = '#FFFFFF'
  487. ctx.textAlign = 'center'
  488. ctx.textBaseline = 'middle'
  489. ctx.fillText(node.displayName, x, y + 1)
  490. }
  491. },
  492. /* ===========================
  493. * 交互
  494. * =========================== */
  495. _hitTest: function(x, y) {
  496. // 从后往前测试,优先触手节点
  497. for (var i = this.nodeData.length - 1; i >= 0; i--) {
  498. var node = this.nodeData[i]
  499. var dx = x - node.x
  500. var dy = y - node.y
  501. if (dx * dx + dy * dy <= node.radius * node.radius) {
  502. return i
  503. }
  504. }
  505. return -1
  506. },
  507. onTouchStart: function(e) {
  508. if (!this.interactive) return
  509. var touches = e.touches || e.changedTouches
  510. if (!touches || touches.length === 0) return
  511. var touch = touches[0]
  512. var canvasRect = this._getCanvasRect()
  513. if (!canvasRect) return
  514. var x = touch.clientX - canvasRect.left
  515. var y = touch.clientY - canvasRect.top
  516. var hitIndex = this._hitTest(x, y)
  517. if (hitIndex >= 0 && !this.nodeData[hitIndex].isCenter) {
  518. this.dragging = {
  519. nodeIndex: hitIndex,
  520. offsetX: x - this.nodeData[hitIndex].x,
  521. offsetY: y - this.nodeData[hitIndex].y
  522. }
  523. this.dragStartPos = { x: x, y: y }
  524. this.nodeData[hitIndex].fixed = true
  525. } else if (hitIndex >= 0 && this.nodeData[hitIndex].isCenter) {
  526. // 点击中心节点不拖拽
  527. } else {
  528. // 点击空白处关闭弹窗
  529. this.selectedTentacle = null
  530. }
  531. },
  532. onTouchMove: function(e) {
  533. if (!this.dragging) return
  534. var touches = e.touches || e.changedTouches
  535. if (!touches || touches.length === 0) return
  536. var touch = touches[0]
  537. var canvasRect = this._getCanvasRect()
  538. if (!canvasRect) return
  539. var x = touch.clientX - canvasRect.left
  540. var y = touch.clientY - canvasRect.top
  541. var node = this.nodeData[this.dragging.nodeIndex]
  542. node.x = x - this.dragging.offsetX
  543. node.y = y - this.dragging.offsetY
  544. node.vx = 0
  545. node.vy = 0
  546. if (!this.isSimulating) {
  547. this._doRender()
  548. }
  549. },
  550. onTouchEnd: function(e) {
  551. if (this.dragging) {
  552. var node = this.nodeData[this.dragging.nodeIndex]
  553. node.fixed = false
  554. this.dragging = null
  555. // 判断是否点击(移动距离很小)
  556. if (this.dragStartPos) {
  557. var touches = e.changedTouches
  558. if (touches && touches.length > 0) {
  559. var canvasRect = this._getCanvasRect()
  560. if (canvasRect) {
  561. var x = touches[0].clientX - canvasRect.left
  562. var y = touches[0].clientY - canvasRect.top
  563. var dx = x - this.dragStartPos.x
  564. var dy = y - this.dragStartPos.y
  565. if (dx * dx + dy * dy < 100) { // 10px 以内算点击
  566. this._onNodeClick(this.dragging.nodeIndex)
  567. }
  568. }
  569. }
  570. }
  571. if (!this.isSimulating) {
  572. this.startSimulation()
  573. }
  574. }
  575. },
  576. _onNodeClick: function(nodeIndex) {
  577. if (nodeIndex <= 0) return // 中心节点不响应
  578. var node = this.nodeData[nodeIndex]
  579. if (!node.isCenter) {
  580. // 找到对应的触手数据
  581. var tentacle = this.tentacles.find(function(t) { return t.contactId == node.contactId })
  582. if (tentacle) {
  583. this.selectedTentacle = Object.assign({}, tentacle, {
  584. contactId: node.contactId
  585. })
  586. }
  587. }
  588. },
  589. viewRecords: function(contactId) {
  590. this.$emit('view-records', contactId)
  591. },
  592. goAddHelp: function() {
  593. this.$emit('add-help')
  594. },
  595. /* ===========================
  596. * 工具方法
  597. * =========================== */
  598. _getCanvasRect: function() {
  599. if (this._canvasRect) {
  600. return {
  601. left: this._canvasRect.left,
  602. top: this._canvasRect.top,
  603. width: this.canvasWidth,
  604. height: this.canvasHeight
  605. }
  606. }
  607. return {
  608. left: 0,
  609. top: 0,
  610. width: this.canvasWidth,
  611. height: this.canvasHeight
  612. }
  613. },
  614. _hexToRgba: function(hex, alpha) {
  615. var r = parseInt(hex.slice(1, 3), 16)
  616. var g = parseInt(hex.slice(3, 5), 16)
  617. var b = parseInt(hex.slice(5, 7), 16)
  618. return 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')'
  619. },
  620. _formatAmountShort: function(amount) {
  621. var num = typeof amount === 'string' ? parseFloat(amount) : amount
  622. if (num >= 10000) {
  623. return (num / 10000).toFixed(1) + 'w'
  624. }
  625. return String(num)
  626. },
  627. formatAmount: function(amount) {
  628. var num = typeof amount === 'string' ? parseFloat(amount) : amount
  629. return num.toLocaleString(undefined, { minimumFractionDigits: 0, maximumFractionDigits: 2 })
  630. },
  631. /* ===========================
  632. * Canvas 初始化
  633. * =========================== */
  634. updateCanvasSize: function() {
  635. var self = this
  636. var query = uni.createSelectorQuery().in(this)
  637. query.select('.octopus-graph-wrapper').boundingClientRect(function(rect) {
  638. if (rect) {
  639. self.canvasWidth = rect.width || 340
  640. }
  641. }).exec()
  642. },
  643. initCanvas: function(cb) {
  644. var self = this
  645. uni.createSelectorQuery().in(this)
  646. .select('#' + 'octopusGraphCanvas')
  647. .fields({ node: true, size: true, rect: true })
  648. .exec(function(res) {
  649. try {
  650. if (self._destroyed) return
  651. if (!res || !res[0]) {
  652. if (cb) cb(false)
  653. return
  654. }
  655. var info = res[0]
  656. if (!info.node) {
  657. if (cb) cb(false)
  658. return
  659. }
  660. var canvas = info.node
  661. var width = info.width
  662. var height = info.height
  663. if (!width || !height) {
  664. var winWidth = 375
  665. try {
  666. var winInfo = uni.getWindowInfo()
  667. winWidth = winInfo.windowWidth || 375
  668. } catch (e2) {}
  669. width = Math.round((self.canvasWidth || 340) * winWidth / 750)
  670. height = Math.round((self.canvasHeight || 280) * winWidth / 750)
  671. }
  672. var pixelRatio = 2
  673. try {
  674. var system = uni.getWindowInfo()
  675. pixelRatio = system.pixelRatio || 2
  676. } catch (e) {}
  677. canvas.width = width * pixelRatio
  678. canvas.height = height * pixelRatio
  679. var ctx = canvas.getContext('2d')
  680. if (!ctx) {
  681. if (cb) cb(false)
  682. return
  683. }
  684. ctx.scale(pixelRatio, pixelRatio)
  685. self.ctx = ctx
  686. self._canvasNode = canvas
  687. self.canvasWidth = width
  688. self.canvasHeight = height
  689. self.dpr = pixelRatio
  690. self.canvasReady = true
  691. // 存储 canvas 在页面中的真实位置(触摸坐标转换用)
  692. if (info.rect) {
  693. self._canvasRect = { left: info.rect.left || 0, top: info.rect.top || 0 }
  694. }
  695. if (Array.isArray(self.tentacles) && self.tentacles.length > 0) {
  696. self.buildNodesAndEdges()
  697. self.startSimulation()
  698. } else {
  699. self._doRender()
  700. }
  701. if (cb) cb(true)
  702. } catch (e) {
  703. if (cb) cb(false)
  704. }
  705. })
  706. }
  707. }
  708. }
  709. </script>
  710. <style scoped>
  711. .octopus-graph-wrapper {
  712. position: relative;
  713. width: 100%;
  714. min-height: 200px;
  715. height: auto;
  716. margin: 10rpx 0;
  717. background: #FFFFFF;
  718. border-radius: 24rpx;
  719. overflow: hidden;
  720. }
  721. .octopus-graph-canvas {
  722. position: absolute;
  723. top: 0;
  724. left: 0;
  725. z-index: 1;
  726. }
  727. /* 详情弹窗 */
  728. .octopus-detail-popup {
  729. position: absolute;
  730. z-index: 10;
  731. pointer-events: none;
  732. transform: translate(-50%, -100%);
  733. margin-top: -16rpx;
  734. }
  735. .popup-content {
  736. background: #FFFFFF;
  737. border-radius: 16rpx;
  738. padding: 20rpx;
  739. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
  740. min-width: 200rpx;
  741. max-width: 280rpx;
  742. pointer-events: auto;
  743. }
  744. .popup-header {
  745. display: flex;
  746. align-items: center;
  747. margin-bottom: 16rpx;
  748. }
  749. .popup-avatar {
  750. width: 56rpx;
  751. height: 56rpx;
  752. border-radius: 50%;
  753. margin-right: 12rpx;
  754. }
  755. .popup-info {
  756. display: flex;
  757. flex-direction: column;
  758. }
  759. .popup-name {
  760. font-size: 28rpx;
  761. font-weight: 600;
  762. color: #1E293B;
  763. }
  764. .popup-relation {
  765. font-size: 22rpx;
  766. color: #64748B;
  767. margin-top: 2rpx;
  768. }
  769. .popup-stats {
  770. display: flex;
  771. justify-content: space-around;
  772. padding: 12rpx 0;
  773. border-top: 1rpx solid #F1F5F9;
  774. border-bottom: 1rpx solid #F1F5F9;
  775. margin-bottom: 16rpx;
  776. }
  777. .stat-item {
  778. display: flex;
  779. flex-direction: column;
  780. align-items: center;
  781. }
  782. .stat-value {
  783. font-size: 24rpx;
  784. font-weight: 700;
  785. color: #10B981;
  786. }
  787. .stat-label {
  788. font-size: 20rpx;
  789. color: #94A3B8;
  790. margin-top: 4rpx;
  791. }
  792. .stat-divider {
  793. width: 1rpx;
  794. height: 32rpx;
  795. background: linear-gradient(180deg, transparent, #E2E8F0, transparent);
  796. }
  797. .popup-actions {
  798. text-align: center;
  799. }
  800. .action-btn {
  801. display: inline-block;
  802. padding: 10rpx 24rpx;
  803. background: #10B981;
  804. color: #FFFFFF;
  805. border-radius: 20rpx;
  806. font-size: 24rpx;
  807. }
  808. .popup-arrow {
  809. position: absolute;
  810. bottom: -8rpx;
  811. left: 50%;
  812. margin-left: -8rpx;
  813. width: 0;
  814. height: 0;
  815. border-left: 8rpx solid transparent;
  816. border-right: 8rpx solid transparent;
  817. border-top: 8rpx solid #FFFFFF;
  818. }
  819. /* 空状态 */
  820. .octopus-graph-empty,
  821. .octopus-loading {
  822. display: flex;
  823. flex-direction: column;
  824. align-items: center;
  825. justify-content: center;
  826. padding: 60rpx 20rpx;
  827. background: #FFFFFF;
  828. border-radius: 24rpx;
  829. margin: 10rpx 0;
  830. }
  831. .empty-icon {
  832. font-size: 80rpx;
  833. margin-bottom: 16rpx;
  834. }
  835. .empty-text {
  836. font-size: 28rpx;
  837. font-weight: 600;
  838. color: #1E293B;
  839. margin-bottom: 8rpx;
  840. }
  841. .empty-hint {
  842. font-size: 24rpx;
  843. color: #94A3B8;
  844. margin-bottom: 24rpx;
  845. }
  846. .loading-text {
  847. font-size: 28rpx;
  848. color: #64748B;
  849. }
  850. </style>