commonMixins.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 'data-room-ui/js/utils/eventBus'
  9. import { getChatInfo, getUpdateChartInfo } from '../api/bigScreenApi'
  10. import axiosFormatting from '../../js/utils/httpParamsFormatting'
  11. export default {
  12. data () {
  13. return {
  14. filterList: [],
  15. treeParentId: 0,
  16. dataLoading: false
  17. }
  18. },
  19. computed: {
  20. ...mapState({
  21. pageCode: state => state.bigScreen.pageInfo.code
  22. }),
  23. isPreview () {
  24. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  25. }
  26. },
  27. mounted () {
  28. if (!['tables'].includes(this.config.type)) {
  29. this.chartInit()
  30. }
  31. this.watchCacheData()
  32. },
  33. methods: {
  34. ...mapMutations({
  35. changeChartConfig: 'bigScreen/changeChartConfig'
  36. }),
  37. /**
  38. * 初始化组件
  39. */
  40. chartInit () {
  41. let config = this.config
  42. // key和code相等,说明是一进来刷新,调用list接口
  43. if (this.isPreview) {
  44. // 改变样式
  45. config = this.changeStyle(config) ? this.changeStyle(config) : config
  46. // 改变数据
  47. config = this.changeDataByCode(config)
  48. } else {
  49. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  50. // eslint-disable-next-line no-unused-vars
  51. config = this.changeData(config)
  52. }
  53. },
  54. /**
  55. * 初始化组件时获取后端返回的数据, 返回数据和当前组件的配置_list
  56. * @param settingConfig 设置时的配置。不传则为当前组件的配置
  57. * @returns {Promise<unknown>}
  58. */
  59. changeDataByCode (config) {
  60. let currentPage = 1
  61. let size = 10
  62. if (config?.option?.pagination) {
  63. currentPage = config.option.pagination.currentPage
  64. size = config.option.pagination.pageSize
  65. }
  66. return new Promise((resolve, reject) => {
  67. getChatInfo({
  68. // innerChartCode: this.pageCode ? config.code : undefined,
  69. chartCode: config.code,
  70. current: currentPage,
  71. pageCode: this.pageCode,
  72. size: size,
  73. type: config.type
  74. }).then((data) => {
  75. // 如果是http数据集的前端代理,则需要调封装的axios请求
  76. // if (data.executionByFrontend) {
  77. // axiosFormatting(data.data).then(res => {
  78. // config = this.dataFormatting(config, res)
  79. // this.changeChartConfig(config)
  80. // })
  81. // }
  82. if (data.datasetType !== 'http' && data.executionByFrontend) {
  83. try {
  84. const scriptAfterReplacement = data.data.replace(/\${(.*?)}/g, (match, p) => {
  85. // 根据parmas的key获取value
  86. return `'${this.config.dataSource?.params[p]}' || '${p}'`
  87. })
  88. // eslint-disable-next-line no-new-func
  89. const scriptMethod = new Function(scriptAfterReplacement)
  90. data.data = scriptMethod()
  91. } catch (error) {
  92. console.error('数据集脚本执行失败', error)
  93. }
  94. }
  95. config = this.dataFormatting(config, data)
  96. this.changeChartConfig(config)
  97. }).catch((err) => {
  98. console.log(err)
  99. }).finally(() => {
  100. resolve(config)
  101. })
  102. })
  103. },
  104. /**
  105. * @description: 更新chart
  106. * @param {Object} config
  107. */
  108. changeData (config, filterList) {
  109. const params = {
  110. chart: {
  111. ...config,
  112. option: undefined
  113. },
  114. current: 1,
  115. pageCode: this.pageCode,
  116. type: config.type,
  117. filterList: filterList || this.filterList
  118. }
  119. return new Promise((resolve, reject) => {
  120. getUpdateChartInfo(params).then((data) => {
  121. // 如果是http数据集的前端代理,则需要调封装的axios请求
  122. // if (data.executionByFrontend) {
  123. // axiosFormatting(data.data).then(res => {
  124. // config = this.dataFormatting(config, res)
  125. // this.changeChartConfig(config)
  126. // })
  127. // }
  128. if (data.executionByFrontend) {
  129. try {
  130. const scriptAfterReplacement = data.data.replace(/\${(.*?)}/g, (match, p) => {
  131. // 根据parmas的key获取value
  132. return `'${this.config.dataSource?.params[p]}' || '${p}'`
  133. })
  134. // eslint-disable-next-line no-new-func
  135. const scriptMethod = new Function(scriptAfterReplacement)
  136. data.data = scriptMethod()
  137. } catch (error) {
  138. console.error('数据集脚本执行失败', error)
  139. }
  140. }
  141. config = this.dataFormatting(config, data)
  142. this.changeChartConfig(config)
  143. if (this.chart) {
  144. // 单指标组件和多指标组件的changeData传参不同
  145. if (['Liquid', 'Gauge', 'RingProgress'].includes(config.chartType)) {
  146. this.chart.changeData(config.option.percent)
  147. } else {
  148. this.chart.changeData(config.option.data)
  149. }
  150. }
  151. }).catch(err => {
  152. console.log(err)
  153. }).finally(() => {
  154. resolve(config)
  155. })
  156. })
  157. },
  158. dataFormatting (config, data) {
  159. // 覆盖
  160. },
  161. newChart (option) {
  162. // 覆盖
  163. },
  164. changeStyle (config) {
  165. // this.changeChartConfig(config)
  166. // return config
  167. },
  168. // 缓存组件数据监听
  169. watchCacheData () {
  170. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  171. // 如果是缓存数据集
  172. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  173. if (
  174. this.config.dataSource.dataSetType === '2' &&
  175. this.config.dataSource.businessKey === dataSetId
  176. ) {
  177. const config = this.dataFormatting(this.config, data)
  178. config.key = new Date().getTime()
  179. this.changeChartConfig(config)
  180. this.newChart(config.option)
  181. }
  182. })
  183. }
  184. }
  185. }