commonMixins.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 'packages/js/utils/eventBus'
  9. import { getChatInfo, getUpdateChartInfo } from '../api/bigScreenApi'
  10. export default {
  11. data () {
  12. return {
  13. filterList: [],
  14. treeParentId: 0,
  15. dataLoading: false
  16. }
  17. },
  18. computed: {
  19. ...mapState({
  20. pageCode: state => state.bigScreen.pageInfo.code
  21. }),
  22. isPreview () {
  23. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  24. }
  25. },
  26. mounted () {
  27. if (!['digitalFlop', 'screenScrollRanking', 'screenScrollBoard', 'tables'].includes(this.config.type)) {
  28. this.chartInit()
  29. }
  30. this.watchCacheData()
  31. },
  32. methods: {
  33. ...mapMutations({
  34. changeChartConfig: 'bigScreen/changeChartConfig'
  35. }),
  36. /**
  37. * 初始化组件
  38. */
  39. chartInit () {
  40. // 初始化组件和数据,若自己的组件的初始化和数据处理不一样,可重写该方法
  41. // 如果key和code相等,说明是一进来刷新,调用/chart/data/list,否则是更新,调用 chart/data/chart
  42. // 或者是组件联动isLink,也需要调用/chart/data/list更新
  43. if (this.config.code === this.config.key) {
  44. // 根据缓存数据初始化的组件
  45. if (this.config.dataSource.dataSetType === '2') {
  46. this.config = this.buildOption(this.config, { success: false })
  47. this.changeChartConfig(this.config)
  48. this.newChart(this.config.option)
  49. } else {
  50. // 根据数据集初始化的组件
  51. if (this.isPreview) {
  52. this.getCurrentOption().then(({ config, data }) => {
  53. config = this.buildOption(config, data)
  54. this.changeChartConfig(config)
  55. this.newChart(config.option)
  56. })
  57. } else {
  58. this.updateChartData(this.config)
  59. }
  60. }
  61. } else {
  62. this.newChart(this.config.option)
  63. }
  64. },
  65. /**
  66. * 初始化组件时获取后端返回的数据, 返回数据和当前组件的配置
  67. * @param settingConfig 设置时的配置。不传则为当前组件的配置
  68. * @returns {Promise<unknown>}
  69. */
  70. getCurrentOption (settingConfig) {
  71. const pageCode = this.pageCode
  72. const chartCode = this.config.code
  73. const type = this.config.type
  74. const config = _.cloneDeep(settingConfig || this.config)
  75. let currentPage = 1
  76. let size = 10
  77. if (config?.option?.pagination) {
  78. currentPage = config.option.pagination.currentPage
  79. size = config.option.pagination.pageSize
  80. }
  81. return new Promise((resolve, reject) => {
  82. this.getDataByCode(pageCode, chartCode, type, currentPage, size).then((data) => {
  83. resolve({
  84. config, data
  85. })
  86. }).catch((err) => {
  87. reject(err)
  88. })
  89. })
  90. },
  91. /**
  92. * 根据 chatCode 获取后端返回的数据
  93. * @param pageCode
  94. * @param chartCode
  95. * @param type
  96. * @param current
  97. * @param size
  98. * @returns {Promise<*>}
  99. */
  100. async getDataByCode (
  101. pageCode,
  102. chartCode,
  103. type,
  104. current = 1,
  105. size = 10
  106. ) {
  107. let parentCode
  108. const data = await getChatInfo({
  109. innerChartCode: parentCode ? chartCode : undefined,
  110. chartCode: parentCode || chartCode,
  111. current: current,
  112. pageCode: pageCode,
  113. size: size,
  114. type: type
  115. })
  116. return data
  117. },
  118. /**
  119. * @description: 更新chart
  120. * @param {Object} config
  121. */
  122. updateChartData (config) {
  123. const filterList = this.filterList
  124. const params = {
  125. chart: {
  126. ...config,
  127. option: undefined
  128. },
  129. current: 1,
  130. pageCode: this.pageCode,
  131. type: config.type,
  132. filterList
  133. }
  134. // if (config.type === 'remoteComponent') {
  135. // config = this.buildOption(config, { success: false })
  136. // config.key = new Date().getTime()
  137. // this.changeChartConfig(config)
  138. // return
  139. // }
  140. getUpdateChartInfo(params).then((res) => {
  141. console.log('更新组件数据', res)
  142. console.log('更新组件数据', config)
  143. // 数据集脚本前端执行
  144. if (res.executionByFrontend) {
  145. try {
  146. const returnResult = eval(`(${res.data})`)()
  147. res.data = returnResult
  148. } catch (error) {
  149. console.error('数据集脚本执行失败', error)
  150. }
  151. }
  152. config = this.buildOption(config, res)
  153. config.key = new Date().getTime()
  154. this.changeChartConfig(config)
  155. // 获取数据后更新组件配置
  156. // this.$message.success('更新成功')
  157. }).catch((err) => {
  158. console.error(err)
  159. // this.$message.error('更新失败')
  160. })
  161. },
  162. newChart () {
  163. // 需要在自己的组件中重写此方法,用于构建自己的组件
  164. },
  165. buildOption (config, data) {
  166. // 需要在自己的组件中重写此方法:config当前组件的配置,data后端返回的数据
  167. return config
  168. },
  169. // 缓存组件数据监听
  170. watchCacheData () {
  171. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  172. // 如果是缓存数据集
  173. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  174. if (
  175. this.config.dataSource.dataSetType === '2' &&
  176. this.config.dataSource.businessKey === dataSetId
  177. ) {
  178. const config = this.buildOption(this.config, data)
  179. config.key = new Date().getTime()
  180. this.changeChartConfig(config)
  181. this.newChart(config.option)
  182. }
  183. })
  184. }
  185. }
  186. }