page-tab-util.js 5.1 KB

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