西藏巴青项目

home-panel.vue 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <template>
  2. <scroll-view scroll-y class="sp-scroll" enable-back-to-top :show-scrollbar="false">
  3. <view class="sp-section">
  4. <view class="sp-section__head">
  5. <text class="sp-section__title">交易数据</text>
  6. <view class="sp-range-tabs">
  7. <view
  8. v-for="tab in rangeTabs"
  9. :key="tab.value"
  10. class="sp-range-tab"
  11. :class="{ 'sp-range-tab--on': activeRange === tab.value }"
  12. @tap="switchRange(tab.value)"
  13. >
  14. <text class="sp-range-tab__txt">{{ tab.label }}</text>
  15. </view>
  16. </view>
  17. </view>
  18. <view v-if="loading" class="sp-loading">
  19. <text class="sp-loading__txt">加载中…</text>
  20. </view>
  21. <view v-else class="sp-metrics">
  22. <view v-for="item in metricCards" :key="item.key" class="sp-metric">
  23. <text class="sp-metric__label">{{ item.label }}</text>
  24. <text class="sp-metric__value">{{ item.value }}</text>
  25. </view>
  26. </view>
  27. </view>
  28. <view class="sp-section sp-section--chart">
  29. <view class="sp-section__head sp-section__head--chart">
  30. <text class="sp-section__title">交易金额</text>
  31. <text class="sp-updated">更新时间:{{ updatedAt }}</text>
  32. </view>
  33. <view class="sp-chart-host">
  34. <mpvueEcharts
  35. ref="amountChart"
  36. :onInit="onChartInit"
  37. canvasId="supplierHomeAmount"
  38. :disableTouch="true"
  39. />
  40. </view>
  41. </view>
  42. <view class="sp-section">
  43. <text class="sp-section__title sp-section__title--block">实时牦牛状态</text>
  44. <view class="sp-yak-row">
  45. <view v-for="item in yakStatusItems" :key="item.key" class="sp-yak-col">
  46. <text class="sp-yak-col__num">{{ item.value }}</text>
  47. <text class="sp-yak-col__label">{{ item.label }}</text>
  48. </view>
  49. </view>
  50. </view>
  51. <view class="sp-footer-spacer" />
  52. </scroll-view>
  53. </template>
  54. <script>
  55. import * as echarts from '@/uni_modules/mpvue-echarts/components/echarts.esm.min.js'
  56. import mpvueEcharts from '@/uni_modules/mpvue-echarts/components/echarts.vue'
  57. import { getSupplierDashboard } from '@/api/supplier/home'
  58. import { DASH, formatCount, formatWanAmount, formatYuanAmount, toWanNumber } from '@/utils/format'
  59. import { clearHomeRealtimePoll, syncHomeRealtimePoll } from '@/utils/homeRealtimePoll'
  60. const RANGE_TABS = [
  61. { label: '实时', value: 'TODAY' },
  62. { label: '昨日', value: 'YESTERDAY' },
  63. { label: '7日', value: 'DAYS_7' },
  64. { label: '30日', value: 'DAYS_30' }
  65. ]
  66. export default {
  67. components: { mpvueEcharts },
  68. data() {
  69. return {
  70. rangeTabs: RANGE_TABS,
  71. activeRange: 'TODAY',
  72. loading: false,
  73. updatedAt: DASH,
  74. tradeSummary: {},
  75. settlementSummary: {},
  76. amountTrend: [],
  77. yakStatus: {},
  78. trendGranularity: 'HOUR'
  79. }
  80. },
  81. computed: {
  82. metricCards() {
  83. const t = this.tradeSummary || {}
  84. const s = this.settlementSummary || {}
  85. return [
  86. { key: 'totalAmount', label: '交易金额', value: formatWanAmount(t.totalAmount) },
  87. { key: 'orderCount', label: '交易订单数', value: formatCount(t.orderCount, '笔') },
  88. { key: 'payingBuyerCount', label: '支付买家数', value: formatCount(t.payingBuyerCount, '个') },
  89. { key: 'totalHeads', label: '交易牦牛数', value: formatCount(t.totalHeads, '头') },
  90. { key: 'payableAmount', label: '结算金额', value: formatWanAmount(s.payableAmount) },
  91. { key: 'serviceFeeAmount', label: '服务费金额', value: formatYuanAmount(s.serviceFeeAmount) }
  92. ]
  93. },
  94. yakStatusItems() {
  95. const y = this.yakStatus || {}
  96. return [
  97. { key: 'tradedHeads', label: '已交易', value: y.tradedHeads != null ? y.tradedHeads : DASH },
  98. { key: 'isolationEarTags', label: '隔离中', value: y.isolationEarTags != null ? y.isolationEarTags : DASH },
  99. { key: 'enteredPenEarTags', label: '已进栏', value: y.enteredPenEarTags != null ? y.enteredPenEarTags : DASH },
  100. { key: 'pendingPayHeads', label: '待支付', value: y.pendingPayHeads != null ? y.pendingPayHeads : DASH }
  101. ]
  102. }
  103. },
  104. beforeDestroy() {
  105. clearHomeRealtimePoll(this)
  106. },
  107. methods: {
  108. /** Tab 页 onShow 时刷新数据 */
  109. reload() {
  110. this.loadDashboard()
  111. this.syncRealtimePoll()
  112. },
  113. /** Tab 页 onHide 时停止轮询 */
  114. stopRealtimePoll() {
  115. clearHomeRealtimePoll(this)
  116. },
  117. syncRealtimePoll() {
  118. syncHomeRealtimePoll(this, () => this.loadDashboard(true))
  119. },
  120. switchRange(range) {
  121. if (this.activeRange === range) return
  122. this.activeRange = range
  123. this.loadDashboard()
  124. this.syncRealtimePoll()
  125. },
  126. loadDashboard(silent = false) {
  127. if (!silent) {
  128. this.loading = true
  129. }
  130. getSupplierDashboard(this.activeRange)
  131. .then((res) => {
  132. const d = res.data || {}
  133. this.updatedAt = d.updatedAt || DASH
  134. this.tradeSummary = d.tradeSummary || {}
  135. this.settlementSummary = d.settlementSummary || {}
  136. this.amountTrend = d.amountTrend || []
  137. this.yakStatus = d.yakStatus || {}
  138. this.trendGranularity = d.trendGranularity || 'HOUR'
  139. this.refreshChart()
  140. })
  141. .catch(() => {
  142. this.tradeSummary = {}
  143. this.settlementSummary = {}
  144. this.amountTrend = []
  145. this.yakStatus = {}
  146. this.refreshChart()
  147. })
  148. .finally(() => {
  149. if (!silent) {
  150. this.loading = false
  151. }
  152. })
  153. },
  154. refreshChart() {
  155. this.$nextTick(() => {
  156. const inst = this.$refs.amountChart
  157. if (inst && inst.chart) {
  158. inst.chart.setOption(this.buildChartOption(), true)
  159. }
  160. })
  161. },
  162. onChartInit(canvas, width, height) {
  163. const chart = echarts.init(canvas, null, { width, height })
  164. canvas.setChart(chart)
  165. chart.setOption(this.buildChartOption())
  166. return chart
  167. },
  168. buildChartOption() {
  169. const trend = this.amountTrend || []
  170. const labels = trend.map((p) => p.bucketLabel || '')
  171. const values = trend.map((p) => toWanNumber(p.amount))
  172. const rotate = labels.length > 8 ? 30 : 0
  173. return {
  174. animation: false,
  175. backgroundColor: '#f0f6fb',
  176. color: ['#3b82f6'],
  177. grid: { left: 10, right: 14, top: 28, bottom: 10, containLabel: true },
  178. tooltip: {
  179. trigger: 'axis',
  180. formatter: (params) => {
  181. const p = params && params[0]
  182. if (!p) return ''
  183. const raw = trend[p.dataIndex]
  184. const amt = raw && raw.amount != null ? Number(raw.amount).toFixed(2) : '0'
  185. return `${p.name}<br/>${amt} 元`
  186. }
  187. },
  188. xAxis: [{
  189. type: 'category',
  190. data: labels,
  191. boundaryGap: false,
  192. axisLine: { lineStyle: { color: '#cbd5e1' } },
  193. axisLabel: { color: '#64748b', fontSize: 10, rotate }
  194. }],
  195. yAxis: [{
  196. type: 'value',
  197. name: '万元',
  198. nameTextStyle: { color: '#64748b', fontSize: 10 },
  199. splitLine: { lineStyle: { color: '#e2e8f0' } },
  200. axisLabel: { color: '#64748b', fontSize: 10 }
  201. }],
  202. series: [{
  203. type: 'line',
  204. data: values,
  205. step: 'end',
  206. symbol: 'circle',
  207. symbolSize: 5,
  208. lineStyle: { width: 2, color: '#3b82f6' },
  209. itemStyle: { color: '#3b82f6' },
  210. areaStyle: { color: 'rgba(59, 130, 246, 0.08)' }
  211. }]
  212. }
  213. }
  214. }
  215. }
  216. </script>
  217. <style lang="scss" scoped>
  218. @import '@/styles/morandi.scss';
  219. @import '@/styles/scroll-fill.scss';
  220. .sp-scroll {
  221. @include flex-scroll-y;
  222. background: $morandi-bg-page;
  223. }
  224. .sp-section {
  225. margin: 24rpx 24rpx 0;
  226. padding: 24rpx;
  227. border-radius: 16rpx;
  228. background: $morandi-bg-card;
  229. border: 1rpx solid $morandi-border-soft;
  230. box-sizing: border-box;
  231. }
  232. .sp-section--chart {
  233. padding-bottom: 16rpx;
  234. }
  235. .sp-section__head {
  236. display: flex;
  237. flex-direction: row;
  238. align-items: center;
  239. justify-content: space-between;
  240. gap: 16rpx;
  241. margin-bottom: 20rpx;
  242. min-width: 0;
  243. }
  244. .sp-section__head--chart {
  245. flex-wrap: wrap;
  246. }
  247. .sp-section__title {
  248. font-size: 30rpx;
  249. font-weight: 600;
  250. color: $morandi-text;
  251. flex-shrink: 0;
  252. }
  253. .sp-section__title--block {
  254. display: block;
  255. margin-bottom: 20rpx;
  256. }
  257. .sp-updated {
  258. font-size: 22rpx;
  259. color: $morandi-text-muted;
  260. flex: 1;
  261. text-align: right;
  262. }
  263. .sp-range-tabs {
  264. display: flex;
  265. flex-direction: row;
  266. flex-wrap: wrap;
  267. gap: 8rpx;
  268. justify-content: flex-end;
  269. }
  270. .sp-range-tab {
  271. padding: 6rpx 16rpx;
  272. border-radius: 8rpx;
  273. border: 1rpx solid $morandi-border-strong;
  274. background: #ffffff;
  275. }
  276. .sp-range-tab--on {
  277. border-color: #3b82f6;
  278. background: rgba(59, 130, 246, 0.1);
  279. }
  280. .sp-range-tab__txt {
  281. font-size: 22rpx;
  282. color: $morandi-text-secondary;
  283. }
  284. .sp-range-tab--on .sp-range-tab__txt {
  285. color: #2563eb;
  286. font-weight: 600;
  287. }
  288. .sp-loading {
  289. padding: 32rpx 0;
  290. text-align: center;
  291. }
  292. .sp-loading__txt {
  293. font-size: 26rpx;
  294. color: $morandi-text-muted;
  295. }
  296. .sp-metrics {
  297. display: flex;
  298. flex-direction: row;
  299. flex-wrap: wrap;
  300. gap: 16rpx;
  301. }
  302. .sp-metric {
  303. width: calc(50% - 8rpx);
  304. padding: 20rpx 16rpx;
  305. border-radius: 12rpx;
  306. background: #f5f7fa;
  307. box-sizing: border-box;
  308. display: flex;
  309. flex-direction: column;
  310. gap: 10rpx;
  311. }
  312. .sp-metric__label {
  313. font-size: 24rpx;
  314. color: $morandi-text-muted;
  315. }
  316. .sp-metric__value {
  317. font-size: 30rpx;
  318. font-weight: 600;
  319. color: $morandi-text;
  320. word-break: break-word;
  321. }
  322. .sp-chart-host {
  323. width: 100%;
  324. height: 360rpx;
  325. border-radius: 8rpx;
  326. overflow: hidden;
  327. background: #f0f6fb;
  328. }
  329. .sp-chart-host :deep(.ec-canvas) {
  330. display: block;
  331. width: 100% !important;
  332. height: 100% !important;
  333. }
  334. .sp-yak-row {
  335. display: flex;
  336. flex-direction: row;
  337. align-items: stretch;
  338. }
  339. .sp-yak-col {
  340. flex: 1;
  341. min-width: 0;
  342. display: flex;
  343. flex-direction: column;
  344. align-items: center;
  345. gap: 8rpx;
  346. }
  347. .sp-yak-col__num {
  348. font-size: 40rpx;
  349. font-weight: 700;
  350. color: $morandi-text;
  351. }
  352. .sp-yak-col__label {
  353. font-size: 24rpx;
  354. color: $morandi-text-muted;
  355. }
  356. .sp-footer-spacer {
  357. height: 40rpx;
  358. }
  359. </style>