commonMixins.js 11 KB

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