commonMixins.js 7.3 KB

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