commonMixins.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * @description: bigScreen公共方法
  3. * @Date: 2023-03-24 17:10:43
  4. * @Author: xing.heng
  5. */
  6. // import _ from 'lodash'
  7. import cloneDeep from 'lodash/cloneDeep'
  8. import { mapMutations, mapState } from 'vuex'
  9. import { EventBus } from 'data-room-ui/js/utils/eventBus'
  10. import { getChatInfo, getUpdateChartInfo } from '../api/bigScreenApi'
  11. import axiosFormatting from '../../js/utils/httpParamsFormatting'
  12. export default {
  13. data () {
  14. return {
  15. filterList: [],
  16. treeParentId: 0,
  17. dataLoading: false
  18. }
  19. },
  20. computed: {
  21. ...mapState({
  22. pageCode: state => state.bigScreen.pageInfo.code
  23. }),
  24. isPreview () {
  25. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  26. }
  27. },
  28. mounted () {
  29. if (!['tables'].includes(this.config.type)) {
  30. this.chartInit()
  31. }
  32. this.watchCacheData()
  33. },
  34. methods: {
  35. ...mapMutations({
  36. changeChartConfig: 'bigScreen/changeChartConfig'
  37. }),
  38. /**
  39. * 初始化组件
  40. */
  41. chartInit () {
  42. let config = this.config
  43. // key和code相等,说明是一进来刷新,调用list接口
  44. if (this.isPreview) {
  45. // 改变样式
  46. config = this.changeStyle(config) ? this.changeStyle(config) : config
  47. // 改变数据
  48. config = this.changeDataByCode(config)
  49. } else {
  50. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  51. // eslint-disable-next-line no-unused-vars
  52. config = this.changeData(config)
  53. }
  54. },
  55. /**
  56. * 初始化组件时获取后端返回的数据, 返回数据和当前组件的配置_list
  57. * @param settingConfig 设置时的配置。不传则为当前组件的配置
  58. * @returns {Promise<unknown>}
  59. */
  60. changeDataByCode (config) {
  61. let currentPage = 1
  62. let size = 10
  63. if (config?.option?.pagination) {
  64. currentPage = config.option.pagination.currentPage
  65. size = config.option.pagination.pageSize
  66. }
  67. return new Promise((resolve, reject) => {
  68. getChatInfo({
  69. // innerChartCode: this.pageCode ? config.code : undefined,
  70. chartCode: config.code,
  71. current: currentPage,
  72. pageCode: this.pageCode,
  73. size: size,
  74. type: config.type
  75. }).then(async (res) => {
  76. let _res = cloneDeep(res)
  77. // 如果是http数据集的前端代理,则需要调封装的axios请求
  78. if (res.executionByFrontend) {
  79. if (res.data.datasetType === 'http') {
  80. _res = await axiosFormatting(res.data)
  81. _res = this.httpDataFormatting(res, _res)
  82. }
  83. if (res.data.datasetType === 'js') {
  84. try {
  85. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  86. const value = this.config.dataSource?.params[p]
  87. if (!isNaN(value)) {
  88. return value || p
  89. } else {
  90. return `'${value}' || '${p}'`
  91. }
  92. })
  93. // eslint-disable-next-line no-new-func
  94. const scriptMethod = new Function(scriptAfterReplacement)
  95. _res.data = scriptMethod()
  96. } catch (error) {
  97. console.error('JS数据集脚本执行失败', error)
  98. }
  99. }
  100. }
  101. config = this.dataFormatting(config, _res)
  102. this.changeChartConfig(config)
  103. }).catch((err) => {
  104. console.error(err)
  105. }).finally(() => {
  106. resolve(config)
  107. })
  108. })
  109. },
  110. /**
  111. * @description: 更新chart
  112. * @param {Object} config
  113. */
  114. changeData (config, filterList) {
  115. const params = {
  116. chart: {
  117. ...config,
  118. option: undefined
  119. },
  120. current: 1,
  121. pageCode: this.pageCode,
  122. type: config.type,
  123. filterList: filterList || this.filterList
  124. }
  125. return new Promise((resolve, reject) => {
  126. getUpdateChartInfo(params).then(async (res) => {
  127. let _res = cloneDeep(res)
  128. // 如果是http数据集的前端代理,则需要调封装的axios请求
  129. if (res.executionByFrontend) {
  130. if (res.data.datasetType === 'http') {
  131. _res = await axiosFormatting(res.data)
  132. _res = this.httpDataFormatting(res, _res)
  133. }
  134. if (res.data.datasetType === 'js') {
  135. try {
  136. const scriptAfterReplacement = res.data.script.replace(/\${(.*?)}/g, (match, p) => {
  137. const value = this.config.dataSource?.params[p]
  138. if (!isNaN(value)) {
  139. return value || p
  140. } else {
  141. return `'${value}' || '${p}'`
  142. }
  143. })
  144. // eslint-disable-next-line no-new-func
  145. const scriptMethod = new Function(scriptAfterReplacement)
  146. _res.data = scriptMethod()
  147. } catch (error) {
  148. console.error('JS数据集脚本执行失败', error)
  149. }
  150. }
  151. }
  152. config = this.dataFormatting(config, _res)
  153. if (this.chart) {
  154. // 单指标组件和多指标组件的changeData传参不同
  155. if (['Liquid', 'Gauge', 'RingProgress'].includes(config.chartType)) {
  156. this.chart.changeData(config.option.percent)
  157. } else {
  158. this.chart.changeData(config.option.data)
  159. }
  160. }
  161. }).catch(err => {
  162. console.error(err)
  163. }).finally(() => {
  164. resolve(config)
  165. })
  166. })
  167. },
  168. // http前台代理需要对返回的数据进行重新组装
  169. httpDataFormatting (chartRes, httpRes) {
  170. let result = {}
  171. result = {
  172. columnData: chartRes.columnData,
  173. data: httpRes,
  174. success: chartRes.success
  175. }
  176. return result
  177. },
  178. dataFormatting (config, data) {
  179. // 覆盖
  180. },
  181. newChart (option) {
  182. // 覆盖
  183. },
  184. changeStyle (config) {
  185. // this.changeChartConfig(config)
  186. // return config
  187. },
  188. // 缓存组件数据监听
  189. watchCacheData () {
  190. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  191. // 如果是缓存数据集
  192. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  193. if (
  194. this.config.dataSource.dataSetType === '2' &&
  195. this.config.dataSource.businessKey === dataSetId
  196. ) {
  197. const config = this.dataFormatting(this.config, data)
  198. config.key = new Date().getTime()
  199. this.changeChartConfig(config)
  200. this.newChart(config.option)
  201. }
  202. })
  203. }
  204. }
  205. }