commonMixins.js 7.8 KB

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