PageDesignTop.vue 16 KB

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