PageDesignTop.vue 17 KB

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