usePage.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import type { RouteLocationRaw, Router } from 'vue-router';
  2. import { PageEnum } from '/@/enums/pageEnum';
  3. import { isString } from '/@/utils/is';
  4. import { unref } from 'vue';
  5. import { useRouter } from 'vue-router';
  6. import { REDIRECT_NAME } from '/@/router/constant';
  7. import { useUserStore } from '/@/store/modules/user';
  8. import { useGlobSetting } from '/@/hooks/setting';
  9. export type RouteLocationRawEx = Omit<RouteLocationRaw, 'path'> & { path: PageEnum };
  10. function handleError(e: Error) {
  11. console.error(e);
  12. }
  13. // page switch
  14. export function useGo(_router?: Router) {
  15. // update-begin--author:liaozhiyang---date:20230908---for:【issues/694】404返回首页问题
  16. const userStore = useUserStore();
  17. const homePath = userStore.getUserInfo.homePath || PageEnum.BASE_HOME;
  18. // update-end--author:liaozhiyang---date:20230908---for:【issues/694】404返回首页问题
  19. let router;
  20. if (!_router) {
  21. router = useRouter();
  22. }
  23. const { push, replace } = _router || router;
  24. function go(opt: PageEnum | RouteLocationRawEx | string = homePath, isReplace = false) {
  25. if (!opt) {
  26. return;
  27. }
  28. if (isString(opt)) {
  29. isReplace ? replace(opt).catch(handleError) : push(opt).catch(handleError);
  30. } else {
  31. const o = opt as RouteLocationRaw;
  32. isReplace ? replace(o).catch(handleError) : push(o).catch(handleError);
  33. }
  34. }
  35. return go;
  36. }
  37. /**
  38. * @description: redo current page
  39. */
  40. export const useRedo = (_router?: Router) => {
  41. const { push, currentRoute } = _router || useRouter();
  42. const { query, params = {}, name, fullPath } = unref(currentRoute.value);
  43. function redo(): Promise<boolean> {
  44. return new Promise((resolve) => {
  45. if (name === REDIRECT_NAME) {
  46. resolve(false);
  47. return;
  48. }
  49. if (name && Object.keys(params).length > 0) {
  50. //update-begin-author:taoyan date:2022-10-19 for: VUEN-2356 【vue3】online表单、表单设计器 功能测试 右键刷新时 404
  51. if(isDynamicRoute(params, name)){
  52. params['_redirect_type'] = 'path';
  53. params['path'] = fullPath;
  54. }else{
  55. params['_redirect_type'] = 'name';
  56. params['path'] = String(name);
  57. }
  58. //update-end-author:taoyan date:2022-10-19 for: VUEN-2356 【vue3】online表单、表单设计器 功能测试 右键刷新时 404
  59. } else {
  60. params['_redirect_type'] = 'path';
  61. params['path'] = fullPath;
  62. }
  63. push({ name: REDIRECT_NAME, params, query }).then(() => resolve(true));
  64. });
  65. }
  66. return redo;
  67. };
  68. /**
  69. * 判断是不是动态路由的跳转
  70. * @param params
  71. * @param name
  72. */
  73. function isDynamicRoute(params, name){
  74. let arr = Object.keys(params);
  75. let flag = false;
  76. for(let i=0;i<arr.length;i++){
  77. let key = '@'+arr[i];
  78. if((name as string).indexOf(key)>0){
  79. flag = true;
  80. break;
  81. }
  82. }
  83. return flag;
  84. }