app.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { RouteNameRaw } from '@/router'
  2. import type { RoutesRef } from '@/router/info'
  3. import type { MenuRaw, MenuRelation } from '@/views/sys/menu/menu'
  4. import type { ComputedRef, Ref } from 'vue'
  5. import type { ModeFlag } from './sys'
  6. export type AuthName<T extends ModeFlag = any> =
  7. RouteNameRaw<T>[keyof RouteNameRaw<T>]
  8. export type RouteAuth = {
  9. name: AuthName
  10. open: boolean
  11. }
  12. export type RouteAuths = RouteAuth[]
  13. export type AppConfig = {
  14. routerRef: RoutesRef
  15. menu: ComputedRef<{
  16. list: MenuRaw
  17. allList?: MenuRaw
  18. relation: MenuRelation
  19. }>
  20. preset?: () => Promise<void>
  21. auth?: {
  22. list: Ref<RouteAuths>
  23. include: (name: AuthName | AuthName[] | string) => void
  24. exclude: (name: AuthName | AuthName[] | string) => void
  25. inExclude: (name: AuthName | AuthName[] | string) => boolean
  26. inInclude: (name: AuthName | AuthName[] | string) => boolean
  27. request: () => Promise<any>
  28. save: () => Promise<any>
  29. backups: () => void
  30. recovery: () => void
  31. }
  32. logo: string
  33. basePath?: string
  34. }
  35. export const getResources = uri => {
  36. if (
  37. !currentApp.basePath ||
  38. ~uri.indexOf('base64') ||
  39. ~uri.indexOf('bolb') ||
  40. ~uri.indexOf('//')
  41. )
  42. return uri
  43. const baseURL = new URL(currentApp.basePath)
  44. const url = new URL(uri, currentApp.basePath)
  45. const basePath =
  46. baseURL.pathname[baseURL.pathname.length - 1] === '/'
  47. ? baseURL.pathname.substring(0, baseURL.pathname.length - 1)
  48. : baseURL.pathname
  49. url.pathname = basePath + url.pathname
  50. return url.href
  51. }
  52. // 保存每个app的状态
  53. export let currentApp: AppConfig = {
  54. menu: null,
  55. routerRef: null,
  56. logo: null
  57. }
  58. export const setCurrentApp = (config: AppConfig) => {
  59. currentApp = config
  60. }