PageDesignTop.vue 16 KB

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