commonMixins.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 { getChatInfo, getUpdateChartInfo } from '../api/bigScreenApi'
  9. export default {
  10. data () {
  11. return {
  12. filterList: [],
  13. treeParentId: 0,
  14. dataLoading: false
  15. }
  16. },
  17. computed: {
  18. ...mapState({
  19. pageCode: state => state.bigScreen.pageInfo.code
  20. }),
  21. isPreview () {
  22. return this.$route.path !== window?.BS_CONFIG?.routers?.designUrl
  23. }
  24. },
  25. mounted () {},
  26. methods: {
  27. ...mapMutations({
  28. changeChartConfig: 'bigScreen/changeChartConfig'
  29. }),
  30. /**
  31. * 初始化组件
  32. */
  33. chartInit () {
  34. // 初始化组件和数据,若自己的组件的初始化和数据处理不一样,可重写该方法
  35. // 如果key和code相等,说明是一进来刷新,调用/chart/data/list,否则是更新,调用 chart/data/chart
  36. // 或者是组件联动isLink,也需要调用/chart/data/list更新
  37. if (this.config.code === this.config.key || this.isPreview) {
  38. // 根据数据集初始化的组件
  39. if (this.isPreview) {
  40. this.getCurrentOption().then(({ config, data }) => {
  41. config = this?.buildOption(config, data)
  42. if (config) {
  43. this.changeChartConfig(config)
  44. this?.newChart(config.option)
  45. }
  46. })
  47. } else {
  48. this.updateChartData(this.config)
  49. }
  50. } else {
  51. this?.newChart(this.config.option)
  52. }
  53. },
  54. /**
  55. * 初始化组件时获取后端返回的数据, 返回数据和当前组件的配置
  56. * @param settingConfig 设置时的配置。不传则为当前组件的配置
  57. * @returns {Promise<unknown>}
  58. */
  59. getCurrentOption (settingConfig) {
  60. const pageCode = this.pageCode
  61. const chartCode = this.config.code
  62. const type = this.config.type
  63. const config = _.cloneDeep(settingConfig || this.config)
  64. let currentPage = 1
  65. let size = 10
  66. if (config?.option?.pagination) {
  67. currentPage = config.option.pagination.currentPage
  68. size = config.option.pagination.pageSize
  69. }
  70. return new Promise((resolve, reject) => {
  71. this.getDataByCode(pageCode, chartCode, type, currentPage, size).then((data) => {
  72. resolve({
  73. config, data
  74. })
  75. }).catch((err) => {
  76. reject(err)
  77. })
  78. })
  79. },
  80. /**
  81. * 根据 chatCode 获取后端返回的数据
  82. * @param pageCode
  83. * @param chartCode
  84. * @param type
  85. * @param current
  86. * @param size
  87. * @returns {Promise<*>}
  88. */
  89. async getDataByCode (
  90. pageCode,
  91. chartCode,
  92. type,
  93. current = 1,
  94. size = 10
  95. ) {
  96. let parentCode
  97. const data = await getChatInfo({
  98. innerChartCode: parentCode ? chartCode : undefined,
  99. chartCode: parentCode || chartCode,
  100. current: current,
  101. pageCode: pageCode,
  102. size: size,
  103. type: type
  104. })
  105. return data
  106. },
  107. /**
  108. * @description: 更新chart
  109. * @param {Object} config
  110. */
  111. updateChartData (config) {
  112. const filterList = this.filterList
  113. const params = {
  114. chart: {
  115. ...config,
  116. option: undefined
  117. },
  118. current: 1,
  119. pageCode: this.pageCode,
  120. type: config.type,
  121. filterList
  122. }
  123. getUpdateChartInfo(params).then((res) => {
  124. // 获取数据后更新组件配置
  125. config.key = new Date().getTime()
  126. config = this.buildOption(config, res)
  127. if (config) {
  128. this.changeChartConfig(config)
  129. }
  130. // this.$message.success('更新成功')
  131. }).catch((err) => {
  132. console.error(err)
  133. // this.$message.error('更新失败')
  134. })
  135. },
  136. buildOption (config, data) {
  137. // 覆盖
  138. },
  139. newChart (option) {
  140. // 覆盖
  141. }
  142. }
  143. }