index.vue 11 KB

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