commonMixins.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. watch: {
  22. 'config.expression': { // 表达式发生变化
  23. handler (val) {
  24. this.getDataByExpression(this.config)
  25. }
  26. },
  27. currentDataset: { // 关联的数据发生变化
  28. handler (val) {
  29. if (val && Object.keys(val).length) {
  30. this.getDataByExpression(this.config)
  31. }
  32. },
  33. deep: true,
  34. immediate: true
  35. },
  36. currentComputedDatas: { // 关联的数据发生变化
  37. handler (val) {
  38. if (val && Object.keys(val).length) {
  39. this.getDataByExpression(this.config)
  40. }
  41. },
  42. deep: true,
  43. immediate: true
  44. }
  45. },
  46. computed: {
  47. ...mapState({
  48. pageCode: state => state.bigScreen.pageInfo.code,
  49. customTheme: state => state.bigScreen.pageInfo.pageConfig.customTheme,
  50. activeCode: state => state.bigScreen.activeCode
  51. // dataset: state => state.bigScreen.dataset
  52. }),
  53. // 所有组件的数据集合
  54. dataset () {
  55. return this.$store.state.bigScreen.dataset
  56. },
  57. // 所有组件的数据集合
  58. computedDatas () {
  59. return this.$store.state.bigScreen.computedDatas
  60. },
  61. // 跟当前组件计算表达式关联的组件的数据集合
  62. currentDataset () {
  63. // ['RiTkJGDa','PEKwsHbf']this.config.expressionCodes
  64. const newDataset = this.config.expressionCodes?.map(code => {
  65. return this.dataset[code]
  66. })
  67. if (newDataset?.some(item => !item)) {
  68. return null
  69. } else {
  70. return newDataset
  71. }
  72. },
  73. // 跟当前组件计算表达式关联的组件的数据集合
  74. currentComputedDatas () {
  75. // ['RiTkJGDa','PEKwsHbf']this.config.expressionCodes
  76. const newDataset = this.config.expressionCodes?.map(code => {
  77. return this.computedDatas[code]
  78. })
  79. if (newDataset?.some(item => !item)) {
  80. return null
  81. } else {
  82. return newDataset
  83. }
  84. },
  85. // 组件数据加载时的背景颜色
  86. loadingBackground () {
  87. return this.customTheme === 'light' ? '#ffffff' : '#151A26'
  88. },
  89. isPreview () {
  90. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  91. }
  92. },
  93. mounted () {
  94. if (!['tables', 'flyMap', 'map'].includes(this.config.type)) {
  95. this.chartInit()
  96. }
  97. this.watchCacheData()
  98. },
  99. methods: {
  100. ...mapMutations({
  101. changeChartConfig: 'bigScreen/changeChartConfig',
  102. changeActiveItemConfig: 'bigScreen/changeActiveItemConfig',
  103. updateDataset: 'bigScreen/updateDataset',
  104. updateComputedDatas: 'bigScreen/updateComputedDatas'
  105. }),
  106. /**
  107. * 初始化组件
  108. */
  109. chartInit () {
  110. let config = this.config
  111. // key和code相等,说明是一进来刷新,调用list接口
  112. if (this.isPreview) {
  113. // 改变样式
  114. config = this.changeStyle(config) ? this.changeStyle(config) : config
  115. // 改变数据
  116. config = this.changeDataByCode(config)
  117. } else {
  118. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  119. // eslint-disable-next-line no-unused-vars
  120. config = this.changeData(config)
  121. }
  122. },
  123. /**
  124. * 初始化组件时获取后端返回的数据, 返回数据和当前组件的配置_list
  125. * @param settingConfig 设置时的配置。不传则为当前组件的配置
  126. * @returns {Promise<unknown>}
  127. */
  128. changeDataByCode (config) {
  129. let currentPage = 1
  130. let size = 10
  131. if (config?.option?.pagination) {
  132. currentPage = config.option.pagination.currentPage
  133. size = config.option.pagination.pageSize
  134. }
  135. return new Promise((resolve, reject) => {
  136. config.loading = true
  137. getChatInfo({
  138. // innerChartCode: this.pageCode ? config.code : undefined,
  139. chartCode: config.code,
  140. current: currentPage,
  141. pageCode: this.pageCode,
  142. size: size,
  143. type: config.type
  144. }).then(async (res) => {
  145. config.loading = false
  146. let _res = cloneDeep(res)
  147. // 如果是http数据集的前端代理,则需要调封装的axios请求
  148. if (res.executionByFrontend) {
  149. if (res.data.datasetType === 'http') {
  150. _res = await axiosFormatting(res.data)
  151. _res = this.httpDataFormatting(res, _res)
  152. }
  153. if (res.data.datasetType === 'js') {
  154. try {
  155. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  156. const value = this.config.dataSource?.params[p]
  157. if (!isNaN(value)) {
  158. return value || p
  159. } else {
  160. return `'${value}' || '${p}'`
  161. }
  162. })
  163. // eslint-disable-next-line no-new-func
  164. const scriptMethod = new Function(scriptAfterReplacement)
  165. _res.data = scriptMethod()
  166. } catch (error) {
  167. console.info('JS数据集脚本执行失败', error)
  168. }
  169. }
  170. }
  171. // 将后端返回的数据保存
  172. if (_res.success) {
  173. this.updateDataset({ code: config.code, title: config.title, data: _res?.data })
  174. }
  175. config = this.dataFormatting(config, _res)
  176. this.changeChartConfig(config)
  177. }).catch((err) => {
  178. console.info(err)
  179. }).finally(() => {
  180. resolve(config)
  181. })
  182. })
  183. },
  184. /**
  185. * @description: 更新chart
  186. * @param {Object} config
  187. */
  188. changeData (config, filterList) {
  189. const list = config?.paramsList?.map((item) => {
  190. if (item.value === '${level}') {
  191. return { ...item, value: config.customize.level }
  192. } else if (item.value === '${name}') {
  193. return { ...item, value: config.customize.scope }
  194. } else {
  195. return item
  196. }
  197. })
  198. const params = {
  199. chart: {
  200. ...config,
  201. paramsList: list ? [...list] : [],
  202. option: undefined
  203. },
  204. current: 1,
  205. pageCode: this.pageCode,
  206. type: config.type,
  207. filterList: filterList || this.filterList
  208. }
  209. return new Promise((resolve, reject) => {
  210. config.loading = true
  211. getUpdateChartInfo(params).then(async (res) => {
  212. config.loading = false
  213. let _res = cloneDeep(res)
  214. // 如果是http数据集的前端代理,则需要调封装的axios请求
  215. if (res.executionByFrontend) {
  216. if (res.data.datasetType === 'http') {
  217. _res = await axiosFormatting(res.data)
  218. _res = this.httpDataFormatting(res, _res)
  219. }
  220. if (res.data.datasetType === 'js') {
  221. try {
  222. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  223. const value = this.config.dataSource?.params[p]
  224. if (!isNaN(value)) {
  225. return value || p
  226. } else {
  227. return `'${value}' || '${p}'`
  228. }
  229. })
  230. // eslint-disable-next-line no-new-func
  231. const scriptMethod = new Function(scriptAfterReplacement)
  232. _res.data = scriptMethod()
  233. } catch (error) {
  234. console.info('JS数据集脚本执行失败', error)
  235. }
  236. }
  237. }
  238. // 将后端返回的数据保存
  239. if (_res.success) {
  240. this.updateDataset({ code: config.code, title: config.title, data: _res?.data })
  241. }
  242. config = this.dataFormatting(config, _res)
  243. if (this.chart) {
  244. // 单指标组件和多指标组件的changeData传参不同
  245. if (['Liquid', 'Gauge', 'RingProgress'].includes(config.chartType)) {
  246. this.chart.changeData(config.option.percent)
  247. } else {
  248. this.chart.changeData(config.option.data)
  249. }
  250. }
  251. }).catch(err => {
  252. console.info(err)
  253. }).finally(() => {
  254. if (config) {
  255. config.loading = false
  256. }
  257. resolve(config)
  258. })
  259. })
  260. },
  261. // http前台代理需要对返回的数据进行重新组装
  262. httpDataFormatting (chartRes, httpRes) {
  263. let result = {}
  264. result = {
  265. columnData: chartRes.columnData,
  266. data: httpRes,
  267. success: chartRes.success
  268. }
  269. return result
  270. },
  271. dataFormatting (config, data) {
  272. // 覆盖
  273. },
  274. newChart (option) {
  275. // 覆盖
  276. },
  277. // 通过表达式计算获取组件的值
  278. getDataByExpression (config) {
  279. // 覆盖
  280. },
  281. changeStyle (config) {
  282. config = { ...this.config, ...config }
  283. // 样式改变时更新主题配置
  284. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  285. this.changeChartConfig(config)
  286. if (config.code === this.activeCode) {
  287. this.changeActiveItemConfig(config)
  288. }
  289. },
  290. // 缓存组件数据监听
  291. watchCacheData () {
  292. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  293. // 如果是缓存数据集
  294. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  295. if (
  296. this.config.dataSource.dataSetType === '2' &&
  297. this.config.dataSource.businessKey === dataSetId
  298. ) {
  299. const config = this.dataFormatting(this.config, data)
  300. config.key = new Date().getTime()
  301. this.changeChartConfig(config)
  302. this.newChart(config.option)
  303. }
  304. })
  305. }
  306. }
  307. }