commonMixins.js 7.9 KB

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