FamilyRelationGraph.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. var self = this
  204. this.dpr = uni.getSystemInfoSync().pixelRatio || 1
  205. this.$nextTick(function() {
  206. self.updateCanvasSize()
  207. self.$nextTick(function() {
  208. self.initSimulation()
  209. })
  210. })
  211. },
  212. beforeDestroy: function() {
  213. this.stopSimulation()
  214. },
  215. methods: {
  216. // ===== 物理引擎 =====
  217. resetSimulation: function() {
  218. this.stopSimulation()
  219. // 重新初始化节点位置
  220. var centerX = this.canvasWidth / 2
  221. var centerY = this.canvasHeight / 2
  222. var self = this
  223. this.nodeData.forEach(function(n, idx) {
  224. var angle = (2 * Math.PI * idx) / self.nodeData.length
  225. var r = Math.min(self.canvasWidth, self.canvasHeight) * 0.32
  226. n.x = centerX + r * Math.cos(angle)
  227. n.y = centerY + r * Math.sin(angle)
  228. n.vx = 0
  229. n.vy = 0
  230. n.fx = 0
  231. n.fy = 0
  232. })
  233. this.startSimulation()
  234. },
  235. initSimulation: function() {
  236. if (!this.nodeData || this.nodeData.length === 0) return
  237. this.startSimulation()
  238. },
  239. // hex → rgba(小程序不支持 8 位 hex 颜色 #RRGGBBAA)
  240. hexToRgba: function(hex, alpha) {
  241. var r = parseInt(hex.slice(1, 3), 16)
  242. var g = parseInt(hex.slice(3, 5), 16)
  243. var b = parseInt(hex.slice(5, 7), 16)
  244. return 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')'
  245. },
  246. startSimulation: function() {
  247. if (this.isSimulating) return
  248. this.isSimulating = true
  249. this.simulationTick()
  250. },
  251. stopSimulation: function() {
  252. this.isSimulating = false
  253. if (this.animFrameId) {
  254. clearTimeout(this.animFrameId)
  255. this.animFrameId = null
  256. }
  257. },
  258. simulationTick: function() {
  259. if (!this.isSimulating) return
  260. var self = this
  261. var nodes = this.nodeData
  262. var edges = this.edges
  263. var p = this.physics
  264. // 1. 清零力
  265. for (var i = 0; i < nodes.length; i++) {
  266. if (!nodes[i].fixed) {
  267. nodes[i].fx = 0
  268. nodes[i].fy = 0
  269. }
  270. }
  271. // 2. 斥力 (所有节点对之间)
  272. for (var a = 0; a < nodes.length; a++) {
  273. for (var b = a + 1; b < nodes.length; b++) {
  274. var dx = nodes[b].x - nodes[a].x
  275. var dy = nodes[b].y - nodes[a].y
  276. var distSq = dx * dx + dy * dy
  277. var dist = Math.sqrt(distSq) || 0.1
  278. // F = repulsion / dist^2
  279. var force = p.repulsion / distSq
  280. var fx = (dx / dist) * force
  281. var fy = (dy / dist) * force
  282. if (!nodes[a].fixed) {
  283. nodes[a].fx -= fx
  284. nodes[a].fy -= fy
  285. }
  286. if (!nodes[b].fixed) {
  287. nodes[b].fx += fx
  288. nodes[b].fy += fy
  289. }
  290. }
  291. }
  292. // 3. 弹簧引力 (边)
  293. for (var e = 0; e < edges.length; e++) {
  294. var edge = edges[e]
  295. var s = edge.source
  296. var t = edge.target
  297. var dx = t.x - s.x
  298. var dy = t.y - s.y
  299. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  300. var displacement = dist - p.springLength
  301. var force = p.springStrength * displacement
  302. var fx = (dx / dist) * force
  303. var fy = (dy / dist) * force
  304. if (!s.fixed) {
  305. s.fx += fx
  306. s.fy += fy
  307. }
  308. if (!t.fixed) {
  309. t.fx -= fx
  310. t.fy -= fy
  311. }
  312. }
  313. // 4. 中心引力
  314. var cx = this.canvasWidth / 2
  315. var cy = this.canvasHeight / 2
  316. for (var n = 0; n < nodes.length; n++) {
  317. if (!nodes[n].fixed) {
  318. nodes[n].fx += (cx - nodes[n].x) * p.centering
  319. nodes[n].fy += (cy - nodes[n].y) * p.centering
  320. }
  321. }
  322. // 5. 更新速度 + 位置
  323. for (var i = 0; i < nodes.length; i++) {
  324. var node = nodes[i]
  325. if (node.fixed) continue
  326. node.vx += node.fx
  327. node.vy += node.fy
  328. // 阻尼
  329. node.vx *= p.damping
  330. node.vy *= p.damping
  331. // 限速
  332. var speed = Math.sqrt(node.vx * node.vx + node.vy * node.vy)
  333. if (speed > p.maxSpeed) {
  334. node.vx = (node.vx / speed) * p.maxSpeed
  335. node.vy = (node.vy / speed) * p.maxSpeed
  336. }
  337. // 最小距离约束
  338. for (var j = 0; j < nodes.length; j++) {
  339. if (i === j) continue
  340. var other = nodes[j]
  341. var dx = other.x - node.x
  342. var dy = other.y - node.y
  343. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  344. var minDist = node.radius + other.radius
  345. if (dist < minDist && dist > 0) {
  346. var overlap = (minDist - dist) / 2
  347. var nx = dx / dist
  348. var ny = dy / dist
  349. node.x -= nx * overlap
  350. node.y -= ny * overlap
  351. other.x += nx * overlap
  352. other.y += ny * overlap
  353. }
  354. }
  355. node.x += node.vx
  356. node.y += node.vy
  357. // 边界约束
  358. var r = node.radius + 4
  359. if (node.x < r) { node.x = r; node.vx *= -0.5 }
  360. if (node.x > this.canvasWidth - r) { node.x = this.canvasWidth - r; node.vx *= -0.5 }
  361. if (node.y < r) { node.y = r; node.vy *= -0.5 }
  362. if (node.y > this.canvasHeight - r) { node.y = this.canvasHeight - r; node.vy *= -0.5 }
  363. }
  364. // 6. 渲染
  365. this.render()
  366. // 7. 下一帧(小程序无 requestAnimationFrame,用 setTimeout 降级)
  367. this.animFrameId = setTimeout(function() {
  368. self.simulationTick()
  369. }, 16)
  370. },
  371. // ===== 渲染 =====
  372. render: function() {
  373. var self = this
  374. var query = uni.createSelectorQuery().in(this)
  375. query.select('#forceGraphCanvas')
  376. .fields({ node: true, size: true })
  377. .exec(function(res) {
  378. if (!res || !res[0] || !res[0].node) {
  379. self.renderLegacy()
  380. return
  381. }
  382. var canvas = res[0].node
  383. var ctx = canvas.getContext('2d')
  384. canvas.width = self.canvasWidth * self.dpr
  385. canvas.height = self.canvasHeight * self.dpr
  386. ctx.scale(self.dpr, self.dpr)
  387. // 清空
  388. ctx.clearRect(0, 0, self.canvasWidth, self.canvasHeight)
  389. // 画边
  390. self.drawEdges(ctx)
  391. // 画节点
  392. self.drawNodes(ctx)
  393. })
  394. },
  395. drawEdges: function(ctx) {
  396. var edges = this.edges
  397. for (var i = 0; i < edges.length; i++) {
  398. var edge = edges[i]
  399. var s = edge.source
  400. var t = edge.target
  401. // 边颜色
  402. var trust = edge.trust || 75
  403. var comm = edge.communication || 50
  404. var hue = 120 * trust / 100
  405. ctx.strokeStyle = 'hsla(' + hue + ', 75%, 45%, 0.6)'
  406. ctx.lineWidth = 1 + (comm / 100) * 4
  407. // 画线
  408. ctx.beginPath()
  409. ctx.moveTo(s.x, s.y)
  410. ctx.lineTo(t.x, t.y)
  411. ctx.stroke()
  412. }
  413. },
  414. drawNodes: function(ctx) {
  415. var nodes = this.nodeData
  416. for (var i = 0; i < nodes.length; i++) {
  417. var node = nodes[i]
  418. var r = node.radius
  419. // 节点背景
  420. ctx.beginPath()
  421. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  422. if (node.isSelf) {
  423. ctx.fillStyle = '#FFFFFF'
  424. ctx.fill()
  425. ctx.strokeStyle = this.themeColor
  426. ctx.lineWidth = 3
  427. ctx.stroke()
  428. } else {
  429. ctx.fillStyle = this.hexToRgba(this.themeColor, 0.13)
  430. ctx.fill()
  431. }
  432. // 字母
  433. ctx.fillStyle = node.isSelf ? this.themeColor : '#4A5568'
  434. ctx.font = 'bold ' + (r * 0.9) + 'px sans-serif'
  435. ctx.textAlign = 'center'
  436. ctx.textBaseline = 'middle'
  437. ctx.fillText(node.displayName, node.x, node.y)
  438. // 昵称
  439. ctx.fillStyle = node.isSelf ? this.themeColor : '#94A3B8'
  440. ctx.font = (r * 0.4) + 'px sans-serif'
  441. ctx.fillText(node.nickname.substring(0, 3), node.x, node.y + r + 10)
  442. }
  443. },
  444. // 降级渲染 (CanvasContext)
  445. renderLegacy: function() {
  446. var ctx = uni.createCanvasContext('forceGraphCanvas', this)
  447. var self = this
  448. ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
  449. // 边
  450. var edges = this.edges
  451. for (var i = 0; i < edges.length; i++) {
  452. var edge = edges[i]
  453. var trust = edge.trust || 75
  454. var comm = edge.communication || 50
  455. var hue = 120 * trust / 100
  456. ctx.setStrokeStyle('hsla(' + hue + ', 75%, 45%, 0.6)')
  457. ctx.setLineWidth(1 + (comm / 100) * 4)
  458. ctx.beginPath()
  459. ctx.moveTo(edge.source.x, edge.source.y)
  460. ctx.lineTo(edge.target.x, edge.target.y)
  461. ctx.stroke()
  462. }
  463. // 节点
  464. var nodes = this.nodeData
  465. for (var j = 0; j < nodes.length; j++) {
  466. var node = nodes[j]
  467. var r = node.radius
  468. ctx.beginPath()
  469. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  470. if (node.isSelf) {
  471. ctx.setFillStyle('#FFFFFF')
  472. ctx.fill()
  473. ctx.setStrokeStyle(this.themeColor)
  474. ctx.setLineWidth(3)
  475. ctx.stroke()
  476. } else {
  477. ctx.setFillStyle(this.hexToRgba(this.themeColor, 0.13))
  478. ctx.fill()
  479. }
  480. ctx.setFillStyle(node.isSelf ? this.themeColor : '#4A5568')
  481. ctx.font = 'bold ' + (r * 0.9) + 'px sans-serif'
  482. ctx.setTextAlign('center')
  483. ctx.setTextBaseline('middle')
  484. ctx.fillText(node.displayName, node.x, node.y)
  485. ctx.setFillStyle(node.isSelf ? this.themeColor : '#94A3B8')
  486. ctx.font = (r * 0.4) + 'px sans-serif'
  487. ctx.fillText(node.nickname.substring(0, 3), node.x, node.y + r + 10)
  488. }
  489. ctx.draw()
  490. },
  491. // ===== 交互 =====
  492. getTouchNode: function(touchX, touchY) {
  493. var nodes = this.nodeData
  494. for (var i = 0; i < nodes.length; i++) {
  495. var node = nodes[i]
  496. var dx = touchX - node.x
  497. var dy = touchY - node.y
  498. var dist = Math.sqrt(dx * dx + dy * dy)
  499. if (dist <= node.radius + 8) {
  500. return node
  501. }
  502. }
  503. return null
  504. },
  505. onTouchStart: function(e) {
  506. if (!this.interactive) return
  507. var touch = e.touches ? e.touches[0] : e.detail
  508. if (!touch) return
  509. var self = this
  510. var rect = {}
  511. var query = uni.createSelectorQuery().in(this)
  512. query.select('#forceGraphCanvas').boundingClientRect()
  513. query.exec(function(res) {
  514. if (res && res[0]) {
  515. rect = res[0]
  516. var x = touch.clientX - rect.left
  517. var y = touch.clientY - rect.top
  518. var node = self.getTouchNode(x, y)
  519. if (node) {
  520. if (node.isSelf) {
  521. // 自身节点 → 显示 + 号
  522. self.touchingSelf = true
  523. } else {
  524. // 其他节点 → 拖拽
  525. self.dragging = {
  526. node: node,
  527. offsetX: x - node.x,
  528. offsetY: y - node.y
  529. }
  530. self.dragStartPos = { x: x, y: y }
  531. node.fx = node.x
  532. node.fy = node.y
  533. node.fixed = true
  534. }
  535. }
  536. }
  537. }.bind(this))
  538. },
  539. onTouchMove: function(e) {
  540. if (!this.dragging) return
  541. e.preventDefault && e.preventDefault()
  542. var touch = e.touches ? e.touches[0] : e.detail
  543. if (!touch) return
  544. var rect = {}
  545. var self = this
  546. var query = uni.createSelectorQuery().in(this)
  547. query.select('#forceGraphCanvas').boundingClientRect()
  548. query.exec(function(res) {
  549. if (res && res[0]) {
  550. rect = res[0]
  551. var x = touch.clientX - rect.left
  552. var y = touch.clientY - rect.top
  553. var node = self.dragging.node
  554. node.x = x - self.dragging.offsetX
  555. node.y = y - self.dragging.offsetY
  556. node.fx = node.x
  557. node.fy = node.y
  558. }
  559. }.bind(this))
  560. },
  561. onTouchEnd: function(e) {
  562. // 如果正在触摸自身节点 → 跳转添加成员
  563. if (this.touchingSelf) {
  564. this.touchingSelf = false
  565. if (this.interactive) {
  566. this.$emit('addMemberTap')
  567. this.goAddMember()
  568. }
  569. this.dragging = null
  570. this.dragStartPos = null
  571. return
  572. }
  573. if (!this.dragging) return
  574. var wasDragged = this.dragStartPos && (
  575. Math.abs(this.dragging.node.x - this.dragStartPos.x) > 5 ||
  576. Math.abs(this.dragging.node.y - this.dragStartPos.y) > 5
  577. )
  578. var node = this.dragging.node
  579. node.fixed = false
  580. node.fx = 0
  581. node.fy = 0
  582. // 如果没有真正拖拽(点击),触发 memberTap
  583. if (!wasDragged && this.interactive) {
  584. this.$emit('memberTap', {
  585. memberId: node.id,
  586. memberType: node.memberType,
  587. nickname: node.nickname
  588. })
  589. }
  590. this.dragging = null
  591. this.dragStartPos = null
  592. },
  593. // 跳转到添加成员页面
  594. goAddMember: function() {
  595. uni.navigateTo({
  596. url: '/pages/family/add-member'
  597. })
  598. },
  599. // 点击右上角小齿轮 → 跳转成员管理页
  600. onManageTap: function() {
  601. uni.navigateTo({
  602. url: '/pages/profile-extra/family-members'
  603. })
  604. },
  605. // ===== 工具方法 =====
  606. getEnergy: function(memberId) {
  607. var data = this.energyMap[memberId]
  608. if (!data) return 0
  609. var key = this.dimensionCode + 'Score'
  610. var val = data[key]
  611. return (typeof val !== 'undefined' && val !== null) ? val : 0
  612. },
  613. getIntimacy: function(memberId, key) {
  614. var data = this.intimacyMap[memberId]
  615. if (!data) {
  616. var defaults = { closeness: 75, communication: 50, trust: 85 }
  617. return defaults[key]
  618. }
  619. var val = data[key]
  620. return (typeof val !== 'undefined' && val !== null) ? val : 75
  621. },
  622. updateCanvasSize: function() {
  623. var self = this
  624. var cnt = this.members ? this.members.length : 0
  625. self.canvasHeight = Math.max(200, cnt * 90)
  626. var query = uni.createSelectorQuery().in(this)
  627. query.select('.family-graph-wrapper').boundingClientRect(function(rect) {
  628. if (rect) {
  629. self.canvasWidth = rect.width || 340
  630. }
  631. }).exec()
  632. }
  633. }
  634. }
  635. </script>
  636. <style scoped>
  637. .family-graph-wrapper {
  638. position: relative;
  639. width: 100%;
  640. min-height: 200px;
  641. height: auto;
  642. margin: 10rpx 0;
  643. background: #FFFFFF;
  644. border-radius: 24rpx;
  645. overflow: hidden;
  646. }
  647. .family-graph-canvas {
  648. position: absolute;
  649. top: 0;
  650. left: 0;
  651. z-index: 1;
  652. }
  653. /* 右上角小齿轮 */
  654. .gear-icon {
  655. position: absolute;
  656. top: 12rpx;
  657. right: 12rpx;
  658. z-index: 10;
  659. width: 56rpx;
  660. height: 56rpx;
  661. display: flex;
  662. align-items: center;
  663. justify-content: center;
  664. background: rgba(255, 255, 255, 0.9);
  665. border-radius: 50%;
  666. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  667. }
  668. .gear-text {
  669. font-size: 32rpx;
  670. color: #94A3B8;
  671. }
  672. /* + 添加成员提示 */
  673. .add-hint {
  674. position: absolute;
  675. z-index: 5;
  676. width: 40rpx;
  677. height: 40rpx;
  678. border-radius: 50%;
  679. background: #10B981;
  680. display: flex;
  681. align-items: center;
  682. justify-content: center;
  683. opacity: 0;
  684. transform: scale(0.5);
  685. transition: opacity 0.15s, transform 0.15s;
  686. pointer-events: none;
  687. }
  688. .add-hint.add-hint-visible {
  689. opacity: 1;
  690. transform: scale(1);
  691. }
  692. .add-hint-text {
  693. font-size: 28rpx;
  694. color: #FFFFFF;
  695. font-weight: bold;
  696. line-height: 1;
  697. }
  698. .family-graph-empty {
  699. display: flex;
  700. align-items: center;
  701. justify-content: center;
  702. padding: 60rpx 0;
  703. background: #FFFFFF;
  704. border-radius: 24rpx;
  705. margin: 10rpx 0;
  706. }
  707. .empty-text {
  708. font-size: 28rpx;
  709. color: #94A3B8;
  710. }
  711. </style>