router.ts 4.5 KB

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