PageDesignTop.vue 19 KB

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