page-tab-util.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * 页签操作封装
  3. */
  4. import store from '@/store';
  5. import router from '@/router';
  6. import { Message } from 'element-ui';
  7. import { removeToken } from '@/utils/token-util';
  8. import { setDocumentTitle } from '@/utils/document-title-util';
  9. import { SYSTEM_NAME } from '@/config/setting';
  10. import {
  11. HOME_PATH,
  12. LAYOUT_PATH,
  13. REDIRECT_PATH,
  14. REPEATABLE_TABS
  15. } from '@/config/setting';
  16. const HOME_ROUTE = HOME_PATH || LAYOUT_PATH;
  17. const BASE_URL = process.env.BASE_URL;
  18. /**
  19. * 刷新当前路由
  20. */
  21. export function reloadPageTab (option) {
  22. if (!option) {
  23. // 刷新当前路由
  24. const { path, fullPath, query } = router.currentRoute;
  25. if (path.includes(REDIRECT_PATH)) {
  26. return;
  27. }
  28. const isHome = isHomeRoute(router.currentRoute);
  29. setRouteReload({
  30. reloadHome: isHome,
  31. reloadPath: isHome ? void 0 : fullPath
  32. }).then(() => {
  33. router.replace({
  34. path: REDIRECT_PATH + path,
  35. query
  36. });
  37. });
  38. } else {
  39. // 刷新指定页签
  40. const { fullPath, isHome } = option;
  41. setRouteReload({
  42. reloadHome: isHome,
  43. reloadPath: isHome ? void 0 : fullPath
  44. });
  45. router.replace(REDIRECT_PATH + fullPath);
  46. }
  47. }
  48. /**
  49. * 关闭当前页签
  50. */
  51. export function finishPageTab () {
  52. const key = getRouteTabKey();
  53. removePageTab({ key, active: key });
  54. }
  55. /**
  56. * 关闭页签
  57. * @param key 页签的key
  58. * @param active 选中页签的key
  59. */
  60. export function removePageTab (option) {
  61. store
  62. .dispatch('theme/tabRemove', option)
  63. .then(({ path, home }) => {
  64. if (path) {
  65. router.push(path);
  66. } else if (home) {
  67. router.push(HOME_ROUTE);
  68. }
  69. })
  70. .catch(() => {
  71. Message.error('当前页签不可关闭');
  72. });
  73. }
  74. /**
  75. * 关闭左侧页签
  76. */
  77. export function removeLeftPageTab (option) {
  78. store
  79. .dispatch('theme/tabRemoveLeft', option)
  80. .then(({ path }) => {
  81. if (path) {
  82. router.push(path);
  83. }
  84. })
  85. .catch(() => {
  86. Message.error('左侧没有可关闭的页签');
  87. });
  88. }
  89. /**
  90. * 关闭右侧页签
  91. */
  92. export function removeRightPageTab (option) {
  93. store
  94. .dispatch('theme/tabRemoveRight', option)
  95. .then(({ path, home }) => {
  96. if (path) {
  97. router.push(path);
  98. } else if (home) {
  99. router.push(HOME_ROUTE);
  100. }
  101. })
  102. .catch(() => {
  103. Message.error('右侧没有可关闭的页签');
  104. });
  105. }
  106. /**
  107. * 关闭其它页签
  108. */
  109. export function removeOtherPageTab (option) {
  110. store
  111. .dispatch('theme/tabRemoveOther', option)
  112. .then(({ path, home }) => {
  113. if (path) {
  114. router.push(path);
  115. } else if (home) {
  116. router.push(HOME_ROUTE);
  117. }
  118. })
  119. .catch(() => {
  120. Message.error('没有可关闭的页签');
  121. });
  122. }
  123. /**
  124. * 关闭全部页签
  125. * @param active 当前选中页签
  126. */
  127. export function removeAllPageTab (active) {
  128. store
  129. .dispatch('theme/tabRemoveAll', active)
  130. .then(({ home }) => {
  131. if (home) {
  132. router.push(HOME_ROUTE);
  133. }
  134. })
  135. .catch(() => {
  136. Message.error('没有可关闭的页签');
  137. });
  138. }
  139. /**
  140. * 登录成功后清空页签
  141. */
  142. export function cleanPageTabs () {
  143. store.dispatch('theme/setTabs', []);
  144. }
  145. /**
  146. * 添加页签
  147. * @param data 页签数据
  148. */
  149. export function addPageTab (data) {
  150. store.dispatch('theme/tabAdd', data);
  151. }
  152. /**
  153. * 修改页签
  154. * @param data 页签数据
  155. */
  156. export function setPageTab (data) {
  157. store.dispatch('theme/tabSetItem', data);
  158. }
  159. /**
  160. * 更新页签数据
  161. * @param data 页签数据
  162. */
  163. export function setPageTabs (data) {
  164. store.dispatch('theme/setTabs', data);
  165. }
  166. /**
  167. * 修改页签标题
  168. * @param title 标题
  169. */
  170. export function setPageTabTitle (title) {
  171. setPageTab({ key: getRouteTabKey(), title });
  172. setDocumentTitle(title);
  173. }
  174. /**
  175. * 获取当前路由对应的页签 key
  176. */
  177. export function getRouteTabKey () {
  178. const { path, fullPath, meta } = router.currentRoute;
  179. const isUnique = meta.tabUnique === false || REPEATABLE_TABS.includes(path);
  180. return `${window.__POWERED_BY_QIANKUN__ ? '/page-' + SYSTEM_NAME : ''}${
  181. isUnique ? fullPath : path
  182. }`;
  183. }
  184. /**
  185. * 设置主页的组件名称
  186. * @param components 组件名称
  187. */
  188. export function setHomeComponents (components) {
  189. store.dispatch('theme/setHomeComponents', components);
  190. }
  191. /**
  192. * 设置路由刷新信息
  193. * @param option 路由刷新参数
  194. */
  195. export function setRouteReload (option) {
  196. return store.dispatch('theme/setRouteReload', option);
  197. }
  198. /**
  199. * 判断路由是否是主页
  200. * @param route 路由信息
  201. */
  202. export function isHomeRoute (route) {
  203. const { path, matched } = route;
  204. if (HOME_ROUTE === path) {
  205. return true;
  206. }
  207. return (
  208. matched[0] &&
  209. matched[0].path === LAYOUT_PATH &&
  210. matched[0].redirect === path
  211. );
  212. }
  213. /**
  214. * 登录成功后跳转首页
  215. * @param from 登录前的地址
  216. */
  217. export function goHomeRoute (from) {
  218. router.replace(from || HOME_ROUTE);
  219. }
  220. /**
  221. * 退出登录
  222. * @param from 是否使用路由跳转
  223. * @param from 登录后跳转的地址
  224. */
  225. export function logout (route, from) {
  226. removeToken();
  227. if (route) {
  228. router.push({
  229. path: '/login',
  230. query: from ? { from } : void 0
  231. });
  232. } else {
  233. // 这样跳转避免再次登录重复注册动态路由
  234. location.replace(BASE_URL + 'login' + (from ? '?from=' + from : ''));
  235. }
  236. }