Files
tgCrewUser/src/router/index.ts
2025-08-01 13:38:52 +03:00

105 lines
3.2 KiB
TypeScript

import { defineRouter } from '#q-app/wrappers'
import {
createMemoryHistory,
createRouter,
createWebHashHistory,
createWebHistory,
} from 'vue-router'
import routes from './routes'
import { useProjectsStore } from 'stores/projects'
import { useAuthStore } from 'stores/auth'
import { parseStartParams } from 'helpers/helpers'
export default defineRouter(function (/* { store, ssrContext } */) {
const createHistory = process.env.SERVER
? createMemoryHistory
: (process.env.VUE_ROUTER_MODE === 'history' ? createWebHistory : createWebHashHistory)
const startRouteInfo = typeof window !== 'undefined' && window.Telegram?.WebApp?.initDataUnsafe?.start_param
? parseStartParams(window.Telegram.WebApp.initDataUnsafe.start_param)
: null
const Router = createRouter({
scrollBehavior: () => ({ left: 0, top: 0 }),
routes,
// Leave this as is and make changes in quasar.conf.js instead!
// quasar.conf.js -> build -> vueRouterMode
// quasar.conf.js -> build -> publicPath
history: createHistory(process.env.VUE_ROUTER_BASE)
})
Router.beforeEach(async (to) => {
console.log(window.Telegram.WebApp.initDataUnsafe.start_param, startRouteInfo)
console.log(112, to)
if (to.name === 'settings' || to.name === '404' || to.name === 'accept-terms') return true
const authStore = useAuthStore()
const projectsStore = useProjectsStore()
if (!authStore.isInit) await authStore.init(window.Telegram.WebApp)
// if (!authStore.isTermsAccepted) return { name: 'accept-terms' }
if (to.path === '/' && startRouteInfo) {
console.log(222, startRouteInfo)
const { id, taskId, meetingId } = startRouteInfo
if (!projectsStore.isInit) await projectsStore.init()
const project = projectsStore.projectById(id)
if (!project) return { name: '404' }
return taskId
? { name: 'task_info', params: { id, taskId } }
: meetingId
? { name: 'meeting_info', params: { id, meetingId } }
: { name: 'files', params: { id } }
}
if (to.params.id) {
const projectId = Number(to.params.id)
if (!projectsStore.isInit) await projectsStore.init()
const project = projectsStore.projectById(projectId)
if (!project) return { name: '404' }
if (projectsStore.currentProjectId !== projectId) {
projectsStore.setCurrentProjectId(projectId)
}
} else {
return { name: '404' }
}
})
const handleBackButton = async () => {
const currentRoute = Router.currentRoute.value
if (currentRoute.meta.backRoute) {
await Router.push(currentRoute.meta.backRoute);
} else {
if (window.history.length > 1) {
Router.go(-1)
} else {
await Router.push({ name: 'main'})
}
}
}
Router.afterEach((to) => {
const BackButton = window.Telegram?.WebApp?.BackButton
if (BackButton) {
// Управление видимостью
if (to.meta.hideBackButton) {
BackButton.hide()
} else {
BackButton.show()
}
// Обновляем обработчик клика
BackButton.offClick(handleBackButton as () => void)
BackButton.onClick(handleBackButton as () => void)
}
})
return Router
})