commonMixins.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 (value === null || value === undefined || value === '') {
  158. return "''"
  159. } else if (!isNaN(value)) {
  160. return value || p
  161. } else {
  162. return `'${value}' || '${p}'`
  163. }
  164. })
  165. // eslint-disable-next-line no-new-func
  166. const scriptMethod = new Function(scriptAfterReplacement)
  167. _res.data = scriptMethod()
  168. } catch (error) {
  169. console.info('JS数据集脚本执行失败', error)
  170. }
  171. }
  172. }
  173. // 将后端返回的数据保存
  174. if (_res.success) {
  175. this.updateDataset({ code: config.code, title: config.title, data: _res?.data })
  176. }
  177. config = this.dataFormatting(config, _res)
  178. this.changeChartConfig(config)
  179. }).catch((err) => {
  180. console.info(err)
  181. }).finally(() => {
  182. resolve(config)
  183. })
  184. })
  185. },
  186. /**
  187. * @description: 更新chart
  188. * @param {Object} config
  189. */
  190. changeData (config, filterList) {
  191. const list = config?.paramsList?.map((item) => {
  192. if (item.value === '${level}') {
  193. return { ...item, value: config.customize.level }
  194. } else if (item.value === '${name}') {
  195. return { ...item, value: config.customize.scope }
  196. } else {
  197. return item
  198. }
  199. })
  200. const params = {
  201. chart: {
  202. ...config,
  203. paramsList: list ? [...list] : [],
  204. option: undefined
  205. },
  206. current: 1,
  207. pageCode: this.pageCode,
  208. type: config.type,
  209. filterList: filterList || this.filterList
  210. }
  211. return new Promise((resolve, reject) => {
  212. config.loading = true
  213. getUpdateChartInfo(params).then(async (res) => {
  214. config.loading = false
  215. let _res = cloneDeep(res)
  216. // 如果是http数据集的前端代理,则需要调封装的axios请求
  217. if (res.executionByFrontend) {
  218. if (res.data.datasetType === 'http') {
  219. _res = await axiosFormatting(res.data)
  220. _res = this.httpDataFormatting(res, _res)
  221. }
  222. if (res.data.datasetType === 'js') {
  223. try {
  224. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  225. const value = this.config.dataSource?.params[p]
  226. if (value === null || value === undefined || value === '') {
  227. return "''"
  228. } else if (!isNaN(value)) {
  229. return value || p
  230. } else {
  231. return `'${value}' || '${p}'`
  232. }
  233. })
  234. // eslint-disable-next-line no-new-func
  235. const scriptMethod = new Function(scriptAfterReplacement)
  236. _res.data = scriptMethod()
  237. } catch (error) {
  238. console.info('JS数据集脚本执行失败', error)
  239. }
  240. }
  241. }
  242. // 将后端返回的数据保存
  243. if (_res.success) {
  244. this.updateDataset({ code: config.code, title: config.title, data: _res?.data })
  245. }
  246. config = this.dataFormatting(config, _res)
  247. if (this.chart) {
  248. // 单指标组件和多指标组件的changeData传参不同
  249. if (['Liquid', 'Gauge', 'RingProgress'].includes(config.chartType)) {
  250. this.chart.changeData(config.option.percent)
  251. } else {
  252. this.chart.changeData(config.option.data)
  253. }
  254. }
  255. }).catch(err => {
  256. console.info(err)
  257. }).finally(() => {
  258. if (config) {
  259. config.loading = false
  260. }
  261. resolve(config)
  262. })
  263. })
  264. },
  265. // http前台代理需要对返回的数据进行重新组装
  266. httpDataFormatting (chartRes, httpRes) {
  267. let result = {}
  268. result = {
  269. columnData: chartRes.columnData,
  270. data: httpRes,
  271. success: chartRes.success
  272. }
  273. return result
  274. },
  275. dataFormatting (config, data) {
  276. // 覆盖
  277. },
  278. newChart (option) {
  279. // 覆盖
  280. },
  281. // 通过表达式计算获取组件的值
  282. getDataByExpression (config) {
  283. // 覆盖
  284. },
  285. changeStyle (config) {
  286. config = { ...this.config, ...config }
  287. // 样式改变时更新主题配置
  288. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  289. this.changeChartConfig(config)
  290. if (config.code === this.activeCode) {
  291. this.changeActiveItemConfig(config)
  292. }
  293. },
  294. // 缓存组件数据监听
  295. watchCacheData () {
  296. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  297. // 如果是缓存数据集
  298. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  299. if (
  300. this.config.dataSource.dataSetType === '2' &&
  301. this.config.dataSource.businessKey === dataSetId
  302. ) {
  303. const config = this.dataFormatting(this.config, data)
  304. config.key = new Date().getTime()
  305. this.changeChartConfig(config)
  306. this.newChart(config.option)
  307. }
  308. })
  309. }
  310. }
  311. }