commonMixins.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. changeData () {},
  67. // 组件仅更新样式
  68. changeStyle () {},
  69. /**
  70. * 初始化组件时获取后端返回的数据, 返回数据和当前组件的配置
  71. * @param settingConfig 设置时的配置。不传则为当前组件的配置
  72. * @returns {Promise<unknown>}
  73. */
  74. getCurrentOption (settingConfig) {
  75. const pageCode = this.pageCode
  76. const chartCode = this.config.code
  77. const type = this.config.type
  78. const config = _.cloneDeep(settingConfig || this.config)
  79. let currentPage = 1
  80. let size = 10
  81. if (config?.option?.pagination) {
  82. currentPage = config.option.pagination.currentPage
  83. size = config.option.pagination.pageSize
  84. }
  85. return new Promise((resolve, reject) => {
  86. this.getDataByCode(pageCode, chartCode, type, currentPage, size).then((data) => {
  87. resolve({
  88. config, data
  89. })
  90. }).catch((err) => {
  91. reject(err)
  92. })
  93. })
  94. },
  95. /**
  96. * 根据 chatCode 获取后端返回的数据
  97. * @param pageCode
  98. * @param chartCode
  99. * @param type
  100. * @param current
  101. * @param size
  102. * @returns {Promise<*>}
  103. */
  104. async getDataByCode (
  105. pageCode,
  106. chartCode,
  107. type,
  108. current = 1,
  109. size = 10
  110. ) {
  111. let parentCode
  112. const data = await getChatInfo({
  113. innerChartCode: parentCode ? chartCode : undefined,
  114. chartCode: parentCode || chartCode,
  115. current: current,
  116. pageCode: pageCode,
  117. size: size,
  118. type: type
  119. })
  120. return data
  121. },
  122. /**
  123. * @description: 更新chart
  124. * @param {Object} config
  125. */
  126. updateChartData (config) {
  127. const filterList = this.filterList
  128. const params = {
  129. chart: {
  130. ...config,
  131. option: undefined
  132. },
  133. current: 1,
  134. pageCode: this.pageCode,
  135. type: config.type,
  136. filterList
  137. }
  138. // if (config.type === 'remoteComponent') {
  139. // config = this.buildOption(config, { success: false })
  140. // config.key = new Date().getTime()
  141. // this.changeChartConfig(config)
  142. // return
  143. // }
  144. getUpdateChartInfo(params).then((res) => {
  145. // 数据集脚本前端执行
  146. if (res.executionByFrontend) {
  147. try {
  148. const returnResult = eval(`(${res.data})`)()
  149. res.data = returnResult
  150. } catch (error) {
  151. console.error('数据集脚本执行失败', error)
  152. }
  153. }
  154. config = this.buildOption(config, res)
  155. config.key = new Date().getTime()
  156. this.changeChartConfig(config)
  157. // 获取数据后更新组件配置
  158. // this.$message.success('更新成功')
  159. }).catch((err) => {
  160. console.error(err)
  161. // this.$message.error('更新失败')
  162. })
  163. },
  164. newChart () {
  165. // 需要在自己的组件中重写此方法,用于构建自己的组件
  166. },
  167. buildOption (config, data) {
  168. // 需要在自己的组件中重写此方法:config当前组件的配置,data后端返回的数据
  169. return config
  170. },
  171. // 缓存组件数据监听
  172. watchCacheData () {
  173. EventBus.$on('cacheDataInit', (data, dataSetId) => {
  174. // 如果是缓存数据集
  175. // 且当前组件的businessKey和缓存的dataSetId相等时,更新组件
  176. if (
  177. this.config.dataSource.dataSetType === '2' &&
  178. this.config.dataSource.businessKey === dataSetId
  179. ) {
  180. const config = this.buildOption(this.config, data)
  181. config.key = new Date().getTime()
  182. this.changeChartConfig(config)
  183. this.newChart(config.option)
  184. }
  185. })
  186. }
  187. }
  188. }