index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <template>
  2. <div v-if="hasPermission">
  3. <div
  4. v-loading="pageLoading"
  5. element-loading-background="#151A26"
  6. class="bs-preview-wrap"
  7. :style="previewWrapStyle"
  8. >
  9. <div
  10. class="bs-render-wrap render-theme-wrap"
  11. :style="renderStyle"
  12. >
  13. <div
  14. v-for="chart in chartList"
  15. :key="chart.code"
  16. :style="{
  17. position: 'absolute',
  18. width: chart.w + 'px',
  19. height: chart.h + 'px',
  20. left: chart.x + 'px',
  21. top: chart.y + 'px',
  22. zIndex: chart.z || 0
  23. }"
  24. >
  25. <RenderCard
  26. ref="RenderCardRef"
  27. :key="chart.key"
  28. :config="chart"
  29. />
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <NotPermission v-else />
  35. </template>
  36. <script>
  37. import RenderCard from 'data-room-ui/Render/RenderCard.vue'
  38. import { mapActions, mapMutations, mapState } from 'vuex'
  39. import { getThemeConfig } from 'data-room-ui/js/api/bigScreenApi'
  40. import { compile } from 'tiny-sass-compiler/dist/tiny-sass-compiler.esm-browser.prod.js'
  41. import { G2 } from '@antv/g2plot'
  42. import NotPermission from 'data-room-ui/NotPermission'
  43. export default {
  44. name: 'BigScreenRun',
  45. components: {
  46. RenderCard,
  47. NotPermission
  48. },
  49. props: {
  50. config: {
  51. type: Object,
  52. default: () => ({
  53. code: '',
  54. fitSelector: '.inner-preview-wrap',
  55. fitMode: 'auto'
  56. })
  57. }
  58. },
  59. data () {
  60. return {
  61. innerHeight: window.innerHeight,
  62. innerWidth: window.innerWidth,
  63. timer: null,
  64. hasPermission: true
  65. }
  66. },
  67. computed: {
  68. ...mapState({
  69. pageInfo: state => state.bigScreen.pageInfo,
  70. pageConfig: state => state.bigScreen.pageInfo.pageConfig,
  71. chartList: state => state.bigScreen.pageInfo.chartList,
  72. stateFitMode: state => state.bigScreen.pageInfo.pageConfig.fitMode
  73. }),
  74. pageCode () {
  75. // 内部系统取到外部iframe上src链接的code参数
  76. const iframeCode = this.getIframeCode()
  77. // 兼容外部网页上的code,iframe上的code以及传入的code
  78. return this.$route.query.code ||
  79. iframeCode ||
  80. this.config.code
  81. },
  82. fitMode () {
  83. return this.config.fitMode || this.stateFitMode
  84. },
  85. fitSelector () {
  86. return this.config.fitSelector
  87. },
  88. pageLoading () {
  89. return this.$store.state.bigScreen.pageLoading
  90. },
  91. fitPageConfig () {
  92. return this.resolvePageConfig(this.pageConfig)
  93. },
  94. previewWrapStyle () {
  95. const bg = this.fitMode !== 'none'
  96. ? {
  97. backgroundColor: this.fitPageConfig.bgColor,
  98. backgroundImage: `url(${this.fitPageConfig.bg})`,
  99. backgroundSize: 'cover'
  100. }
  101. : {}
  102. return {
  103. overflowX: `${this.fitPageConfig.overflowX}`,
  104. overflowY: `${this.fitPageConfig.overflowY}`,
  105. ...bg
  106. }
  107. },
  108. renderStyle () {
  109. const style = {
  110. width: this.fitPageConfig.w,
  111. height: this.fitPageConfig.h,
  112. transform: `scaleX(${this.fitPageConfig.scaleX}) scaleY(${this.fitPageConfig.scaleY}) translate(${this.fitPageConfig.translate})`
  113. }
  114. const bg = this.fitMode === 'none'
  115. ? {
  116. backgroundColor: this.fitPageConfig.bgColor,
  117. backgroundImage: `url(${this.fitPageConfig.bg})`,
  118. backgroundSize: 'cover'
  119. }
  120. : {}
  121. return {
  122. ...style,
  123. ...bg
  124. }
  125. }
  126. },
  127. watch: {
  128. pageCode (val) {
  129. if (val) {
  130. this.init()
  131. }
  132. },
  133. 'pageInfo.pageConfig.refreshConfig.length': {
  134. handler (val) {
  135. if (val) {
  136. this.startTimer()
  137. }
  138. }
  139. }
  140. },
  141. beforeRouteLeave (to, from, next) {
  142. // 离开的时候 重置大屏的vuex store
  143. this.$store.commit('bigScreen/resetStoreData')
  144. next()
  145. },
  146. created () {
  147. this.permission()
  148. this.getParentWH()
  149. this.windowSize()
  150. },
  151. mounted () {
  152. if (this.pageInfo.pageConfig.refreshConfig && this.pageInfo.pageConfig.refreshConfig.length > 0) {
  153. this.startTimer()
  154. }
  155. },
  156. beforeDestroy () {
  157. this.stopTimer()
  158. },
  159. methods: {
  160. ...mapActions('bigScreen', [
  161. 'initLayout' // -> this.initLayout()
  162. ]),
  163. ...mapMutations('bigScreen', [
  164. 'changeLayout',
  165. 'changePageLoading',
  166. 'changePageConfig',
  167. 'changeChartConfig'
  168. ]),
  169. permission () {
  170. this.$dataRoomAxios.get(`/bigScreen/permission/check/${this.pageCode}`).then(res => {
  171. this.hasPermission = res
  172. if (res) {
  173. this.init()
  174. }
  175. })
  176. },
  177. init () {
  178. if (!this.pageCode) { return }
  179. this.changePageLoading(true)
  180. this.initLayout(this.pageCode).then(() => {
  181. const themeName = this.pageConfig.customTheme
  182. if (this.pageConfig.customTheme === 'custom') {
  183. getThemeConfig().then((res) => {
  184. // 初始化时如果就是自定义主题则统一注册
  185. const { registerTheme } = G2
  186. registerTheme(themeName, { ...res.chart })
  187. const pageConfig = this.pageConfig
  188. pageConfig.themeJson = res
  189. this.changePageConfig(pageConfig)
  190. this.styleSet()
  191. this.changePageLoading(false)
  192. })
  193. } else {
  194. this.changePageLoading(false)
  195. }
  196. this.getParentWH()
  197. })
  198. },
  199. // 设置定时器
  200. startTimer () {
  201. let time = 1
  202. const that = this
  203. // 使用setTimeout代替setInterval,并在每次循环结束后重新设置定时器。这样可以避免定时器的堆积和性能问题
  204. // 同时,为了方便清除定时器,可以将定时器的引用保存在变量中,以便后续清除
  205. this.timer = setTimeout(function refresh () {
  206. that.pageInfo.pageConfig.refreshConfig.forEach(item => {
  207. if (item.code) {
  208. if (time === 1) {
  209. item.originTime = item.time
  210. }
  211. that.chartList.forEach((chart, index) => {
  212. if (item.code === chart.code && item.time === time) {
  213. item.time = item.time + item.originTime
  214. that.$refs.RenderCardRef[index].$refs[chart.code].changeData(chart)
  215. }
  216. })
  217. }
  218. })
  219. time += 1
  220. that.timer = setTimeout(refresh, 1000)
  221. }, 1000)
  222. },
  223. // 清除定时器
  224. stopTimer () {
  225. clearTimeout(this.timer)
  226. },
  227. getIframeCode () {
  228. // 获取当前页面的URL
  229. const url = window.location.href
  230. // 解析URL的参数
  231. let code = null
  232. // 检查URL中是否包含哈希值
  233. if (url.indexOf('#') !== -1) {
  234. // 获取哈希部分的URL
  235. const hashUrl = url.split('#')[1]
  236. // 解析哈希部分URL的参数
  237. const hashParams = new URLSearchParams(hashUrl)
  238. code = hashParams.get('code')
  239. } else {
  240. // 获取URL的查询字符串部分
  241. const searchParams = new URLSearchParams(url.split('?')[1])
  242. code = searchParams.get('code')
  243. }
  244. return code
  245. },
  246. windowSize () {
  247. window.onresize = () => {
  248. this.getParentWH()
  249. }
  250. },
  251. getParentWH () {
  252. this.$nextTick(() => {
  253. const parent = document.querySelector(this.fitSelector)
  254. // 如果设置了自适应的选择器
  255. if (parent) {
  256. this.innerHeight = parent.offsetHeight
  257. this.innerWidth = parent.offsetWidth
  258. } else {
  259. this.innerHeight = window.innerHeight
  260. this.innerWidth = window.innerWidth
  261. }
  262. // 设置bs-preview-wrap的高度为父元素的高度
  263. const previewWrap = document.querySelector('.bs-preview-wrap')
  264. if (previewWrap) {
  265. previewWrap.style.height = this.innerHeight + 'px'
  266. previewWrap.style.width = this.innerWidth + 'px'
  267. }
  268. })
  269. },
  270. // 获取到后端传来的主题样式并进行修改
  271. styleSet () {
  272. const style = document.createElement('style')
  273. if (this.pageConfig.themeJson && this.pageConfig.themeJson.themeCss) {
  274. const styleStr = this.pageConfig.themeJson.themeCss
  275. const themeCss = compile(styleStr).code
  276. style.type = 'text/css'
  277. style.innerText = themeCss
  278. document.getElementsByTagName('head')[0].appendChild(style)
  279. } else {
  280. style.remove()
  281. }
  282. },
  283. // 处理自适应下的页面配置
  284. resolvePageConfig (pageConfig) {
  285. const { w, h } = pageConfig
  286. let scaleX = 1
  287. let scaleY = 1
  288. let translate = '0px, 0px'
  289. let overflowX = 'auto'
  290. let overflowY = 'auto'
  291. // 自适应模式, 画布等比例自适应后保持一屏展示,会存在某一个方向两边留白,留白部分颜色背景和画布中的背景色一致
  292. if (this.fitMode === 'auto') {
  293. const scaleW = this.innerWidth / w
  294. const scaleH = this.innerHeight / h
  295. scaleX = Math.min(scaleW, scaleH)
  296. scaleY = Math.min(scaleW, scaleH)
  297. translate = `${((this.innerWidth - w) / 2) / scaleX}px, ${((this.innerHeight - h) / 2) / scaleY}px`
  298. overflowX = 'hidden'
  299. overflowY = 'hidden'
  300. }
  301. // 宽度铺满 预览时画布横向铺满,纵向超出时出现滚动条
  302. if (this.fitMode === 'fitWidth') {
  303. scaleX = this.innerWidth / w
  304. // 如果实际高度小于屏幕,纵向居中
  305. if (this.innerHeight > h) {
  306. translate = `${((this.innerWidth - w) / 2) / scaleX}px, ${((this.innerHeight - h) / 2) / scaleY}px`
  307. } else {
  308. translate = `${((this.innerWidth - w) / 2) / scaleX}px, 0px`
  309. }
  310. overflowX = 'hidden'
  311. }
  312. // 高度铺满 预览时画布纵向铺满,横向超出时出现滚动条
  313. if (this.fitMode === 'fitHeight') {
  314. scaleY = this.innerHeight / h
  315. // 如果实际宽度小于屏幕,横向居中
  316. if (this.innerWidth > w) {
  317. translate = `${((this.innerWidth - w) / 2) / scaleX}px, ${((this.innerHeight - h) / 2) / scaleY}px`
  318. } else {
  319. translate = `0px, ${((this.innerHeight - h) / 2) / scaleY}px`
  320. }
  321. overflowY = 'hidden'
  322. }
  323. // 双向铺满 预览时画布横向纵向铺满,无滚动条,但是元素可能会出现拉伸情况
  324. if (this.fitMode === 'cover') {
  325. scaleX = this.innerWidth / w
  326. scaleY = this.innerHeight / h
  327. translate = `${((this.innerWidth - w) / 2) / scaleX}px, ${((this.innerHeight - h) / 2) / scaleY}px`
  328. overflowX = 'hidden'
  329. overflowY = 'hidden'
  330. }
  331. // 无
  332. const newPageConfig = {
  333. ...pageConfig,
  334. w: w + 'px',
  335. h: h + 'px',
  336. scaleX,
  337. scaleY,
  338. translate,
  339. overflowX,
  340. overflowY
  341. }
  342. return newPageConfig
  343. }
  344. }
  345. }
  346. </script>
  347. <style lang="scss" scoped>
  348. .bs-preview-wrap {
  349. position: absolute;
  350. width: 100%;
  351. height: 100%;
  352. overflow: auto;
  353. .bs-render-wrap {
  354. position: relative;
  355. background-size: cover;
  356. }
  357. }
  358. </style>