commonMixins.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(async (res) => {
  75. let _res = _.cloneDeep(res)
  76. // 如果是http数据集的前端代理,则需要调封装的axios请求
  77. if (res.executionByFrontend) {
  78. if (res.data.datasetType === 'http') {
  79. _res = await axiosFormatting(res.data)
  80. }
  81. if (res.data.datasetType === 'js') {
  82. try {
  83. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  84. // 根据parmas的key获取value
  85. return `'${this.config.dataSource?.params[p]}' || '${p}'`
  86. })
  87. // eslint-disable-next-line no-new-func
  88. const scriptMethod = new Function(scriptAfterReplacement)
  89. _res.data = scriptMethod()
  90. } catch (error) {
  91. console.error('JS数据集脚本执行失败', error)
  92. }
  93. }
  94. }
  95. config = this.dataFormatting(config, _res)
  96. this.changeChartConfig(config)
  97. }).catch((err) => {
  98. console.error(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(async (res) => {
  121. let _res = _.cloneDeep(res)
  122. // 如果是http数据集的前端代理,则需要调封装的axios请求
  123. if (res.executionByFrontend) {
  124. if (res.data.datasetType === 'http') {
  125. _res = await axiosFormatting(res.data)
  126. }
  127. if (res.data.datasetType === 'js') {
  128. try {
  129. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  130. // 根据parmas的key获取value
  131. return `'${this.config.dataSource?.params[p]}' || '${p}'`
  132. })
  133. // eslint-disable-next-line no-new-func
  134. const scriptMethod = new Function(scriptAfterReplacement)
  135. _res.data = scriptMethod()
  136. } catch (error) {
  137. console.error('JS数据集脚本执行失败', error)
  138. }
  139. }
  140. }
  141. config = this.dataFormatting(config, _res)
  142. if (this.chart) {
  143. // 单指标组件和多指标组件的changeData传参不同
  144. if (['Liquid', 'Gauge', 'RingProgress'].includes(config.chartType)) {
  145. this.chart.changeData(config.option.percent)
  146. } else {
  147. this.chart.changeData(config.option.data)
  148. }
  149. }
  150. }).catch(err => {
  151. console.error(err)
  152. }).finally(() => {
  153. resolve(config)
  154. })
  155. })
  156. },
  157. dataFormatting (config, data) {
  158. // 覆盖
  159. },
  160. newChart (option) {
  161. // 覆盖
  162. },
  163. changeStyle (config) {
  164. // this.changeChartConfig(config)
  165. // return config
  166. },
  167. // 缓存组件数据监听
  168. watchCacheData () {
  169. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  170. // 如果是缓存数据集
  171. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  172. if (
  173. this.config.dataSource.dataSetType === '2' &&
  174. this.config.dataSource.businessKey === dataSetId
  175. ) {
  176. const config = this.dataFormatting(this.config, data)
  177. config.key = new Date().getTime()
  178. this.changeChartConfig(config)
  179. this.newChart(config.option)
  180. }
  181. })
  182. }
  183. }
  184. }