indicator-curve.vue 16 KB

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