OctopusDiagram.vue 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <template>
  2. <!-- 章鱼图 - 关键人拓展图谱 -->
  3. <view class="octopus-wrapper" v-if="effects && effects.list && effects.list.length > 0" :style="{ '--font-scale': fontScale, minHeight: canvasHeight + 'px' }">
  4. <canvas
  5. class="octopus-canvas"
  6. canvas-id="octopusCanvas"
  7. id="octopusCanvas"
  8. type="2d"
  9. :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
  10. @touchstart="onTouchStart"
  11. @touchmove="onTouchMove"
  12. @touchend="onTouchEnd" />
  13. <!-- 成效节点点击弹窗(显示关键人列表) -->
  14. <view class="effect-popup" v-if="selectedEffect" :style="effectPopupPosition">
  15. <view class="popup-content">
  16. <view class="popup-header">
  17. <text class="popup-name">{{ selectedEffect.name }}</text>
  18. <text class="popup-member" v-if="familyMode && selectedEffect.memberName">{{ selectedEffect.memberName }}</text>
  19. <text class="popup-tags">
  20. <text class="popup-tag" v-for="t in selectedEffect.effectTypeList" :key="t">{{ t }}</text>
  21. </text>
  22. </view>
  23. <view class="popup-keypersons" v-if="keyPersons[selectedEffect.id] && keyPersons[selectedEffect.id].length > 0">
  24. <text class="popup-title">关键人</text>
  25. <view class="kp-item-row" v-for="kp in keyPersons[selectedEffect.id]" :key="kp.id">
  26. <text class="kp-item">{{ kp.name }} · {{ kp.title || kp.organization || '' }}</text>
  27. <text class="kp-sys-badge" v-if="kp.matched">✓ 系统用户</text>
  28. </view>
  29. </view>
  30. <view class="popup-empty" v-else>
  31. <text class="empty-text">{{ familyMode ? '暂无关键人' : '暂无关键人,点击添加' }}</text>
  32. </view>
  33. <view class="popup-actions" v-if="!familyMode">
  34. <text class="action-btn action-btn-primary" @tap="onAddKeyPerson(selectedEffect)">添加关键人</text>
  35. </view>
  36. </view>
  37. </view>
  38. <!-- 重新分析按钮(家庭汇集只读,隐藏个人操作) -->
  39. <view class="reanalyze-btn" @tap="$emit('reanalyze')" v-if="!familyMode">
  40. <text>🔄 重新分析</text>
  41. </view>
  42. <!-- 家庭汇集:成员图例 -->
  43. <view class="octopus-member-legend" v-if="familyMode && memberLegend.length > 0" :style="{ marginTop: canvasHeight + 'px' }">
  44. <view class="legend-item" v-for="m in memberLegend" :key="m.memberId">
  45. <view class="legend-dot" :style="{ background: memberColorFor(m.colorKey) }"></view>
  46. <text class="legend-label">{{ m.name }}</text>
  47. </view>
  48. </view>
  49. </view>
  50. <!-- 空状态 -->
  51. <view class="octopus-empty" v-else-if="effects && effects.list && effects.list.length === 0">
  52. <text class="empty-icon">🧭</text>
  53. <text class="empty-text">{{ familyMode ? '暂无家庭感恩记录' : '暂无感恩记录' }}</text>
  54. <text class="empty-hint">{{ familyMode ? '家庭成员暂未记录成效' : '记录你的感恩时刻,画出章鱼图' }}</text>
  55. <text class="action-btn action-btn-primary" @tap="$emit('add-effect')" v-if="!familyMode">+ 感恩记录</text>
  56. </view>
  57. <!-- 加载状态 -->
  58. <view class="octopus-loading" v-else>
  59. <text class="loading-text">加载章鱼图...</text>
  60. </view>
  61. </template>
  62. <script>
  63. import { BrandColors } from '../../../utils/brand-colors.js'
  64. // 家庭汇集成员色板(8 色循环,稳定):引用 BrandColors 令牌镜像(SCSS 侧等值令牌见 uni.scss),禁止硬编码 hex
  65. var FAMILY_MEMBER_COLORS = [
  66. BrandColors.success, // emerald($status-success / $dim-action)
  67. BrandColors.info, // sky($status-info)
  68. BrandColors.wisdom, // indigo($dim-wisdom)
  69. BrandColors.wealth, // amber($dim-wealth)
  70. BrandColors.primary, // orange($brand-primary)
  71. BrandColors.mind, // pink($dim-mind)
  72. BrandColors.danger, // red($status-danger)
  73. BrandColors.violet // violet-500($facade-violet)
  74. ]
  75. /**
  76. * OctopusDiagram - 章鱼图(关键人拓展)
  77. *
  78. * 三层结构:
  79. * 1. 中心节点:"我"(当前用户)
  80. * 2. 第1层:成效记录节点(标签:金额大/频次高/新成交)
  81. * 3. 第2层:拓展方向节点(虚线,≤8个)
  82. */
  83. import fontScaleMixin from '@/mixins/font-scale.js'
  84. export default {
  85. mixins: [fontScaleMixin],
  86. name: 'OctopusDiagram',
  87. props: {
  88. selfId: { type: [Number, String], default: null },
  89. effects: { type: Object, default: null },
  90. expansions: { type: Array, default: function() { return [] } },
  91. canvasWidth: { type: Number, default: 340 },
  92. canvasHeight: { type: Number, default: 420 },
  93. interactive: { type: Boolean, default: true },
  94. /** 家庭汇集模式:节点按成员 colorKey 着色,隐藏个人操作(只读合并展示) */
  95. familyMode: { type: Boolean, default: false },
  96. /** 家庭汇集成员图例 [{ name, colorKey }] */
  97. memberLegend: { type: Array, default: function() { return [] } }
  98. },
  99. data: function() {
  100. return {
  101. dpr: 1,
  102. ctx: null,
  103. _canvasNode: null,
  104. _canvasRect: null,
  105. canvasReady: false,
  106. _destroyed: false,
  107. // 节点数据
  108. effectNodes: [],
  109. expansionNodes: [],
  110. centerNode: null,
  111. // 选中状态
  112. selectedEffect: null,
  113. _touchStartPos: null,
  114. // 关键人缓存(effectId -> [{...}])
  115. keyPersons: {},
  116. // 颜色
  117. effectColors: { '金额大': BrandColors.primary, '频次高': BrandColors.wealth, '新成交': BrandColors.success },
  118. expansionColor: BrandColors.wisdom
  119. }
  120. },
  121. computed: {
  122. effectPopupPosition: function() {
  123. if (!this.selectedEffect) return { display: 'none' }
  124. var px = this.selectedEffect.x
  125. var py = this.selectedEffect.y
  126. var halfPopup = 120
  127. var left = Math.max(halfPopup, Math.min(this.canvasWidth - halfPopup, px))
  128. return { left: left + 'px', top: (py - 20) + 'px' }
  129. }
  130. },
  131. watch: {
  132. effects: {
  133. handler: function() {
  134. this.selectedEffect = null
  135. this.buildLayout()
  136. if (this.canvasReady) this.drawAll()
  137. },
  138. deep: true
  139. },
  140. expansions: {
  141. handler: function() {
  142. this.buildExpansionNodes()
  143. if (this.canvasReady) this.drawAll()
  144. },
  145. deep: true
  146. }
  147. },
  148. mounted: function() {
  149. var self = this
  150. this._destroyed = false
  151. try {
  152. this.dpr = uni.getWindowInfo().pixelRatio || 1
  153. } catch (e) {
  154. this.dpr = 1
  155. }
  156. var retryCount = 0
  157. var maxRetry = 5
  158. function tryInit() {
  159. if (self._destroyed) return
  160. self.initCanvas(function(success) {
  161. if (self._destroyed) return
  162. if (!success && retryCount < maxRetry) {
  163. retryCount++
  164. setTimeout(tryInit, 60)
  165. }
  166. })
  167. }
  168. this.$nextTick(function() {
  169. if (!self._destroyed) tryInit()
  170. })
  171. },
  172. beforeDestroy: function() {
  173. this._destroyed = true
  174. },
  175. methods: {
  176. buildLayout: function() {
  177. this.effectNodes = []
  178. this.expansionNodes = []
  179. if (!this.effects || !this.effects.list) return
  180. var cx = this.canvasWidth / 2
  181. var cy = this.canvasHeight / 2
  182. var minDim = Math.min(this.canvasWidth, this.canvasHeight)
  183. var effects = this.effects.list
  184. var n = effects.length
  185. // 成效节点均匀分布在上方半圆
  186. for (var i = 0; i < n; i++) {
  187. var angle = -Math.PI / 2 - (Math.PI / (n + 1)) * (i + 1)
  188. var r = minDim * 0.30
  189. this.effectNodes.push({
  190. id: effects[i].id,
  191. name: '成交 #' + effects[i].id,
  192. displayName: 'E' + (i + 1),
  193. x: cx + r * Math.cos(angle),
  194. y: cy + r * Math.sin(angle),
  195. radius: 26,
  196. effectType: effects[i].effectType,
  197. effectTypeList: effects[i].effectTypeList || [],
  198. effectAmount: effects[i].effectAmount,
  199. // 家庭汇集:节点按成员着色,并携带成员标注
  200. color: this.familyMode ? this._familyMemberColor(effects[i].colorKey || '') : this._getTypeColor(effects[i].effectType),
  201. memberId: effects[i].memberId || null,
  202. memberName: effects[i].memberName || ''
  203. })
  204. }
  205. },
  206. buildExpansionNodes: function() {
  207. if (!this.expansions || this.expansions.length === 0) {
  208. this.expansionNodes = []
  209. return
  210. }
  211. var cx = this.canvasWidth / 2
  212. var cy = this.canvasHeight / 2
  213. var minDim = Math.min(this.canvasWidth, this.canvasHeight)
  214. var n = this.expansions.length
  215. var baseRadius = minDim * 0.38
  216. for (var i = 0; i < n; i++) {
  217. var angle = -Math.PI / 2 - (Math.PI / (n + 1)) * (i + 1)
  218. var r = baseRadius + 40
  219. this.expansionNodes.push({
  220. id: 'exp_' + i,
  221. name: this.expansions[i].keyword,
  222. displayName: this.expansions[i].keyword.substring(0, 3),
  223. x: cx + r * Math.cos(angle),
  224. y: cy + r * Math.sin(angle),
  225. radius: 20,
  226. dimension: this.expansions[i].dimension,
  227. isExpansion: true
  228. })
  229. }
  230. },
  231. drawAll: function() {
  232. if (!this.ctx) return
  233. var ctx = this.ctx
  234. var w = this.canvasWidth
  235. var h = this.canvasHeight
  236. ctx.clearRect(0, 0, w, h)
  237. this._drawOctopusBackground(ctx)
  238. this._drawCenterNode(ctx)
  239. this._drawEdges(ctx)
  240. for (var i = 0; i < this.effectNodes.length; i++) {
  241. this._drawEffectNode(ctx, this.effectNodes[i])
  242. }
  243. for (var j = 0; j < this.expansionNodes.length; j++) {
  244. this._drawExpansionNode(ctx, this.expansionNodes[j])
  245. }
  246. this._drawLabels(ctx)
  247. },
  248. /** 绘制章鱼背景:头部 + 8 条波浪触手(低透明度,衬托节点) */
  249. _drawOctopusBackground: function(ctx) {
  250. var cx = this.canvasWidth / 2
  251. var cy = this.canvasHeight / 2
  252. var minDim = Math.min(this.canvasWidth, this.canvasHeight)
  253. var headR = minDim * 0.12
  254. // 触手:8 条波浪曲线向外延伸
  255. for (var i = 0; i < 8; i++) {
  256. var angle = (i / 8) * Math.PI * 2 - Math.PI / 2
  257. var len = minDim * 0.36
  258. var sx = cx + Math.cos(angle) * headR
  259. var sy = cy + Math.sin(angle) * headR
  260. var ex = cx + Math.cos(angle) * (headR + len)
  261. var ey = cy + Math.sin(angle) * (headR + len)
  262. // 控制点:垂直偏移制造波浪
  263. var perp = angle + Math.PI / 2
  264. var mid = headR + len * 0.5
  265. var wave = Math.sin(i * 2.2) * len * 0.16
  266. var mx = cx + Math.cos(angle) * mid + Math.cos(perp) * wave
  267. var my = cy + Math.sin(angle) * mid + Math.sin(perp) * wave
  268. ctx.beginPath()
  269. ctx.moveTo(sx, sy)
  270. ctx.quadraticCurveTo(mx, my, ex, ey)
  271. ctx.strokeStyle = 'rgba(16, 185, 129, 0.12)'
  272. ctx.lineWidth = 6
  273. ctx.lineCap = 'round'
  274. ctx.stroke()
  275. }
  276. // 头部(半透明圆)
  277. ctx.beginPath()
  278. ctx.arc(cx, cy, headR, 0, Math.PI * 2)
  279. ctx.fillStyle = 'rgba(16, 185, 129, 0.08)'
  280. ctx.fill()
  281. // 眼睛
  282. var eyeOff = headR * 0.35
  283. var eyeR = Math.max(3, headR * 0.09)
  284. ctx.beginPath()
  285. ctx.arc(cx - eyeOff, cy - headR * 0.12, eyeR, 0, Math.PI * 2)
  286. ctx.fillStyle = 'rgba(16, 185, 129, 0.22)'
  287. ctx.fill()
  288. ctx.beginPath()
  289. ctx.arc(cx + eyeOff, cy - headR * 0.12, eyeR, 0, Math.PI * 2)
  290. ctx.fill()
  291. },
  292. _drawCenterNode: function(ctx) {
  293. var cx = this.canvasWidth / 2
  294. var cy = this.canvasHeight / 2
  295. var r = 30
  296. ctx.beginPath()
  297. ctx.arc(cx, cy, r + 4, 0, Math.PI * 2)
  298. ctx.fillStyle = this._hexToRgba(BrandColors.success, 0.15)
  299. ctx.fill()
  300. ctx.beginPath()
  301. ctx.arc(cx, cy, r, 0, Math.PI * 2)
  302. ctx.fillStyle = BrandColors.success
  303. ctx.fill()
  304. ctx.strokeStyle = BrandColors.white
  305. ctx.lineWidth = 3
  306. ctx.stroke()
  307. ctx.font = 'bold 18px PingFang SC, sans-serif'
  308. ctx.fillStyle = BrandColors.white
  309. ctx.textAlign = 'center'
  310. ctx.textBaseline = 'middle'
  311. ctx.fillText('我', cx, cy + 1)
  312. },
  313. _drawEffectNode: function(ctx, node) {
  314. var x = node.x, y = node.y, r = node.radius
  315. if (x - r > this.canvasWidth || x + r < 0 || y - r > this.canvasHeight || y + r < 0) return
  316. // 光晕
  317. ctx.beginPath()
  318. ctx.arc(x, y, r + 3, 0, Math.PI * 2)
  319. ctx.fillStyle = this._hexToRgba(node.color, 0.12)
  320. ctx.fill()
  321. // 主体
  322. ctx.beginPath()
  323. ctx.arc(x, y, r, 0, Math.PI * 2)
  324. ctx.fillStyle = node.color
  325. ctx.fill()
  326. ctx.strokeStyle = BrandColors.white
  327. ctx.lineWidth = 2
  328. ctx.stroke()
  329. // 首字符
  330. ctx.font = 'bold ' + Math.max(14, r * 0.7) + 'px PingFang SC, sans-serif'
  331. ctx.fillStyle = BrandColors.white
  332. ctx.textAlign = 'center'
  333. ctx.textBaseline = 'middle'
  334. ctx.fillText(node.displayName, x, y + 1)
  335. },
  336. _drawExpansionNode: function(ctx, node) {
  337. var x = node.x, y = node.y, r = node.radius
  338. if (x - r > this.canvasWidth || x + r < 0 || y - r > this.canvasHeight || y + r < 0) return
  339. // 虚线圆
  340. ctx.beginPath()
  341. ctx.arc(x, y, r, 0, Math.PI * 2)
  342. ctx.strokeStyle = this.expansionColor
  343. ctx.lineWidth = 2
  344. ctx.setLineDash([4, 4])
  345. ctx.stroke()
  346. ctx.setLineDash([])
  347. // 填充
  348. ctx.beginPath()
  349. ctx.arc(x, y, r - 1, 0, Math.PI * 2)
  350. ctx.fillStyle = this._hexToRgba(this.expansionColor, 0.15)
  351. ctx.fill()
  352. // 文字
  353. ctx.font = '11px PingFang SC, sans-serif'
  354. ctx.fillStyle = this.expansionColor
  355. ctx.textAlign = 'center'
  356. ctx.textBaseline = 'middle'
  357. ctx.fillText(node.displayName, x, y + 1)
  358. },
  359. _drawEdges: function(ctx) {
  360. var cx = this.canvasWidth / 2
  361. var cy = this.canvasHeight / 2
  362. for (var i = 0; i < this.effectNodes.length; i++) {
  363. var node = this.effectNodes[i]
  364. ctx.beginPath()
  365. ctx.moveTo(cx, cy)
  366. ctx.lineTo(node.x, node.y)
  367. ctx.strokeStyle = this._hexToRgba(node.color, 0.3)
  368. ctx.lineWidth = 1.5
  369. ctx.stroke()
  370. }
  371. // 成效节点到拓展方向节点的边(虚线)
  372. for (var i = 0; i < this.effectNodes.length && i < this.expansionNodes.length; i++) {
  373. var e1 = this.effectNodes[i]
  374. var e2 = this.expansionNodes[i]
  375. ctx.beginPath()
  376. ctx.moveTo(e1.x, e1.y)
  377. ctx.lineTo(e2.x, e2.y)
  378. ctx.strokeStyle = this._hexToRgba(this.expansionColor, 0.2)
  379. ctx.lineWidth = 1
  380. ctx.setLineDash([3, 3])
  381. ctx.stroke()
  382. ctx.setLineDash([])
  383. }
  384. },
  385. _drawLabels: function(ctx) {
  386. var cx = this.canvasWidth / 2
  387. var cy = this.canvasHeight / 2
  388. // 成效标签(每侧显示)
  389. for (var i = 0; i < this.effectNodes.length; i++) {
  390. var node = this.effectNodes[i]
  391. var dx = node.x - cx
  392. var dy = node.y - cy
  393. var dist = Math.sqrt(dx * dx + dy * dy) || 1
  394. var lx = node.x + (dx / dist) * 36
  395. var ly = node.y + (dy / dist) * 20
  396. ctx.font = '11px PingFang SC, sans-serif'
  397. ctx.fillStyle = BrandColors.slate500
  398. ctx.textAlign = 'center'
  399. ctx.textBaseline = 'middle'
  400. var label = ''
  401. if (node.effectTypeList && node.effectTypeList.length > 0) {
  402. label = node.effectTypeList.slice(0, 2).join('/')
  403. }
  404. if (label) ctx.fillText(label, lx, ly)
  405. }
  406. },
  407. onTouchStart: function(e) {
  408. if (!this.interactive) return
  409. var touches = e.touches || e.changedTouches
  410. if (!touches || touches.length === 0) return
  411. var touch = touches[0]
  412. var rect = this._getCanvasRect()
  413. if (!rect) return
  414. var x = touch.clientX - rect.left
  415. var y = touch.clientY - rect.top
  416. this._touchStartPos = { x: x, y: y }
  417. var hit = this.hitTest(x, y)
  418. if (!hit) {
  419. this.selectedEffect = null
  420. }
  421. },
  422. onTouchMove: function(e) {
  423. if (!this._touchStartPos) return
  424. var touches = e.touches || e.changedTouches
  425. if (!touches || touches.length === 0) return
  426. var touch = touches[0]
  427. var rect = this._getCanvasRect()
  428. if (!rect) return
  429. var x = touch.clientX - rect.left
  430. var y = touch.clientY - rect.top
  431. var dx = x - this._touchStartPos.x
  432. var dy = y - this._touchStartPos.y
  433. if (dx * dx + dy * dy > 100) {
  434. this._touchStartPos = null
  435. }
  436. },
  437. onTouchEnd: function(e) {
  438. if (!this._touchStartPos) return
  439. var touches = e.changedTouches
  440. if (!touches || touches.length === 0) {
  441. this._touchStartPos = null
  442. return
  443. }
  444. var touch = touches[0]
  445. var rect = this._getCanvasRect()
  446. if (!rect) { this._touchStartPos = null; return }
  447. var x = touch.clientX - rect.left
  448. var y = touch.clientY - rect.top
  449. var dx = x - this._touchStartPos.x
  450. var dy = y - this._touchStartPos.y
  451. this._touchStartPos = null
  452. if (dx * dx + dy * dy > 100) return
  453. var hit = this.hitTest(x, y)
  454. if (hit) {
  455. this.selectedEffect = hit
  456. } else {
  457. this.selectedEffect = null
  458. }
  459. },
  460. hitTest: function(x, y) {
  461. // 先检查成效节点
  462. for (var i = 0; i < this.effectNodes.length; i++) {
  463. var n = this.effectNodes[i]
  464. var dx = x - n.x
  465. var dy = y - n.y
  466. if (dx * dx + dy * dy <= n.radius * n.radius) return n
  467. }
  468. // 再检查拓展方向节点
  469. for (var j = 0; j < this.expansionNodes.length; j++) {
  470. var n = this.expansionNodes[j]
  471. var dx = x - n.x
  472. var dy = y - n.y
  473. if (dx * dx + dy * dy <= n.radius * n.radius) {
  474. // 拓展节点 → 触发 expansion-click 事件
  475. this.$emit('expansion-click', n)
  476. return n
  477. }
  478. }
  479. return null
  480. },
  481. onAddKeyPerson: function(effect) {
  482. this.$emit('effect-click', effect)
  483. // 如果有关键人列表则显示,否则触发 add-key-person
  484. if (!this.keyPersons[effect.id] || this.keyPersons[effect.id].length === 0) {
  485. this.$emit('add-key-person')
  486. }
  487. },
  488. initCanvas: function(cb) {
  489. var self = this
  490. uni.createSelectorQuery().in(this)
  491. .select('#octopusCanvas')
  492. .fields({ node: true, size: true, rect: true })
  493. .exec(function(res) {
  494. try {
  495. if (self._destroyed) return
  496. if (!res || !res[0]) { if (cb) cb(false); return }
  497. var info = res[0]
  498. if (!info.node) { if (cb) cb(false); return }
  499. var canvas = info.node
  500. var width = info.width
  501. var height = info.height
  502. if (!width || !height) {
  503. var winWidth = 375
  504. try {
  505. var wi = uni.getWindowInfo()
  506. winWidth = wi.windowWidth || 375
  507. } catch (e) {}
  508. width = Math.round((self.canvasWidth || 340) * winWidth / 750)
  509. height = Math.round((self.canvasHeight || 420) * winWidth / 750)
  510. }
  511. var pixelRatio = 2
  512. try {
  513. var sys = uni.getWindowInfo()
  514. pixelRatio = sys.pixelRatio || 2
  515. } catch (e) {}
  516. canvas.width = width * pixelRatio
  517. canvas.height = height * pixelRatio
  518. var ctx = canvas.getContext('2d')
  519. if (!ctx) { if (cb) cb(false); return }
  520. ctx.scale(pixelRatio, pixelRatio)
  521. self.ctx = ctx
  522. self._canvasNode = canvas
  523. self.canvasWidth = width
  524. self.canvasHeight = height
  525. self.dpr = pixelRatio
  526. self.canvasReady = true
  527. if (info.rect) {
  528. self._canvasRect = { left: info.rect.left || 0, top: info.rect.top || 0 }
  529. }
  530. self.buildLayout()
  531. self.buildExpansionNodes()
  532. self.drawAll()
  533. if (cb) cb(true)
  534. } catch (e) {
  535. if (cb) cb(false)
  536. }
  537. })
  538. },
  539. _getCanvasRect: function() {
  540. if (this._canvasRect) {
  541. return { left: this._canvasRect.left, top: this._canvasRect.top, width: this.canvasWidth, height: this.canvasHeight }
  542. }
  543. return { left: 0, top: 0, width: this.canvasWidth, height: this.canvasHeight }
  544. },
  545. _hexToRgba: function(hex, alpha) {
  546. var r = parseInt(hex.slice(1, 3), 16)
  547. var g = parseInt(hex.slice(3, 5), 16)
  548. var b = parseInt(hex.slice(5, 7), 16)
  549. return 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')'
  550. },
  551. _getTypeColor: function(type) {
  552. if (!type) return BrandColors.slate500
  553. // 返回第一个类型对应的颜色
  554. var list = []
  555. if (type & 1) list.push('金额大')
  556. if (type & 2) list.push('频次高')
  557. if (type & 4) list.push('新成交')
  558. if (list.length === 0) return BrandColors.slate500
  559. return this.effectColors[list[0]] || BrandColors.slate500
  560. },
  561. /** 家庭汇集成员色:colorKey 稳定哈希取模映射到 8 色板 */
  562. memberColorFor: function(colorKey) {
  563. if (!colorKey || typeof colorKey !== 'string') return FAMILY_MEMBER_COLORS[0]
  564. var hash = 0
  565. for (var i = 0; i < colorKey.length; i++) {
  566. hash = (hash * 31 + colorKey.charCodeAt(i)) >>> 0
  567. }
  568. return FAMILY_MEMBER_COLORS[hash % FAMILY_MEMBER_COLORS.length]
  569. },
  570. /** 家庭汇集成员色(canvas 用,同 memberColorFor) */
  571. _familyMemberColor: function(colorKey) {
  572. return this.memberColorFor(colorKey)
  573. },
  574. formatDate: function(dateStr) {
  575. if (!dateStr) return ''
  576. return String(dateStr).substring(0, 10)
  577. }
  578. }
  579. }
  580. </script>
  581. <style scoped lang="scss">
  582. .octopus-wrapper {
  583. position: relative;
  584. width: 100%;
  585. min-height: 200px;
  586. height: auto;
  587. margin: 10rpx 0;
  588. background: $app-surface;
  589. border-radius: 24rpx;
  590. overflow: hidden;
  591. }
  592. .octopus-canvas {
  593. position: absolute;
  594. top: 0;
  595. left: 0;
  596. z-index: 1;
  597. }
  598. /* 家庭汇集:成员图例(canvas 下方,按成员着色) */
  599. .octopus-member-legend {
  600. position: relative;
  601. z-index: 2;
  602. display: flex;
  603. flex-direction: row;
  604. flex-wrap: wrap;
  605. align-items: center;
  606. gap: 8rpx 20rpx;
  607. padding: 20rpx 8rpx 8rpx;
  608. }
  609. .legend-item {
  610. display: flex;
  611. flex-direction: row;
  612. align-items: center;
  613. gap: 8rpx;
  614. }
  615. .legend-dot {
  616. width: 20rpx;
  617. height: 20rpx;
  618. border-radius: 50%;
  619. flex-shrink: 0;
  620. }
  621. .legend-label {
  622. font-size: calc(22rpx * var(--font-scale, 1));
  623. color: $app-text-subtle;
  624. }
  625. .octopus-empty, .octopus-loading {
  626. display: flex;
  627. flex-direction: column;
  628. align-items: center;
  629. justify-content: center;
  630. padding: 60rpx 20rpx;
  631. background: $app-surface;
  632. border-radius: 24rpx;
  633. margin: 10rpx 0;
  634. }
  635. .empty-icon {
  636. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  637. margin-bottom: 16rpx;
  638. }
  639. .empty-text {
  640. font-size: calc(28rpx * var(--font-scale, 1));
  641. font-weight: 600;
  642. color: $uni-text-color;
  643. margin-bottom: 8rpx;
  644. }
  645. .empty-hint {
  646. font-size: calc(24rpx * var(--font-scale, 1));
  647. color: $app-text-subtle;
  648. margin-bottom: 24rpx;
  649. }
  650. .loading-text {
  651. font-size: calc(28rpx * var(--font-scale, 1));
  652. color: $uni-text-color-secondary;
  653. }
  654. .action-btn {
  655. display: inline-block;
  656. padding: 12rpx 32rpx;
  657. border-radius: 24rpx;
  658. font-size: calc(26rpx * var(--font-scale, 1));
  659. font-weight: 600;
  660. }
  661. .action-btn-primary {
  662. background: $brand-primary;
  663. color: $app-surface;
  664. }
  665. .reanalyze-btn {
  666. position: absolute;
  667. top: 12rpx;
  668. right: 16rpx;
  669. z-index: 10;
  670. background: rgba(255,255,255,0.9);
  671. border-radius: 24rpx;
  672. padding: 8rpx 16rpx;
  673. font-size: calc(22rpx * var(--font-scale, 1));
  674. color: $uni-text-color-secondary;
  675. }
  676. /* 成效弹窗 */
  677. .effect-popup {
  678. position: absolute;
  679. z-index: 10;
  680. pointer-events: none;
  681. transform: translate(-50%, -100%);
  682. margin-top: -16rpx;
  683. }
  684. .popup-content {
  685. background: $app-surface;
  686. border-radius: 16rpx;
  687. padding: 20rpx;
  688. box-shadow: 0 8rpx 24rpx rgba(0,0,0,0.12);
  689. min-width: 200rpx;
  690. max-width: 320rpx;
  691. pointer-events: auto;
  692. }
  693. .popup-header {
  694. display: flex;
  695. align-items: center;
  696. margin-bottom: 12rpx;
  697. }
  698. .popup-name {
  699. font-size: calc(26rpx * var(--font-scale, 1));
  700. font-weight: 600;
  701. color: $uni-text-color;
  702. }
  703. .popup-member {
  704. font-size: calc(20rpx * var(--font-scale, 1));
  705. color: $facade-text-slate;
  706. margin-top: 2rpx;
  707. }
  708. .popup-tags {
  709. display: flex;
  710. flex-wrap: wrap;
  711. margin-top: 4rpx;
  712. }
  713. .popup-tag {
  714. font-size: calc(18rpx * var(--font-scale, 1));
  715. padding: 2rpx 10rpx;
  716. border-radius: 8rpx;
  717. margin-right: 8rpx;
  718. margin-top: 4rpx;
  719. background: $uni-primary-pale;
  720. color: $brand-primary;
  721. }
  722. .popup-keypersons {
  723. border-top: 1rpx solid $facade-fill-slate;
  724. padding-top: 10rpx;
  725. margin-top: 8rpx;
  726. }
  727. .popup-title {
  728. font-size: calc(22rpx * var(--font-scale, 1));
  729. color: $app-text-subtle;
  730. display: block;
  731. margin-bottom: 6rpx;
  732. }
  733. .kp-item {
  734. font-size: calc(22rpx * var(--font-scale, 1));
  735. color: $facade-text-slate-deep;
  736. display: block;
  737. margin-bottom: 4rpx;
  738. }
  739. .kp-item-row {
  740. display: flex;
  741. align-items: center;
  742. gap: 8rpx;
  743. }
  744. .kp-sys-badge {
  745. font-size: calc(18rpx * var(--font-scale, 1));
  746. color: $status-success;
  747. background: $status-success-tint;
  748. padding: 2rpx 10rpx;
  749. border-radius: 8rpx;
  750. flex-shrink: 0;
  751. }
  752. .popup-empty {
  753. padding: 8rpx 0;
  754. }
  755. .empty-text {
  756. font-size: calc(22rpx * var(--font-scale, 1));
  757. color: $app-text-subtle;
  758. }
  759. .popup-actions {
  760. display: flex;
  761. justify-content: center;
  762. margin-top: 12rpx;
  763. padding-top: 12rpx;
  764. border-top: 1rpx solid $facade-fill-slate;
  765. }
  766. </style>