indicator-curve.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <template>
  2. <view class="page">
  3. <!-- 顶部筛选:成员 / 报告类型 -->
  4. <view class="filter-bar">
  5. <picker :range="memberNames" @change="onMemberChange">
  6. <view class="filter-item">{{ currentMemberName }} ▾</view>
  7. </picker>
  8. <picker :range="reportTypeNames" @change="onReportTypeChange">
  9. <view class="filter-item">{{ currentReportTypeName }} ▾</view>
  10. </picker>
  11. </view>
  12. <!-- 曲线区:指标选择器 + TrendChart -->
  13. <view class="curve-section">
  14. <view class="section-title">指标变化曲线</view>
  15. <view class="indicator-picker">
  16. <scroll-view scroll-x class="indicator-scroll">
  17. <view
  18. class="indicator-chip"
  19. :class="def.definitionId === theIndicatorId ? 'chip-active' : ''"
  20. v-for="def in indicatorOptions"
  21. :key="getIndicatorKey(def)"
  22. @tap="onIndicatorTap(getIndicatorId(def))"
  23. >{{ getIndicatorName(def) }}</view>
  24. </scroll-view>
  25. </view>
  26. <TrendChart v-if="series && series.length > 0" :series="series" />
  27. <view class="empty-tip" v-else>暂无趋势数据</view>
  28. </view>
  29. <!-- 变化排名区 -->
  30. <view class="ranking-section">
  31. <view class="section-title">变化排名</view>
  32. <view class="rank-tabs">
  33. <view
  34. class="rank-tab"
  35. :class="'rank-' + activeRankTab"
  36. v-for="(tab, ti) in rankTabs"
  37. :key="getTabKey(tab, ti)"
  38. @tap="onRankTabTap(tab.value)"
  39. >{{ tab.label }}</view>
  40. </view>
  41. <view class="rank-sorts">
  42. <view
  43. class="sort-btn"
  44. :class="sortBy === s.value ? 'sort-active' : ''"
  45. v-for="s in sortOptions"
  46. :key="getSortKey(s)"
  47. @tap="onSortTap(s.value)"
  48. >{{ s.label }}</view>
  49. </view>
  50. <view class="rank-list" v-if="rankItems.length > 0">
  51. <view class="rank-item" v-for="item in rankItems" :key="getRankKey(item)">
  52. <view class="rank-name-row">
  53. <text class="rank-name">{{ item.name }}</text>
  54. <text class="rank-category">{{ item.category }}</text>
  55. </view>
  56. <view class="rank-value-row">
  57. <text class="rank-before">{{ item.before ? item.before.value : '--' }}</text>
  58. <text :class="'rank-arrow ' + item._cssClass">→</text>
  59. <text class="rank-after">{{ item.after ? item.after.value : '--' }}</text>
  60. <text :class="'rank-delta ' + item._cssClass">{{ item._deltaText }}</text>
  61. </view>
  62. <text :class="'rank-trend ' + item._cssClass">{{ item._trendLabel }} {{ item._arrow }}</text>
  63. <text class="rank-gap" v-if="item.skipGaps > 0">中间 {{ item.skipGaps }} 期缺失</text>
  64. </view>
  65. </view>
  66. <view class="rank-empty" v-else>暂无变化数据</view>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. import TrendChart from '@/pages/health/components/TrendChart.vue'
  72. import { getIndicatorTrend, getIndicatorChangeRanking, getIndicatorSourceTypes } from '@/utils/api'
  73. import { getFamilyMemberList } from '@/utils/api'
  74. import { resolveChangeStyle, formatDelta, colorClassFor } from '@/utils/indicator-change'
  75. import { parseDate } from '@/utils/format'
  76. export default {
  77. components: { TrendChart },
  78. data: function() {
  79. return {
  80. members: [], currentMemberId: null,
  81. reportTypeOptions: [],
  82. activeReportType: '',
  83. indicatorOptions: [], theIndicatorId: null,
  84. series: [], loadedDefinitionIds: [],
  85. rankTabs: [ { value: 'flora', label: '菌群变化' }, { value: 'food', label: '食材变化' }, { value: 'indicator', label: '指标变化' } ],
  86. activeRankTab: 'flora',
  87. sortOptions: [ { value: 'magnitude', label: '绝对幅度' }, { value: 'up', label: '升幅↑' }, { value: 'down', label: '降幅↓' } ],
  88. sortBy: 'magnitude',
  89. rankItems: [], overall: {},
  90. _onLoadFired: false, _loading: false
  91. }
  92. },
  93. computed: {
  94. memberNames: function() { return this.members.map(function(m) { return m.name || m.nickname || '' }) },
  95. reportTypeNames: function() {
  96. var self = this
  97. return this.reportTypeOptions.map(function(o) { return o.label })
  98. },
  99. currentMemberName: function() {
  100. var self = this
  101. var m = this.members.find(function(x) { return x.id === self.currentMemberId })
  102. return (m && (m.name || m.nickname)) || '选择成员'
  103. },
  104. currentReportTypeName: function() {
  105. var self = this
  106. var o = this.reportTypeOptions.find(function(x) { return x.sourceType === self.activeReportType })
  107. return (o && o.label) || ''
  108. }
  109. },
  110. onLoad: function(options) {
  111. this.loadMembers()
  112. if (options && options.definitionId) {
  113. this.theIndicatorId = Number(options.definitionId)
  114. }
  115. this._onLoadFired = true
  116. },
  117. onShow: function() {
  118. if (this._onLoadFired) {
  119. this._onLoadFired = false
  120. } else if (this.currentMemberId) {
  121. this.reloadAll()
  122. }
  123. },
  124. methods: {
  125. getIndicatorId: function(def) { return def ? def.definitionId : null },
  126. getIndicatorName: function(def) { return (def && def.name) || '' },
  127. getIndicatorKey: function(def) { return def ? (def.definitionId || def.name) : '' },
  128. getTabKey: function(tab, i) { return tab.value || i },
  129. getSortKey: function(s) { return s.value },
  130. getRankKey: function(item) { return item.definitionId },
  131. loadMembers: function() {
  132. var self = this
  133. getFamilyMemberList({ visibleOnly: true }).then(function(res) {
  134. var arr = (res.data && res.data.list) || (res.data instanceof Array ? res.data : [])
  135. self.members = arr
  136. if (!self.currentMemberId && arr.length > 0) {
  137. self.currentMemberId = arr[0].id
  138. self.onMemberSelected()
  139. self.loadSourceTypes()
  140. }
  141. })
  142. },
  143. onMemberSelected: function() { this.reloadAll() },
  144. loadSourceTypes: function() {
  145. var self = this
  146. if (!this.currentMemberId) return
  147. // 报告类型动态化:以该成员 indicator_values 实际存在的 source_type 为准,避免硬编码类型错配
  148. getIndicatorSourceTypes({ subjectId: this.currentMemberId }).then(function(res) {
  149. var arr = (res.data instanceof Array) ? res.data : []
  150. if (arr.length > 0) {
  151. self.reportTypeOptions = arr
  152. var first = arr[0].sourceType
  153. if (self.activeReportType !== first) {
  154. self.activeReportType = first
  155. self.reloadAll()
  156. }
  157. } else {
  158. // 接口空/失败回退历史默认,避免筛选器无选项
  159. self.reportTypeOptions = [
  160. { sourceType: 'gut_flora', label: '肠道菌群' },
  161. { sourceType: 'food_recommend', label: '食材推荐' }
  162. ]
  163. if (!self.activeReportType) { self.activeReportType = 'gut_flora'; self.reloadAll() }
  164. }
  165. })
  166. },
  167. onMemberChange: function(e) {
  168. var idx = Number(e.detail.value)
  169. var m = this.members[idx]
  170. if (m) { this.currentMemberId = m.id; this.reloadAll() }
  171. },
  172. onReportTypeChange: function(e) {
  173. var idx = Number(e.detail.value)
  174. var o = this.reportTypeOptions[idx]
  175. if (o) { this.activeReportType = o.sourceType; this.reloadAll() }
  176. },
  177. reloadAll: function() {
  178. if (!this.currentMemberId) return
  179. this.loadCurve()
  180. this.loadRanking()
  181. },
  182. loadCurve: function() {
  183. // 曲线页:先拉 ranking 获得指标候选,再拉 trend(避免硬编码 definition 列表)
  184. this.loadRankingThenCurve()
  185. },
  186. loadRankingThenCurve: function() {
  187. var self = this
  188. if (!this.currentMemberId) return
  189. // food_recommend 无时序数据:FoodItem 为评分型数据,NormalizeIndicatorPipeline 不写 indicator_values,
  190. // 此处回退为全量指标参与曲线/排名(有意为之,非 bug)
  191. var reportType = (!this.activeReportType || this.activeReportType === 'food_recommend') ? null : this.activeReportType
  192. getIndicatorChangeRanking({
  193. subjectId: this.currentMemberId,
  194. reportType: reportType,
  195. category: this.activeRankTab === 'food' ? null : (this.activeRankTab === 'flora' ? '核心菌属' : null),
  196. scope: 'adjacent', orderBy: 'magnitude', limit: 30
  197. }).then(function(res) {
  198. var items = (res.data && res.data.items) || []
  199. // 用排名结果作为指标候选
  200. self.indicatorOptions = items.slice(0, 10).map(function(it) {
  201. return { definitionId: it.definitionId, name: it.name }
  202. })
  203. if (self.indicatorOptions.length > 0) {
  204. var target = self.theIndicatorId
  205. var found = self.indicatorOptions.find(function(d) { return d.definitionId === target })
  206. if (!found) { self.theIndicatorId = self.indicatorOptions[0].definitionId }
  207. self.loadTrendFor([self.theIndicatorId])
  208. } else {
  209. self.series = []
  210. }
  211. })
  212. },
  213. loadTrendFor: function(definitionIds) {
  214. var self = this
  215. if (!this.currentMemberId || !definitionIds || definitionIds.length === 0) return
  216. getIndicatorTrend({ subjectId: this.currentMemberId, definitionIds: definitionIds }).then(function(res) {
  217. var items = (res.data && res.data.items) || []
  218. var series = items.map(function(it) {
  219. var color = ''
  220. return { name: it.name, color: color || '#F97316', points: (it.points || []).map(function(p) { return { reportDate: String(p.reportDate).slice(0, 10), value: p.value, unit: p.unit } }) }
  221. })
  222. self.series = series
  223. })
  224. },
  225. onIndicatorTap: function(definitionId) {
  226. if (definitionId === this.theIndicatorId) return
  227. this.theIndicatorId = definitionId
  228. this.loadTrendFor([definitionId])
  229. },
  230. onRankTabTap: function(value) { this.activeRankTab = value; this.loadRanking() },
  231. onSortTap: function(value) { this.sortBy = value; this.loadRanking() },
  232. loadRanking: function() {
  233. var self = this
  234. if (!this.currentMemberId) return
  235. var category = null
  236. if (this.activeRankTab === 'flora') category = '核心菌属'
  237. else if (this.activeRankTab === 'indicator') category = null
  238. var reportType = this.activeRankTab === 'food' ? 'food_recommend' : (this.activeRankTab === 'flora' ? 'gut_flora' : null)
  239. getIndicatorChangeRanking({
  240. subjectId: this.currentMemberId,
  241. reportType: reportType,
  242. category: category,
  243. scope: 'adjacent', orderBy: this.sortBy, limit: 50
  244. }).then(function(res) {
  245. var items = (res.data && res.data.items) || []
  246. // 预计算展示字段,避免小程序模板在 :class 中调用方法(微信模板编译器不支持带参方法调用)
  247. self.rankItems = items.map(function(it) {
  248. var st = resolveChangeStyle(it.goodDirection, it.movement)
  249. it._cssClass = colorClassFor(st.cssClass)
  250. it._trendLabel = st.trendLabel
  251. it._arrow = st.arrow
  252. it._deltaText = formatDelta(it.delta, it.deltaPct)
  253. return it
  254. })
  255. self.overall = (res.data && res.data.overall) || {}
  256. })
  257. }
  258. }
  259. }
  260. </script>
  261. <style scoped>
  262. .page {
  263. min-height: 100vh;
  264. background-color: #F5F9FC;
  265. padding: 24rpx;
  266. box-sizing: border-box;
  267. }
  268. /* 顶部筛选 */
  269. .filter-bar {
  270. display: flex;
  271. flex-direction: row;
  272. margin-bottom: 24rpx;
  273. }
  274. .filter-item {
  275. flex: 1;
  276. background-color: #FFFFFF;
  277. border: 1rpx solid #E2E8F0;
  278. border-radius: 16rpx;
  279. padding: 16rpx 24rpx;
  280. margin-right: 16rpx;
  281. font-size: 26rpx;
  282. color: #1E293B;
  283. text-align: center;
  284. }
  285. .filter-item:last-child {
  286. margin-right: 0;
  287. }
  288. /* 区块通用 */
  289. .curve-section,
  290. .ranking-section {
  291. background-color: #FFFFFF;
  292. border-radius: 20rpx;
  293. padding: 24rpx;
  294. margin-bottom: 24rpx;
  295. box-shadow: 0 4rpx 16rpx rgba(30, 41, 59, 0.06);
  296. }
  297. .section-title {
  298. font-size: 30rpx;
  299. font-weight: 600;
  300. color: #1E293B;
  301. margin-bottom: 20rpx;
  302. }
  303. /* 指标选择器 */
  304. .indicator-picker {
  305. margin-bottom: 20rpx;
  306. }
  307. .indicator-scroll {
  308. white-space: nowrap;
  309. width: 100%;
  310. }
  311. .indicator-chip {
  312. display: inline-flex;
  313. align-items: center;
  314. padding: 10rpx 24rpx;
  315. margin-right: 16rpx;
  316. border-radius: 32rpx;
  317. background-color: #EDF2F7;
  318. font-size: 24rpx;
  319. color: #64748B;
  320. }
  321. .chip-active {
  322. background-color: #5B9BD5;
  323. color: #FFFFFF;
  324. }
  325. .empty-tip {
  326. padding: 60rpx 0;
  327. text-align: center;
  328. font-size: 26rpx;
  329. color: #94A3B8;
  330. }
  331. .rank-gap {
  332. display: inline-block;
  333. margin: 4rpx 0;
  334. color: #e53e3e;
  335. font-size: 20rpx;
  336. }
  337. .rank-empty {
  338. padding: 60rpx 0;
  339. text-align: center;
  340. font-size: 24rpx;
  341. color: #94A3B8;
  342. }
  343. /* 排名 Tab */
  344. .rank-tabs {
  345. display: flex;
  346. flex-direction: row;
  347. margin-bottom: 16rpx;
  348. }
  349. .rank-tab {
  350. flex: 1;
  351. text-align: center;
  352. padding: 14rpx 0;
  353. font-size: 26rpx;
  354. color: #64748B;
  355. border-bottom: 4rpx solid transparent;
  356. }
  357. .rank-tab.rank-flora,
  358. .rank-tab.rank-food,
  359. .rank-tab.rank-indicator {
  360. color: #64748B;
  361. }
  362. .rank-sorts {
  363. display: flex;
  364. flex-direction: row;
  365. margin-bottom: 20rpx;
  366. }
  367. .sort-btn {
  368. padding: 8rpx 24rpx;
  369. margin-right: 16rpx;
  370. border-radius: 24rpx;
  371. font-size: 24rpx;
  372. color: #64748B;
  373. background-color: #EDF2F7;
  374. }
  375. .sort-active {
  376. background-color: #D6EAF8;
  377. color: #3A7CC4;
  378. }
  379. /* 排名列表 */
  380. .rank-list {
  381. display: flex;
  382. flex-direction: column;
  383. }
  384. .rank-item {
  385. padding: 20rpx 0;
  386. border-bottom: 1rpx solid #E2E8F0;
  387. }
  388. .rank-item:last-child {
  389. border-bottom: none;
  390. }
  391. .rank-name-row {
  392. display: flex;
  393. flex-direction: row;
  394. align-items: center;
  395. margin-bottom: 10rpx;
  396. }
  397. .rank-name {
  398. font-size: 28rpx;
  399. color: #1E293B;
  400. font-weight: 500;
  401. margin-right: 16rpx;
  402. }
  403. .rank-category {
  404. font-size: 22rpx;
  405. color: #94A3B8;
  406. }
  407. .rank-value-row {
  408. display: flex;
  409. flex-direction: row;
  410. align-items: center;
  411. margin-bottom: 8rpx;
  412. }
  413. .rank-before {
  414. font-size: 26rpx;
  415. color: #64748B;
  416. }
  417. .rank-arrow {
  418. font-size: 26rpx;
  419. margin: 0 12rpx;
  420. }
  421. .rank-after {
  422. font-size: 28rpx;
  423. color: #1E293B;
  424. font-weight: 600;
  425. }
  426. .rank-delta {
  427. font-size: 24rpx;
  428. margin-left: 16rpx;
  429. }
  430. .rank-trend {
  431. display: block;
  432. font-size: 22rpx;
  433. }
  434. /* 变化着色:改善=绿 / 恶化=红 / 中性=灰 */
  435. .green-text {
  436. color: #16A34A;
  437. }
  438. .red-text {
  439. color: #DC2626;
  440. }
  441. .gray-text {
  442. color: #94A3B8;
  443. }
  444. </style>