TabLayout.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. if (!this.linkList.includes("/account/stepForm")) {
  147. localStorage.removeItem("step")
  148. localStorage.removeItem("campaignId")
  149. localStorage.removeItem("pans")
  150. localStorage.removeItem("pansKey")
  151. localStorage.removeItem("appId")
  152. }
  153. if (!this.linkList.includes("/account/advertisingGroup") && !this.linkList.includes("/account/stepForm")) {
  154. localStorage.removeItem("advertisingGroup")
  155. localStorage.removeItem("advertisingGroupKey")
  156. }
  157. if (!this.linkList.includes("/account/originality") && !this.linkList.includes("/account/stepForm")) {
  158. localStorage.removeItem("originality")
  159. localStorage.removeItem("originalityKey")
  160. }
  161. },
  162. 'activePage': function (key) {
  163. let index = this.linkList.lastIndexOf(key)
  164. let waitRouter = this.pageList[index]
  165. this.$router.push(Object.assign({}, waitRouter));
  166. // this.changeTitle(waitRouter.meta.title)
  167. },
  168. 'multipage': function (newVal) {
  169. if (this.reloadFlag) {
  170. if (!newVal) {
  171. this.linkList = [this.$route.fullPath]
  172. this.pageList = [this.$route]
  173. }
  174. }
  175. },
  176. // update-begin-author:sunjianlei date:20191223 for: 修复从单页模式切换回多页模式后首页不居第一位的 BUG
  177. device() {
  178. if (this.multipage && this.linkList.indexOf(indexKey) === -1) {
  179. this.addIndexToFirst()
  180. }
  181. },
  182. // update-end-author:sunjianlei date:20191223 for: 修复从单页模式切换回多页模式后首页不居第一位的 BUG
  183. },
  184. methods: {
  185. // update-begin-author:sunjianlei date:20191223 for: 修复从单页模式切换回多页模式后首页不居第一位的 BUG
  186. // 将首页添加到第一位
  187. addIndexToFirst() {
  188. this.pageList.splice(0, 0, {
  189. name: 'dashboard-analysis',
  190. path: indexKey,
  191. fullPath: indexKey,
  192. meta: {
  193. icon: 'dashboard',
  194. title: '首页'
  195. }
  196. })
  197. this.linkList.splice(0, 0, indexKey)
  198. },
  199. // update-end-author:sunjianlei date:20191223 for: 修复从单页模式切换回多页模式后首页不居第一位的 BUG
  200. // update-begin-author:sunjianlei date:20200120 for: 动态更改页面标题
  201. changeTitle(title) {
  202. // let projectTitle = "Jeecg-Boot 企业级快速开发平台"
  203. // 首页特殊处理
  204. // if (this.$route.path === indexKey) {
  205. // document.title = projectTitle
  206. // } else {
  207. // document.title = title + ' · ' + projectTitle
  208. // }
  209. },
  210. // update-end-author:sunjianlei date:20200120 for: 动态更改页面标题
  211. changePage(key) {
  212. this.activePage = key
  213. },
  214. tabCallBack() {
  215. this.$nextTick(() => {
  216. triggerWindowResizeEvent()
  217. })
  218. },
  219. editPage(key, action) {
  220. this[action](key)
  221. },
  222. remove(key) {
  223. if (key == indexKey) {
  224. this.$message.warning('首页不能关闭!')
  225. return
  226. }
  227. if (this.pageList.length === 1) {
  228. this.$message.warning('这是最后一页,不能再关闭了啦')
  229. return
  230. }
  231. console.log("this.pageList ", this.pageList);
  232. // this.pageList = this.pageList.filter(item => item.fullPath !== key)
  233. // let index = this.linkList.indexOf(key)
  234. // this.linkList = this.linkList.filter(item => item !== key)
  235. // index = index >= this.linkList.length ? this.linkList.length - 1 : index
  236. // this.activePage = this.linkList[index]
  237. if (key == "/account/advertisingGroup") {
  238. if (this.linkList.includes("/account/stepForm") || this.linkList.includes("/account/originality")) {
  239. this.$message.error('已开启创意或创建页面,不可关闭组页面')
  240. return
  241. } else {
  242. localStorage.removeItem("advertisingGroup")
  243. localStorage.removeItem("advertisingGroupKey")
  244. }
  245. }
  246. if (key == "/account/stepForm") {
  247. localStorage.removeItem("step")
  248. localStorage.removeItem("pans")
  249. localStorage.removeItem("pansKey")
  250. }
  251. if (key == "/account/originality") {
  252. localStorage.removeItem("originality")
  253. localStorage.removeItem("originalityKey")
  254. }
  255. if (key == "/modules/material/materialList") {
  256. localStorage.removeItem("key")
  257. }
  258. if (key == "/account/accountIndex") {
  259. if (this.linkList.includes("/account/stepForm") || this.linkList.includes("/account/originality") || this
  260. .linkList.includes("/account/advertisingGroup")) {
  261. this.$message.error('正在操作,不可关闭计划列表')
  262. return
  263. } else {
  264. this.routeReload()
  265. }
  266. }
  267. if (key == '/modules/BatchCreate/TouTiaoBatch' && !(this.linkList.includes('/modules/BatchCreate/newCreate') ||
  268. this.linkList.includes('/modules/BatchCreate/newPlan') || this.linkList.includes(
  269. '/modules/BatchCreate/newGroup'))) {
  270. localStorage.setItem('accountInfo', '')
  271. localStorage.setItem("groupInfo", '')
  272. localStorage.setItem("planInfo", '')
  273. }
  274. // if(!this.linkList.includes('/modules/BatchCreate/newGroup')&&!this.linkList.includes('/modules/BatchCreate/newPlan')&&!this.linkList.includes('/modules/BatchCreate/newCreate')){
  275. // localStorage.setItem("planInfo",'')
  276. // localStorage.setItem("groupInfo",'')
  277. // }
  278. if (key == '/modules/BatchCreate/newGroup' && !(this.linkList.includes('/modules/BatchCreate/newCreate') || this
  279. .linkList.includes('/modules/BatchCreate/newPlan') || this.linkList.includes(
  280. '/modules/BatchCreate/TouTiaoBatch'))) {
  281. console.log('newGroup')
  282. localStorage.setItem('accountInfo', '')
  283. localStorage.setItem("groupInfo", '')
  284. }
  285. if (key == '/modules/BatchCreate/newPlan' && !(this.linkList.includes('/modules/BatchCreate/newCreate') || this
  286. .linkList.includes('/modules/BatchCreate/newGroup') || this.linkList.includes(
  287. '/modules/BatchCreate/TouTiaoBatch'))) {
  288. console.log('newPlan')
  289. localStorage.setItem('accountInfo', '')
  290. localStorage.setItem("groupInfo", '')
  291. localStorage.setItem("planInfo", '')
  292. }
  293. if (key == '/modules/BatchCreate/newCreate' && !(this.linkList.includes('/modules/BatchCreate/TouTiaoBatch') ||
  294. this.linkList.includes('/modules/BatchCreate/newPlan') || this.linkList.includes(
  295. '/modules/BatchCreate/newGroup'))) {
  296. console.log('newCreate')
  297. localStorage.setItem('accountInfo', '')
  298. localStorage.setItem("groupInfo", '')
  299. localStorage.setItem("planInfo", '')
  300. }
  301. // this.pageList = this.pageList.filter(item => item.fullPath !== key)
  302. // let index = this.linkList.indexOf(key)
  303. // this.linkList = this.linkList.filter(item => item !== key)
  304. // index = index >= this.linkList.length ? this.linkList.length - 1 : index
  305. // this.activePage = this.linkList[index]
  306. this.pageList = this.pageList.filter(item => item.fullPath !== key)
  307. let index = this.linkList.indexOf(key)
  308. this.linkList = this.linkList.filter(item => item !== key)
  309. index = index >= this.linkList.length ? this.linkList.length - 1 : index
  310. if (index != -1) {
  311. this.activePage = this.linkList[index]
  312. } else {
  313. this.activePage = this.$route.fullPath
  314. }
  315. },
  316. onContextmenu(e) {
  317. const pagekey = this.getPageKey(e.target)
  318. if (pagekey !== null) {
  319. e.preventDefault()
  320. this.menuVisible = true
  321. }
  322. },
  323. getPageKey(target, depth) {
  324. depth = depth || 0
  325. if (depth > 2) {
  326. return null
  327. }
  328. let pageKey = target.getAttribute('pagekey')
  329. pageKey = pageKey || (target.previousElementSibling ? target.previousElementSibling.getAttribute('pagekey') :
  330. null)
  331. return pageKey || (target.firstElementChild ? this.getPageKey(target.firstElementChild, ++depth) : null)
  332. },
  333. onMenuSelect(key, target) {
  334. let pageKey = this.getPageKey(target)
  335. switch (key) {
  336. case '1':
  337. this.closeLeft(pageKey)
  338. break
  339. case '2':
  340. this.closeRight(pageKey)
  341. break
  342. case '3':
  343. this.closeOthers(pageKey)
  344. break
  345. case '4':
  346. this.routeReload()
  347. break
  348. default:
  349. break
  350. }
  351. },
  352. /* update_begin author:wuxianquan date:20190828 for: 关闭当前tab页,供子页面调用->望菜单能配置外链,直接弹出新页面而不是嵌入iframe #428 */
  353. closeCurrent() {
  354. this.remove(this.activePage);
  355. },
  356. /* update_end author:wuxianquan date:20190828 for: 关闭当前tab页,供子页面调用->望菜单能配置外链,直接弹出新页面而不是嵌入iframe #428 */
  357. closeOthers(pageKey) {
  358. let index = this.linkList.indexOf(pageKey)
  359. if (pageKey == indexKey || pageKey.indexOf('?ticke=') >= 0) {
  360. this.linkList = this.linkList.slice(index, index + 1)
  361. this.pageList = this.pageList.slice(index, index + 1)
  362. this.activePage = this.linkList[0]
  363. } else {
  364. let indexContent = this.pageList.slice(0, 1)[0]
  365. this.linkList = this.linkList.slice(index, index + 1)
  366. this.pageList = this.pageList.slice(index, index + 1)
  367. this.linkList.unshift(indexContent.fullPath)
  368. this.pageList.unshift(indexContent)
  369. this.activePage = this.linkList[1]
  370. }
  371. },
  372. closeLeft(pageKey) {
  373. if (pageKey == indexKey) {
  374. return
  375. }
  376. let tempList = [...this.pageList]
  377. let indexContent = tempList.slice(0, 1)[0]
  378. let index = this.linkList.indexOf(pageKey)
  379. this.linkList = this.linkList.slice(index)
  380. this.pageList = this.pageList.slice(index)
  381. this.linkList.unshift(indexContent.fullPath)
  382. this.pageList.unshift(indexContent)
  383. if (this.linkList.indexOf(this.activePage) < 0) {
  384. this.activePage = this.linkList[0]
  385. }
  386. },
  387. closeRight(pageKey) {
  388. let index = this.linkList.indexOf(pageKey)
  389. this.linkList = this.linkList.slice(0, index + 1)
  390. this.pageList = this.pageList.slice(0, index + 1)
  391. if (this.linkList.indexOf(this.activePage < 0)) {
  392. this.activePage = this.linkList[this.linkList.length - 1]
  393. }
  394. },
  395. //update-begin-author:taoyan date:20190430 for:动态路由title显示配置的菜单title而不是其对应路由的title
  396. dynamicRouterShow(key, title) {
  397. let keyIndex = this.linkList.indexOf(key)
  398. if (keyIndex >= 0) {
  399. let currRouter = this.pageList[keyIndex]
  400. let meta = Object.assign({}, currRouter.meta, {
  401. title: title
  402. })
  403. this.pageList.splice(keyIndex, 1, Object.assign({}, currRouter, {
  404. meta: meta
  405. }))
  406. if (key === this.activePage) {
  407. this.changeTitle(title)
  408. }
  409. }
  410. },
  411. //update-end-author:taoyan date:20190430 for:动态路由title显示配置的菜单title而不是其对应路由的title
  412. //update-begin-author:taoyan date:20191008 for:路由刷新
  413. routeReload() {
  414. this.reloadFlag = false
  415. let ToggleMultipage = "ToggleMultipage"
  416. this.$store.dispatch(ToggleMultipage, false)
  417. this.$nextTick(() => {
  418. this.$store.dispatch(ToggleMultipage, true)
  419. this.reloadFlag = true
  420. })
  421. }
  422. //update-end-author:taoyan date:20191008 for:路由刷新
  423. }
  424. }
  425. </script>
  426. <style lang="less">
  427. /*
  428. * The following styles are auto-applied to elements with
  429. * transition="page-transition" when their visibility is toggled
  430. * by Vue.js.
  431. *
  432. * You can easily play with the page transition by editing
  433. * these styles.
  434. */
  435. .page-transition-enter {
  436. opacity: 0;
  437. }
  438. .page-transition-leave-active {
  439. opacity: 0;
  440. }
  441. .page-transition-enter .page-transition-container,
  442. .page-transition-leave-active .page-transition-container {
  443. -webkit-transform: scale(1.1);
  444. transform: scale(1.1);
  445. }
  446. /*美化弹出Tab样式*/
  447. .ant-tabs-nav-container {
  448. margin-top: 4px;
  449. }
  450. /* 修改 ant-tabs 样式 */
  451. .tab-layout-tabs.ant-tabs {
  452. border-bottom: 1px solid #ccc;
  453. border-left: 1px solid #ccc;
  454. background-color: white;
  455. padding: 0 20px;
  456. .ant-tabs-bar {
  457. margin: 4px 0 0;
  458. border: none;
  459. }
  460. }
  461. .ant-tabs {
  462. &.ant-tabs-card .ant-tabs-tab {
  463. padding: 0 24px !important;
  464. background-color: white !important;
  465. margin-right: 10px !important;
  466. .ant-tabs-close-x {
  467. width: 12px !important;
  468. height: 12px !important;
  469. opacity: 0 !important;
  470. cursor: pointer !important;
  471. font-size: 12px !important;
  472. margin: 0 !important;
  473. position: absolute;
  474. top: 36%;
  475. right: 6px;
  476. }
  477. &:hover .ant-tabs-close-x {
  478. opacity: 1 !important;
  479. }
  480. }
  481. }
  482. .ant-tabs.ant-tabs-card>.ant-tabs-bar {
  483. .ant-tabs-tab {
  484. border: none !important;
  485. border-bottom: 1px solid transparent !important;
  486. }
  487. // .ant-tabs-tab-active {
  488. // border-color: @primary-color!important;
  489. // }
  490. }
  491. </style>