PageDesignTop.vue 20 KB

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