| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import { getAccessToken } from './tokenStorage'
- import { useAuthStore } from '@/store/modules/auth'
- import { ROUTE_LOGIN_INDEX } from '@/constants/routes'
- const TAB_BAR_PAGES = ['/pages/meal/index', '/pages/order/index', '/pages/profile/index']
- const DEFAULT_LOGIN_PROMPT = {
- title: '需要登录',
- content: '该功能需登录后使用,是否前往登录?',
- confirmText: '去登录',
- cancelText: '稍后再说'
- }
- export function getToken() {
- return getAccessToken()
- }
- export function isLoggedIn() {
- const auth = useAuthStore()
- if (!auth.token) auth.initAuth()
- return auth.loggedIn
- }
- export function isTabBarPage(url: string) {
- const path = url.split('?')[0]
- return TAB_BAR_PAGES.includes(path)
- }
- function goLogin(redirect?: string) {
- const target = `${ROUTE_LOGIN_INDEX}${redirect ? `?redirect=${encodeURIComponent(redirect)}` : ''}`
- uni.navigateTo({
- url: target,
- fail: () => uni.reLaunch({ url: target })
- })
- }
- export function requireLogin() {
- if (isLoggedIn()) return true
- uni.showModal({
- ...DEFAULT_LOGIN_PROMPT,
- success: res => {
- if (res.confirm) goLogin()
- }
- })
- return false
- }
- export function requireLoginNavigate(redirect?: string) {
- if (isLoggedIn()) return true
- goLogin(redirect)
- return false
- }
|