IndicatorTrendArrow.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <view class="it-anchor">
  3. <!-- 触发区:有箭头才渲染;点击弹出曲线浮层 -->
  4. <view
  5. v-if="arrowText"
  6. class="indicator-trend-arrow"
  7. @tap.stop="openPopup"
  8. >
  9. <text :class="arrowCssClass">{{ arrowText }}</text>
  10. <text v-if="showLatest && effectiveLatestText" class="it-a-latest">{{ effectiveLatestText }}</text>
  11. </view>
  12. <!-- 曲线弹层 -->
  13. <view v-if="popupVisible" class="it-popup-mask" @tap.stop="closePopup">
  14. <view class="it-popup-card" @tap.stop>
  15. <view class="it-popup-head">
  16. <text class="it-popup-title">{{ titleText }}</text>
  17. <text v-if="effectiveLatestText" class="it-popup-latest">最新:{{ effectiveLatestText }}</text>
  18. <text class="it-popup-close" @tap.stop="closePopup">✕</text>
  19. </view>
  20. <view class="it-popup-body">
  21. <view v-if="!hasTrend" class="it-empty">
  22. <text v-if="!item" class="it-empty-note">暂无趋势数据</text>
  23. <text v-else class="it-empty-note">仅一次记录,暂无趋势</text>
  24. </view>
  25. <TrendChart
  26. v-else
  27. :series="series"
  28. mode="line"
  29. :ref-low="item.refLow"
  30. :ref-high="item.refHigh"
  31. :all-period-dates="allPeriodDates"
  32. :show-values="false"
  33. />
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import { getIndicatorTrend } from '@/utils/api'
  41. import { resolveChange } from '@/utils/indicator-change'
  42. import TrendChart from '@/pages/health/components/TrendChart.vue'
  43. export default {
  44. name: 'IndicatorTrendArrow',
  45. components: { TrendChart },
  46. props: {
  47. /** 用户 id(必传) */
  48. subjectId: { type: [Number, String], required: true },
  49. /** 指标定义 id(必传) */
  50. definitionId: { type: [Number, String], required: true },
  51. /** 报告来源类型(可选,透传给后端) */
  52. sourceType: { type: String, default: '' },
  53. /** 外部已加载的 trend 数据,传入则零请求 */
  54. trend: { type: Object, default: null },
  55. /** 最新值文本(可选,供弹层标题/内联展示;缺省时回退 item.latest) */
  56. latestText: { type: String, default: '' },
  57. /** 是否在内联触发区同时展示最新值(默认 true;宿主已展示值时传 false 避免重复) */
  58. showLatest: { type: Boolean, default: true }
  59. },
  60. data: function() {
  61. return {
  62. item: this.trend,
  63. popupVisible: false,
  64. loading: false
  65. }
  66. },
  67. computed: {
  68. /** 方向箭头:movement up→↑ down→↓ flat→→;不可用时返回 ''
  69. * 注:箭头判定只依赖 change.movement(quicklook 行 item 无 points,
  70. * 不能因 points.length<2 而拦截箭头);openPopup 时若无 points,
  71. * 弹窗内已降级为「暂无趋势数据」,不会崩。 */
  72. arrowText: function() {
  73. if (!this.item || !this.item.change) return ''
  74. var ch = this.item.change
  75. if (ch.comparable === false) return ''
  76. var mv = ch.movement
  77. if (mv === 'up') return '↑'
  78. if (mv === 'down') return '↓'
  79. if (mv === 'flat') return '→'
  80. return ''
  81. },
  82. /** 箭头颜色类(消费后端 trendCode,前端不自判改善/恶化) */
  83. arrowCssClass: function() {
  84. if (!this.item || !this.item.change) return 'gray-text'
  85. var r = resolveChange(this.item.change.trendCode, this.item.change.movement, this.item.change.trendLabel)
  86. return r.colorClass
  87. },
  88. /** 是否有可画的 trend(>=1 期) */
  89. hasTrend: function() {
  90. return !!(this.item && this.item.points && this.item.points.length >= 1)
  91. },
  92. /** series 构造:与 indicator-timeline.vue series 口径一致 */
  93. series: function() {
  94. if (!this.item || !Array.isArray(this.item.points)) return []
  95. return [{
  96. name: this.item.name || '',
  97. color: '#F97316',
  98. points: this.item.points.map(function(p) {
  99. return {
  100. reportDate: String(p.reportDate).slice(0, 10),
  101. value: p.value,
  102. numericValue: p.numericValue,
  103. unit: p.unit,
  104. dateSource: p.dateSource
  105. }
  106. })
  107. }]
  108. },
  109. /** X 轴日期全集(points 日期 + missingPeriods 日期合并去重升序) */
  110. allPeriodDates: function() {
  111. if (!this.item) return []
  112. var set = {}
  113. var pts = this.item.points || []
  114. for (var i = 0; i < pts.length; i++) {
  115. var d = pts[i] && pts[i].reportDate
  116. if (d) set[String(d).slice(0, 10)] = true
  117. }
  118. var misses = this.item.missingPeriods || []
  119. for (var j = 0; j < misses.length; j++) {
  120. var md = misses[j] && misses[j].reportDate
  121. if (md) set[String(md).slice(0, 10)] = true
  122. }
  123. var keys = Object.keys(set)
  124. keys.sort(function(a, b) { return a < b ? -1 : (a > b ? 1 : 0) })
  125. return keys
  126. },
  127. titleText: function() {
  128. if (!this.item) return ''
  129. var n = this.item.name || ''
  130. var u = this.item.unit ? (' (' + this.item.unit + ')') : ''
  131. return n + u
  132. },
  133. /** 最新值展示文本:优先 prop,缺省回退 item.latest(弹层标题/内联共用) */
  134. effectiveLatestText: function() {
  135. if (this.latestText) return this.latestText
  136. if (!this.item || !this.item.latest) return ''
  137. var v = this.item.latest
  138. if (v.numericValue !== undefined && v.numericValue !== null) return String(v.numericValue)
  139. return v.value || ''
  140. }
  141. },
  142. watch: {
  143. trend: function(val) { this.item = val }
  144. },
  145. mounted: function() {
  146. var self = this
  147. var hasPoints = !!(this.item && this.item.points && this.item.points.length > 0)
  148. // 已有 trend prop 且含 points:零请求直接消费
  149. if (this.item && hasPoints) return
  150. var sid = Number(this.subjectId)
  151. var did = Number(this.definitionId)
  152. if (isNaN(sid) || isNaN(did)) return
  153. // 无 trend 或 trend 缺 points(quicklook 行 item):自拉补全,供箭头/弹窗曲线使用
  154. this.loading = true
  155. getIndicatorTrend({
  156. subjectId: sid,
  157. definitionIds: [did],
  158. sourceType: this.sourceType || undefined
  159. }).then(function(res) {
  160. self.loading = false
  161. var full = (res.data && res.data.items && res.data.items[0]) || null
  162. // 已拿到完整 trend(含 points)时整体替换;仅当请求失败(full 为空)才保留行 item 的 change 供箭头展示
  163. if (full) { self.item = full }
  164. }).catch(function() {
  165. self.loading = false
  166. })
  167. },
  168. methods: {
  169. openPopup: function() {
  170. this.popupVisible = true
  171. },
  172. closePopup: function() {
  173. this.popupVisible = false
  174. }
  175. }
  176. }
  177. </script>
  178. <style scoped lang="scss">
  179. /* 锚点容器:默认行内,不打断宿主布局 */
  180. .it-anchor {
  181. display: inline;
  182. }
  183. /* 箭头触发区:30rpx 字号,行内对齐指标值展示 */
  184. .indicator-trend-arrow {
  185. display: inline-flex;
  186. align-items: center;
  187. margin-left: 6rpx;
  188. vertical-align: baseline;
  189. }
  190. .indicator-trend-arrow text {
  191. font-size: 30rpx;
  192. line-height: 1;
  193. }
  194. .it-a-latest {
  195. font-size: calc(20rpx * var(--font-scale, 1));
  196. color: $facade-text-body;
  197. margin-left: 6rpx;
  198. }
  199. /* 遮罩:固定全屏,深色半透明 */
  200. .it-popup-mask {
  201. position: fixed;
  202. top: 0;
  203. left: 0;
  204. right: 0;
  205. bottom: 0;
  206. z-index: 999;
  207. background: rgba(0, 0, 0, 0.55);
  208. display: flex;
  209. align-items: center;
  210. justify-content: center;
  211. }
  212. /* 卡片:居中,圆角,内边距对齐指标页风格 */
  213. .it-popup-card {
  214. width: 85vw;
  215. max-height: 80vh;
  216. background: #fff;
  217. border-radius: 16rpx;
  218. padding: 24rpx;
  219. display: flex;
  220. flex-direction: column;
  221. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.18);
  222. }
  223. .it-popup-head {
  224. display: flex;
  225. flex-direction: row;
  226. align-items: baseline;
  227. justify-content: space-between;
  228. margin-bottom: 16rpx;
  229. }
  230. .it-popup-title {
  231. font-size: calc(28rpx * var(--font-scale, 1));
  232. font-weight: 700;
  233. color: #111827;
  234. flex: 1;
  235. }
  236. .it-popup-latest {
  237. font-size: calc(22rpx * var(--font-scale, 1));
  238. color: $facade-text-body;
  239. margin-right: 16rpx;
  240. }
  241. .it-popup-close {
  242. font-size: calc(28rpx * var(--font-scale, 1));
  243. color: #9ca3af;
  244. padding: 4rpx 8rpx;
  245. }
  246. .it-popup-body {
  247. flex: 1;
  248. min-height: 0;
  249. }
  250. .it-empty {
  251. display: flex;
  252. align-items: center;
  253. justify-content: center;
  254. min-height: 200rpx;
  255. }
  256. .it-empty-note {
  257. font-size: calc(24rpx * var(--font-scale, 1));
  258. color: #9ca3af;
  259. }
  260. /* 颜色类:全局未定义时兜底;scoped 下不影响其他组件 */
  261. .green-text { color: #2BA24C; }
  262. .red-text { color: #E64340; }
  263. .gray-text { color: #999; }
  264. </style>