TabLayout.vue 17 KB

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