PageDesignTop.vue 16 KB

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