西藏巴青项目

home-panel.vue 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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" @touchmove.stop>
  34. <mpvueEcharts
  35. ref="amountChart"
  36. :onInit="onChartInit"
  37. canvasId="distributorHomeAmount"
  38. :disableTouch="false"
  39. />
  40. </view>
  41. </view>
  42. <view class="sp-footer-spacer" />
  43. </scroll-view>
  44. </template>
  45. <script>
  46. import * as echarts from '@/uni_modules/mpvue-echarts/components/echarts.esm.min.js'
  47. import mpvueEcharts from '@/uni_modules/mpvue-echarts/components/echarts.vue'
  48. import { getDistributorDashboard } from '@/api/distributor/home'
  49. import {
  50. DASH,
  51. formatCount,
  52. formatPercent,
  53. formatWanAmount,
  54. formatWanPerHead,
  55. toWanNumber
  56. } from '@/utils/format'
  57. import { clearHomeRealtimePoll, syncHomeRealtimePoll } from '@/utils/homeRealtimePoll'
  58. const RANGE_TABS = [
  59. { label: '实时', value: 'TODAY' },
  60. { label: '昨日', value: 'YESTERDAY' },
  61. { label: '7日', value: 'DAYS_7' },
  62. { label: '30日', value: 'DAYS_30' }
  63. ]
  64. export default {
  65. components: { mpvueEcharts },
  66. data() {
  67. return {
  68. rangeTabs: RANGE_TABS,
  69. activeRange: 'TODAY',
  70. loading: false,
  71. updatedAt: DASH,
  72. summary: {},
  73. amountTrend: []
  74. }
  75. },
  76. computed: {
  77. metricCards() {
  78. const s = this.summary || {}
  79. return [
  80. { key: 'orderCount', label: '累计下单数', value: formatCount(s.orderCount, '笔') },
  81. { key: 'totalAmount', label: '采购总金额', value: formatWanAmount(s.totalAmount) },
  82. { key: 'totalHeads', label: '已购牦牛数', value: formatCount(s.totalHeads, '头') },
  83. { key: 'completionRate', label: '订单完成率', value: formatPercent(s.completionRate) },
  84. {key: 'supplierCount', label: '成交供应商数', value: formatCount(s.supplierCount, '个') },
  85. { key: 'avgUnitPrice', label: '近期交易均价', value: formatWanPerHead(s.avgUnitPrice) }
  86. ]
  87. }
  88. },
  89. beforeDestroy() {
  90. clearHomeRealtimePoll(this)
  91. },
  92. methods: {
  93. reload() {
  94. this.loadDashboard()
  95. this.syncRealtimePoll()
  96. },
  97. stopRealtimePoll() {
  98. clearHomeRealtimePoll(this)
  99. },
  100. syncRealtimePoll() {
  101. syncHomeRealtimePoll(this, () => this.loadDashboard(true))
  102. },
  103. switchRange(range) {
  104. if (this.activeRange === range) return
  105. this.activeRange = range
  106. this.loadDashboard()
  107. this.syncRealtimePoll()
  108. },
  109. loadDashboard(silent = false) {
  110. if (!silent) {
  111. this.loading = true
  112. }
  113. getDistributorDashboard(this.activeRange)
  114. .then((res) => {
  115. const d = res.data || {}
  116. this.updatedAt = d.updatedAt || DASH
  117. this.summary = d.summary || {}
  118. this.amountTrend = d.amountTrend || []
  119. this.refreshChart()
  120. })
  121. .catch(() => {
  122. this.summary = {}
  123. this.amountTrend = []
  124. this.refreshChart()
  125. })
  126. .finally(() => {
  127. if (!silent) {
  128. this.loading = false
  129. }
  130. })
  131. },
  132. refreshChart() {
  133. this.$nextTick(() => {
  134. const inst = this.$refs.amountChart
  135. if (inst && inst.chart) {
  136. inst.chart.setOption(this.buildChartOption(), true)
  137. }
  138. })
  139. },
  140. onChartInit(canvas, width, height) {
  141. const chart = echarts.init(canvas, null, { width, height })
  142. canvas.setChart(chart)
  143. chart.setOption(this.buildChartOption())
  144. return chart
  145. },
  146. buildChartOption() {
  147. const trend = this.amountTrend || []
  148. const labels = trend.map((p) => p.bucketLabel || '')
  149. const values = trend.map((p) => toWanNumber(p.amount))
  150. const rotate = labels.length > 8 ? 30 : 0
  151. return {
  152. animation: false,
  153. backgroundColor: '#f0f6fb',
  154. color: ['#3b82f6'],
  155. grid: { left: 10, right: 14, top: 28, bottom: 10, containLabel: true },
  156. tooltip: {
  157. trigger: 'axis',
  158. triggerOn: 'click',
  159. confine: true,
  160. formatter: (params) => {
  161. const p = params && params[0]
  162. if (!p) return ''
  163. const raw = trend[p.dataIndex]
  164. const amt = raw && raw.amount != null ? Number(raw.amount).toFixed(2) : '0'
  165. return `${p.name}<br/>${amt} 元`
  166. }
  167. },
  168. xAxis: [{
  169. type: 'category',
  170. data: labels,
  171. boundaryGap: false,
  172. axisLine: { lineStyle: { color: '#cbd5e1' } },
  173. axisLabel: { color: '#64748b', fontSize: 10, rotate }
  174. }],
  175. yAxis: [{
  176. type: 'value',
  177. name: '万元',
  178. nameTextStyle: { color: '#64748b', fontSize: 10 },
  179. splitLine: { lineStyle: { color: '#e2e8f0' } },
  180. axisLabel: { color: '#64748b', fontSize: 10 }
  181. }],
  182. series: [{
  183. type: 'line',
  184. data: values,
  185. step: 'end',
  186. symbol: 'circle',
  187. symbolSize: 5,
  188. lineStyle: { width: 2, color: '#3b82f6' },
  189. itemStyle: { color: '#3b82f6' },
  190. areaStyle: { color: 'rgba(59, 130, 246, 0.08)' }
  191. }]
  192. }
  193. }
  194. }
  195. }
  196. </script>
  197. <style lang="scss" scoped>
  198. @import '@/styles/morandi.scss';
  199. @import '@/styles/scroll-fill.scss';
  200. .sp-scroll {
  201. @include flex-scroll-y;
  202. background: $morandi-bg-page;
  203. }
  204. .sp-section {
  205. margin: 24rpx 24rpx 0;
  206. padding: 24rpx;
  207. border-radius: 16rpx;
  208. background: $morandi-bg-card;
  209. border: 1rpx solid $morandi-border-soft;
  210. box-sizing: border-box;
  211. }
  212. .sp-section--chart {
  213. padding-bottom: 16rpx;
  214. }
  215. .sp-section__head {
  216. display: flex;
  217. flex-direction: row;
  218. align-items: center;
  219. justify-content: space-between;
  220. gap: 16rpx;
  221. margin-bottom: 20rpx;
  222. min-width: 0;
  223. }
  224. .sp-section__head--chart {
  225. flex-wrap: wrap;
  226. }
  227. .sp-section__title {
  228. font-size: 30rpx;
  229. font-weight: 600;
  230. color: $morandi-text;
  231. flex-shrink: 0;
  232. }
  233. .sp-updated {
  234. font-size: 22rpx;
  235. color: $morandi-text-muted;
  236. flex: 1;
  237. text-align: right;
  238. }
  239. .sp-range-tabs {
  240. display: flex;
  241. flex-direction: row;
  242. flex-wrap: wrap;
  243. gap: 8rpx;
  244. justify-content: flex-end;
  245. }
  246. .sp-range-tab {
  247. padding: 6rpx 16rpx;
  248. border-radius: 8rpx;
  249. border: 1rpx solid $morandi-border-strong;
  250. background: #ffffff;
  251. }
  252. .sp-range-tab--on {
  253. border-color: #3b82f6;
  254. background: rgba(59, 130, 246, 0.1);
  255. }
  256. .sp-range-tab__txt {
  257. font-size: 22rpx;
  258. color: $morandi-text-secondary;
  259. }
  260. .sp-range-tab--on .sp-range-tab__txt {
  261. color: #2563eb;
  262. font-weight: 600;
  263. }
  264. .sp-loading {
  265. padding: 32rpx 0;
  266. text-align: center;
  267. }
  268. .sp-loading__txt {
  269. font-size: 26rpx;
  270. color: $morandi-text-muted;
  271. }
  272. .sp-metrics {
  273. display: flex;
  274. flex-direction: row;
  275. flex-wrap: wrap;
  276. gap: 16rpx;
  277. }
  278. .sp-metric {
  279. width: calc(50% - 8rpx);
  280. padding: 20rpx 16rpx;
  281. border-radius: 12rpx;
  282. background: #f5f7fa;
  283. box-sizing: border-box;
  284. display: flex;
  285. flex-direction: column;
  286. gap: 10rpx;
  287. }
  288. .sp-metric__label {
  289. font-size: 24rpx;
  290. color: $morandi-text-muted;
  291. }
  292. .sp-metric__value {
  293. font-size: 30rpx;
  294. font-weight: 600;
  295. color: $morandi-text;
  296. word-break: break-word;
  297. }
  298. .sp-chart-host {
  299. width: 100%;
  300. height: 360rpx;
  301. border-radius: 8rpx;
  302. overflow: hidden;
  303. background: #f0f6fb;
  304. }
  305. .sp-chart-host :deep(.ec-canvas) {
  306. display: block;
  307. width: 100% !important;
  308. height: 100% !important;
  309. }
  310. .sp-footer-spacer {
  311. height: 40rpx;
  312. }
  313. </style>