page.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { ResultEnum } from '@/enums/httpEnum'
  2. import { ErrorPageNameMap, PageEnum } from '@/enums/pageEnum'
  3. import { RouteLocation } from 'vue-router'
  4. import router from '@/router'
  5. import { docPath, giteeSourceCodePath } from '@/settings/pathConst'
  6. /**
  7. * * 根据名字跳转路由
  8. * @param pageName
  9. * @param isReplace
  10. * @param windowOpen
  11. */
  12. export const routerTurnByName = (
  13. pageName: string,
  14. isReplace?: boolean,
  15. windowOpen?: boolean
  16. ) => {
  17. if (windowOpen) {
  18. const path = fetchPathByName(pageName, 'href')
  19. openNewWindow(path)
  20. return
  21. }
  22. if (isReplace) {
  23. router.replace({
  24. name: pageName
  25. })
  26. return
  27. }
  28. router.push({
  29. name: pageName
  30. })
  31. }
  32. /**
  33. * * 根据名称获取路由信息
  34. * @param pageName
  35. * @param pageName
  36. */
  37. export const fetchPathByName = (pageName: string, p?: string) => {
  38. const pathData = router.resolve({
  39. name: pageName
  40. })
  41. return p ? (pathData as any)[p] : pathData
  42. }
  43. /**
  44. * * 根据路径跳转路由
  45. * @param { String } path
  46. * @param { Array } query
  47. * @param isReplace
  48. */
  49. export const routerTurnByPath = (
  50. path: string,
  51. query?: Array<string | number>,
  52. isReplace?: boolean,
  53. windowOpen?: boolean
  54. ) => {
  55. let fullPath = ''
  56. if (query?.length) {
  57. fullPath = `${path}/${query.join('/')}`
  58. }
  59. if (windowOpen) {
  60. openNewWindow(fullPath)
  61. return
  62. }
  63. if (isReplace) {
  64. router.replace({
  65. path: fullPath
  66. })
  67. return
  68. }
  69. router.push({
  70. path: fullPath
  71. })
  72. }
  73. /**
  74. * * 错误页重定向
  75. * @param icon
  76. * @returns
  77. */
  78. export const redirectErrorPage = (code: ResultEnum) => {
  79. if (!code) return false
  80. const pageName = ErrorPageNameMap.get(code)
  81. if (!pageName) return false
  82. routerTurnByName(pageName)
  83. }
  84. /**
  85. * * 退出
  86. */
  87. export const logout = () => {
  88. routerTurnByName(PageEnum.BASE_LOGIN_NAME)
  89. }
  90. /**
  91. * * 新开页面
  92. * @param url
  93. */
  94. export const openNewWindow = (url: string) => {
  95. window.open(url, 'blank')
  96. }
  97. /**
  98. * * 打开项目文档
  99. * @param url
  100. */
  101. export const openDoc = () => {
  102. openNewWindow(docPath)
  103. }
  104. /**
  105. * * 打开码云仓库地址
  106. * @param url
  107. */
  108. export const openGiteeSourceCode = () => {
  109. openNewWindow(giteeSourceCodePath)
  110. }