feat(auth): 分享卡直达详情页未登录时先跳登录,登录后回跳原页面
- 家庭详情页 useDidShow 检测无 token 时 reLaunch 登录页并携带 redirect 参数 - 登录页登录成功/已有 token 时优先回跳 redirect 目标,无参数仍进首页
This commit is contained in:
parent
7f82b2f132
commit
a027c6ce2f
|
|
@ -1,6 +1,7 @@
|
|||
import { useState } from 'react'
|
||||
import { useRouter, useDidShow, useShareAppMessage } from '@tarojs/taro'
|
||||
import { View, Text, Button } from '@tarojs/components'
|
||||
import { useAppSelector } from '@/store/hooks'
|
||||
import { Navbar } from '@/components/navbar'
|
||||
import { Loading } from '@/components/loading'
|
||||
import { StateView } from '@/components/state-view'
|
||||
|
|
@ -10,7 +11,7 @@ import { getHomeDetail, getHomeMemberList, type HomeInfo, type HomeMember } from
|
|||
import { getCareMemberList, type CareMember } from '@/api/careMember'
|
||||
import { MAX_CARED_PER_FAMILY } from '@/constants/family'
|
||||
import { ROUTES } from '@/constants/routes'
|
||||
import { navigateBack, navigateTo } from '@/utils/navigation'
|
||||
import { navigateBack, navigateTo, reLaunch } from '@/utils/navigation'
|
||||
import { showToast } from '@/utils/toast'
|
||||
import styles from './detail.module.scss'
|
||||
|
||||
|
|
@ -29,6 +30,7 @@ const contactsSummary = (c: CareMember): string => {
|
|||
export default function FamilyDetail() {
|
||||
const router = useRouter()
|
||||
const familyId = router.params.id ?? ''
|
||||
const token = useAppSelector((s) => s.auth.token)
|
||||
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [family, setFamily] = useState<HomeInfo | null>(null)
|
||||
|
|
@ -36,6 +38,12 @@ export default function FamilyDetail() {
|
|||
const [caredList, setCaredList] = useState<CareMember[]>([])
|
||||
|
||||
const load = async () => {
|
||||
// 分享卡直达本页且未登录:先去登录,登录后回跳本页
|
||||
if (!token) {
|
||||
const target = encodeURIComponent(`${ROUTES.FAMILY_DETAIL}?id=${familyId}&invite=1`)
|
||||
reLaunch(`${ROUTES.LOGIN}?redirect=${target}`)
|
||||
return
|
||||
}
|
||||
setLoading(true)
|
||||
try {
|
||||
const [home, memberList, careList] = await Promise.all([
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useState } from 'react'
|
||||
import { View, Text } from '@tarojs/components'
|
||||
import { useDidShow } from '@tarojs/taro'
|
||||
import { useRouter, useDidShow } from '@tarojs/taro'
|
||||
import { useAppDispatch, useAppSelector } from '@/store/hooks'
|
||||
import { setAuth } from '@/store/authSlice'
|
||||
import { performWxLogin, completeUserInfoLogin, getLoginSubState } from './wxLoginFlow'
|
||||
|
|
@ -14,15 +14,23 @@ import styles from './index.module.scss'
|
|||
export default function Login() {
|
||||
const dispatch = useAppDispatch()
|
||||
const toast = useToast()
|
||||
const router = useRouter()
|
||||
const token = useAppSelector((s) => s.auth.token)
|
||||
const [loading, setLoading] = useState(false)
|
||||
// 注册中间态:needUserInfo(待用户资料授权)→ null(初始/完成)
|
||||
const [needAuth, setNeedAuth] = useState(false)
|
||||
const [openId, setOpenId] = useState('')
|
||||
|
||||
// 登录后落地页:携带 redirect 参数时优先回跳(守卫踢登录/分享卡直达场景),否则进首页
|
||||
const afterLoginUrl = () => {
|
||||
const raw = router.params.redirect
|
||||
const target = typeof raw === 'string' && raw.startsWith('/') ? decodeURIComponent(raw) : ''
|
||||
return target || ROUTES.HOME
|
||||
}
|
||||
|
||||
// 已有 token 直接进首页(本期不做 checkLogin 校验,spec D5)
|
||||
useDidShow(() => {
|
||||
if (token && !loading) reLaunch(ROUTES.HOME)
|
||||
if (token && !loading) reLaunch(afterLoginUrl())
|
||||
})
|
||||
|
||||
// 完整登录态校验与落地;未完成时推进授权态
|
||||
|
|
@ -49,7 +57,7 @@ export default function Login() {
|
|||
console.log('[login-page] 登录完成,token 已落地')
|
||||
setNeedAuth(false)
|
||||
dispatch(setAuth({ token, userInfo: result.userInfo }))
|
||||
await reLaunch(ROUTES.HOME)
|
||||
await reLaunch(afterLoginUrl())
|
||||
}
|
||||
|
||||
const handleWxLogin = async () => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue