fix(mini): refresh page after login
This commit is contained in:
@@ -5,19 +5,46 @@ import Taro from '@tarojs/taro'
|
|||||||
import { getToken, wechatLogin } from '@/services/auth'
|
import { getToken, wechatLogin } from '@/services/auth'
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
|
const LOGIN_PAGE_URL = '/pages/login/index'
|
||||||
|
const DEFAULT_POST_LOGIN_URL = '/pages/device/index'
|
||||||
|
const TAB_BAR_ROUTES = new Set([
|
||||||
|
'/pages/device/index',
|
||||||
|
'/pages/chat/index',
|
||||||
|
'/pages/location/index',
|
||||||
|
'/pages/sleep/index',
|
||||||
|
])
|
||||||
|
|
||||||
|
function consumePostLoginRedirect(): string {
|
||||||
|
const storedUrl = String(Taro.getStorageSync('postLoginRedirect') || '').trim()
|
||||||
|
Taro.removeStorageSync('postLoginRedirect')
|
||||||
|
|
||||||
|
if (!storedUrl || storedUrl === LOGIN_PAGE_URL || !storedUrl.startsWith('/pages/')) {
|
||||||
|
return DEFAULT_POST_LOGIN_URL
|
||||||
|
}
|
||||||
|
|
||||||
|
return storedUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
function navigateAfterLogin(url: string): void {
|
||||||
|
const route = url.split('?')[0]
|
||||||
|
if (!TAB_BAR_ROUTES.has(route)) {
|
||||||
|
Taro.reLaunch({ url })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tab pages must be opened with switchTab so their lifecycle and custom tab bar refresh.
|
||||||
|
void Promise.resolve(Taro.switchTab({ url: route })).catch(() => {
|
||||||
|
Taro.reLaunch({ url: route })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export default function Login() {
|
export default function Login() {
|
||||||
const [submitting, setSubmitting] = useState(false)
|
const [submitting, setSubmitting] = useState(false)
|
||||||
const [agreementAccepted, setAgreementAccepted] = useState(false)
|
const [agreementAccepted, setAgreementAccepted] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (getToken()) {
|
if (getToken()) {
|
||||||
const redirectUrl = String(Taro.getStorageSync('postLoginRedirect') || '').trim()
|
navigateAfterLogin(consumePostLoginRedirect())
|
||||||
if (redirectUrl) {
|
|
||||||
Taro.removeStorageSync('postLoginRedirect')
|
|
||||||
Taro.reLaunch({ url: redirectUrl })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Taro.reLaunch({ url: '/pages/device/index' })
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,13 +87,7 @@ export default function Login() {
|
|||||||
|
|
||||||
Taro.showToast({ title: '登录成功', icon: 'success' })
|
Taro.showToast({ title: '登录成功', icon: 'success' })
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const redirectUrl = String(Taro.getStorageSync('postLoginRedirect') || '').trim()
|
navigateAfterLogin(consumePostLoginRedirect())
|
||||||
if (redirectUrl) {
|
|
||||||
Taro.removeStorageSync('postLoginRedirect')
|
|
||||||
Taro.reLaunch({ url: redirectUrl })
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Taro.reLaunch({ url: '/pages/device/index' })
|
|
||||||
}, 300)
|
}, 300)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[login] failed:', error)
|
console.error('[login] failed:', error)
|
||||||
|
|||||||
Reference in New Issue
Block a user