commonMixins.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. if (data.executionByFrontend) {
  75. try {
  76. const scriptAfterReplacement = data.data.replace(/\${(.*?)}/g, (match, p) => {
  77. // 根据parmas的key获取value
  78. return `'${this.config.dataSource?.params[p]}' || '${p}'`
  79. })
  80. // eslint-disable-next-line no-new-func
  81. const scriptMethod = new Function(scriptAfterReplacement)
  82. data.data = scriptMethod()
  83. } catch (error) {
  84. console.error('数据集脚本执行失败', error)
  85. }
  86. }
  87. config = this.dataFormatting(config, data)
  88. this.changeChartConfig(config)
  89. }).catch((err) => {
  90. console.log(err)
  91. }).finally(() => {
  92. resolve(config)
  93. })
  94. })
  95. },
  96. /**
  97. * @description: 更新chart
  98. * @param {Object} config
  99. */
  100. changeData (config, filterList) {
  101. const params = {
  102. chart: {
  103. ...config,
  104. option: undefined
  105. },
  106. current: 1,
  107. pageCode: this.pageCode,
  108. type: config.type,
  109. filterList: filterList || this.filterList
  110. }
  111. return new Promise((resolve, reject) => {
  112. getUpdateChartInfo(params).then((data) => {
  113. if (data.executionByFrontend) {
  114. try {
  115. const scriptAfterReplacement = data.data.replace(/\${(.*?)}/g, (match, p) => {
  116. // 根据parmas的key获取value
  117. return `'${this.config.dataSource?.params[p]}' || '${p}'`
  118. })
  119. // eslint-disable-next-line no-new-func
  120. const scriptMethod = new Function(scriptAfterReplacement)
  121. data.data = scriptMethod()
  122. } catch (error) {
  123. console.error('数据集脚本执行失败', error)
  124. }
  125. }
  126. config = this.dataFormatting(config, data)
  127. // this.changeChartConfig(config)
  128. if (this.chart) {
  129. // 单指标组件和多指标组件的changeData传参不同
  130. if (['Liquid', 'Gauge', 'RingProgress'].includes(config.chartType)) {
  131. this.chart.changeData(config.option.percent)
  132. } else {
  133. this.chart.changeData(config.option.data)
  134. }
  135. }
  136. }).catch(err => {
  137. console.log(err)
  138. }).finally(() => {
  139. resolve(config)
  140. })
  141. })
  142. },
  143. dataFormatting (config, data) {
  144. // 覆盖
  145. },
  146. newChart (option) {
  147. // 覆盖
  148. },
  149. changeStyle (config) {
  150. // this.changeChartConfig(config)
  151. // return config
  152. },
  153. // 缓存组件数据监听
  154. watchCacheData () {
  155. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  156. // 如果是缓存数据集
  157. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  158. if (
  159. this.config.dataSource.dataSetType === '2' &&
  160. this.config.dataSource.businessKey === dataSetId
  161. ) {
  162. const config = this.dataFormatting(this.config, data)
  163. config.key = new Date().getTime()
  164. this.changeChartConfig(config)
  165. this.newChart(config.option)
  166. }
  167. })
  168. }
  169. }
  170. }