fix: 无 openid 不再 devLogin 共享账号,改为微信授权引导页(requireAuth)
This commit is contained in:
@@ -3,13 +3,15 @@ import { useNavigate } from "react-router";
|
||||
import { motion } from "motion/react";
|
||||
import { Heart } from "lucide-react";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import { devLogin, fetchApi, getToken, clearToken, decodeToken } from "../services/api";
|
||||
import { fetchApi, getToken, clearToken, decodeToken } from "../services/api";
|
||||
import { debugLog, clearDebugLogs } from "../services/debug";
|
||||
import bridge from "../services/bridge";
|
||||
|
||||
export function LoginPage() {
|
||||
const navigate = useNavigate();
|
||||
const { setTokenAndUser, fetchProfile } = useUser();
|
||||
const [error, setError] = useState("");
|
||||
const [needAuth, setNeedAuth] = useState(false);
|
||||
const { addDebug, debugInfo } = useUser();
|
||||
|
||||
useEffect(() => {
|
||||
@@ -78,23 +80,13 @@ export function LoginPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
addDebug(`no openid, fallback to dev-login`);
|
||||
devLogin().then((res) => {
|
||||
debugLog(`dev-login: openid=${res.openid}, phone=${res.mobile}`);
|
||||
setTokenAndUser(res.token, { id: res.user_id, openid: res.openid, phone: res.mobile });
|
||||
fetchProfile().then((dto) => {
|
||||
if (dto) debugLog(`fetchProfile: name=${dto.name}, phone=${dto.phone}`);
|
||||
navigate(dto?.is_onboarded ? "/home" : "/onboarding", { replace: true });
|
||||
});
|
||||
}).catch((err) => {
|
||||
debugLog(`dev-login error: ${err?.message}`);
|
||||
setError(err?.message || "登录失败,请刷新重试");
|
||||
});
|
||||
addDebug(`no openid, 显示授权引导(不再 devLogin 共享账号)`);
|
||||
setNeedAuth(true);
|
||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
return (
|
||||
<div className="min-h-dvh bg-morandi-50 flex items-center justify-center p-4 relative">
|
||||
<div className="text-center">
|
||||
<div className="text-center max-w-sm w-full">
|
||||
<motion.div
|
||||
animate={{ rotate: 360 }}
|
||||
transition={{ repeat: Infinity, duration: 1.5, ease: "linear" }}
|
||||
@@ -102,7 +94,18 @@ export function LoginPage() {
|
||||
>
|
||||
<Heart className="w-8 h-8 text-white fill-white" />
|
||||
</motion.div>
|
||||
{error ? (
|
||||
{needAuth ? (
|
||||
<div>
|
||||
<p className="text-sm text-gray-700 mb-1">需要微信授权才能使用完整功能</p>
|
||||
<p className="text-xs text-gray-400 mb-4">请在微信小程序中点击下方按钮完成授权</p>
|
||||
<button
|
||||
onClick={() => bridge.requireAuth()}
|
||||
className="w-full py-2.5 rounded-full bg-morandi-400 text-white text-sm font-medium"
|
||||
>
|
||||
微信授权登录
|
||||
</button>
|
||||
</div>
|
||||
) : error ? (
|
||||
<p className="text-sm text-red-500">{error}</p>
|
||||
) : (
|
||||
<p className="text-sm text-gray-500">加载中...</p>
|
||||
|
||||
Reference in New Issue
Block a user