FamilyRelationGraph.vue 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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. effectiveRole: m.effectiveRole || m.relativeLabel || '',
  152. gender: m.gender || '',
  153. relativeLabel: m.relativeLabel || '',
  154. radius: radius,
  155. x: initX,
  156. y: initY,
  157. vx: 0,
  158. vy: 0,
  159. fx: 0,
  160. fy: 0
  161. }
  162. })
  163. },
  164. // 构建边 (self 连接到其他所有成员)
  165. edges: function() {
  166. var self = this
  167. var selfNode = this.nodeData.find(function(n) { return n.isSelf })
  168. if (!selfNode) return []
  169. var result = []
  170. this.nodeData.forEach(function(n) {
  171. if (n.id !== selfNode.id) {
  172. // 获取亲密度
  173. var pairKey = selfNode.id < n.id
  174. ? selfNode.id + '-' + n.id
  175. : n.id + '-' + selfNode.id
  176. var comp = self.compatibilityMap[pairKey]
  177. var trust = self.getIntimacy(n.id, 'trust')
  178. var comm = self.getIntimacy(n.id, 'communication')
  179. var closeness = self.getIntimacy(n.id, 'closeness')
  180. result.push({
  181. source: selfNode,
  182. target: n,
  183. // 边权重用于可视化
  184. trust: trust,
  185. communication: comm,
  186. closeness: closeness,
  187. compatibility: comp && comp.overallScore !== undefined ? comp.overallScore : null
  188. })
  189. }
  190. })
  191. return result
  192. }
  193. },
  194. watch: {
  195. members: function() {
  196. this.resetSimulation()
  197. },
  198. canvasWidth: function() {
  199. this.resetSimulation()
  200. },
  201. canvasHeight: function() {
  202. this.resetSimulation()
  203. }
  204. },
  205. mounted: function() {
  206. this._destroyed = false
  207. var self = this
  208. try {
  209. this.dpr = uni.getSystemInfoSync().pixelRatio || 1
  210. } catch (e) {
  211. this.dpr = 1
  212. }
  213. this.$nextTick(function() {
  214. if (self._destroyed) return
  215. self.updateCanvasSize()
  216. self.$nextTick(function() {
  217. if (!self._destroyed) {
  218. self.initSimulation()
  219. }
  220. })
  221. })
  222. },
  223. beforeDestroy: function() {
  224. this._destroyed = true
  225. this.stopSimulation()
  226. },
  227. methods: {
  228. // ===== 物理引擎 =====
  229. resetSimulation: function() {
  230. this.stopSimulation()
  231. // 重新初始化节点位置
  232. var centerX = this.canvasWidth / 2
  233. var centerY = this.canvasHeight / 2
  234. var self = this
  235. this.nodeData.forEach(function(n, idx) {
  236. var angle = (2 * Math.PI * idx) / self.nodeData.length
  237. var r = Math.min(self.canvasWidth, self.canvasHeight) * 0.32
  238. n.x = centerX + r * Math.cos(angle)
  239. n.y = centerY + r * Math.sin(angle)
  240. n.vx = 0
  241. n.vy = 0
  242. n.fx = 0
  243. n.fy = 0
  244. })
  245. this.startSimulation()
  246. },
  247. initSimulation: function() {
  248. if (!this.nodeData || this.nodeData.length === 0) return
  249. this.startSimulation()
  250. },
  251. // hex → rgba(小程序不支持 8 位 hex 颜色 #RRGGBBAA)
  252. hexToRgba: function(hex, alpha) {
  253. var r = parseInt(hex.slice(1, 3), 16)
  254. var g = parseInt(hex.slice(3, 5), 16)
  255. var b = parseInt(hex.slice(5, 7), 16)
  256. return 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')'
  257. },
  258. startSimulation: function() {
  259. if (this.isSimulating) return
  260. this.isSimulating = true
  261. this.simulationTick()
  262. },
  263. stopSimulation: function() {
  264. this.isSimulating = false
  265. if (this.animFrameId) {
  266. clearTimeout(this.animFrameId)
  267. this.animFrameId = null
  268. }
  269. },
  270. simulationTick: function() {
  271. if (!this.isSimulating) return
  272. if (this._destroyed) {
  273. this.isSimulating = false
  274. return
  275. }
  276. var self = this
  277. var nodes = this.nodeData
  278. var edges = this.edges
  279. var p = this.physics
  280. // 1. 清零力
  281. for (var i = 0; i < nodes.length; i++) {
  282. if (!nodes[i].fixed) {
  283. nodes[i].fx = 0
  284. nodes[i].fy = 0
  285. }
  286. }
  287. // 2. 斥力 (所有节点对之间)
  288. for (var a = 0; a < nodes.length; a++) {
  289. for (var b = a + 1; b < nodes.length; b++) {
  290. var dx = nodes[b].x - nodes[a].x
  291. var dy = nodes[b].y - nodes[a].y
  292. var distSq = dx * dx + dy * dy
  293. var dist = Math.sqrt(distSq) || 0.1
  294. // F = repulsion / dist^2
  295. var force = p.repulsion / distSq
  296. var fx = (dx / dist) * force
  297. var fy = (dy / dist) * force
  298. if (!nodes[a].fixed) {
  299. nodes[a].fx -= fx
  300. nodes[a].fy -= fy
  301. }
  302. if (!nodes[b].fixed) {
  303. nodes[b].fx += fx
  304. nodes[b].fy += fy
  305. }
  306. }
  307. }
  308. // 3. 弹簧引力 (边)
  309. for (var e = 0; e < edges.length; e++) {
  310. var edge = edges[e]
  311. var s = edge.source
  312. var t = edge.target
  313. var dx = t.x - s.x
  314. var dy = t.y - s.y
  315. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  316. var displacement = dist - p.springLength
  317. var force = p.springStrength * displacement
  318. var fx = (dx / dist) * force
  319. var fy = (dy / dist) * force
  320. if (!s.fixed) {
  321. s.fx += fx
  322. s.fy += fy
  323. }
  324. if (!t.fixed) {
  325. t.fx -= fx
  326. t.fy -= fy
  327. }
  328. }
  329. // 4. 中心引力
  330. var cx = this.canvasWidth / 2
  331. var cy = this.canvasHeight / 2
  332. for (var n = 0; n < nodes.length; n++) {
  333. if (!nodes[n].fixed) {
  334. nodes[n].fx += (cx - nodes[n].x) * p.centering
  335. nodes[n].fy += (cy - nodes[n].y) * p.centering
  336. }
  337. }
  338. // 5. 更新速度 + 位置
  339. for (var i = 0; i < nodes.length; i++) {
  340. var node = nodes[i]
  341. if (node.fixed) continue
  342. node.vx += node.fx
  343. node.vy += node.fy
  344. // 阻尼
  345. node.vx *= p.damping
  346. node.vy *= p.damping
  347. // 限速
  348. var speed = Math.sqrt(node.vx * node.vx + node.vy * node.vy)
  349. if (speed > p.maxSpeed) {
  350. node.vx = (node.vx / speed) * p.maxSpeed
  351. node.vy = (node.vy / speed) * p.maxSpeed
  352. }
  353. // 最小距离约束
  354. for (var j = 0; j < nodes.length; j++) {
  355. if (i === j) continue
  356. var other = nodes[j]
  357. var dx = other.x - node.x
  358. var dy = other.y - node.y
  359. var dist = Math.sqrt(dx * dx + dy * dy) || 0.1
  360. var minDist = node.radius + other.radius
  361. if (dist < minDist && dist > 0) {
  362. var overlap = (minDist - dist) / 2
  363. var nx = dx / dist
  364. var ny = dy / dist
  365. node.x -= nx * overlap
  366. node.y -= ny * overlap
  367. other.x += nx * overlap
  368. other.y += ny * overlap
  369. }
  370. }
  371. node.x += node.vx
  372. node.y += node.vy
  373. // 边界约束
  374. var r = node.radius + 4
  375. if (node.x < r) { node.x = r; node.vx *= -0.5 }
  376. if (node.x > this.canvasWidth - r) { node.x = this.canvasWidth - r; node.vx *= -0.5 }
  377. if (node.y < r) { node.y = r; node.vy *= -0.5 }
  378. if (node.y > this.canvasHeight - r) { node.y = this.canvasHeight - r; node.vy *= -0.5 }
  379. }
  380. // 6. 渲染
  381. this.render()
  382. // 7. 下一帧(小程序无 requestAnimationFrame,用 setTimeout 降级)
  383. this.animFrameId = setTimeout(function() {
  384. self.simulationTick()
  385. }, 16)
  386. },
  387. // ===== 渲染 =====
  388. render: function() {
  389. var self = this
  390. var query = uni.createSelectorQuery().in(this)
  391. query.select('#forceGraphCanvas')
  392. .fields({ node: true, size: true })
  393. .exec(function(res) {
  394. if (!res || !res[0] || !res[0].node) {
  395. self.renderLegacy()
  396. return
  397. }
  398. var canvas = res[0].node
  399. var ctx = canvas.getContext('2d')
  400. canvas.width = self.canvasWidth * self.dpr
  401. canvas.height = self.canvasHeight * self.dpr
  402. ctx.scale(self.dpr, self.dpr)
  403. // 清空
  404. ctx.clearRect(0, 0, self.canvasWidth, self.canvasHeight)
  405. // 画边
  406. self.drawEdges(ctx)
  407. // 画节点
  408. self.drawNodes(ctx)
  409. })
  410. },
  411. drawEdges: function(ctx) {
  412. var edges = this.edges
  413. for (var i = 0; i < edges.length; i++) {
  414. var edge = edges[i]
  415. var s = edge.source
  416. var t = edge.target
  417. // 连线粗细 = 信任度 (1-6px, 信任越高越粗)
  418. var trust = edge.trust || 75
  419. ctx.lineWidth = 1 + (trust / 100) * 5
  420. // 虚线密度 = 沟通频率 (0=稀疏虚线, 100=实线)
  421. var comm = edge.communication || 50
  422. if (comm >= 90) {
  423. ctx.setLineDash([])
  424. } else if (comm >= 70) {
  425. ctx.setLineDash([8, 3])
  426. } else if (comm >= 50) {
  427. ctx.setLineDash([5, 5])
  428. } else if (comm >= 30) {
  429. ctx.setLineDash([3, 8])
  430. } else {
  431. ctx.setLineDash([2, 12])
  432. }
  433. // 连线颜色 = 亲密度 (低=冷色蓝紫, 高=暖色橙红)
  434. var closeness = edge.closeness || 50
  435. var hue = 220 - (closeness / 100) * 200
  436. ctx.strokeStyle = 'hsla(' + hue + ', 75%, 50%, 0.65)'
  437. ctx.beginPath()
  438. ctx.moveTo(s.x, s.y)
  439. ctx.lineTo(t.x, t.y)
  440. ctx.stroke()
  441. }
  442. },
  443. // ===== 身份 → 形状映射 =====
  444. getNodeShapeType: function(node) {
  445. var role = node.effectiveRole || node.relativeLabel || ''
  446. var type = node.memberType || ''
  447. // 长辈 (爷爷/奶奶/外公/外婆)
  448. if (role.indexOf('爷爷') !== -1 || role.indexOf('奶奶') !== -1 ||
  449. role.indexOf('外公') !== -1 || role.indexOf('外婆') !== -1 ||
  450. role.indexOf('祖父') !== -1 || role.indexOf('祖母') !== -1) {
  451. return 'diamond'
  452. }
  453. // 父亲/男性家长
  454. if (role.indexOf('爸爸') !== -1 || role.indexOf('父亲') !== -1 ||
  455. role.indexOf('爸') !== -1 || role.indexOf('爹') !== -1) {
  456. return 'roundedRect'
  457. }
  458. // 母亲/女性家长
  459. if (role.indexOf('妈妈') !== -1 || role.indexOf('母亲') !== -1 ||
  460. role.indexOf('妈') !== -1 || role.indexOf('娘') !== -1) {
  461. return 'circle'
  462. }
  463. // 通用 parent → 根据性别区分
  464. if (type === 'parent') {
  465. return node.gender === 'male' ? 'roundedRect' : 'circle'
  466. }
  467. // child → 圆形
  468. if (type === 'child') {
  469. return 'childCircle'
  470. }
  471. return 'circle'
  472. },
  473. // 绘制圆角矩形
  474. drawRoundedRectNode: function(ctx, x, y, r) {
  475. var w = r * 1.8
  476. var h = r * 1.8
  477. var cr = r * 0.3
  478. ctx.beginPath()
  479. ctx.moveTo(x - w/2 + cr, y - h/2)
  480. ctx.lineTo(x + w/2 - cr, y - h/2)
  481. ctx.arcTo(x + w/2, y - h/2, x + w/2, y - h/2 + cr, cr)
  482. ctx.lineTo(x + w/2, y + h/2 - cr)
  483. ctx.arcTo(x + w/2, y + h/2, x + w/2 - cr, y + h/2, cr)
  484. ctx.lineTo(x - w/2 + cr, y + h/2)
  485. ctx.arcTo(x - w/2, y + h/2, x - w/2, y + h/2 - cr, cr)
  486. ctx.lineTo(x - w/2, y - h/2 + cr)
  487. ctx.arcTo(x - w/2, y - h/2, x - w/2 + cr, y - h/2, cr)
  488. ctx.closePath()
  489. },
  490. // 绘制菱形
  491. drawDiamondNode: function(ctx, x, y, r) {
  492. ctx.beginPath()
  493. ctx.moveTo(x, y - r * 1.2)
  494. ctx.lineTo(x + r * 1.2, y)
  495. ctx.lineTo(x, y + r * 1.2)
  496. ctx.lineTo(x - r * 1.2, y)
  497. ctx.closePath()
  498. },
  499. drawNodes: function(ctx) {
  500. var nodes = this.nodeData
  501. for (var i = 0; i < nodes.length; i++) {
  502. var node = nodes[i]
  503. var r = node.radius
  504. var shapeType = this.getNodeShapeType(node)
  505. // 按身份绘制不同形状
  506. if (shapeType === 'roundedRect') {
  507. this.drawRoundedRectNode(ctx, node.x, node.y, r)
  508. } else if (shapeType === 'diamond') {
  509. this.drawDiamondNode(ctx, node.x, node.y, r)
  510. } else {
  511. // 默认圆形 (母亲/女性家长/孩子)
  512. ctx.beginPath()
  513. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  514. }
  515. // 填充 + 描边
  516. if (node.isSelf) {
  517. ctx.fillStyle = '#FFFFFF'
  518. ctx.fill()
  519. ctx.strokeStyle = this.themeColor
  520. ctx.lineWidth = 3
  521. ctx.stroke()
  522. } else {
  523. ctx.fillStyle = this.hexToRgba(this.themeColor, 0.13)
  524. ctx.fill()
  525. }
  526. // 第一字符 (昵称首字)
  527. ctx.fillStyle = node.isSelf ? this.themeColor : '#4A5568'
  528. ctx.font = 'bold ' + (r * 0.9) + 'px sans-serif'
  529. ctx.textAlign = 'center'
  530. ctx.textBaseline = 'middle'
  531. ctx.fillText(node.displayName, node.x, node.y)
  532. // 昵称
  533. ctx.fillStyle = node.isSelf ? this.themeColor : '#94A3B8'
  534. ctx.font = (r * 0.4) + 'px sans-serif'
  535. ctx.fillText(node.nickname.substring(0, 3), node.x, node.y + r + 10)
  536. }
  537. },
  538. // WeChat mini-program compatible fallback renderer.
  539. // Note: uni-app legacy canvas uses setFontSize(size), not setFont().
  540. renderLegacy: function() {
  541. if (this._destroyed) return
  542. try {
  543. var ctx = uni.createCanvasContext('forceGraphCanvas', this)
  544. var self = this
  545. ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
  546. var edges = this.edges
  547. for (var i = 0; i < edges.length; i++) {
  548. var edge = edges[i]
  549. var trust = edge.trust || 75
  550. var comm = edge.communication || 50
  551. var closeness = edge.closeness || 50
  552. // 连线粗细 = 信任度
  553. ctx.setLineWidth(1 + (trust / 100) * 5)
  554. // 虚线密度 = 沟通频率
  555. if (comm >= 90) {
  556. ctx.setLineDash([])
  557. } else if (comm >= 70) {
  558. ctx.setLineDash([8, 3])
  559. } else if (comm >= 50) {
  560. ctx.setLineDash([5, 5])
  561. } else if (comm >= 30) {
  562. ctx.setLineDash([3, 8])
  563. } else {
  564. ctx.setLineDash([2, 12])
  565. }
  566. // 连线颜色 = 亲密度 (低=冷色蓝紫, 高=暖色橙红)
  567. var hue = 220 - (closeness / 100) * 200
  568. ctx.setStrokeStyle('hsla(' + hue + ', 75%, 50%, 0.65)')
  569. ctx.beginPath()
  570. ctx.moveTo(edge.source.x, edge.source.y)
  571. ctx.lineTo(edge.target.x, edge.target.y)
  572. ctx.stroke()
  573. }
  574. var nodes = this.nodeData
  575. for (var j = 0; j < nodes.length; j++) {
  576. var node = nodes[j]
  577. var r = node.radius
  578. var shapeType = this.getNodeShapeType(node)
  579. // legacy API 的形状绘制
  580. if (shapeType === 'roundedRect') {
  581. var w = r * 1.8
  582. var h = r * 1.8
  583. var cr = r * 0.3
  584. ctx.beginPath()
  585. ctx.moveTo(node.x - w/2 + cr, node.y - h/2)
  586. ctx.lineTo(node.x + w/2 - cr, node.y - h/2)
  587. ctx.arcTo(node.x + w/2, node.y - h/2, node.x + w/2, node.y - h/2 + cr, cr)
  588. ctx.lineTo(node.x + w/2, node.y + h/2 - cr)
  589. ctx.arcTo(node.x + w/2, node.y + h/2, node.x + w/2 - cr, node.y + h/2, cr)
  590. ctx.lineTo(node.x - w/2 + cr, node.y + h/2)
  591. ctx.arcTo(node.x - w/2, node.y + h/2, node.x - w/2, node.y + h/2 - cr, cr)
  592. ctx.lineTo(node.x - w/2, node.y - h/2 + cr)
  593. ctx.arcTo(node.x - w/2, node.y - h/2, node.x - w/2 + cr, node.y - h/2, cr)
  594. ctx.closePath()
  595. } else if (shapeType === 'diamond') {
  596. ctx.beginPath()
  597. ctx.moveTo(node.x, node.y - r * 1.2)
  598. ctx.lineTo(node.x + r * 1.2, node.y)
  599. ctx.lineTo(node.x, node.y + r * 1.2)
  600. ctx.lineTo(node.x - r * 1.2, node.y)
  601. ctx.closePath()
  602. } else {
  603. ctx.beginPath()
  604. ctx.arc(node.x, node.y, r, 0, 2 * Math.PI)
  605. }
  606. if (node.isSelf) {
  607. ctx.setFillStyle('#FFFFFF')
  608. ctx.fill()
  609. ctx.setStrokeStyle(this.themeColor)
  610. ctx.setLineWidth(3)
  611. ctx.stroke()
  612. } else {
  613. ctx.setFillStyle(this.hexToRgba(this.themeColor, 0.13))
  614. ctx.fill()
  615. }
  616. ctx.setFillStyle(node.isSelf ? this.themeColor : '#4A5568')
  617. ctx.setFontSize(Math.max(10, Math.round(r * 0.9)))
  618. ctx.setTextAlign('center')
  619. ctx.setTextBaseline('middle')
  620. ctx.fillText(node.displayName, node.x, node.y)
  621. ctx.setFillStyle(node.isSelf ? this.themeColor : '#94A3B8')
  622. ctx.setFontSize(Math.max(9, Math.round(r * 0.4)))
  623. ctx.fillText((node.nickname || '').substring(0, 3), node.x, node.y + r + 10)
  624. }
  625. ctx.draw()
  626. } catch (err) {
  627. // Legacy canvas fallback must never crash page initialization.
  628. }
  629. },
  630. // ===== 交互 =====
  631. getTouchNode: function(touchX, touchY) {
  632. var nodes = this.nodeData
  633. for (var i = 0; i < nodes.length; i++) {
  634. var node = nodes[i]
  635. var dx = touchX - node.x
  636. var dy = touchY - node.y
  637. var dist = Math.sqrt(dx * dx + dy * dy)
  638. if (dist <= node.radius + 8) {
  639. return node
  640. }
  641. }
  642. return null
  643. },
  644. onTouchStart: function(e) {
  645. if (!this.interactive) return
  646. var touch = e.touches ? e.touches[0] : e.detail
  647. if (!touch) return
  648. var self = this
  649. var rect = {}
  650. var query = uni.createSelectorQuery().in(this)
  651. query.select('#forceGraphCanvas').boundingClientRect()
  652. query.exec(function(res) {
  653. if (res && res[0]) {
  654. rect = res[0]
  655. var x = touch.clientX - rect.left
  656. var y = touch.clientY - rect.top
  657. var node = self.getTouchNode(x, y)
  658. if (node) {
  659. if (node.isSelf) {
  660. // 自身节点 → 显示 + 号
  661. self.touchingSelf = true
  662. } else {
  663. // 其他节点 → 拖拽
  664. self.dragging = {
  665. node: node,
  666. offsetX: x - node.x,
  667. offsetY: y - node.y
  668. }
  669. self.dragStartPos = { x: x, y: y }
  670. node.fx = node.x
  671. node.fy = node.y
  672. node.fixed = true
  673. }
  674. }
  675. }
  676. }.bind(this))
  677. },
  678. onTouchMove: function(e) {
  679. if (!this.dragging) return
  680. e.preventDefault && e.preventDefault()
  681. var touch = e.touches ? e.touches[0] : e.detail
  682. if (!touch) return
  683. var rect = {}
  684. var self = this
  685. var query = uni.createSelectorQuery().in(this)
  686. query.select('#forceGraphCanvas').boundingClientRect()
  687. query.exec(function(res) {
  688. if (res && res[0]) {
  689. rect = res[0]
  690. var x = touch.clientX - rect.left
  691. var y = touch.clientY - rect.top
  692. var node = self.dragging.node
  693. node.x = x - self.dragging.offsetX
  694. node.y = y - self.dragging.offsetY
  695. node.fx = node.x
  696. node.fy = node.y
  697. }
  698. }.bind(this))
  699. },
  700. onTouchEnd: function(e) {
  701. // 如果正在触摸自身节点 → 跳转成员维护页
  702. if (this.touchingSelf) {
  703. this.touchingSelf = false
  704. if (this.interactive) {
  705. this.goSelfMaintenance()
  706. }
  707. this.dragging = null
  708. this.dragStartPos = null
  709. return
  710. }
  711. if (!this.dragging) return
  712. var wasDragged = this.dragStartPos && (
  713. Math.abs(this.dragging.node.x - this.dragStartPos.x) > 5 ||
  714. Math.abs(this.dragging.node.y - this.dragStartPos.y) > 5
  715. )
  716. var node = this.dragging.node
  717. node.fixed = false
  718. node.fx = 0
  719. node.fy = 0
  720. // 如果没有真正拖拽(点击),触发 memberTap
  721. if (!wasDragged && this.interactive) {
  722. this.$emit('memberTap', {
  723. memberId: node.id,
  724. memberType: node.memberType,
  725. nickname: node.nickname
  726. })
  727. }
  728. this.dragging = null
  729. this.dragStartPos = null
  730. },
  731. // 跳转到成员维护页面 (点自己进入维护)
  732. goSelfMaintenance: function() {
  733. uni.navigateTo({
  734. url: '/pages/profile-extra/family-members'
  735. })
  736. },
  737. // 点击右上角小齿轮 → 跳转成员管理页
  738. onManageTap: function() {
  739. uni.navigateTo({
  740. url: '/pages/profile-extra/family-members'
  741. })
  742. },
  743. // ===== 工具方法 =====
  744. getEnergy: function(memberId) {
  745. var data = this.energyMap[memberId]
  746. if (!data) return 0
  747. var key = this.dimensionCode + 'Score'
  748. var val = data[key]
  749. return (typeof val !== 'undefined' && val !== null) ? val : 0
  750. },
  751. getIntimacy: function(memberId, key) {
  752. var data = this.intimacyMap[memberId]
  753. if (!data) {
  754. var defaults = { closeness: 75, communication: 50, trust: 85 }
  755. return defaults[key]
  756. }
  757. var val = data[key]
  758. return (typeof val !== 'undefined' && val !== null) ? val : 75
  759. },
  760. updateCanvasSize: function() {
  761. var self = this
  762. var cnt = this.members ? this.members.length : 0
  763. self.canvasHeight = Math.max(200, cnt * 90)
  764. var query = uni.createSelectorQuery().in(this)
  765. query.select('.family-graph-wrapper').boundingClientRect(function(rect) {
  766. if (rect) {
  767. self.canvasWidth = rect.width || 340
  768. }
  769. }).exec()
  770. }
  771. }
  772. }
  773. </script>
  774. <style scoped>
  775. .family-graph-wrapper {
  776. position: relative;
  777. width: 100%;
  778. min-height: 200px;
  779. height: auto;
  780. margin: 10rpx 0;
  781. background: #FFFFFF;
  782. border-radius: 24rpx;
  783. overflow: hidden;
  784. }
  785. .family-graph-canvas {
  786. position: absolute;
  787. top: 0;
  788. left: 0;
  789. z-index: 1;
  790. }
  791. /* 右上角小齿轮 */
  792. .gear-icon {
  793. position: absolute;
  794. top: 12rpx;
  795. right: 12rpx;
  796. z-index: 10;
  797. width: 56rpx;
  798. height: 56rpx;
  799. display: flex;
  800. align-items: center;
  801. justify-content: center;
  802. background: rgba(255, 255, 255, 0.9);
  803. border-radius: 50%;
  804. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  805. }
  806. .gear-text {
  807. font-size: 32rpx;
  808. color: #94A3B8;
  809. }
  810. /* ⚙ 维护页提示 */
  811. .add-hint {
  812. position: absolute;
  813. z-index: 5;
  814. width: 40rpx;
  815. height: 40rpx;
  816. border-radius: 50%;
  817. background: #10B981;
  818. display: flex;
  819. align-items: center;
  820. justify-content: center;
  821. opacity: 0;
  822. transform: scale(0.5);
  823. transition: opacity 0.15s, transform 0.15s;
  824. pointer-events: none;
  825. }
  826. .add-hint.add-hint-visible {
  827. opacity: 1;
  828. transform: scale(1);
  829. }
  830. .add-hint-text {
  831. font-size: 28rpx;
  832. color: #FFFFFF;
  833. font-weight: bold;
  834. line-height: 1;
  835. }
  836. .family-graph-empty {
  837. display: flex;
  838. align-items: center;
  839. justify-content: center;
  840. padding: 60rpx 0;
  841. background: #FFFFFF;
  842. border-radius: 24rpx;
  843. margin: 10rpx 0;
  844. }
  845. .empty-text {
  846. font-size: 28rpx;
  847. color: #94A3B8;
  848. }
  849. </style>