commonMixins.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * @description: bigScreen公共方法
  3. * @Date: 2023-03-24 17:10:43
  4. * @Author: xing.heng
  5. */
  6. import _ from 'lodash'
  7. import { mapMutations, mapState } from 'vuex'
  8. import { EventBus } from 'packages/js/utils/eventBus'
  9. import { getChatInfo, getUpdateChartInfo } from '../api/bigScreenApi'
  10. export default {
  11. data () {
  12. return {
  13. filterList: [],
  14. treeParentId: 0,
  15. dataLoading: false
  16. }
  17. },
  18. computed: {
  19. ...mapState({
  20. pageCode: state => state.bigScreen.pageInfo.code
  21. }),
  22. isPreview () {
  23. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  24. }
  25. },
  26. mounted () {
  27. if (!['tables'].includes(this.config.type)) {
  28. this.chartInit()
  29. }
  30. this.watchCacheData()
  31. },
  32. methods: {
  33. ...mapMutations({
  34. changeChartConfig: 'bigScreen/changeChartConfig'
  35. }),
  36. /**
  37. * 初始化组件
  38. */
  39. chartInit () {
  40. let config = this.config
  41. // key和code相等,说明是一进来刷新,调用list接口
  42. if (this.isPreview) {
  43. // 改变样式
  44. config = this.changeStyle(config) ? this.changeStyle(config) : config
  45. // 改变数据
  46. config = this.changeDataByCode(config)
  47. } else {
  48. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  49. // eslint-disable-next-line no-unused-vars
  50. config = this.changeData(config)
  51. }
  52. },
  53. /**
  54. * 初始化组件时获取后端返回的数据, 返回数据和当前组件的配置_list
  55. * @param settingConfig 设置时的配置。不传则为当前组件的配置
  56. * @returns {Promise<unknown>}
  57. */
  58. changeDataByCode (config) {
  59. let currentPage = 1
  60. let size = 10
  61. if (config?.option?.pagination) {
  62. currentPage = config.option.pagination.currentPage
  63. size = config.option.pagination.pageSize
  64. }
  65. return new Promise((resolve, reject) => {
  66. getChatInfo({
  67. // innerChartCode: this.pageCode ? config.code : undefined,
  68. chartCode: config.code,
  69. current: currentPage,
  70. pageCode: this.pageCode,
  71. size: size,
  72. type: config.type
  73. }).then((data) => {
  74. config = this.dataFormatting(config, data)
  75. this.changeChartConfig(config)
  76. }).catch((err) => {
  77. console.log(err)
  78. }).finally(() => {
  79. resolve(config)
  80. })
  81. })
  82. },
  83. /**
  84. * @description: 更新chart
  85. * @param {Object} config
  86. */
  87. changeData (config, filterList) {
  88. const params = {
  89. chart: {
  90. ...config,
  91. option: undefined
  92. },
  93. current: 1,
  94. pageCode: this.pageCode,
  95. type: config.type,
  96. filterList: filterList || this.filterList
  97. }
  98. return new Promise((resolve, reject) => {
  99. getUpdateChartInfo(params).then((data) => {
  100. config = this.dataFormatting(config, data)
  101. // this.changeChartConfig(config)
  102. if (this.chart) {
  103. // 单指标组件和多指标组件的changeData传参不同
  104. if (['Liquid', 'Gauge', 'RingProgress'].includes(config.chartType)) {
  105. this.chart.changeData(config.option.percent)
  106. } else {
  107. this.chart.changeData(config.option.data)
  108. }
  109. }
  110. }).catch(err => {
  111. console.log(err)
  112. }).finally(() => {
  113. resolve(config)
  114. })
  115. })
  116. },
  117. dataFormatting (config, data) {
  118. // 覆盖
  119. },
  120. newChart (option) {
  121. // 覆盖
  122. },
  123. changeStyle (config) {
  124. // this.changeChartConfig(config)
  125. // return config
  126. },
  127. // 缓存组件数据监听
  128. watchCacheData () {
  129. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  130. // 如果是缓存数据集
  131. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  132. if (
  133. this.config.dataSource.dataSetType === '2' &&
  134. this.config.dataSource.businessKey === dataSetId
  135. ) {
  136. const config = this.dataFormatting(this.config, data)
  137. config.key = new Date().getTime()
  138. this.changeChartConfig(config)
  139. this.newChart(config.option)
  140. }
  141. })
  142. }
  143. }
  144. }