index.vue 13 KB

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