diff --git a/src/components/AddCartBar.tsx b/src/components/AddCartBar.tsx index 2b78bd4..d3168fc 100644 --- a/src/components/AddCartBar.tsx +++ b/src/components/AddCartBar.tsx @@ -1,5 +1,4 @@ import {Headphones, Share} from '@nutui/icons-react-taro' -import navTo from "@/utils/common"; import Taro, { getCurrentInstance } from '@tarojs/taro'; import {getUserInfo} from "@/api/layout"; import {useEffect, useState} from "react"; @@ -8,7 +7,6 @@ import {CmsArticle} from "@/api/cms/cmsArticle/model"; function AddCartBar() { const { router } = getCurrentInstance(); - const [id, setId] = useState() const [article, setArticle] = useState() const [IsLogin, setIsLogin] = useState(false) const onPay = () => { @@ -24,7 +22,7 @@ function AddCartBar() { return false; } } - const reload = (id) => { + const reload = (id: any) => { getCmsArticle(id).then(data => { setArticle(data) }) @@ -40,7 +38,6 @@ function AddCartBar() { useEffect(() => { const id = router?.params.id as number | undefined; - setId(id) reload(id); }, []); diff --git a/src/hooks/useUser.ts b/src/hooks/useUser.ts index ccf3481..17e9636 100644 --- a/src/hooks/useUser.ts +++ b/src/hooks/useUser.ts @@ -253,7 +253,7 @@ export const useUser = () => { // 获取用户显示名称 const getDisplayName = () => { - return user?.nickname || user?.realName || user?.username || '未登录'; + return user?.nickname || user?.realName || user?.username || '点击登录'; }; // 角色名称:优先取用户 roles 数组的第一个角色名称 diff --git a/src/pages/index/HeaderWithHook.tsx b/src/pages/index/HeaderWithHook.tsx index c9eee89..1d927de 100644 --- a/src/pages/index/HeaderWithHook.tsx +++ b/src/pages/index/HeaderWithHook.tsx @@ -197,7 +197,7 @@ const Header = (props: any) => {
Logo: logo

用户信息

-
登录状态: {isLoggedIn ? '已登录' : '未登录'}
+
登录状态: {isLoggedIn ? '已登录' : '点击登录'}
{user && ( <>
用户ID: {user.userId}
diff --git a/src/pages/index/QuickActions.tsx b/src/pages/index/QuickActions.tsx index 59a4318..4542e6b 100644 --- a/src/pages/index/QuickActions.tsx +++ b/src/pages/index/QuickActions.tsx @@ -9,6 +9,7 @@ import { Wallet } from '@nutui/icons-react-taro' import navTo from '@/utils/common' +import { requireLogin } from '@/utils/common' import './QuickActions.scss' const QuickActions: React.FC = () => { @@ -42,10 +43,7 @@ const QuickActions: React.FC = () => { ] const handleClick = (action: { path: string }) => { - if (!Taro.getStorageSync('access_token') || !Taro.getStorageSync('UserId')) { - Taro.navigateTo({url: '/passport/login'}) - return - } + if (!requireLogin(action.path)) return navTo(action.path) } diff --git a/src/passport/login.scss b/src/passport/login.scss index bde80cb..c22f999 100644 --- a/src/passport/login.scss +++ b/src/passport/login.scss @@ -127,7 +127,7 @@ width: 180px; height: 180px; border-radius: 40px; - background: rgba(255, 255, 255, 0.95); + background-color: #ffffff; display: flex; align-items: center; justify-content: center; diff --git a/src/passport/login.tsx b/src/passport/login.tsx index 7236fa0..a34516b 100644 --- a/src/passport/login.tsx +++ b/src/passport/login.tsx @@ -275,7 +275,7 @@ const Login = () => { - 点击上方按钮,快速登录 + 点击上方按钮,快速登录 diff --git a/src/utils/common.ts b/src/utils/common.ts index 4938f63..291c008 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -1,15 +1,41 @@ import Taro from '@tarojs/taro' +/** + * 检查是否已登录 + * @returns 已登录返回 true,未登录返回 false + */ +export function isLoggedIn(): boolean { + return !!(Taro.getStorageSync('access_token') && Taro.getStorageSync('UserId')) +} + +/** + * 要求登录:未登录时弹出提示,3秒后自动跳转到登录页 + * @param redirect 登录成功后跳转的目标页面(可选) + * @returns 已登录返回 true;未登录时执行跳转并返回 false + */ +export function requireLogin(redirect?: string): boolean { + if (isLoggedIn()) return true + + const redirectParam = redirect + ? `?redirect=${encodeURIComponent(redirect)}` + : '' + + Taro.showToast({ + title: '请先登录', + icon: 'none', + duration: 2000, + }) + + setTimeout(() => { + Taro.navigateTo({ url: `/passport/login${redirectParam}` }) + }, 1500) + + return false +} + export default function navTo(url: string, isLogin = false) { if (isLogin && url != '/pages/user/user') { - if (!Taro.getStorageSync('access_token') || !Taro.getStorageSync('UserId')) { - Taro.showToast({ - title: '请先登录', - icon: 'none', - duration: 500 - }); - return false; - } + if (!requireLogin(url)) return false } Taro.navigateTo({ url: url