FamilyRelationGraph.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. <template>
  2. <!-- 全 Canvas 关系图谱 - Force-directed 物理布局 -->
  3. <view class="family-graph-wrapper" v-if="members && members.length >= 1" :style="{ minHeight: canvasHeight + 'px' }">
  4. <!-- 右上角小齿轮 → 成员管理 -->
  5. <view class="gear-icon" @click="onManageTap" @tap.stop="onManageTap">
  6. <text class="gear-text">⚙</text>
  7. </view>
  8. <canvas
  9. class="family-graph-canvas"
  10. canvas-id="forceGraphCanvas"
  11. id="forceGraphCanvas"
  12. type="2d"
  13. :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
  14. @touchstart="onTouchStart"
  15. @touchmove="onTouchMove"
  16. @touchend="onTouchEnd" />
  17. <!-- + 添加成员提示(触摸自身节点时显示) -->
  18. <view class="add-hint" :class="{ 'add-hint-visible': touchingSelf }" :style="addHintPosition">
  19. <text class="add-hint-text">+</text>
  20. </view>
  21. </view>
  22. <!-- 空状态 -->
  23. <view class="family-graph-empty" v-else-if="members && members.length === 0">
  24. <text class="empty-text">暂无家庭成员</text>
  25. </view>
  26. </template>
  27. <script>
  28. /**
  29. * FamilyRelationGraph - 全 Canvas force-directed 家庭关系图
  30. *
  31. * 物理引擎: 手动实现的 force-directed 布局
  32. * - 节点间斥力 (repulsion)
  33. * - 边弹簧引力 (spring attraction)
  34. * - 中心引力 (centering)
  35. * - 阻尼 (damping)
  36. *
  37. * 渲染: 全 Canvas 绘制 (节点 + 连线)
  38. * 交互: 节点拖拽 + 点击检测
  39. */
  40. export default {
  41. name: 'FamilyRelationGraph',
  42. props: {
  43. dimensionCode: {
  44. type: String,
  45. required: true,
  46. validator: function(val) {
  47. return ['body', 'mind', 'action', 'home', 'wisdom'].indexOf(val) !== -1
  48. }
  49. },
  50. selfId: {
  51. type: [Number, String],
  52. default: null
  53. },
  54. members: {
  55. type: Array,
  56. default: function() { return [] }
  57. },
  58. energyMap: {
  59. type: Object,
  60. default: function() { return {} }
  61. },
  62. intimacyMap: {
  63. type: Object,
  64. default: function() { return {} }
  65. },
  66. interactive: {
  67. type: Boolean,
  68. default: true
  69. },
  70. compatibilityMap: {
  71. type: Object,
  72. default: function() { return {} }
  73. }
  74. },
  75. data: function() {
  76. return {
  77. canvasWidth: 340,
  78. canvasHeight: 280,
  79. dpr: 1,
  80. // 物理参数
  81. physics: {
  82. repulsion: 800, // 节点间斥力强度
  83. springLength: 120, // 弹簧自然长度
  84. springStrength: 0.04, // 弹簧劲度系数
  85. centering: 0.03, // 中心引力
  86. damping: 0.88, // 速度阻尼
  87. maxSpeed: 12, // 最大速度
  88. minDistance: 30 // 最小节点距离
  89. },
  90. // 拖拽状态
  91. dragging: null, // { nodeIndex, offsetX, offsetY }
  92. dragStartPos: null, // 用于判断是否真的拖拽了
  93. // 触摸/悬停状态(显示 + 号)
  94. touchingSelf: false,
  95. hoveringSelf: false,
  96. // 是否 PC 环境(h5 编译)
  97. isPC: false,
  98. // 动画
  99. animFrameId: null,
  100. isSimulating: false,
  101. // 主题色
  102. themeColors: {
  103. body: '#8D6E63',
  104. mind: '#FF6B35',
  105. action: '#4CAF50',
  106. home: '#5B9BD5',
  107. wisdom: '#FFD700'
  108. }
  109. }
  110. },
  111. computed: {
  112. themeColor: function() {
  113. return this.themeColors[this.dimensionCode] || '#8D6E63'
  114. },
  115. // + 提示位置(跟随中心节点偏移)
  116. addHintPosition: function() {
  117. var selfNode = this.nodeData.find(function(n) { return n.isSelf })
  118. if (!selfNode) return {}
  119. return {
  120. left: (selfNode.x + selfNode.radius * 0.7) + 'px',
  121. top: (selfNode.y - selfNode.radius * 0.5) + 'px'
  122. }
  123. },
  124. // 将 members 转换为 simNodes
  125. nodeData: function() {
  126. var self = this
  127. if (!this.members || this.members.length === 0) return []
  128. var selfMemberId = null
  129. var centerX = this.canvasWidth / 2
  130. var centerY = this.canvasHeight / 2
  131. return this.members.map(function(m, idx) {
  132. var fid = m.memberId || m.id
  133. if (fid == self.selfId) selfMemberId = fid
  134. var energy = self.getEnergy(fid)
  135. var radius = 24 + (Math.max(0, Math.min(100, energy)) / 100) * 16 // 24-40rpx
  136. var nick = m.nickname || m.name || '成员'
  137. if (nick === '用户') nick = '成员'
  138. var isSelf = (fid == self.selfId)
  139. var displayName = nick.charAt(0)
  140. // 圆形布局初始位置(避免初始重叠)
  141. var angle = (2 * Math.PI * idx) / self.members.length
  142. var r = Math.min(self.canvasWidth, self.canvasHeight) * 0.32
  143. var initX = centerX + r * Math.cos(angle)
  144. var initY = centerY + r * Math.sin(angle)
  145. return {
  146. id: fid,
  147. nickname: nick,
  148. displayName: displayName,
  149. isSelf: isSelf,
  150. memberType: m.memberType || 'child',
  151. radius: radius,
  152. x: initX,
  153. y: initY,
  154. vx: 0,
  155. vy: 0,
  156. fx: 0, // 固定 x (拖拽时)
  157. fy: 0 // 固定 y (拖拽时)
  158. }
  159. })
  160. },
  161. // 构建边 (self 连接到其他所有成员)
  162. edges: function() {
  163. var self = this
  164. var selfNode = this.nodeData.find(function(n) { return n.isSelf })
  165. if (!selfNode) return []
  166. var result = []
  167. this.nodeData.forEach(function(n) {
  168. if (n.id !== selfNode.id) {
  169. // 获取亲密度
  170. var pairKey = selfNode.id < n.id
  171. ? selfNode.id + '-' + n.id
  172. : n.id + '-' + selfNode.id
  173. var comp = self.compatibilityMap[pairKey]
  174. var trust = self.getIntimacy(n.id, 'trust')
  175. var comm = self.getIntimacy(n.id, 'communication')
  176. var closeness = self.getIntimacy(n.id, 'closeness')
  177. result.push({
  178. source: selfNode,
  179. target: n,
  180. // 边权重用于可视化
  181. trust: trust,
  182. communication: comm,
  183. closeness: closeness,
  184. compatibility: comp && comp.overallScore !== undefined ? comp.overallScore : null
  185. })
  186. }
  187. })
  188. return result
  189. }
  190. },
  191. watch: {
  192. members: function() {
  193. this.resetSimulation()
  194. },
  195. canvasWidth: function() {
  196. this.resetSimulation()
  197. },
  198. canvasHeight: function() {
  199. this.resetSimulation()
  200. }
  201. },
  202. mounted: function() {
  203. this._destroyed = false
  204. var self = this
  205. try {
  206. this.dpr = uni.getSystemInfoSync().pixelRatio || 1
  207. } catch (e) {
  208. this.dpr = 1
  209. }
  210. this.$nextTick(function() {
  211. if (self._destroyed) return
  212. self.updateCanvasSize()
  213. self.$nextTick(function() {
  214. if (!self._destroyed) {
  215. self.initSimulation()
  216. }
  217. })
  218. })
  219. },
  220. beforeDestroy: function() {
  221. this._destroyed = true
  222. this.stopSimulation()
  223. },
  224. methods: {
  225. // ===== 物理引擎 =====
  226. resetSimulation: function() {
  227. this.stopSimulation()
  228. // 重新初始化节点位置
  229. var centerX = this.canvasWidth / 2
  230. var centerY = this.canvasHeight / 2
  231. var self = this
  232. this.nodeData.forEach(function(n, idx) {
  233. var angle = (2 * Math.PI * idx) / self.nodeData.length
  234. var r = Math.min(self.canvasWidth, self.canvasHeight) * 0.32
  235. n.x = centerX + r * Math.cos(angle)
  236. n.y = centerY + r * Math.sin(angle)
  237. n.vx = 0
  238. n.vy = 0
  239. n.fx = 0
  240. n.fy = 0
  241. })
  242. this.startSimulation()
  243. },
  244. initSimulation: function() {
  245. if (!this.nodeData || this.nodeData.length === 0) return
  246. this.startSimulation()
  247. },
  248. // hex → rgba(小程序不支持 8 位 hex 颜色 #RRGGBBAA)
  249. hexToRgba: function(hex, alpha) {
  250. var r = parseInt(hex.slice(1, 3), 16)
  251. var g = parseInt(hex.slice(3, 5), 16)
  252. var b = parseInt(hex.slice(5, 7), 16)
  253. return 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')'
  254. },
  255. startSimulation: function() {
  256. if (this.isSimulating) return
  257. this.isSimulating = true
  258. this.simulationTick()
  259. },
  260. stopSimulation: function() {
  261. this.isSimulating = false
  262. if (this.animFrameId) {
  263. clearTimeout(this.animFrameId)
  264. this.animFrameId = null
  265. }
  266. },
  267. simulationTick: function() {
  268. if (!this.isSimulating) return
  269. if (this._destroyed) {
  270. this.isSimulating = false
  271. return
  272. }
  273. var self = this
  274. var nodes = this.nodeData
  275. var edges = this.edges
  276. var p = this.physics
  277. // 1. 清零力
  278. for (var i = 0; i < nodes.length; i++) {
  279. if (!nodes[i].fixed) {
  280. nodes[i].fx = 0
  281. nodes[i].fy = 0
  282. }
  283. }
  284. // 2. 斥力 (所有节点对之间)
  285. for (var a = 0; a < nodes.length; a++) {
  286. for (var b = a + 1; b < nodes.length; b++) {
  287. var dx = nodes[b].x - nodes[a].x
  288. var dy = nodes[b].y - nodes[a].y
  289. var distSq = dx * dx + dy * dy
  290. var dist = Math.sqrt(distSq) || 0.1
  291. // F = repulsion / dist^2
  292. var force = p.repulsion / distSq
  293. var fx = (dx / dist) * force
  294. var fy = (dy / dist) * force
  295. if (!nodes[a].fixed) {
  296. nodes[a].fx -= fx
  297. nodes[a].fy -= fy
  298. }
  299. if (!nodes[b].fixed) {
  300. nodes[b].fx += fx
  301. nodes[b].fy += fy
  302. }
  303. }
  304. }
  305. // 3. 弹簧引力 (边)
  306. for (var e = 0; e < edges.length; e++) {
  307. var edge = edges[e]
  308. var s = edge.source
  309. var t = edge.target
  310. var dx = t.x - s.x
  311. var dy = t.y - s.y
  312. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  313. var displacement = dist - p.springLength
  314. var force = p.springStrength * displacement
  315. var fx = (dx / dist) * force
  316. var fy = (dy / dist) * force
  317. if (!s.fixed) {
  318. s.fx += fx
  319. s.fy += fy
  320. }
  321. if (!t.fixed) {
  322. t.fx -= fx
  323. t.fy -= fy
  324. }
  325. }
  326. // 4. 中心引力
  327. var cx = this.canvasWidth / 2
  328. var cy = this.canvasHeight / 2
  329. for (var n = 0; n < nodes.length; n++) {
  330. if (!nodes[n].fixed) {
  331. nodes[n].fx += (cx - nodes[n].x) * p.centering
  332. nodes[n].fy += (cy - nodes[n].y) * p.centering
  333. }
  334. }
  335. // 5. 更新速度 + 位置
  336. for (var i = 0; i < nodes.length; i++) {
  337. var node = nodes[i]
  338. if (node.fixed) continue
  339. node.vx += node.fx
  340. node.vy += node.fy
  341. // 阻尼
  342. node.vx *= p.damping
  343. node.vy *= p.damping
  344. // 限速
  345. var speed = Math.sqrt(node.vx * node.vx + node.vy * node.vy)
  346. if (speed > p.maxSpeed) {
  347. node.vx = (node.vx / speed) * p.maxSpeed
  348. node.vy = (node.vy / speed) * p.maxSpeed
  349. }
  350. // 最小距离约束
  351. for (var j = 0; j < nodes.length; j++) {
  352. if (i === j) continue
  353. var other = nodes[j]
  354. var dx = other.x - node.x
  355. var dy = other.y - node.y
  356. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  357. var minDist = node.radius + other.radius
  358. if (dist < minDist && dist > 0) {
  359. var overlap = (minDist - dist) / 2
  360. var nx = dx / dist
  361. var ny = dy / dist
  362. node.x -= nx * overlap
  363. node.y -= ny * overlap
  364. other.x += nx * overlap
  365. other.y += ny * overlap
  366. }
  367. }
  368. node.x += node.vx
  369. node.y += node.vy
  370. // 边界约束
  371. var r = node.radius + 4
  372. if (node.x < r) { node.x = r; node.vx *= -0.5 }
  373. if (node.x > this.canvasWidth - r) { node.x = this.canvasWidth - r; node.vx *= -0.5 }
  374. if (node.y < r) { node.y = r; node.vy *= -0.5 }
  375. if (node.y > this.canvasHeight - r) { node.y = this.canvasHeight - r; node.vy *= -0.5 }
  376. }
  377. // 6. 渲染
  378. this.render()
  379. // 7. 下一帧(小程序无 requestAnimationFrame,用 setTimeout 降级)
  380. this.animFrameId = setTimeout(function() {
  381. self.simulationTick()
  382. }, 16)
  383. },
  384. // ===== 渲染 =====
  385. render: function() {
  386. var self = this
  387. var query = uni.createSelectorQuery().in(this)
  388. query.select('#forceGraphCanvas')
  389. .fields({ node: true, size: true })
  390. .exec(function(res) {
  391. if (!res || !res[0] || !res[0].node) {
  392. self.renderLegacy()
  393. return
  394. }
  395. var canvas = res[0].node
  396. var ctx = canvas.getContext('2d')
  397. canvas.width = self.canvasWidth * self.dpr
  398. canvas.height = self.canvasHeight * self.dpr
  399. ctx.scale(self.dpr, self.dpr)
  400. // 清空
  401. ctx.clearRect(0, 0, self.canvasWidth, self.canvasHeight)
  402. // 画边
  403. self.drawEdges(ctx)
  404. // 画节点
  405. self.drawNodes(ctx)
  406. })
  407. },
  408. drawEdges: function(ctx) {
  409. var edges = this.edges
  410. for (var i = 0; i < edges.length; i++) {
  411. var edge = edges[i]
  412. var s = edge.source
  413. var t = edge.target
  414. // 边颜色
  415. var trust = edge.trust || 75
  416. var comm = edge.communication || 50
  417. var hue = 120 * trust / 100
  418. ctx.strokeStyle = 'hsla(' + hue + ', 75%, 45%, 0.6)'
  419. ctx.lineWidth = 1 + (comm / 100) * 4
  420. // 画线
  421. ctx.beginPath()
  422. ctx.moveTo(s.x, s.y)
  423. ctx.lineTo(t.x, t.y)
  424. ctx.stroke()
  425. }
  426. },
  427. drawNodes: function(ctx) {
  428. var nodes = this.nodeData
  429. for (var i = 0; i < nodes.length; i++) {
  430. var node = nodes[i]
  431. var r = node.radius
  432. // 节点背景
  433. ctx.beginPath()
  434. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  435. if (node.isSelf) {
  436. ctx.fillStyle = '#FFFFFF'
  437. ctx.fill()
  438. ctx.strokeStyle = this.themeColor
  439. ctx.lineWidth = 3
  440. ctx.stroke()
  441. } else {
  442. ctx.fillStyle = this.hexToRgba(this.themeColor, 0.13)
  443. ctx.fill()
  444. }
  445. // 字母
  446. ctx.fillStyle = node.isSelf ? this.themeColor : '#4A5568'
  447. ctx.font = 'bold ' + (r * 0.9) + 'px sans-serif'
  448. ctx.textAlign = 'center'
  449. ctx.textBaseline = 'middle'
  450. ctx.fillText(node.displayName, node.x, node.y)
  451. // 昵称
  452. ctx.fillStyle = node.isSelf ? this.themeColor : '#94A3B8'
  453. ctx.font = (r * 0.4) + 'px sans-serif'
  454. ctx.fillText(node.nickname.substring(0, 3), node.x, node.y + r + 10)
  455. }
  456. },
  457. // WeChat mini-program compatible fallback renderer.
  458. // Note: uni-app legacy canvas uses setFontSize(size), not setFont().
  459. renderLegacy: function() {
  460. if (this._destroyed) return
  461. try {
  462. var ctx = uni.createCanvasContext('forceGraphCanvas', this)
  463. var self = this
  464. ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
  465. var edges = this.edges
  466. for (var i = 0; i < edges.length; i++) {
  467. var edge = edges[i]
  468. var trust = edge.trust || 75
  469. var comm = edge.communication || 50
  470. var hue = 120 * trust / 100
  471. ctx.setStrokeStyle('hsla(' + hue + ', 75%, 45%, 0.6)')
  472. ctx.setLineWidth(1 + (comm / 100) * 4)
  473. ctx.beginPath()
  474. ctx.moveTo(edge.source.x, edge.source.y)
  475. ctx.lineTo(edge.target.x, edge.target.y)
  476. ctx.stroke()
  477. }
  478. var nodes = this.nodeData
  479. for (var j = 0; j < nodes.length; j++) {
  480. var node = nodes[j]
  481. var r = node.radius
  482. ctx.beginPath()
  483. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  484. if (node.isSelf) {
  485. ctx.setFillStyle('#FFFFFF')
  486. ctx.fill()
  487. ctx.setStrokeStyle(this.themeColor)
  488. ctx.setLineWidth(3)
  489. ctx.stroke()
  490. } else {
  491. ctx.setFillStyle(this.hexToRgba(this.themeColor, 0.13))
  492. ctx.fill()
  493. }
  494. ctx.setFillStyle(node.isSelf ? this.themeColor : '#4A5568')
  495. ctx.setFontSize(Math.max(10, Math.round(r * 0.9)))
  496. ctx.setTextAlign('center')
  497. ctx.setTextBaseline('middle')
  498. ctx.fillText(node.displayName, node.x, node.y)
  499. ctx.setFillStyle(node.isSelf ? this.themeColor : '#94A3B8')
  500. ctx.setFontSize(Math.max(9, Math.round(r * 0.4)))
  501. ctx.fillText((node.nickname || '').substring(0, 3), node.x, node.y + r + 10)
  502. }
  503. ctx.draw()
  504. } catch (err) {
  505. // Legacy canvas fallback must never crash page initialization.
  506. }
  507. },
  508. // ===== 交互 =====
  509. getTouchNode: function(touchX, touchY) {
  510. var nodes = this.nodeData
  511. for (var i = 0; i < nodes.length; i++) {
  512. var node = nodes[i]
  513. var dx = touchX - node.x
  514. var dy = touchY - node.y
  515. var dist = Math.sqrt(dx * dx + dy * dy)
  516. if (dist <= node.radius + 8) {
  517. return node
  518. }
  519. }
  520. return null
  521. },
  522. onTouchStart: function(e) {
  523. if (!this.interactive) return
  524. var touch = e.touches ? e.touches[0] : e.detail
  525. if (!touch) return
  526. var self = this
  527. var rect = {}
  528. var query = uni.createSelectorQuery().in(this)
  529. query.select('#forceGraphCanvas').boundingClientRect()
  530. query.exec(function(res) {
  531. if (res && res[0]) {
  532. rect = res[0]
  533. var x = touch.clientX - rect.left
  534. var y = touch.clientY - rect.top
  535. var node = self.getTouchNode(x, y)
  536. if (node) {
  537. if (node.isSelf) {
  538. // 自身节点 → 显示 + 号
  539. self.touchingSelf = true
  540. } else {
  541. // 其他节点 → 拖拽
  542. self.dragging = {
  543. node: node,
  544. offsetX: x - node.x,
  545. offsetY: y - node.y
  546. }
  547. self.dragStartPos = { x: x, y: y }
  548. node.fx = node.x
  549. node.fy = node.y
  550. node.fixed = true
  551. }
  552. }
  553. }
  554. }.bind(this))
  555. },
  556. onTouchMove: function(e) {
  557. if (!this.dragging) return
  558. e.preventDefault && e.preventDefault()
  559. var touch = e.touches ? e.touches[0] : e.detail
  560. if (!touch) return
  561. var rect = {}
  562. var self = this
  563. var query = uni.createSelectorQuery().in(this)
  564. query.select('#forceGraphCanvas').boundingClientRect()
  565. query.exec(function(res) {
  566. if (res && res[0]) {
  567. rect = res[0]
  568. var x = touch.clientX - rect.left
  569. var y = touch.clientY - rect.top
  570. var node = self.dragging.node
  571. node.x = x - self.dragging.offsetX
  572. node.y = y - self.dragging.offsetY
  573. node.fx = node.x
  574. node.fy = node.y
  575. }
  576. }.bind(this))
  577. },
  578. onTouchEnd: function(e) {
  579. // 如果正在触摸自身节点 → 跳转添加成员
  580. if (this.touchingSelf) {
  581. this.touchingSelf = false
  582. if (this.interactive) {
  583. this.$emit('addMemberTap')
  584. this.goAddMember()
  585. }
  586. this.dragging = null
  587. this.dragStartPos = null
  588. return
  589. }
  590. if (!this.dragging) return
  591. var wasDragged = this.dragStartPos && (
  592. Math.abs(this.dragging.node.x - this.dragStartPos.x) > 5 ||
  593. Math.abs(this.dragging.node.y - this.dragStartPos.y) > 5
  594. )
  595. var node = this.dragging.node
  596. node.fixed = false
  597. node.fx = 0
  598. node.fy = 0
  599. // 如果没有真正拖拽(点击),触发 memberTap
  600. if (!wasDragged && this.interactive) {
  601. this.$emit('memberTap', {
  602. memberId: node.id,
  603. memberType: node.memberType,
  604. nickname: node.nickname
  605. })
  606. }
  607. this.dragging = null
  608. this.dragStartPos = null
  609. },
  610. // 跳转到添加成员页面
  611. goAddMember: function() {
  612. uni.navigateTo({
  613. url: '/pages/family/add-member'
  614. })
  615. },
  616. // 点击右上角小齿轮 → 跳转成员管理页
  617. onManageTap: function() {
  618. uni.navigateTo({
  619. url: '/pages/profile-extra/family-members'
  620. })
  621. },
  622. // ===== 工具方法 =====
  623. getEnergy: function(memberId) {
  624. var data = this.energyMap[memberId]
  625. if (!data) return 0
  626. var key = this.dimensionCode + 'Score'
  627. var val = data[key]
  628. return (typeof val !== 'undefined' && val !== null) ? val : 0
  629. },
  630. getIntimacy: function(memberId, key) {
  631. var data = this.intimacyMap[memberId]
  632. if (!data) {
  633. var defaults = { closeness: 75, communication: 50, trust: 85 }
  634. return defaults[key]
  635. }
  636. var val = data[key]
  637. return (typeof val !== 'undefined' && val !== null) ? val : 75
  638. },
  639. updateCanvasSize: function() {
  640. var self = this
  641. var cnt = this.members ? this.members.length : 0
  642. self.canvasHeight = Math.max(200, cnt * 90)
  643. var query = uni.createSelectorQuery().in(this)
  644. query.select('.family-graph-wrapper').boundingClientRect(function(rect) {
  645. if (rect) {
  646. self.canvasWidth = rect.width || 340
  647. }
  648. }).exec()
  649. }
  650. }
  651. }
  652. </script>
  653. <style scoped>
  654. .family-graph-wrapper {
  655. position: relative;
  656. width: 100%;
  657. min-height: 200px;
  658. height: auto;
  659. margin: 10rpx 0;
  660. background: #FFFFFF;
  661. border-radius: 24rpx;
  662. overflow: hidden;
  663. }
  664. .family-graph-canvas {
  665. position: absolute;
  666. top: 0;
  667. left: 0;
  668. z-index: 1;
  669. }
  670. /* 右上角小齿轮 */
  671. .gear-icon {
  672. position: absolute;
  673. top: 12rpx;
  674. right: 12rpx;
  675. z-index: 10;
  676. width: 56rpx;
  677. height: 56rpx;
  678. display: flex;
  679. align-items: center;
  680. justify-content: center;
  681. background: rgba(255, 255, 255, 0.9);
  682. border-radius: 50%;
  683. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  684. }
  685. .gear-text {
  686. font-size: 32rpx;
  687. color: #94A3B8;
  688. }
  689. /* + 添加成员提示 */
  690. .add-hint {
  691. position: absolute;
  692. z-index: 5;
  693. width: 40rpx;
  694. height: 40rpx;
  695. border-radius: 50%;
  696. background: #10B981;
  697. display: flex;
  698. align-items: center;
  699. justify-content: center;
  700. opacity: 0;
  701. transform: scale(0.5);
  702. transition: opacity 0.15s, transform 0.15s;
  703. pointer-events: none;
  704. }
  705. .add-hint.add-hint-visible {
  706. opacity: 1;
  707. transform: scale(1);
  708. }
  709. .add-hint-text {
  710. font-size: 28rpx;
  711. color: #FFFFFF;
  712. font-weight: bold;
  713. line-height: 1;
  714. }
  715. .family-graph-empty {
  716. display: flex;
  717. align-items: center;
  718. justify-content: center;
  719. padding: 60rpx 0;
  720. background: #FFFFFF;
  721. border-radius: 24rpx;
  722. margin: 10rpx 0;
  723. }
  724. .empty-text {
  725. font-size: 28rpx;
  726. color: #94A3B8;
  727. }
  728. </style>