commonMixins.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. const newDataset = {}
  64. this.config.expressionCodes?.forEach(code => {
  65. if (this.dataset[code]) {
  66. newDataset[code] = this.dataset[code]
  67. }
  68. })
  69. return newDataset
  70. },
  71. // 跟当前组件计算表达式关联的组件的数据集合
  72. currentComputedDatas () {
  73. const newDataset = {}
  74. this.config.expressionCodes?.forEach(code => {
  75. if (this.computedDatas[code]) {
  76. newDataset[code] = this.computedDatas[code]
  77. }
  78. })
  79. return newDataset
  80. },
  81. // 组件数据加载时的背景颜色
  82. loadingBackground () {
  83. return this.customTheme === 'light' ? '#ffffff' : '#151A26'
  84. },
  85. isPreview () {
  86. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  87. }
  88. },
  89. mounted () {
  90. if (!['tables', 'flyMap', 'map'].includes(this.config.type)) {
  91. this.chartInit()
  92. }
  93. this.watchCacheData()
  94. },
  95. methods: {
  96. ...mapMutations({
  97. changeChartConfig: 'bigScreen/changeChartConfig',
  98. changeActiveItemConfig: 'bigScreen/changeActiveItemConfig',
  99. updateDataset: 'bigScreen/updateDataset',
  100. updateComputedDatas: 'bigScreen/updateComputedDatas'
  101. }),
  102. /**
  103. * 初始化组件
  104. */
  105. chartInit () {
  106. let config = this.config
  107. // key和code相等,说明是一进来刷新,调用list接口
  108. if (this.isPreview) {
  109. // 改变样式
  110. config = this.changeStyle(config) ? this.changeStyle(config) : config
  111. // 改变数据
  112. config = this.changeDataByCode(config)
  113. } else {
  114. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  115. // eslint-disable-next-line no-unused-vars
  116. config = this.changeData(config)
  117. }
  118. },
  119. /**
  120. * 初始化组件时获取后端返回的数据, 返回数据和当前组件的配置_list
  121. * @param settingConfig 设置时的配置。不传则为当前组件的配置
  122. * @returns {Promise<unknown>}
  123. */
  124. changeDataByCode (config) {
  125. let currentPage = 1
  126. let size = 10
  127. if (config?.option?.pagination) {
  128. currentPage = config.option.pagination.currentPage
  129. size = config.option.pagination.pageSize
  130. }
  131. return new Promise((resolve, reject) => {
  132. config.loading = true
  133. getChatInfo({
  134. // innerChartCode: this.pageCode ? config.code : undefined,
  135. chartCode: config.code,
  136. current: currentPage,
  137. pageCode: this.pageCode,
  138. size: size,
  139. type: config.type
  140. }).then(async (res) => {
  141. config.loading = false
  142. let _res = cloneDeep(res)
  143. // 如果是http数据集的前端代理,则需要调封装的axios请求
  144. if (res.executionByFrontend) {
  145. if (res.data.datasetType === 'http') {
  146. _res = await axiosFormatting(res.data)
  147. _res = this.httpDataFormatting(res, _res)
  148. }
  149. if (res.data.datasetType === 'js') {
  150. try {
  151. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  152. const value = this.config.dataSource?.params[p]
  153. if (value === null || value === undefined || value === '') {
  154. return "''"
  155. } else if (!isNaN(value)) {
  156. return value || p
  157. } else {
  158. return `'${value}' || '${p}'`
  159. }
  160. })
  161. // eslint-disable-next-line no-new-func
  162. const scriptMethod = new Function(scriptAfterReplacement)
  163. _res.data = scriptMethod()
  164. } catch (error) {
  165. console.info('JS数据集脚本执行失败', error)
  166. }
  167. }
  168. }
  169. // 将后端返回的数据保存
  170. if (_res.success) {
  171. this.updateDataset({ code: config.code, name: config.name, data: _res?.data })
  172. }
  173. config = this.dataFormatting(config, _res)
  174. this.changeChartConfig(config)
  175. }).catch((err) => {
  176. console.info(err)
  177. }).finally(() => {
  178. resolve(config)
  179. })
  180. })
  181. },
  182. /**
  183. * @description: 更新chart
  184. * @param {Object} config
  185. */
  186. changeData (config, filterList) {
  187. const list = config?.paramsList?.map((item) => {
  188. if (item.value === '${level}') {
  189. return { ...item, value: config.customize.level }
  190. } else if (item.value === '${name}') {
  191. return { ...item, value: config.customize.scope }
  192. } else {
  193. return item
  194. }
  195. })
  196. const params = {
  197. chart: {
  198. ...config,
  199. paramsList: list ? [...list] : [],
  200. option: undefined
  201. },
  202. current: 1,
  203. pageCode: this.pageCode,
  204. type: config.type,
  205. filterList: filterList || this.filterList
  206. }
  207. return new Promise((resolve, reject) => {
  208. config.loading = true
  209. getUpdateChartInfo(params).then(async (res) => {
  210. config.loading = false
  211. let _res = cloneDeep(res)
  212. // 如果是http数据集的前端代理,则需要调封装的axios请求
  213. if (res.executionByFrontend) {
  214. if (res.data.datasetType === 'http') {
  215. _res = await axiosFormatting(res.data)
  216. _res = this.httpDataFormatting(res, _res)
  217. }
  218. if (res.data.datasetType === 'js') {
  219. try {
  220. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  221. const value = this.config.dataSource?.params[p]
  222. if (value === null || value === undefined || value === '') {
  223. return "''"
  224. } else 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, name: config.name, 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. }