index.vue 12 KB

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