TabLayout.vue 18 KB

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