wuxing-sandbox.vue 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. <template>
  2. <view class="wuxing-sandbox">
  3. <!-- header slot -->
  4. <slot name="header">
  5. <view class="sandbox-header">
  6. <image class="sandbox-logo" src="/static/logo.png" mode="aspectFit"></image>
  7. <text class="sandbox-title">浠艾福能量沙盘</text>
  8. <!-- 右上角:live 模式显示个人/全家切换(preview 为轻量引导条,徽章移入引导条) -->
  9. <view class="sandbox-switch" v-if="!isPreview" @click="onToggle">
  10. <image class="sandbox-switch-icon" :src="activeMemberIcon" mode="aspectFit"></image>
  11. <text class="sandbox-switch-label">{{ activeMemberLabel }}</text>
  12. <image class="sandbox-switch-arrow" :src="uiIcons.swap" mode="aspectFit"></image>
  13. </view>
  14. </view>
  15. </slot>
  16. <!-- live:完整星图(双 Canvas + 点击感应层) -->
  17. <view class="sandbox-star" v-if="!isPreview" :style="{height: starHeight}">
  18. <view class="star-glow-bg"></view>
  19. <!-- Canvas 2D 五行星图(绘制五边形+五维能量,读屏以 aria-label 替代非文本图形,修复 WCAG 1.1.1) -->
  20. <canvas id="starCanvas" type="2d" role="img" :aria-label="starAriaLabel" style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:1"></canvas>
  21. <!-- Canvas 粒子动画覆盖层(纯装饰,读屏忽略) -->
  22. <canvas id="flowCanvas" type="2d" aria-hidden="true" style="width:100%;height:100%;position:absolute;top:0;left:0;z-index:1;pointer-events:none"></canvas>
  23. <!-- Canvas 点击感应层 -->
  24. <view class="canvas-tap-area" @click="onCanvasTap"></view>
  25. <!-- 顶点:sortedDimensions 顺时针 [心, 身, 智, 富, 行](图标形状承载五行语义) -->
  26. <view
  27. v-for="pt in sortedDimensions"
  28. :key="pt.code"
  29. class="star-point"
  30. :class="pt.pointClass"
  31. @click="onPointClick(pt.code)"
  32. >
  33. <view class="point-dot" :class="pt.dotClass">
  34. <image class="point-icon" :src="pt.iconUri" mode="aspectFit"></image>
  35. </view>
  36. <text class="point-label">{{ pt.name }}</text>
  37. <text class="point-energy" :class="pt.energyClass">{{ pt.energy }}</text>
  38. </view>
  39. <!-- 中心点击区域(太极由 Canvas 绘制) -->
  40. <view class="star-center" @click="onCenterClick"></view>
  41. </view>
  42. <!-- preview:轻量引导条(标题 + 徽章 + 弱化五边形轮廓,不画完整星图) -->
  43. <view class="sandbox-preview" v-else @click="onPreviewTap">
  44. <image class="preview-pentagon" :src="uiIcons.pentagon" mode="aspectFit"></image>
  45. <view class="preview-copy">
  46. <text class="preview-title">五维能量沙盘</text>
  47. <text class="preview-sub">亲子共同成长的能量地图</text>
  48. </view>
  49. <text class="preview-badge">{{ displayBadgeText }}</text>
  50. </view>
  51. <!-- dimension-bar slot -->
  52. <slot name="dimension-bar" v-if="showDimensionBar && !isPreview">
  53. <view class="dimension-bar">
  54. <view
  55. v-for="item in linearDimensions"
  56. :key="item.code"
  57. class="dim-item"
  58. :class="item.itemClass"
  59. >
  60. <image class="dim-item-icon" :src="item.iconUri" mode="aspectFit"></image>
  61. <text class="dim-item-text">{{ item.name }} {{ item.energy }}%</text>
  62. </view>
  63. </view>
  64. </slot>
  65. <!-- 成员选择面板 -->
  66. <view v-if="memberPickerVisible" class="member-picker-mask" @click="memberPickerVisible = false">
  67. <view class="member-picker-panel" @click.stop>
  68. <view class="member-picker-title">切换视角</view>
  69. <view
  70. v-for="option in memberOptionsActive"
  71. :key="option.key"
  72. class="member-picker-item"
  73. :class="{ 'member-picker-item--active': option.active }"
  74. @click="selectOption(option)"
  75. >
  76. <text class="member-picker-label">{{ option.label }}</text>
  77. <image class="member-picker-check" v-if="option.active" :src="uiIcons.check" mode="aspectFit"></image>
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import { renderStar, calcStarVertices } from './wuxing-sandbox-helpers/render.js'
  85. import { FillAnimator } from './wuxing-sandbox-helpers/animation.js'
  86. import { detectHitArm, armIndexToDomain } from './wuxing-sandbox-helpers/hitdetect.js'
  87. import { FlowAnimation } from './wuxing-sandbox-helpers/flow-animation.js'
  88. import {
  89. buildMemberOptions,
  90. findMemberById,
  91. getSelfMember,
  92. nextSandboxSelection,
  93. resolveSandboxOverall,
  94. resolveSandboxScores
  95. } from '@/utils/wuxing-sandbox.js'
  96. /**
  97. * 五行图标(内联 SVG,24×24 viewBox / stroke-width 1.8 / 无填充)
  98. *
  99. * mp-weixin WXML 不支持模板内联 <svg>,故用 data:image/svg+xml 作为 <image> src
  100. * (仓库既有模式:pages/shop/order-list/order-list.vue)。SVG 文档是独立渲染上下文,
  101. * currentColor 无法继承宿主 CSS,故 stroke 颜色在此处按 $dim-data-* / $brand-primary-deep
  102. * 词表取色(与 uni.scss 令牌一一对应,注释映射;Canvas/JS 侧无法使用 SCSS 变量)。
  103. *
  104. * 五行语义由形状承载:心=火焰 身=山 智=闪电 富=水滴 行=叶。
  105. */
  106. function svgUri(paths, color) {
  107. var inner = ''
  108. for (var i = 0; i < paths.length; i++) {
  109. inner += '<path d="' + paths[i] + '"/>'
  110. }
  111. var svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="' +
  112. color + '" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">' + inner + '</svg>'
  113. return 'data:image/svg+xml,' + encodeURIComponent(svg)
  114. }
  115. var ICON_PATHS = {
  116. flame: [
  117. 'M12 2.8c1.9 2.7 3.5 4.9 3.5 7.7a3.5 3.5 0 0 1-7 0c0-1.4.6-2.6 1.7-3.7.2 1.2.9 2 1.9 2.4-.6-2.3 0-4.6-.1-6.4z',
  118. 'M12 7.6c1 1.4 1.7 2.4 1.7 3.7a1.7 1.7 0 0 1-3.4 0c0-1.3.7-2.3 1.7-3.7z'
  119. ],
  120. mountain: [
  121. 'M2.8 19.2 8.8 8.6l3.4 5.6 2.2-3.6 6.8 8.6z'
  122. ],
  123. lightning: [
  124. 'M13.4 2.6 5.8 13.4h4.9l-1 8 7.6-10.8h-4.9z'
  125. ],
  126. droplet: [
  127. 'M12 2.8c3.2 3.8 5.2 6.5 5.2 9.2a5.2 5.2 0 0 1-10.4 0c0-2.7 2-5.4 5.2-9.2z'
  128. ],
  129. leaf: [
  130. 'M5.6 18.6C6 11.4 11.6 5.2 19.6 4.6c.4 8.2-5.6 14.2-14 14z',
  131. 'M7 17c2.6-4.3 6-7.4 10-9.6'
  132. ],
  133. person: [
  134. 'M12 11.6a3.4 3.4 0 1 0 0-6.8 3.4 3.4 0 0 0 0 6.8z',
  135. 'M5.8 20.2c.5-3.5 3-5.2 6.2-5.2s5.7 1.7 6.2 5.2'
  136. ],
  137. family: [
  138. 'M8.6 12.2a3 3 0 1 0 0-6 3 3 0 0 0 0 6z',
  139. 'M3.4 19.6c.4-3.1 2.6-4.6 5.2-4.6s4.8 1.5 5.2 4.6',
  140. 'M17.4 8.6a2.2 2.2 0 1 0 0-4.4 2.2 2.2 0 0 0 0 4.4z',
  141. 'M17.8 15.2c1.4.3 2.8 1.6 2.8 3.6'
  142. ],
  143. swap: [
  144. 'M4.4 9.4h14.2',
  145. 'M15.6 6.6l3 2.8-3 2.8',
  146. 'M19.6 14.6H5.4',
  147. 'M8.4 11.8l-3 2.8 3 2.8'
  148. ],
  149. check: [
  150. 'M5 12.8 9.6 17.4 19 7.6'
  151. ],
  152. pentagon: [
  153. 'M12 3.4 20.8 9.2l-3.4 10.2H6.6L3.2 9.2z'
  154. ]
  155. }
  156. // 数据笔触深档(与 uni.scss $dim-data-* 一致)—— 图标颜色 = 数据笔触色,保证对填充 #FCE7CE ≥3:1
  157. var DIM_ICON_URIS = {
  158. mind: svgUri(ICON_PATHS.flame, '#9D174D'), // $dim-data-mind #9D174D
  159. body: svgUri(ICON_PATHS.mountain, '#9C4A1A'), // $dim-data-body #9C4A1A
  160. wisdom: svgUri(ICON_PATHS.lightning,'#4338CA'), // $dim-data-wisdom #4338CA
  161. wealth: svgUri(ICON_PATHS.droplet, '#92400E'), // $dim-data-wealth #92400E
  162. action: svgUri(ICON_PATHS.leaf, '#047857') // $dim-data-action #047857
  163. }
  164. // UI 图标(品牌深橙,对白底 / 品牌浅底均 ≥3:1;$brand-primary-deep #C2410C)
  165. var UI_ICON_URIS = {
  166. person: svgUri(ICON_PATHS.person, '#C2410C'),
  167. family: svgUri(ICON_PATHS.family, '#C2410C'),
  168. swap: svgUri(ICON_PATHS.swap, '#C2410C'),
  169. check: svgUri(ICON_PATHS.check, '#C2410C'),
  170. pentagon: svgUri(ICON_PATHS.pentagon, '#C2410C')
  171. }
  172. /** 环形顶点定位类(与 sortedDimensions 序一一对应:上/右/右下/左下/左) */
  173. var RING_POS = [
  174. 'star-point--top star-point--row',
  175. 'star-point--right',
  176. 'star-point--lr star-point--row',
  177. 'star-point--ll star-point--rev',
  178. 'star-point--left'
  179. ]
  180. /** 线性维度条顺序(规格 §4.2):身 -> 智 -> 富 -> 行 -> 心;idx = activeScores 下标 */
  181. var LINEAR_ORDER = [
  182. { code: 'body', idx: 0 },
  183. { code: 'wisdom', idx: 2 },
  184. { code: 'wealth', idx: 4 },
  185. { code: 'action', idx: 3 },
  186. { code: 'mind', idx: 1 }
  187. ]
  188. /** 维度 -> 五行映射(API 缺字段时的兜底;图标一律用内置 SVG,忽略接口返回的 emoji) */
  189. var DIM_META = {
  190. mind: { name: '心', element: '火' },
  191. body: { name: '身', element: '土' },
  192. wisdom: { name: '智', element: '金' },
  193. wealth: { name: '富', element: '水' },
  194. action: { name: '行', element: '木' }
  195. }
  196. export default {
  197. props: {
  198. /** 模式:'preview' 引导条 / 'live' 真实数据 */
  199. mode: {
  200. type: String,
  201. default: 'preview',
  202. validator: function(v) { return v === 'preview' || v === 'live' }
  203. },
  204. /** 五维分值对象 {body,mind,wisdom,action,wealth},0-100 */
  205. scores: {
  206. type: Object,
  207. default: function() { return { body: 0, mind: 0, wisdom: 0, action: 0, wealth: 0 } }
  208. },
  209. /** 综合分值 0-100 */
  210. overallScore: {
  211. type: Number,
  212. default: 0
  213. },
  214. /** 家庭成员数组(MemberEnergyDTO 格式) */
  215. members: {
  216. type: Array,
  217. default: function() { return [] }
  218. },
  219. /** "我的"个人数据(参数指定成员的 EnergySandboxDTO,含 bodyScore/mindScore/.../overallScore) */
  220. personalData: {
  221. type: Object,
  222. default: null
  223. },
  224. /** 顶部徽章文字,空则自动计算 */
  225. badgeText: {
  226. type: String,
  227. default: ''
  228. },
  229. /** 是否显示底部维度条 */
  230. showDimensionBar: {
  231. type: Boolean,
  232. default: true
  233. },
  234. /** 五维能量数据(账本模式) */
  235. dimensions: {
  236. type: Array,
  237. default: function() { return [] }
  238. },
  239. totalEnergy: {
  240. type: Number,
  241. default: 0
  242. },
  243. totalHealthIndex: {
  244. type: Number,
  245. default: 0
  246. },
  247. /** 星图区域高度 */
  248. starHeight: {
  249. type: String,
  250. default: '540rpx'
  251. },
  252. /** 是否启用全家沙盘切换(控制个人/全家开关显隐) */
  253. familyUnlocked: {
  254. type: Boolean,
  255. default: false
  256. },
  257. /** 家庭真实成员数(来自 /api/family/member/list,用于全家切换判断;缺省时用 members 长度) */
  258. memberCount: {
  259. type: Number,
  260. default: 0
  261. },
  262. /** 当前视角模式(由父组件控制) */
  263. currentView: {
  264. type: String,
  265. default: 'personal',
  266. validator: function(v) { return v === 'personal' || v === 'family' }
  267. }
  268. },
  269. data: function() {
  270. return {
  271. viewMode: 'personal',
  272. selectedMemberId: null,
  273. memberPickerVisible: false,
  274. previewScores: [0, 0, 0, 0, 0],
  275. canvasReady: false,
  276. canvasWidth: 0,
  277. canvasHeight: 0,
  278. ctx: null,
  279. dpr: 1,
  280. _canvasLeft: 0,
  281. _canvasTop: 0,
  282. animBody: 0,
  283. animMind: 0,
  284. animWisdom: 0,
  285. animAction: 0,
  286. animWealth: 0,
  287. _animator: null,
  288. _flowAnim: null,
  289. _flowCanvasReady: false,
  290. uiIcons: UI_ICON_URIS
  291. }
  292. },
  293. computed: {
  294. isPreview: function() {
  295. return this.mode === 'preview'
  296. },
  297. selfMember: function() {
  298. return getSelfMember(this.personalData)
  299. },
  300. memberOptions: function() {
  301. return buildMemberOptions(this.members, this.personalData)
  302. },
  303. /** 成员选择选项(预计算 active 标志,避免模板 :class 中调用方法) */
  304. memberOptionsActive: function() {
  305. return this.memberOptions.map(function(option) {
  306. var active = option.viewMode === this.viewMode &&
  307. (option.viewMode !== 'member' || String(option.memberId) === String(this.selectedMemberId))
  308. return {
  309. key: option.key,
  310. label: option.label,
  311. viewMode: option.viewMode,
  312. memberId: option.memberId,
  313. active: active
  314. }
  315. }, this)
  316. },
  317. totalMemberCount: function() {
  318. if (this.memberCount > 0) return this.memberCount
  319. return this.members ? this.members.length : 0
  320. },
  321. currentMember: function() {
  322. if (this.isPreview || this.viewMode === 'family') return null
  323. if (this.viewMode === 'member') {
  324. return findMemberById(this.members, this.selectedMemberId)
  325. }
  326. return this.selfMember
  327. },
  328. selectedMemberIndex: function() {
  329. if (this.viewMode !== 'member' || this.selectedMemberId == null) return -1
  330. var list = this.members || []
  331. for (var i = 0; i < list.length; i++) {
  332. if (String(list[i].memberId) === String(this.selectedMemberId)) return i
  333. }
  334. return -1
  335. },
  336. activeMemberLabel: function() {
  337. if (this.viewMode === 'family') return '全家'
  338. if (this.viewMode === 'member' && this.currentMember) return this.currentMember.name || '家庭成员'
  339. return '本人'
  340. },
  341. activeMemberIcon: function() {
  342. return this.viewMode === 'family' ? this.uiIcons.family : this.uiIcons.person
  343. },
  344. /** 当前活跃的五维值数组 [body, mind, wisdom, action, wealth] */
  345. activeScores: function() {
  346. if (this.isPreview) return this.previewScores
  347. return resolveSandboxScores(this.viewMode, this.scores, this.personalData, this.currentMember)
  348. },
  349. activeOverall: function() {
  350. if (this.isPreview) return 0
  351. return resolveSandboxOverall(this.viewMode, this.scores, this.personalData, this.currentMember)
  352. },
  353. /**
  354. * 动画目标值 ts,按环形顶点序 [mind, body, wisdom, wealth, action] 排列
  355. * (与 render.js DIM_RING / sortedDimensions 顺序一致,不可改)
  356. */
  357. activeTs: function() {
  358. var s = this.activeScores
  359. return [s[1] / 100, s[0] / 100, s[2] / 100, s[4] / 100, s[3] / 100]
  360. },
  361. displayBadgeText: function() {
  362. if (this.badgeText) return this.badgeText
  363. if (this.isPreview) return '登录查看完整报告'
  364. var label = this.viewMode === 'family' ? '家庭能量' : (this.activeMemberLabel + '能量')
  365. return label + ' ' + (this.activeOverall || 0) + '%'
  366. },
  367. _allDimTs: function() {
  368. return this.activeTs
  369. },
  370. hasMultipleMembers: function() {
  371. return this.totalMemberCount > 1
  372. },
  373. /** 家庭能量值(用于太极阳面) */
  374. familyEnergy: function() {
  375. if (this.isPreview) return 0
  376. return this.overallScore || 0
  377. },
  378. /** 当前选中视角的能量值(用于太极阴面) */
  379. personalEnergy: function() {
  380. if (this.isPreview) return 0
  381. return this.activeOverall || 0
  382. },
  383. /**
  384. * 环形顶点序(= sortedDimensions,不可改):心(火)->身(土)->智(金)->富(水)->行(木)
  385. * 心始终在顶部,其余按五行相生序顺时针:左->右->左下->右下
  386. */
  387. sortedDimensions: function() {
  388. var order = ['mind', 'body', 'wisdom', 'wealth', 'action']
  389. var scoreByCode = {
  390. body: this.activeScores[0] || 0,
  391. mind: this.activeScores[1] || 0,
  392. wisdom: this.activeScores[2] || 0,
  393. action: this.activeScores[3] || 0,
  394. wealth: this.activeScores[4] || 0
  395. }
  396. var dimMap = {}
  397. for (var i = 0; i < this.dimensions.length; i++) {
  398. var d = this.dimensions[i]
  399. if (d && d.code) dimMap[d.code] = d
  400. }
  401. var all = []
  402. for (var j = 0; j < order.length; j++) {
  403. var code = order[j]
  404. var d = dimMap[code] || {}
  405. var meta = DIM_META[code] || {}
  406. var score = scoreByCode[code] || 0
  407. all.push({
  408. code: code,
  409. name: d.name || meta.name || '',
  410. element: d.element || meta.element || '',
  411. energy: score,
  412. healthIndex: score,
  413. pointClass: RING_POS[j],
  414. dotClass: 'point-dot--' + code,
  415. energyClass: 'point-energy--' + code,
  416. iconUri: DIM_ICON_URIS[code] || ''
  417. })
  418. }
  419. return all
  420. // all = [心->顶, 身->右, 智->右下, 富->左下, 行->左]
  421. },
  422. /** 星图读屏文本替代(修复 WCAG 1.1.1:canvas 无文本替代);preview 模式不画星图,返回空串 */
  423. starAriaLabel: function() {
  424. if (this.isPreview) return ''
  425. var dims = this.sortedDimensions || []
  426. var parts = dims.map(function(d) {
  427. return (d.name || '') + ' ' + (d.energy || 0)
  428. })
  429. var overall = this.overallScore || 0
  430. return '五维能量星图:' + parts.join('、') + ',综合 ' + overall
  431. },
  432. /** 底部线性维度条(规格 §4.2):身 -> 智 -> 富 -> 行 -> 心 */
  433. linearDimensions: function() {
  434. var out = []
  435. for (var i = 0; i < LINEAR_ORDER.length; i++) {
  436. var def = LINEAR_ORDER[i]
  437. var meta = DIM_META[def.code] || {}
  438. out.push({
  439. code: def.code,
  440. name: meta.name || '',
  441. energy: this.activeScores[def.idx] || 0,
  442. itemClass: 'dim-item--' + def.code,
  443. iconUri: DIM_ICON_URIS[def.code] || ''
  444. })
  445. }
  446. return out
  447. }
  448. },
  449. watch: {
  450. _allDimTs: function() {
  451. if (this.canvasReady) {
  452. this._startAnimation()
  453. }
  454. },
  455. sortedDimensions: function(newVal) {
  456. if (this._flowAnim && this._flowCanvasReady) {
  457. var dimOrder = newVal.map(function(d) { return d.code })
  458. this._flowAnim.refreshPaths({ dimOrder: dimOrder })
  459. }
  460. },
  461. currentView: function(newVal) {
  462. if (!newVal) return
  463. if (newVal !== this.viewMode) {
  464. this.viewMode = newVal
  465. }
  466. if (newVal === 'personal') {
  467. this.selectedMemberId = null
  468. }
  469. },
  470. memberOptions: function(options) {
  471. if (this.viewMode !== 'member') return
  472. var exists = false
  473. for (var i = 0; i < options.length; i++) {
  474. if (options[i].viewMode === 'member' &&
  475. String(options[i].memberId) === String(this.selectedMemberId)) {
  476. exists = true
  477. break
  478. }
  479. }
  480. if (!exists) {
  481. this.viewMode = 'personal'
  482. this.selectedMemberId = null
  483. }
  484. },
  485. hasMultipleMembers: function(newVal) {
  486. if (!newVal && this.viewMode === 'family') {
  487. this.viewMode = 'personal'
  488. }
  489. }
  490. },
  491. mounted: function() {
  492. var self = this
  493. // preview:轻量引导条,不初始化 Canvas、不画完整星图
  494. if (this.isPreview) return
  495. // 等待 DOM 就绪后初始化 Canvas
  496. setTimeout(function() {
  497. self.initCanvas()
  498. }, 100)
  499. },
  500. beforeDestroy: function() {
  501. if (this._animator) {
  502. this._animator.cancel()
  503. this._animator = null
  504. }
  505. if (this._flowAnim) {
  506. this._flowAnim.stop()
  507. this._flowAnim = null
  508. }
  509. },
  510. methods: {
  511. /* ===========================
  512. 视图切换
  513. =========================== */
  514. selectionPayload: function() {
  515. return {
  516. viewMode: this.viewMode,
  517. memberId: this.selectedMemberId,
  518. selectedMemberIndex: this.selectedMemberIndex,
  519. member: this.currentMember
  520. }
  521. },
  522. applySelection: function(selection) {
  523. if (!selection) return
  524. this.memberPickerVisible = false
  525. this.viewMode = selection.viewMode
  526. this.selectedMemberId = selection.viewMode === 'member' ? selection.memberId : null
  527. if (this.viewMode !== 'member') {
  528. this.$emit('view-change', this.viewMode)
  529. }
  530. this.$emit('mode-change', this.selectionPayload())
  531. },
  532. showFamilyRequirement: function() {
  533. uni.showModal({
  534. title: '需要更多家庭成员',
  535. content: '至少需要 2 位家庭成员才能查看全家能量,快去添加吧!',
  536. confirmText: '去添加',
  537. cancelText: '取消',
  538. success: function(res) {
  539. if (res.confirm) {
  540. uni.navigateTo({ url: '/pages/profile-extra/family-members' })
  541. }
  542. }
  543. })
  544. },
  545. selectOption: function(option) {
  546. if (!option) return
  547. if (option.viewMode === 'family' && this.totalMemberCount < 2) {
  548. this.memberPickerVisible = false
  549. this.showFamilyRequirement()
  550. return
  551. }
  552. this.applySelection(option)
  553. },
  554. onCenterClick: function() {
  555. // 预览模式不切换
  556. if (this.isPreview) {
  557. this.$emit('center-click', this.selectionPayload())
  558. return
  559. }
  560. var next = nextSandboxSelection(this.memberOptions, this.viewMode, this.selectedMemberId)
  561. // 家庭成员不足 2 人时不进入全家视角
  562. if (next.viewMode === 'family' && this.totalMemberCount < 2) {
  563. next = { viewMode: 'personal', memberId: null, label: '本人' }
  564. }
  565. this.applySelection(next)
  566. this.$emit('center-click', this.selectionPayload())
  567. },
  568. onPointClick: function(code) {
  569. this.$emit('point-click', {
  570. code: code,
  571. viewMode: this.viewMode,
  572. member: this.currentMember
  573. })
  574. },
  575. /** preview 引导条点击:仍触发 point-click,由父级驱动登录 */
  576. onPreviewTap: function() {
  577. this.$emit('point-click', { code: '', viewMode: this.viewMode, member: null })
  578. },
  579. /** 右上角:打开成员选择面板(本人 / 各成员 / 全家) */
  580. onToggle: function() {
  581. if (this.isPreview) return
  582. this.memberPickerVisible = true
  583. },
  584. /* ===========================
  585. Canvas 初始化 & 渲染
  586. =========================== */
  587. initCanvas: function() {
  588. var self = this
  589. uni.createSelectorQuery().in(this).select('#starCanvas')
  590. .fields({ node: true, size: true, rect: true })
  591. .exec(function(res) {
  592. if (!res || !res[0] || !res[0].node) return
  593. var canvas = res[0].node
  594. var width = res[0].width
  595. var height = res[0].height
  596. if (!width || !height) return
  597. var pixelRatio = uni.getWindowInfo().pixelRatio || 2
  598. canvas.width = width * pixelRatio
  599. canvas.height = height * pixelRatio
  600. var ctx = canvas.getContext('2d')
  601. ctx.scale(pixelRatio, pixelRatio)
  602. self.ctx = ctx
  603. self.canvasWidth = width
  604. self.canvasHeight = height
  605. self.dpr = pixelRatio
  606. self._canvasLeft = res[0].left || 0
  607. self._canvasTop = res[0].top || 0
  608. self.canvasReady = true
  609. self._startAnimation()
  610. self._initFlowCanvas()
  611. })
  612. },
  613. _doRender: function() {
  614. if (!this.ctx) return
  615. // ts 环形序 [mind, body, wisdom, wealth, action](与 render.js DIM_RING / sortedDimensions 一致)
  616. var ts = [this.animMind, this.animBody, this.animWisdom, this.animWealth, this.animAction]
  617. renderStar(this.ctx, this.canvasWidth, this.canvasHeight, ts, {
  618. familyEnergy: this.familyEnergy,
  619. personalEnergy: this.personalEnergy
  620. })
  621. },
  622. /* ===========================
  623. 动画控制
  624. =========================== */
  625. _prefersReducedMotion: function() {
  626. try {
  627. if (typeof window !== 'undefined' && window.matchMedia) {
  628. return window.matchMedia('(prefers-reduced-motion: reduce)').matches
  629. }
  630. } catch (e) {}
  631. return false
  632. },
  633. _startAnimation: function() {
  634. var self = this
  635. // 降低动效偏好:直接落值,不做填充动画(Canvas 无 CSS 降级,只能 JS 侧判断)
  636. if (this._prefersReducedMotion()) {
  637. var target = this.activeTs
  638. this.animMind = target[0]
  639. this.animBody = target[1]
  640. this.animWisdom = target[2]
  641. this.animWealth = target[3]
  642. this.animAction = target[4]
  643. this._doRender()
  644. return
  645. }
  646. if (!this._animator) {
  647. this._animator = new FillAnimator(
  648. function() {
  649. return [self.animMind, self.animBody, self.animWisdom, self.animWealth, self.animAction]
  650. },
  651. function(interpolated) {
  652. self.animMind = interpolated[0]
  653. self.animBody = interpolated[1]
  654. self.animWisdom = interpolated[2]
  655. self.animWealth = interpolated[3]
  656. self.animAction = interpolated[4]
  657. self._doRender()
  658. }
  659. )
  660. }
  661. this._animator.start(this.activeTs, 600)
  662. },
  663. /* ===========================
  664. 粒子流量动画
  665. =========================== */
  666. _initFlowCanvas: function() {
  667. var self = this
  668. var dimOrder = this.sortedDimensions.map(function(d) { return d.code })
  669. uni.createSelectorQuery().in(this).select('#flowCanvas')
  670. .fields({ node: true, size: true })
  671. .exec(function(res) {
  672. if (!res || !res[0] || !res[0].node) return
  673. var canvas = res[0].node
  674. var width = res[0].width
  675. var height = res[0].height
  676. if (!width || !height) return
  677. var pixelRatio = uni.getWindowInfo().pixelRatio || 2
  678. canvas.width = width * pixelRatio
  679. canvas.height = height * pixelRatio
  680. var ctx = canvas.getContext('2d')
  681. ctx.scale(pixelRatio, pixelRatio)
  682. // 与 render.js / hitdetect.js 保持一致的外接半径系数
  683. var outerR = Math.min(width, height) * 0.46
  684. var vertices = calcStarVertices(width / 2, height / 2, outerR)
  685. self._flowAnim = new FlowAnimation(ctx, width, height, {
  686. outer: vertices.outer,
  687. inner: vertices.inner,
  688. dimOrder: dimOrder
  689. })
  690. // 降低动效偏好:不启动粒子动画
  691. if (self._prefersReducedMotion()) return
  692. self._flowAnim.start()
  693. self._flowCanvasReady = true
  694. })
  695. },
  696. /* ===========================
  697. 点击检测
  698. =========================== */
  699. onCanvasTap: function(e) {
  700. if (!this.canvasReady) return
  701. var pageX = e.detail.x
  702. var pageY = e.detail.y
  703. if (typeof pageX !== 'number') {
  704. var touch = e.touches ? e.touches[0] : null
  705. if (touch) {
  706. pageX = touch.x || touch.clientX || 0
  707. pageY = touch.y || touch.clientY || 0
  708. } else {
  709. var changedTouch = e.changedTouches ? e.changedTouches[0] : null
  710. if (changedTouch) {
  711. pageX = changedTouch.x || changedTouch.clientX || 0
  712. pageY = changedTouch.y || changedTouch.clientY || 0
  713. } else {
  714. return
  715. }
  716. }
  717. }
  718. var relX = pageX - (this._canvasLeft || 0)
  719. var relY = pageY - (this._canvasTop || 0)
  720. if (relX < 0 || relY < 0 || relX > this.canvasWidth || relY > this.canvasHeight) return
  721. var cx = this.canvasWidth / 2
  722. var cy = this.canvasHeight / 2
  723. var outerR = Math.min(this.canvasWidth, this.canvasHeight) * 0.46
  724. var armIdx = detectHitArm(relX, relY, cx, cy, outerR, calcStarVertices)
  725. if (armIdx >= 0) {
  726. this.onPointClick(armIndexToDomain(armIdx))
  727. }
  728. }
  729. }
  730. }
  731. </script>
  732. <style scoped lang="scss">
  733. /* ============================================================
  734. 容器:浅色暖调卡片(与 B1 .dim-band 视觉语言一致)
  735. ============================================================ */
  736. .wuxing-sandbox {
  737. margin: 20rpx $uni-space-lg;
  738. padding: 20rpx 24rpx 24rpx;
  739. background: $app-surface;
  740. border: 1rpx solid rgba($brand-primary, 0.12);
  741. border-radius: $radius-lg;
  742. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
  743. position: relative;
  744. overflow: hidden;
  745. }
  746. /* ---- 标题 ---- */
  747. .sandbox-header {
  748. display: flex;
  749. justify-content: flex-start;
  750. align-items: center;
  751. margin-bottom: 12rpx;
  752. position: relative;
  753. z-index: 5;
  754. }
  755. .sandbox-title { font-size: calc(26rpx * var(--font-scale, 1)); font-weight: bold; color: $app-text; margin-left: 8rpx; }
  756. .sandbox-logo { width: 32rpx; height: 32rpx; flex-shrink: 0; }
  757. /* ---- 右上角切换(触控 ≥44px = 88rpx) ---- */
  758. .sandbox-switch {
  759. display: flex;
  760. align-items: center;
  761. gap: 6rpx;
  762. min-height: 88rpx;
  763. padding: 0 20rpx;
  764. margin-left: auto;
  765. flex-shrink: 0;
  766. border-radius: 44rpx;
  767. color: $brand-primary-deep;
  768. background: $brand-primary-soft;
  769. border: 1rpx solid rgba($brand-primary, 0.18);
  770. transition: opacity $duration-fast;
  771. }
  772. .sandbox-switch:active { opacity: 0.8; }
  773. .sandbox-switch-icon { width: 32rpx; height: 32rpx; flex-shrink: 0; }
  774. .sandbox-switch-label { font-size: calc(22rpx * var(--font-scale, 1)); font-weight: 700; max-width: 140rpx; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  775. .sandbox-switch-arrow { width: 26rpx; height: 26rpx; flex-shrink: 0; opacity: 0.7; }
  776. /* ---- 成员选择面板 ---- */
  777. .member-picker-mask {
  778. position: fixed;
  779. top: 0;
  780. right: 0;
  781. bottom: 0;
  782. left: 0;
  783. z-index: 999;
  784. display: flex;
  785. align-items: flex-end;
  786. background: rgba(0, 0, 0, 0.42);
  787. }
  788. .member-picker-panel {
  789. width: 100%;
  790. max-height: 70vh;
  791. overflow-y: auto;
  792. box-sizing: border-box;
  793. padding: 28rpx 28rpx calc(24rpx + env(safe-area-inset-bottom));
  794. background: $app-surface;
  795. border-radius: 24rpx 24rpx 0 0;
  796. box-shadow: 0 -8rpx 32rpx rgba(0, 0, 0, 0.12);
  797. }
  798. .member-picker-title {
  799. padding-bottom: 16rpx;
  800. font-size: calc(26rpx * var(--font-scale, 1));
  801. font-weight: 700;
  802. color: $app-text;
  803. text-align: center;
  804. }
  805. .member-picker-item {
  806. display: flex;
  807. align-items: center;
  808. justify-content: space-between;
  809. min-height: 88rpx;
  810. padding: 0 24rpx;
  811. margin-top: 8rpx;
  812. border-radius: 16rpx;
  813. background: $app-bg;
  814. transition: background $duration-fast;
  815. }
  816. .member-picker-item--active { background: $brand-primary-soft; }
  817. .member-picker-label { font-size: calc(28rpx * var(--font-scale, 1)); color: $app-text; }
  818. .member-picker-item--active .member-picker-label { color: $brand-primary-deep; font-weight: 700; }
  819. .member-picker-check { width: 34rpx; height: 34rpx; flex-shrink: 0; }
  820. /* ============================================================
  821. 五角星区域(C3:明确高度,避免 flex 下塌陷)
  822. ============================================================ */
  823. .sandbox-star {
  824. position: relative;
  825. width: 100%;
  826. height: 540rpx; /* 兜底(父级 starHeight 内联覆盖) */
  827. min-height: 400rpx;
  828. flex-shrink: 0;
  829. display: flex;
  830. justify-content: center;
  831. align-items: center;
  832. background: $dim-sandbox-fill;
  833. border-radius: $radius-md;
  834. }
  835. /* 背景光晕(主色仅装饰) */
  836. .star-glow-bg {
  837. position: absolute; width: 500rpx; height: 500rpx;
  838. top: 50%; left: 50%; transform: translate(-50%, -50%);
  839. background: radial-gradient(circle, rgba($brand-primary, 0.08), transparent 70%);
  840. border-radius: 50%; pointer-events: none;
  841. }
  842. /* Canvas 点击感应层 */
  843. .canvas-tap-area {
  844. position: absolute;
  845. top: 0; left: 0; right: 0; bottom: 0;
  846. z-index: 2;
  847. }
  848. /* ============================================================
  849. 顶点:图标 + 中文标签(图标形状承载五行语义,深档色承载数据)
  850. ============================================================ */
  851. .star-point {
  852. position: absolute; transform: translate(-50%, -50%); z-index: 3;
  853. display: flex; flex-direction: column; align-items: center;
  854. min-width: 88rpx; min-height: 88rpx; /* 触控 ≥44px */
  855. padding: 8rpx 12rpx;
  856. box-sizing: border-box;
  857. }
  858. .star-point--row { flex-direction: row; gap: 6rpx; }
  859. .star-point--rev { flex-direction: row-reverse; gap: 6rpx; }
  860. /* 顺时针:上=心 右=身 右下=智 左下=富 左=行 */
  861. .star-point--top { top: 6%; left: 50%; }
  862. .star-point--left { top: 40%; left: 5%; }
  863. .star-point--right { top: 40%; left: 95%; }
  864. .star-point--lr { top: 88%; left: 84%; }
  865. .star-point--ll { top: 88%; left: 16%; }
  866. .point-dot {
  867. width: 60rpx; height: 60rpx; border-radius: 50%;
  868. display: flex; align-items: center; justify-content: center;
  869. flex-shrink: 0;
  870. border: 1rpx solid transparent;
  871. }
  872. .point-dot--mind { background: $dim-mind-tint; border-color: rgba($dim-mind, 0.35); box-shadow: 0 0 12rpx rgba($dim-mind, 0.22); }
  873. .point-dot--body { background: $dim-body-tint; border-color: rgba($dim-body, 0.35); box-shadow: 0 0 12rpx rgba($dim-body, 0.22); }
  874. .point-dot--wisdom { background: $dim-wisdom-tint; border-color: rgba($dim-wisdom, 0.35); box-shadow: 0 0 12rpx rgba($dim-wisdom, 0.22); }
  875. .point-dot--wealth { background: $dim-wealth-tint; border-color: rgba($dim-wealth, 0.35); box-shadow: 0 0 12rpx rgba($dim-wealth, 0.22); }
  876. .point-dot--action { background: $dim-action-tint; border-color: rgba($dim-action, 0.35); box-shadow: 0 0 12rpx rgba($dim-action, 0.22); }
  877. .point-icon { width: 40rpx; height: 40rpx; } /* 20px 下可辨识 */
  878. .point-label { font-size: calc(22rpx * var(--font-scale, 1)); font-weight: bold; color: $app-text; margin-top: 6rpx; letter-spacing: 2rpx; }
  879. .star-point--row .point-label,
  880. .star-point--rev .point-label { margin-top: 0; }
  881. .point-energy { font-size: calc(18rpx * var(--font-scale, 1)); font-weight: 600; padding: 1rpx 10rpx; border-radius: 16rpx; margin-top: 2rpx; }
  882. .star-point--row .point-energy,
  883. .star-point--rev .point-energy { margin-top: 0; }
  884. .point-energy--mind { color: $dim-data-mind; background: $dim-mind-tint; }
  885. .point-energy--body { color: $dim-data-body; background: $dim-body-tint; }
  886. .point-energy--wisdom { color: $dim-data-wisdom; background: $dim-wisdom-tint; }
  887. .point-energy--wealth { color: $dim-data-wealth; background: $dim-wealth-tint; }
  888. .point-energy--action { color: $dim-data-action; background: $dim-action-tint; }
  889. .star-center { position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); z-index: 4; width: 180rpx; height: 180rpx; border-radius: 50%; }
  890. /* ============================================================
  891. preview 轻量引导条(标题 + 徽章 + 弱化五边形轮廓)
  892. ============================================================ */
  893. .sandbox-preview {
  894. display: flex;
  895. align-items: center;
  896. min-height: 88rpx;
  897. padding: 16rpx 20rpx;
  898. border-radius: $radius-md;
  899. background: $brand-primary-soft;
  900. border: 1rpx solid rgba($brand-primary, 0.15);
  901. }
  902. .preview-pentagon { width: 56rpx; height: 56rpx; flex-shrink: 0; opacity: 0.45; }
  903. .preview-copy { display: flex; flex-direction: column; margin-left: 16rpx; flex: 1; min-width: 0; }
  904. .preview-title { font-size: calc(26rpx * var(--font-scale, 1)); font-weight: bold; color: $app-text; }
  905. .preview-sub { font-size: calc(20rpx * var(--font-scale, 1)); color: $app-text-secondary; margin-top: 4rpx; }
  906. .preview-badge {
  907. font-size: calc(20rpx * var(--font-scale, 1)); font-weight: 700;
  908. color: $app-surface; background: $brand-primary-deep;
  909. padding: 6rpx 14rpx; border-radius: 24rpx;
  910. margin-left: 12rpx; flex-shrink: 0;
  911. }
  912. /* ============================================================
  913. 维度条(线性顺序:身 -> 智 -> 富 -> 行 -> 心)
  914. ============================================================ */
  915. .dimension-bar {
  916. display: flex; align-items: center; justify-content: center;
  917. margin-top: 16rpx; gap: 8rpx;
  918. position: relative; z-index: 5; flex-wrap: wrap;
  919. }
  920. .dim-item {
  921. display: flex; align-items: center;
  922. font-size: calc(20rpx * var(--font-scale, 1));
  923. padding: 6rpx 12rpx; border-radius: 8rpx; white-space: nowrap;
  924. }
  925. .dim-item--body { color: $dim-data-body; background: $dim-body-tint; }
  926. .dim-item--wisdom { color: $dim-data-wisdom; background: $dim-wisdom-tint; }
  927. .dim-item--wealth { color: $dim-data-wealth; background: $dim-wealth-tint; }
  928. .dim-item--action { color: $dim-data-action; background: $dim-action-tint; }
  929. .dim-item--mind { color: $dim-data-mind; background: $dim-mind-tint; }
  930. .dim-item-icon { width: 24rpx; height: 24rpx; margin-right: 4rpx; flex-shrink: 0; }
  931. /* 降低动效偏好:CSS 侧关闭过渡 */
  932. @media (prefers-reduced-motion: reduce) {
  933. .sandbox-switch,
  934. .member-picker-item,
  935. .dim-item {
  936. transition-duration: 0.01ms !important;
  937. }
  938. }
  939. </style>