router.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { useRoute } from 'vue-router'
  2. import { ResultEnum } from '@/enums/httpEnum'
  3. import { ErrorPageNameMap, PageEnum } from '@/enums/pageEnum'
  4. import { docPath, giteeSourceCodePath } from '@/settings/pathConst'
  5. import { cryptoDecode } from './crypto'
  6. import { StorageEnum } from '@/enums/storageEnum'
  7. import { clearLocalStorage, getLocalStorage } from './storage'
  8. import router from '@/router'
  9. /**
  10. * * 根据名字跳转路由
  11. * @param pageName
  12. * @param isReplace
  13. * @param windowOpen
  14. */
  15. export const routerTurnByName = (
  16. pageName: string,
  17. isReplace?: boolean,
  18. windowOpen?: boolean
  19. ) => {
  20. if (windowOpen) {
  21. const path = fetchPathByName(pageName, 'href')
  22. openNewWindow(path)
  23. return
  24. }
  25. if (isReplace) {
  26. router.replace({
  27. name: pageName,
  28. })
  29. return
  30. }
  31. router.push({
  32. name: pageName,
  33. })
  34. }
  35. /**
  36. * * 根据名称获取路由信息
  37. * @param pageName
  38. * @param pageName
  39. */
  40. export const fetchPathByName = (pageName: string, p?: string) => {
  41. try {
  42. const pathData = router.resolve({
  43. name: pageName,
  44. })
  45. return p ? (pathData as any)[p] : pathData
  46. } catch (error) {
  47. window['$message'].warning('查询路由信息失败,请联系管理员!')
  48. }
  49. }
  50. /**
  51. * * 根据路径跳转路由
  52. * @param path
  53. * @param query
  54. * @param isReplace
  55. * @param windowOpen
  56. */
  57. export const routerTurnByPath = (
  58. path: string,
  59. query?: Array<string | number>,
  60. isReplace?: boolean,
  61. windowOpen?: boolean
  62. ) => {
  63. let fullPath = ''
  64. if (query?.length) {
  65. fullPath = `${path}/${query.join('/')}`
  66. }
  67. if (windowOpen) {
  68. return openNewWindow(fullPath)
  69. }
  70. if (isReplace) {
  71. router.replace({
  72. path: fullPath,
  73. })
  74. return
  75. }
  76. router.push({
  77. path: fullPath,
  78. })
  79. }
  80. /**
  81. * * 错误页重定向
  82. * @param icon
  83. * @returns
  84. */
  85. export const redirectErrorPage = (code: ResultEnum) => {
  86. if (!code) return false
  87. const pageName = ErrorPageNameMap.get(code)
  88. if (!pageName) return false
  89. routerTurnByName(pageName)
  90. }
  91. /**
  92. * * 重新加载当前路由页面
  93. */
  94. export const reloadRoutePage = () => {
  95. routerTurnByName(PageEnum.RELOAD_NAME)
  96. }
  97. /**
  98. * * 退出
  99. */
  100. export const logout = () => {
  101. clearLocalStorage(StorageEnum.GO_LOGIN_INFO_STORE)
  102. routerTurnByName(PageEnum.BASE_LOGIN_NAME)
  103. }
  104. /**
  105. * * 新开页面
  106. * @param url
  107. */
  108. export const openNewWindow = (url: string) => {
  109. return window.open(url, '_blank')
  110. }
  111. /**
  112. * * 打开项目文档
  113. * @param url
  114. */
  115. export const openDoc = () => {
  116. openNewWindow(docPath)
  117. }
  118. /**
  119. * * 打开码云仓库地址
  120. * @param url
  121. */
  122. export const openGiteeSourceCode = () => {
  123. openNewWindow(giteeSourceCodePath)
  124. }
  125. /**
  126. * * 判断是否是预览页
  127. * @returns boolean
  128. */
  129. export const isPreview = () => {
  130. return document.location.hash.includes('preview')
  131. }
  132. /**
  133. * * 获取当前路由下的参数
  134. * @returns object
  135. */
  136. export const fetchRouteParams = () => {
  137. try {
  138. const route = useRoute()
  139. return route.params
  140. } catch (error) {
  141. window['$message'].warning('查询路由信息失败,请联系管理员!')
  142. }
  143. }
  144. /**
  145. * * 通过硬解析获取当前路由下的参数
  146. * @returns object
  147. */
  148. export const fetchRouteParamsLocation = () => {
  149. try {
  150. return document.location.hash.split('/').pop() || ''
  151. } catch (error) {
  152. window['$message'].warning('查询路由信息失败,请联系管理员!')
  153. return ''
  154. }
  155. }
  156. /**
  157. * * 回到主页面
  158. * @param confirm
  159. */
  160. export const goHome = () => {
  161. routerTurnByName(PageEnum.BASE_HOME_NAME)
  162. }
  163. /**
  164. * * 判断是否登录(现阶段是有 login 数据即可)
  165. * @return boolean
  166. */
  167. export const loginCheck = () => {
  168. try {
  169. const info = getLocalStorage(StorageEnum.GO_LOGIN_INFO_STORE)
  170. if (!info) return false
  171. const decodeInfo = cryptoDecode(info)
  172. if (decodeInfo) {
  173. return true
  174. }
  175. return false
  176. } catch (error) {
  177. return false
  178. }
  179. }