commonMixins.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. _res = this.httpDataFormatting(res, _res)
  81. }
  82. if (res.data.datasetType === 'js') {
  83. try {
  84. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  85. const value = this.config.dataSource?.params[p]
  86. if (!isNaN(value)) {
  87. return value || p
  88. } else {
  89. return `'${value}' || '${p}'`
  90. }
  91. })
  92. // eslint-disable-next-line no-new-func
  93. const scriptMethod = new Function(scriptAfterReplacement)
  94. _res.data = scriptMethod()
  95. } catch (error) {
  96. console.error('JS数据集脚本执行失败', error)
  97. }
  98. }
  99. }
  100. config = this.dataFormatting(config, _res)
  101. this.changeChartConfig(config)
  102. }).catch((err) => {
  103. console.error(err)
  104. }).finally(() => {
  105. resolve(config)
  106. })
  107. })
  108. },
  109. /**
  110. * @description: 更新chart
  111. * @param {Object} config
  112. */
  113. changeData (config, filterList) {
  114. const params = {
  115. chart: {
  116. ...config,
  117. option: undefined
  118. },
  119. current: 1,
  120. pageCode: this.pageCode,
  121. type: config.type,
  122. filterList: filterList || this.filterList
  123. }
  124. return new Promise((resolve, reject) => {
  125. getUpdateChartInfo(params).then(async (res) => {
  126. let _res = _.cloneDeep(res)
  127. // 如果是http数据集的前端代理,则需要调封装的axios请求
  128. if (res.executionByFrontend) {
  129. if (res.data.datasetType === 'http') {
  130. _res = await axiosFormatting(res.data)
  131. _res = this.httpDataFormatting(res, _res)
  132. }
  133. if (res.data.datasetType === 'js') {
  134. try {
  135. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  136. const value = this.config.dataSource?.params[p]
  137. if (!isNaN(value)) {
  138. return value || p
  139. } else {
  140. return `'${value}' || '${p}'`
  141. }
  142. })
  143. // eslint-disable-next-line no-new-func
  144. const scriptMethod = new Function(scriptAfterReplacement)
  145. _res.data = scriptMethod()
  146. } catch (error) {
  147. console.error('JS数据集脚本执行失败', error)
  148. }
  149. }
  150. }
  151. config = this.dataFormatting(config, _res)
  152. if (this.chart) {
  153. // 单指标组件和多指标组件的changeData传参不同
  154. if (['Liquid', 'Gauge', 'RingProgress'].includes(config.chartType)) {
  155. this.chart.changeData(config.option.percent)
  156. } else {
  157. this.chart.changeData(config.option.data)
  158. }
  159. }
  160. }).catch(err => {
  161. console.error(err)
  162. }).finally(() => {
  163. resolve(config)
  164. })
  165. })
  166. },
  167. // http前台代理需要对返回的数据进行重新组装
  168. httpDataFormatting (chartRes, httpRes) {
  169. let result = {}
  170. result = {
  171. columnData: chartRes.columnData,
  172. data: httpRes,
  173. success: chartRes.success
  174. }
  175. return result
  176. },
  177. dataFormatting (config, data) {
  178. // 覆盖
  179. },
  180. newChart (option) {
  181. // 覆盖
  182. },
  183. changeStyle (config) {
  184. // this.changeChartConfig(config)
  185. // return config
  186. },
  187. // 缓存组件数据监听
  188. watchCacheData () {
  189. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  190. // 如果是缓存数据集
  191. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  192. if (
  193. this.config.dataSource.dataSetType === '2' &&
  194. this.config.dataSource.businessKey === dataSetId
  195. ) {
  196. const config = this.dataFormatting(this.config, data)
  197. config.key = new Date().getTime()
  198. this.changeChartConfig(config)
  199. this.newChart(config.option)
  200. }
  201. })
  202. }
  203. }
  204. }