PageDesignTop.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <template>
  2. <div class="page-top-setting-wrap">
  3. <div class="logo-wrap item-wrap">
  4. <img
  5. class="menu-img"
  6. src="../BigScreenDesign/images/app.png"
  7. alt="返回"
  8. @click="backManagement"
  9. >
  10. <span class="logo-text name-span">{{ pageInfo.name }}</span>
  11. </div>
  12. <div class="head-btn-group">
  13. <el-tooltip
  14. v-for="(mode,index) in alignList"
  15. :key="mode.value"
  16. popper-class="bs-el-tooltip-dark"
  17. effect="dark"
  18. :content="mode.label"
  19. placement="top"
  20. >
  21. <CusBtn
  22. class="align-btn"
  23. @click="setAlign(mode.value)"
  24. >
  25. <icon-svg
  26. :name="iconList[index]"
  27. />
  28. </CusBtn>
  29. </el-tooltip>
  30. <CusBtn
  31. :loading="saveAndPreviewLoading"
  32. @click.native="designAssign()"
  33. >
  34. 设计分工
  35. </CusBtn>
  36. <CusBtn
  37. @click.native="showHostory"
  38. >
  39. 历史操作
  40. </CusBtn>
  41. <CusBtn
  42. :disabled="undoDisabled"
  43. @click.native="undo(true)"
  44. >
  45. <i class="iconfont-bigscreen icon-jiantouqianjin icon-reverse" />
  46. </CusBtn>
  47. <CusBtn
  48. :disabled="redoDisabled"
  49. @click.native="undo(false)"
  50. >
  51. <i class="iconfont-bigscreen icon-jiantouqianjin" />
  52. </CusBtn>
  53. <CusBtn
  54. :loading="saveAndPreviewLoading"
  55. @click.native="createdImg()"
  56. >
  57. 生成图片
  58. </CusBtn>
  59. <CusBtn
  60. :loading="saveAndPreviewLoading"
  61. @click.native="execRun()"
  62. >
  63. 预览
  64. </CusBtn>
  65. <CusBtn
  66. :loading="saveLoading"
  67. @click="save('saveLoading')"
  68. >
  69. 保存
  70. </CusBtn>
  71. <CusBtn @click="empty">
  72. 清空
  73. </CusBtn>
  74. <CusBtn @click="showPageInfo">
  75. 设置
  76. </CusBtn>
  77. <CusBtn @click="updateRightVisiable">
  78. <i
  79. class="iconfont-bigscreen"
  80. :class="rightFold ? 'icon-zhankaicaidan' : 'icon-shouqicaidan'"
  81. />
  82. </CusBtn>
  83. </div>
  84. <ChooseTemplateDialog
  85. ref="ChooseTemplateDialog"
  86. :has-create="false"
  87. :page-info="pageInfo"
  88. @replaceItByTemplate="replaceItByTemplate"
  89. />
  90. <AssignDialog ref="AssignDialog" />
  91. <HistoryList ref="HistoryList" />
  92. </div>
  93. </template>
  94. <script>
  95. import { EventBus } from 'data-room-ui/js/utils/eventBus'
  96. import { toJpeg, toPng } from 'html-to-image'
  97. import { mapMutations, mapActions, mapState } from 'vuex'
  98. import { saveScreen } from 'data-room-ui/js/api/bigScreenApi'
  99. import ChooseTemplateDialog from 'data-room-ui/BigScreenManagement/ChooseTemplateDialog.vue'
  100. // import _ from 'lodash'
  101. import cloneDeep from 'lodash/cloneDeep'
  102. import uniqBy from 'lodash/uniqBy'
  103. import { stringifyObjectFunctions } from 'data-room-ui/js/utils/evalFunctions'
  104. import AssignDialog from 'data-room-ui/BigScreenDesign/AssignDialog/index.vue'
  105. import HistoryList from 'data-room-ui/BigScreenDesign/HistoryList/index.vue'
  106. import CusBtn from './BtnLoading'
  107. import icons from 'data-room-ui/assets/images/alignIcon/export'
  108. import IconSvg from 'data-room-ui/SvgIcon'
  109. import {
  110. showSize,
  111. dataURLtoBlob,
  112. translateBlobToBase64
  113. } from 'data-room-ui/js/utils/compressImg'
  114. import * as imageConversion from 'image-conversion'
  115. export default {
  116. name: 'PageTopSetting',
  117. components: {
  118. IconSvg,
  119. ChooseTemplateDialog,
  120. AssignDialog,
  121. CusBtn,
  122. HistoryList
  123. },
  124. props: {
  125. code: {
  126. type: String,
  127. default: ''
  128. },
  129. rightFold: {
  130. type: Boolean,
  131. default: false
  132. }
  133. },
  134. data () {
  135. return {
  136. iconList: icons.getNameList(),
  137. alignList: [
  138. {
  139. label: '左侧对齐',
  140. value: 'left'
  141. },
  142. {
  143. label: '居中对齐',
  144. value: 'center'
  145. },
  146. {
  147. label: '右侧对齐',
  148. value: 'right'
  149. },
  150. {
  151. label: '顶部对齐',
  152. value: 'top'
  153. },
  154. {
  155. label: '中部对齐',
  156. value: 'middle'
  157. },
  158. {
  159. label: '底部对齐',
  160. value: 'bottom'
  161. },
  162. {
  163. label: '水平均分',
  164. value: 'levelAround'
  165. },
  166. {
  167. label: '垂直均分',
  168. value: 'verticalAround'
  169. }
  170. ],
  171. appInfo: '',
  172. saveLoading: false,
  173. createdImgLoading: false,
  174. saveAndPreviewLoading: false
  175. }
  176. },
  177. computed: {
  178. ...mapState({
  179. pageInfo: (state) => state.bigScreen.pageInfo,
  180. timelineStore: (state) => state.bigScreen.timelineStore,
  181. currentTimeLine: (state) => state.bigScreen.currentTimeLine,
  182. activeCodes: state => state.bigScreen.activeCodes
  183. }),
  184. pageCode () {
  185. return this.$route.query.code || this.code
  186. },
  187. undoDisabled () {
  188. return Boolean(this.currentTimeLine <= 1)
  189. },
  190. redoDisabled () {
  191. return Boolean(
  192. !this.timelineStore?.length ||
  193. (
  194. this.currentTimeLine &&
  195. this.currentTimeLine === this.timelineStore?.length
  196. )
  197. )
  198. }
  199. },
  200. methods: {
  201. ...mapActions({
  202. initLayout: 'bigScreen/initLayout'
  203. }),
  204. ...mapMutations({
  205. changeActiveCode: 'bigScreen/changeActiveCode',
  206. changeActiveItem: 'bigScreen/changeActiveItem',
  207. changePageInfo: 'bigScreen/changePageInfo',
  208. undoTimeLine: 'bigScreen/undoTimeLine',
  209. saveTimeLine: 'bigScreen/saveTimeLine'
  210. }),
  211. setAlign (command) {
  212. const pageInfo = cloneDeep(this.pageInfo)
  213. // 获取所有选中的组件
  214. let activeChartList = pageInfo.chartList.filter((chart) => {
  215. return this.activeCodes.some(code => (code === chart.code))
  216. })
  217. // 找到选中组件内的xy最大最小值
  218. const maxXW = Math.max.apply(Math, activeChartList.map(item => { return item.x + item.w }))
  219. let maxX = Math.max.apply(Math, activeChartList.map(item => { return item.x }))
  220. const minX = Math.min.apply(Math, activeChartList.map(item => { return item.x }))
  221. const maxYH = Math.max.apply(Math, activeChartList.map(item => { return item.y + item.h }))
  222. const maxY = Math.max.apply(Math, activeChartList.map(item => { return item.y }))
  223. const minY = Math.min.apply(Math, activeChartList.map(item => { return item.y }))
  224. const centerW = maxXW - minX
  225. const centerH = maxY - minY
  226. switch (command) {
  227. case 'left':
  228. activeChartList.forEach((chart) => {
  229. chart.x = minX
  230. })
  231. break
  232. case 'center':
  233. // eslint-disable-next-line no-case-declarations
  234. activeChartList.forEach((chart) => {
  235. chart.x = (centerW - chart.w) / 2 + minX
  236. })
  237. break
  238. case 'right':
  239. activeChartList.forEach((chart) => {
  240. chart.x = maxXW - chart.w
  241. })
  242. break
  243. case 'top':
  244. activeChartList.forEach((chart) => {
  245. chart.y = minY
  246. })
  247. break
  248. case 'middle':
  249. activeChartList.forEach((chart) => {
  250. chart.y = (centerH - chart.h) / 2 + minY
  251. })
  252. break
  253. case 'bottom':
  254. activeChartList.forEach((chart) => {
  255. chart.y = maxYH - chart.h
  256. })
  257. break
  258. case 'levelAround':
  259. // 先让数组根据x的属性进行排序
  260. activeChartList = activeChartList.sort(this.compare('x'))
  261. // eslint-disable-next-line no-case-declarations
  262. const minXW = activeChartList[0].x + activeChartList[0].w
  263. maxX = Math.max.apply(Math, activeChartList.map(item => { return item.x }))
  264. // 中间总的宽度
  265. // eslint-disable-next-line no-case-declarations
  266. let totalW = 0
  267. for (let i = 1; i < activeChartList.length - 1; i++) {
  268. totalW = totalW + activeChartList[i].w
  269. }
  270. // 中间剩余的空格
  271. // eslint-disable-next-line no-case-declarations
  272. const padding = (maxX - minXW - totalW) / (activeChartList.length - 1)
  273. // eslint-disable-next-line no-case-declarations
  274. let useW = 0
  275. for (let i = 1; i < activeChartList.length - 1; i++) {
  276. activeChartList[i].x = minXW + padding * i + useW
  277. useW = useW + activeChartList[i].w
  278. }
  279. break
  280. case 'verticalAround':
  281. // 先让数组根据y的属性进行排序
  282. activeChartList = activeChartList.sort(this.compare('y'))
  283. // eslint-disable-next-line no-case-declarations
  284. const minYH = activeChartList[0].y + activeChartList[0].h
  285. // eslint-disable-next-line no-case-declarations
  286. let totalH = 0
  287. for (let i = 1; i < activeChartList.length - 1; i++) {
  288. totalH = totalH + activeChartList[i].h
  289. }
  290. // eslint-disable-next-line no-case-declarations
  291. const paddingBottom = (maxY - minYH - totalH) / (activeChartList.length - 1)
  292. // eslint-disable-next-line no-case-declarations
  293. let useH = 0
  294. for (let i = 1; i < activeChartList.length - 1; i++) {
  295. activeChartList[i].y = minYH + paddingBottom * i + useH
  296. useH = useH + activeChartList[i].h
  297. }
  298. break
  299. }
  300. pageInfo.chartList = [...pageInfo.chartList, ...activeChartList]
  301. pageInfo.chartList = uniqBy(pageInfo.chartList, 'code')
  302. this.changePageInfo(pageInfo)
  303. },
  304. compare (property) {
  305. return function (obj1, obj2) {
  306. const value1 = obj1[property]
  307. const value2 = obj2[property]
  308. return value1 - value2 // 升序
  309. }
  310. },
  311. backManagement () {
  312. this.$router.push({ path: this.pageInfo.type === 'component' ? (window.BS_CONFIG?.routers?.componentUrl || '/big-screen-components') : (window.BS_CONFIG?.routers?.pageManagementUrl || '/home') })
  313. const data = { componentsManagementType: 'component' }
  314. this.$router.app.$options.globalData = data // 将数据存储在全局变量中
  315. },
  316. undo (isUndo) {
  317. this.undoTimeLine(isUndo)
  318. },
  319. // 清空
  320. empty () {
  321. this.changeActiveCode('')
  322. this.$emit('empty')
  323. },
  324. // 预览
  325. async execRun () {
  326. this.save('saveAndPreviewLoading').then((res) => {
  327. this.preview()
  328. })
  329. },
  330. // 预览
  331. preview () {
  332. const { href } = this.$router.resolve({
  333. path: window.BS_CONFIG?.routers?.previewUrl || '/big-screen/preview',
  334. query: {
  335. code: this.pageCode
  336. }
  337. })
  338. window.open(href, '_blank')
  339. },
  340. // 保存
  341. save (loadingType = 'saveLoading', hasPageTemplateId = false) {
  342. const pageInfo = cloneDeep(this.handleSaveData())
  343. // 保存页面
  344. this[loadingType] = true
  345. return new Promise((resolve, reject) => {
  346. if (!hasPageTemplateId) {
  347. delete pageInfo.pageTemplateId
  348. }
  349. const node = document.querySelector('.render-theme-wrap')
  350. toJpeg(node, { quality: 0.2 })
  351. .then((dataUrl) => {
  352. const that = this
  353. if (showSize(dataUrl) > 200) {
  354. const url = dataURLtoBlob(dataUrl)
  355. // 压缩到500KB,这里的500就是要压缩的大小,可自定义
  356. imageConversion
  357. .compressAccurately(url, {
  358. size: 200, // 图片大小压缩到100kb
  359. width: 1280, // 宽度压缩到1280
  360. height: 720 // 高度压缩到720
  361. })
  362. .then((res) => {
  363. translateBlobToBase64(res, function (e) {
  364. pageInfo.coverPicture = e.result
  365. saveScreen(pageInfo)
  366. .then((res) => {
  367. that.$message.success('保存成功')
  368. resolve(res)
  369. })
  370. .finally(() => {
  371. that[loadingType] = false
  372. })
  373. })
  374. })
  375. } else {
  376. pageInfo.coverPicture = dataUrl
  377. saveScreen(pageInfo)
  378. .then((res) => {
  379. this.$message.success('保存成功')
  380. resolve(res)
  381. })
  382. .finally(() => {
  383. this[loadingType] = false
  384. })
  385. }
  386. })
  387. .catch(() => {
  388. this[loadingType] = false
  389. })
  390. })
  391. },
  392. goBack (path) {
  393. this.$router.push({
  394. path: `/${path}`
  395. })
  396. },
  397. // 得到模板列表
  398. getTemplateList (type) {
  399. this.$nextTick(() => {
  400. this.$refs.ChooseTemplateDialog.init(undefined, type)
  401. })
  402. },
  403. // 选择模版后覆盖配置
  404. selectTemplate (template) {
  405. this.pageInfo.pageTemplateId = template.id
  406. this.save('saveLoading', true).then(() => {
  407. this.initLayout(this.pageCode)
  408. })
  409. },
  410. replaceItByTemplate (config) {
  411. this.changePageInfo(config)
  412. },
  413. // 处理保存数据
  414. handleSaveData () {
  415. const pageInfo = cloneDeep(this.pageInfo)
  416. const chartList = cloneDeep(this.pageInfo.chartList)
  417. pageInfo.pageConfig.cacheDataSets =
  418. pageInfo.pageConfig.cacheDataSets?.map((cache) => ({
  419. name: cache.name,
  420. dataSetId: cache.dataSetId
  421. })) || []
  422. const newChartList = chartList?.map((chart) => {
  423. // 如果是自定义组件,需要将option转换为json字符串,因为其中可能有函数
  424. if (['customComponent', 'remoteComponent'].includes(chart.type)) {
  425. chart.option.data = []
  426. chart.option = stringifyObjectFunctions(chart.option)
  427. }
  428. return chart
  429. })
  430. return cloneDeep({
  431. ...this.pageInfo,
  432. chartList: newChartList
  433. })
  434. },
  435. updateRightVisiable () {
  436. this.$emit('updateRightVisiable', !this.rightFold)
  437. },
  438. showPageInfo () {
  439. this.$emit('showPageInfo')
  440. },
  441. designAssign () {
  442. this.$refs.AssignDialog.init()
  443. },
  444. showHostory () {
  445. this.$refs.HistoryList.init()
  446. },
  447. createdImg () {
  448. this.saveAndPreviewLoading = true
  449. // 暂停跑马灯动画
  450. EventBus.$emit('stopMarquee')
  451. const node = document.querySelector('.render-theme-wrap')
  452. toPng(node)
  453. .then((dataUrl) => {
  454. const link = document.createElement('a')
  455. link.download = `${this.pageInfo.name}.png`
  456. link.href = dataUrl
  457. link.click()
  458. link.addEventListener('click', () => {
  459. link.remove()
  460. })
  461. this.saveAndPreviewLoading = false
  462. // 恢复跑马灯动画
  463. EventBus.$emit('startMarquee')
  464. })
  465. .catch(() => {
  466. this.$message.warning('出现未知错误,请重试')
  467. this.saveAndPreviewLoading = false
  468. })
  469. }
  470. }
  471. }
  472. </script>
  473. <style lang="scss" scoped>
  474. @import '../BigScreenDesign/fonts/iconfont.css';
  475. .default-layout-box {
  476. display: flex;
  477. flex-wrap: wrap;
  478. .default-layout-item {
  479. cursor: pointer;
  480. width: 42%;
  481. margin: 9px;
  482. display: flex;
  483. flex-direction: column;
  484. align-items: center;
  485. .component-name {
  486. font-size: 12px;
  487. }
  488. .sampleImg {
  489. margin: 0 auto;
  490. width: 102px;
  491. height: 73px;
  492. display: block;
  493. }
  494. .img_dispaly {
  495. margin: 0 auto;
  496. text-align: center;
  497. width: 100px;
  498. height: 70px;
  499. line-height: 70px;
  500. background-color: #d7d7d7;
  501. color: #999;
  502. }
  503. .demonstration {
  504. text-align: center;
  505. }
  506. }
  507. .default-layout-item:hover {
  508. cursor: pointer;
  509. }
  510. ::v-deep .el-radio__label {
  511. display: none;
  512. }
  513. }
  514. .page-top-setting-wrap {
  515. height: 40px;
  516. background-color: var(--bs-background-2);
  517. display: flex;
  518. align-items: center;
  519. justify-content: space-between;
  520. position: relative;
  521. color: #ffffff;
  522. padding: 0 5px;
  523. .app-name {
  524. cursor: pointer;
  525. }
  526. .head-btn-group {
  527. display: flex;
  528. margin-left: 50px;
  529. i {
  530. font-size: 14px;
  531. }
  532. .icon-reverse {
  533. transform: rotate(180deg);
  534. }
  535. }
  536. .item-wrap {
  537. display: flex;
  538. align-items: center;
  539. .menu-img {
  540. width: 18px;
  541. height: 18px;
  542. margin-left: 9px;
  543. margin-right: 15px;
  544. cursor: pointer;
  545. }
  546. .logo-text {
  547. user-select: none;
  548. margin-left: 9px;
  549. font-size: 14px;
  550. color: #ffffff;
  551. }
  552. .name-span {
  553. max-width: 300px;
  554. overflow: hidden;
  555. white-space: nowrap;
  556. text-overflow: ellipsis;
  557. }
  558. }
  559. }
  560. </style>