router.ts 886 B

123456789101112131415161718192021222324252627282930313233
  1. import { usePermissions } from '../../composables/permissions'
  2. export const back = () => {
  3. uni.navigateBack()
  4. }
  5. export const useRouter = () => {
  6. const { routes, isLogined } = usePermissions()
  7. const push = async (url: string, switchTab = false) => {
  8. const path = url.split('?')[0]
  9. const route = routes.find((it) => it.path === path)
  10. if (route && !route.meta.canNotLogin && !isLogined.value) {
  11. if (route.meta.showToast) {
  12. uni.showToast({ title: '暂无权限', icon: 'none' })
  13. }
  14. if (route.meta.toLogin) {
  15. push('/pages/login/index')
  16. }
  17. return false
  18. }
  19. if (switchTab) {
  20. uni.switchTab({ url })
  21. } else {
  22. uni.navigateTo({ url })
  23. }
  24. }
  25. const replace = async (path: string) => {
  26. uni.redirectTo({ url: path })
  27. }
  28. const back = () => {
  29. uni.navigateBack()
  30. }
  31. return { push, replace, back }
  32. }