PageDesignTop.vue 18 KB

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