commonMixins.js 7.6 KB

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