TabLayout.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <template>
  2. <global-layout @dynamicRouterShow="dynamicRouterShow">
  3. <!-- update-begin- author:sunjianlei --- date:20191009 --- for: 提升右键菜单的层级 -->
  4. <contextmenu :itemList="menuItemList" :visible.sync="menuVisible" style="z-index: 9999;" @select="onMenuSelect"/>
  5. <!-- update-end- author:sunjianlei --- date:20191009 --- for: 提升右键菜单的层级 -->
  6. <a-tabs
  7. @contextmenu.native="e => onContextmenu(e)"
  8. v-if="multipage"
  9. :active-key="activePage"
  10. class="tab-layout-tabs"
  11. style="height:52px"
  12. :hide-add="true"
  13. type="editable-card"
  14. @change="changePage"
  15. @tabClick="tabCallBack"
  16. @edit="editPage">
  17. <a-tab-pane :id="page.fullPath" :key="page.fullPath" v-for="page in pageList" :closable="!(page.meta.title=='首页')">
  18. <span slot="tab" :pagekey="page.fullPath">{{ page.meta.title }}</span>
  19. </a-tab-pane>
  20. </a-tabs>
  21. <!-- IoT Edge相关 修改框架 -->
  22. <div :style="layoutMode === 'iotmenu'?'margin: 20px 16px 0;':'margin: 12px 12px 0;'">
  23. <!-- <div style="margin: 12px 12px 0;"> -->
  24. <!-- update-begin-author:taoyan date:20201221 for:此处删掉transition标签 不知道为什么加上后 页面路由切换的时候即1及菜单切到2及菜单的时候 两个菜单页面会同时出现300-500秒左右 -->
  25. <keep-alive v-if="multipage">
  26. <router-view v-if="reloadFlag"/>
  27. </keep-alive>
  28. <template v-else>
  29. <router-view v-if="reloadFlag"/>
  30. </template>
  31. <!-- update-end-author:taoyan date:20201221 for:此处删掉transition标签 不知道为什么加上后 页面路由切换的时候即1及菜单切到2及菜单的时候 两个菜单页面会同时出现300-500秒左右 -->
  32. </div>
  33. <!-- update-begin-author:zyf date:20211129 for:qiankun 挂载子应用盒子 -->
  34. <div id="content" class="app-view-box"></div>
  35. <!-- update-end-author:zyf date:20211129 for: qiankun 挂载子应用盒子-->
  36. </global-layout>
  37. </template>
  38. <script>
  39. import GlobalLayout from '@/components/page/GlobalLayout'
  40. import Contextmenu from '@/components/menu/Contextmenu'
  41. import { mixin, mixinDevice } from '@/utils/mixin.js'
  42. import { triggerWindowResizeEvent } from '@/utils/util'
  43. import Vue from 'vue'
  44. import { CACHE_INCLUDED_ROUTES } from '@/store/mutation-types'
  45. import registerApps from "@/qiankun";
  46. const indexKey = '/dashboard/analysis'
  47. export default {
  48. name: 'TabLayout',
  49. components: {
  50. GlobalLayout,
  51. Contextmenu
  52. },
  53. mixins: [mixin, mixinDevice],
  54. data() {
  55. return {
  56. pageList: [],
  57. linkList: [],
  58. activePage: '',
  59. menuVisible: false,
  60. menuItemList: [
  61. { key: '4', icon: 'reload', text: '刷 新' },
  62. { key: '1', icon: 'arrow-left', text: '关闭左侧' },
  63. { key: '2', icon: 'arrow-right', text: '关闭右侧' },
  64. { key: '3', icon: 'close', text: '关闭其它' }
  65. ],
  66. reloadFlag:true
  67. }
  68. },
  69. /* update_begin author:wuxianquan date:20190828 for: 关闭当前tab页,供子页面调用 ->望菜单能配置外链,直接弹出新页面而不是嵌入iframe #428 */
  70. provide(){
  71. return{
  72. closeCurrent:this.closeCurrent,
  73. changeTitle: this.changeTitle,
  74. changeTabTitle: this.changeTabTitle,
  75. }
  76. },
  77. /* update_end author:wuxianquan date:20190828 for: 关闭当前tab页,供子页面调用->望菜单能配置外链,直接弹出新页面而不是嵌入iframe #428 */
  78. computed: {
  79. multipage() {
  80. //判断如果是手机模式,自动切换为单页面模式
  81. if (this.isMobile()) {
  82. return false
  83. } else {
  84. return this.$store.state.app.multipage
  85. }
  86. }
  87. },
  88. created() {
  89. if (this.$route.path != indexKey) {
  90. this.addIndexToFirst()
  91. }
  92. // 复制一个route对象出来,不能影响原route
  93. let currentRoute = Object.assign({}, this.$route)
  94. currentRoute.meta = Object.assign({}, currentRoute.meta)
  95. this.pageList.push(currentRoute)
  96. this.linkList.push(currentRoute.fullPath)
  97. this.activePage = currentRoute.fullPath
  98. },
  99. mounted() {
  100. if (process.env.VUE_APP_QIANKUN == 'true') {
  101. //update-begin-author:zyf date:20211129 for:qiankun 注册子应用
  102. if (!window.qiankunStarted) {
  103. window.qiankunStarted = true;
  104. registerApps();
  105. }
  106. //update-end-author:zyf date:20211129 for:qiankun 注册子应用
  107. }
  108. },
  109. watch: {
  110. '$route': function(newRoute) {
  111. //console.log("新的路由",newRoute)
  112. this.activePage = newRoute.fullPath
  113. if (!this.multipage) {
  114. this.linkList = [newRoute.fullPath]
  115. this.pageList = [Object.assign({},newRoute)]
  116. // update-begin-author:taoyan date:20200211 for: TASK #3368 【路由缓存】首页的缓存设置有问题,需要根据后台的路由配置来实现是否缓存
  117. } else if(indexKey==newRoute.fullPath) {
  118. //首页时 判断是否缓存 没有缓存 刷新之
  119. if (newRoute.meta.keepAlive === false) {
  120. this.routeReload()
  121. }
  122. // update-end-author:taoyan date:20200211 for: TASK #3368 【路由缓存】首页的缓存设置有问题,需要根据后台的路由配置来实现是否缓存
  123. }else if (this.linkList.indexOf(newRoute.fullPath) < 0) {
  124. this.linkList.push(newRoute.fullPath)
  125. this.pageList.push(Object.assign({},newRoute))
  126. //// update-begin-author:sunjianlei date:20200103 for: 如果新增的页面配置了缓存路由,那么就强制刷新一遍 #842
  127. // if (newRoute.meta.keepAlive) {
  128. // this.routeReload()
  129. // }
  130. //// update-end-author:sunjianlei date:20200103 for: 如果新增的页面配置了缓存路由,那么就强制刷新一遍 #842
  131. } else if (this.linkList.indexOf(newRoute.fullPath) >= 0) {
  132. let oldIndex = this.linkList.indexOf(newRoute.fullPath)
  133. let oldPositionRoute = this.pageList[oldIndex]
  134. this.pageList.splice(oldIndex, 1, Object.assign({},newRoute,{meta:oldPositionRoute.meta}))
  135. }
  136. },
  137. 'activePage': function(key) {
  138. let index = this.linkList.lastIndexOf(key)
  139. let waitRouter = this.pageList[index]
  140. // 【TESTA-523】修复:不允许重复跳转路由异常
  141. if (waitRouter.fullPath !== this.$route.fullPath) {
  142. this.$router.push(Object.assign({}, waitRouter))
  143. }
  144. this.changeTitle(waitRouter.meta.title)
  145. },
  146. 'multipage': function(newVal) {
  147. if(this.reloadFlag){
  148. if (!newVal) {
  149. this.linkList = [this.$route.fullPath]
  150. this.pageList = [this.$route]
  151. }
  152. }
  153. },
  154. // update-begin-author:sunjianlei date:20191223 for: 修复从单页模式切换回多页模式后首页不居第一位的 BUG
  155. device() {
  156. if (this.multipage && this.linkList.indexOf(indexKey) === -1) {
  157. this.addIndexToFirst()
  158. }
  159. },
  160. // update-end-author:sunjianlei date:20191223 for: 修复从单页模式切换回多页模式后首页不居第一位的 BUG
  161. },
  162. methods: {
  163. // update-begin-author:sunjianlei date:20191223 for: 修复从单页模式切换回多页模式后首页不居第一位的 BUG
  164. // 将首页添加到第一位
  165. addIndexToFirst() {
  166. this.pageList.splice(0, 0, {
  167. name: 'dashboard-analysis',
  168. path: indexKey,
  169. fullPath: indexKey,
  170. meta: {
  171. icon: 'dashboard',
  172. title: '首页'
  173. }
  174. })
  175. this.linkList.splice(0, 0, indexKey)
  176. },
  177. // update-end-author:sunjianlei date:20191223 for: 修复从单页模式切换回多页模式后首页不居第一位的 BUG
  178. // update-begin-author:sunjianlei date:20200120 for: 动态更改页面标题
  179. /**
  180. * 修改当前页面的窗口标题
  181. * @param title 要修改的新标题
  182. */
  183. changeTitle(title) {
  184. // 修改框架
  185. let projectTitle = "联锁管理平台"
  186. // let projectTitle = "Jeecg-Boot 企业级低代码平台"
  187. // 首页特殊处理
  188. if (this.$route.path === indexKey) {
  189. document.title = projectTitle
  190. } else {
  191. document.title = title + ' · ' + projectTitle
  192. }
  193. },
  194. /**
  195. * 修改tab标签的标题
  196. * @param title 要修改的新标题
  197. * @param fullPath 要修改的路由全路径,如果不填就是修改当前路由
  198. */
  199. changeTabTitle(title, fullPath = '') {
  200. if (title) {
  201. let currentRoute = this.pageList.find((r) => r.fullPath === (fullPath ? fullPath : this.$route.fullPath))
  202. if (currentRoute != null) {
  203. currentRoute.meta = {...currentRoute.meta, title}
  204. }
  205. }
  206. },
  207. // update-end-author:sunjianlei date:20200120 for: 动态更改页面标题
  208. changePage(key) {
  209. this.activePage = key
  210. },
  211. tabCallBack() {
  212. this.$nextTick(() => {
  213. //update-begin-author:taoyan date: 20201211 for:【新版】online报错 JT-100
  214. setTimeout(()=>{
  215. //省市区组件里面给window绑定了个resize事件 导致切换页面的时候触发了他的resize,但是切换页面,省市区组件还没被销毁前就触发了该事件,导致控制台报错,加个延迟
  216. triggerWindowResizeEvent()
  217. },20)
  218. //update-end-author:taoyan date: 20201211 for:【新版】online报错 JT-100
  219. })
  220. },
  221. editPage(key, action) {
  222. this[action](key)
  223. },
  224. remove(key) {
  225. if (key == indexKey) {
  226. this.$message.warning('首页不能关闭!')
  227. return
  228. }
  229. if (this.pageList.length === 1) {
  230. this.$message.warning('这是最后一页,不能再关闭了啦')
  231. return
  232. }
  233. console.log("this.pageList ",this.pageList );
  234. let removeRoute = this.pageList.filter(item => item.fullPath == key)
  235. this.pageList = this.pageList.filter(item => item.fullPath !== key)
  236. let index = this.linkList.indexOf(key)
  237. this.linkList = this.linkList.filter(item => item !== key)
  238. index = index >= this.linkList.length ? this.linkList.length - 1 : index
  239. this.activePage = this.linkList[index]
  240. //update-begin--Author:scott Date:20201015 for:路由缓存问题,关闭了tab页时再打开就不刷新 #842
  241. //关闭页面则从缓存cache_included_routes中删除路由,下次点击菜单会重新加载页面
  242. let cacheRouterArray = Vue.ls.get(CACHE_INCLUDED_ROUTES) || []
  243. if (removeRoute && removeRoute[0]) {
  244. let componentName = removeRoute[0].meta.componentName
  245. console.log("key: ", key);
  246. console.log("componentName: ", componentName);
  247. if(cacheRouterArray.includes(componentName)){
  248. cacheRouterArray.splice(cacheRouterArray.findIndex(item => item === componentName), 1)
  249. Vue.ls.set(CACHE_INCLUDED_ROUTES, cacheRouterArray)
  250. }
  251. this.emitPageClosed(removeRoute[0])
  252. }
  253. //update-end--Author:scott Date:20201015 for:路由缓存问题,关闭了tab页时再打开就不刷新 #842
  254. },
  255. // 触发 page-closed (页面关闭)全局事件
  256. emitPageClosed(closedRoute) {
  257. this.$root.$emit('page-closed', {
  258. closedRoute,
  259. pageList: this.pageList,
  260. linkList: this.linkList,
  261. activePage: this.activePage
  262. })
  263. },
  264. onContextmenu(e) {
  265. const pagekey = this.getPageKey(e.target)
  266. if (pagekey !== null) {
  267. e.preventDefault()
  268. this.menuVisible = true
  269. }
  270. },
  271. getPageKey(target, depth) {
  272. depth = depth || 0
  273. if (depth > 2) {
  274. return null
  275. }
  276. let pageKey = target.getAttribute('pagekey')
  277. pageKey = pageKey || (target.previousElementSibling ? target.previousElementSibling.getAttribute('pagekey') : null)
  278. return pageKey || (target.firstElementChild ? this.getPageKey(target.firstElementChild, ++depth) : null)
  279. },
  280. onMenuSelect(key, target) {
  281. let pageKey = this.getPageKey(target)
  282. switch (key) {
  283. case '1':
  284. this.closeLeft(pageKey)
  285. break
  286. case '2':
  287. this.closeRight(pageKey)
  288. break
  289. case '3':
  290. this.closeOthers(pageKey)
  291. break
  292. case '4':
  293. this.routeReload()
  294. break
  295. default:
  296. break
  297. }
  298. },
  299. /* update_begin author:wuxianquan date:20190828 for: 关闭当前tab页,供子页面调用->望菜单能配置外链,直接弹出新页面而不是嵌入iframe #428 */
  300. closeCurrent(){
  301. this.remove(this.activePage);
  302. },
  303. /* update_end author:wuxianquan date:20190828 for: 关闭当前tab页,供子页面调用->望菜单能配置外链,直接弹出新页面而不是嵌入iframe #428 */
  304. closeOthers(pageKey) {
  305. let index = this.linkList.indexOf(pageKey)
  306. if (pageKey == indexKey || pageKey.indexOf('?ticke=')>=0) {
  307. this.linkList = this.linkList.slice(index, index + 1)
  308. this.pageList = this.pageList.slice(index, index + 1)
  309. this.activePage = this.linkList[0]
  310. } else {
  311. let indexContent = this.pageList.slice(0, 1)[0]
  312. this.linkList = this.linkList.slice(index, index + 1)
  313. this.pageList = this.pageList.slice(index, index + 1)
  314. this.linkList.unshift(indexContent.fullPath)
  315. this.pageList.unshift(indexContent)
  316. this.activePage = this.linkList[1]
  317. }
  318. },
  319. closeLeft(pageKey) {
  320. if (pageKey == indexKey) {
  321. return
  322. }
  323. let tempList = [...this.pageList]
  324. let indexContent = tempList.slice(0, 1)[0]
  325. let index = this.linkList.indexOf(pageKey)
  326. this.linkList = this.linkList.slice(index)
  327. this.pageList = this.pageList.slice(index)
  328. this.linkList.unshift(indexContent.fullPath)
  329. this.pageList.unshift(indexContent)
  330. if (this.linkList.indexOf(this.activePage) < 0) {
  331. this.activePage = this.linkList[0]
  332. }
  333. },
  334. closeRight(pageKey) {
  335. let index = this.linkList.indexOf(pageKey)
  336. this.linkList = this.linkList.slice(0, index + 1)
  337. this.pageList = this.pageList.slice(0, index + 1)
  338. if (this.linkList.indexOf(this.activePage < 0)) {
  339. this.activePage = this.linkList[this.linkList.length - 1]
  340. }
  341. },
  342. //update-begin-author:taoyan date:20190430 for:动态路由title显示配置的菜单title而不是其对应路由的title
  343. dynamicRouterShow(key,title){
  344. let keyIndex = this.linkList.indexOf(key)
  345. if(keyIndex>=0){
  346. let currRouter = this.pageList[keyIndex]
  347. let meta = Object.assign({},currRouter.meta,{title:title})
  348. this.pageList.splice(keyIndex, 1, Object.assign({},currRouter,{meta:meta}))
  349. if (key === this.activePage) {
  350. this.changeTitle(title)
  351. }
  352. }
  353. },
  354. //update-end-author:taoyan date:20190430 for:动态路由title显示配置的菜单title而不是其对应路由的title
  355. //update-begin-author:taoyan date:20191008 for:路由刷新
  356. routeReload(){
  357. this.reloadFlag = false
  358. let ToggleMultipage = "ToggleMultipage"
  359. this.$store.dispatch(ToggleMultipage,false)
  360. this.$nextTick(()=>{
  361. this.$store.dispatch(ToggleMultipage,true)
  362. this.reloadFlag = true
  363. })
  364. },
  365. //update-end-author:taoyan date:20191008 for:路由刷新
  366. //新增一个返回方法
  367. excuteCallback(callback){
  368. callback()
  369. },
  370. }
  371. }
  372. </script>
  373. <style lang="less">
  374. /*
  375. * The following styles are auto-applied to elements with
  376. * transition="page-transition" when their visibility is toggled
  377. * by Vue.js.
  378. *
  379. * You can easily play with the page transition by editing
  380. * these styles.
  381. */
  382. .page-transition-enter {
  383. opacity: 0;
  384. }
  385. .page-transition-leave-active {
  386. opacity: 0;
  387. }
  388. .page-transition-enter .page-transition-container,
  389. .page-transition-leave-active .page-transition-container {
  390. -webkit-transform: scale(1.1);
  391. transform: scale(1.1);
  392. }
  393. /*美化弹出Tab样式*/
  394. .ant-tabs-nav-container {
  395. margin-top: 4px;
  396. }
  397. /* 修改 ant-tabs 样式 */
  398. .tab-layout-tabs.ant-tabs {
  399. border-bottom: 1px solid #ccc;
  400. border-left: 1px solid #ccc;
  401. background-color: white;
  402. padding: 0 20px;
  403. .ant-tabs-bar {
  404. margin: 4px 0 0;
  405. border: none;
  406. }
  407. }
  408. .tab-layout-tabs.ant-tabs {
  409. &.ant-tabs-card .ant-tabs-tab {
  410. padding: 0 24px !important;
  411. background-color: white !important;
  412. margin-right: 10px !important;
  413. .ant-tabs-close-x {
  414. width: 12px !important;
  415. height: 12px !important;
  416. opacity: 0 !important;
  417. cursor: pointer !important;
  418. font-size: 12px !important;
  419. margin: 0 !important;
  420. position: absolute;
  421. top: 36%;
  422. right: 6px;
  423. }
  424. &:hover .ant-tabs-close-x {
  425. opacity: 1 !important;
  426. }
  427. }
  428. }
  429. .tab-layout-tabs.ant-tabs.ant-tabs-card > .ant-tabs-bar {
  430. .ant-tabs-tab {
  431. border: none !important;
  432. border-bottom: 1px solid transparent !important;
  433. }
  434. .ant-tabs-tab-active {
  435. border-color: @primary-color!important;
  436. }
  437. }
  438. </style>