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