update(login): Add request error prompt.

mack-mac
mackt 7 months ago
parent c9fb2f1851
commit 93e3f10e84

@ -36,7 +36,6 @@ export default function Journal() {
// journal: apiGetSongCollect, // journal: apiGetSongCollect,
// }; // };
const result = await apiGetSongCollect({ userId: userInfo?.id, pageNum, pageSize }); const result = await apiGetSongCollect({ userId: userInfo?.id, pageNum, pageSize });
console.log(result.data);
if (result.code === 200) setList(result.data.rows); if (result.code === 200) setList(result.data.rows);
}; };

@ -5,6 +5,9 @@ import { useRouter } from 'next/navigation';
import { useShallow } from 'zustand/react/shallow'; import { useShallow } from 'zustand/react/shallow';
import { Avatar } from '@/components'; import { Avatar } from '@/components';
import logout from '@/utils/logout';
import useUserStore from '@/store/user'; import useUserStore from '@/store/user';
interface Props { interface Props {
@ -19,10 +22,9 @@ interface TDataList {
export default function UserCard({ onClose, className }: Props) { export default function UserCard({ onClose, className }: Props) {
const router = useRouter(); const router = useRouter();
const { userInfo, userLogout } = useUserStore( const { userInfo } = useUserStore(
useShallow((state) => ({ useShallow((state) => ({
userInfo: state.userInfo, userInfo: state.userInfo,
userLogout: state.userLogout,
})), })),
); );
@ -38,12 +40,6 @@ export default function UserCard({ onClose, className }: Props) {
onClose(); onClose();
}; };
const handleLogout = async () => {
userLogout();
router.push('/');
onClose();
};
useEffect(() => { useEffect(() => {
setUserDataList([ setUserDataList([
{ label: '关注', value: userInfo.followCount }, { label: '关注', value: userInfo.followCount },
@ -87,7 +83,7 @@ export default function UserCard({ onClose, className }: Props) {
</li> </li>
<li <li
className="flex flex-row items-center w-[238px] h-[49px] px-[15px] rounded-[3px] text-[15px] leading-[21px] hover:bg-[#f2f3f7] cursor-pointer" className="flex flex-row items-center w-[238px] h-[49px] px-[15px] rounded-[3px] text-[15px] leading-[21px] hover:bg-[#f2f3f7] cursor-pointer"
onClick={handleLogout} onClick={logout}
> >
<span>退</span> <span>退</span>
</li> </li>

@ -5,7 +5,7 @@ import React, { useState, useMemo, useEffect } from 'react';
import { debounce } from 'lodash'; import { debounce } from 'lodash';
import { useShallow } from 'zustand/react/shallow'; import { useShallow } from 'zustand/react/shallow';
import { Input, Button } from '@/components'; import { Button } from '@/components';
import { apiSendSMS } from '@/services'; import { apiSendSMS } from '@/services';
import useUserStore from '@/store/user'; import useUserStore from '@/store/user';
@ -19,6 +19,7 @@ export default function LoginForm() {
const [phone, setPhone] = useState<string>('17861518060'); // 手机号 const [phone, setPhone] = useState<string>('17861518060'); // 手机号
const [authCode, seAuthCode] = useState<string>('111111'); // 验证码 const [authCode, seAuthCode] = useState<string>('111111'); // 验证码
const [errorText, setErrorText] = useState<string>(''); // 验证码
const [time, setTime] = useState<number>(0); // 倒计时 const [time, setTime] = useState<number>(0); // 倒计时
const [btnLoading, setBtnLoading] = useState<boolean>(false); const [btnLoading, setBtnLoading] = useState<boolean>(false);
const deviceId = '1'; // 设备id 暂时写死 const deviceId = '1'; // 设备id 暂时写死
@ -29,23 +30,33 @@ export default function LoginForm() {
setBtnLoading(true); setBtnLoading(true);
const result = await apiSendSMS({ mobile: phone, deviceId }); const result = await apiSendSMS({ mobile: phone, deviceId });
if (result.code === 200) { if (result.code !== 200) {
setErrorText(result.message);
setBtnLoading(false);
return;
}
setTime(59); setTime(59);
setTimeout(() => { setTimeout(() => {
setBtnLoading(false); setBtnLoading(false);
}, 60000); }, 60000);
}
setBtnLoading(false);
}; };
// 登录 // 登录
const handleLogin = async () => { const handleLogin = async () => {
setBtnLoading(true);
const result = await userLogin({ mobile: phone, mobileCheckCode: authCode, deviceId: '1' }); const result = await userLogin({ mobile: phone, mobileCheckCode: authCode, deviceId: '1' });
if (result) setShowLogin(false); if (result.code === 200) {
setShowLogin(false);
window.location.reload();
} else {
setErrorText(result.message);
setBtnLoading(false);
}
}; };
// 输入框,只允许输入数字 // 输入框,只允许输入数字
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>, fn: (text: string) => void) => { const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>, fn: (text: string) => void) => {
if (errorText) setErrorText('');
const inputValue = e.target.value; const inputValue = e.target.value;
const regex = /^[0-9]*$/; const regex = /^[0-9]*$/;
if (regex.test(inputValue) || inputValue === '') fn(e.target.value); if (regex.test(inputValue) || inputValue === '') fn(e.target.value);
@ -64,7 +75,8 @@ export default function LoginForm() {
return ( return (
<form autoComplete="on"> <form autoComplete="on">
<div className="w-full flex flex-col"> <div className="w-full flex flex-col">
<Input <input
className="w-full h-[56px] rounded-[100px] px-[14px] bg-[#fff] leading-[20px]"
type="text" type="text"
value={phone} value={phone}
maxLength={11} maxLength={11}
@ -73,8 +85,8 @@ export default function LoginForm() {
/> />
<div className="relative w-full mt-[20px]"> <div className="relative w-full mt-[20px]">
<Input <input
className="w-full" className="w-full h-[56px] rounded-[100px] px-[14px] bg-[#fff] leading-[20px]"
type="text" type="text"
value={authCode} value={authCode}
maxLength={6} maxLength={6}
@ -83,7 +95,7 @@ export default function LoginForm() {
/> />
<Button <Button
type="black" type="white"
className={`absolute top-[6px] right-[6px] w-[123px] h-[44px] rounded-[100px] font-size-[14px] leading-[20px] ${time ? 'text-[#000]' : ''}`} className={`absolute top-[6px] right-[6px] w-[123px] h-[44px] rounded-[100px] font-size-[14px] leading-[20px] ${time ? 'text-[#000]' : ''}`}
disabled={btnLoading || activeSMS} disabled={btnLoading || activeSMS}
onClick={debounce(handleSendSMS, 300)} onClick={debounce(handleSendSMS, 300)}
@ -101,6 +113,8 @@ export default function LoginForm() {
> >
</Button> </Button>
<p className="mt-[16px] text-center text-brand text-[13px] leading-[18.2px]">{errorText}</p>
</form> </form>
); );
} }

@ -1,7 +1,7 @@
import Image from 'next/image'; import Image from 'next/image';
import { useShallow } from 'zustand/react/shallow'; import { useShallow } from 'zustand/react/shallow';
import LoginForm from '../Login/LoginForm'; import LoginForm from './LoginForm';
import useUserStore from '@/store/user'; import useUserStore from '@/store/user';
@ -18,7 +18,7 @@ export default function LoginCard() {
<div className="fixed inset-0 bg-black opacity-50"></div> <div className="fixed inset-0 bg-black opacity-50"></div>
{/* 登录框 */} {/* 登录框 */}
<div className="relative flex flex-col w-[420px] h-[470px] pt-[49px] p-[40px] rounded-[24px] bg-[#fff] z-10"> <div className="relative flex flex-col w-[420px] h-[470px] pt-[49px] p-[40px] rounded-[24px] bg-[#f2f3f7]">
{/* title */} {/* title */}
<div className="mb-[30px]"> <div className="mb-[30px]">
<p className="text-[24px] leading-[33px] text-[#000] font-bold"></p> <p className="text-[24px] leading-[33px] text-[#000] font-bold"></p>
@ -42,10 +42,14 @@ export default function LoginCard() {
<div className="absolute bottom-[30px] left-0 right-0 text-[13px] leading-[18px] text-[rgba(0,0,0,0.4)] text-center"> <div className="absolute bottom-[30px] left-0 right-0 text-[13px] leading-[18px] text-[rgba(0,0,0,0.4)] text-center">
{' '} {' '}
<a className="text-[#000]" href=""> <a
className="text-[#000]"
href="http://cdn.indie.cn/html/agreement/registrationAgreement.html"
target="_blank"
>
</a> </a>
<a className="text-[#000]" href=""> <a className="text-[#000]" href="http://cdn.indie.cn/html/agreement/privacyPolicy.html" target="_blank">
</a> </a>
</div> </div>

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
interface ButtonProps { interface ButtonProps {
type?: 'default' | 'primary' | 'black'; type?: 'default' | 'primary' | 'black' | 'white';
disabled?: boolean; disabled?: boolean;
className?: string; className?: string;
children: React.ReactNode; children: React.ReactNode;
@ -18,6 +18,7 @@ const Button: React.FC<ButtonProps> = ({
const buttonColorList = { const buttonColorList = {
default: { default: 'bg-blue text-[#000]', disabled: 'opacity-50' }, default: { default: 'bg-blue text-[#000]', disabled: 'opacity-50' },
primary: { default: 'bg-brand text-[#fff]', disabled: 'opacity-50' }, primary: { default: 'bg-brand text-[#fff]', disabled: 'opacity-50' },
white: { default: 'bg-[#fff] text-brand', disabled: 'text-[rgba(0,0,0,0.4)]' },
black: { default: 'bg-[#000] text-[#fff]', disabled: 'opacity-10' }, black: { default: 'bg-[#000] text-[#fff]', disabled: 'opacity-10' },
}; };

@ -7,7 +7,7 @@ export { default as Category } from './Category';
export { default as ContributorCard } from './ContributorCard'; export { default as ContributorCard } from './ContributorCard';
// Login // Login
export { default as LoginModal } from './Header/LoginModal'; export { default as LoginModal } from './Login/LoginModal';
export { default as LoginForm } from './Login/LoginForm'; export { default as LoginForm } from './Login/LoginForm';
// Journal // Journal

@ -41,7 +41,7 @@ export const apiUserLogin = async (params: {
* @description token * @description token
*/ */
export const apiAutoLogin = async (params: { deviceId: string; deviceBrand?: string }) => { export const apiAutoLogin = async (params: { deviceId: string; deviceBrand?: string }) => {
const result: FetchResponse<string> = await clientHttp.post('http://localhost:3001/api/user/autoLogin', params); const result: FetchResponse<string> = await clientHttp.post('/queyueapi/user/autoLogin', params);
return result; return result;
}; };

@ -21,11 +21,11 @@ interface UserState {
/** 更新用户信息 */ /** 更新用户信息 */
setUserInfo: (parmas: UserInfo) => void; setUserInfo: (parmas: UserInfo) => void;
/** 用户登录 */ /** 用户登录 */
userLogin: (params: TRequestLogin) => Promise<boolean>; userLogin: (params: TRequestLogin) => Promise<FetchResponse<string>>;
/** 登出 */ /** 登出 */
userLogout: () => void; userLogout: () => void;
/** 获取用户信息 */ /** 获取用户信息 */
getUserInfo: () => Promise<boolean>; getUserInfo: () => Promise<FetchResponse<UserInfo>>;
} }
const useUserStore = create<UserState>()( const useUserStore = create<UserState>()(
@ -58,7 +58,7 @@ const useUserStore = create<UserState>()(
const getUserInfo = async () => { const getUserInfo = async () => {
const result = await apiGetMyUserInfo(); const result = await apiGetMyUserInfo();
if (result.code === 200) setUserInfo(result.data); if (result.code === 200) setUserInfo(result.data);
return result.code === 200; return result;
}; };
return { return {
@ -68,12 +68,14 @@ const useUserStore = create<UserState>()(
setUserInfo, setUserInfo,
userLogin: async (params) => { userLogin: async (params) => {
const result = await apiUserLogin(params); const result = await apiUserLogin(params);
if (result.code !== 200) return false; if (result.code === 200) {
await setCookie(null, 'token', result.data, { setCookie(null, 'token', result.data, {
maxAge: 7 * 24 * 60 * 60, maxAge: 7 * 24 * 60 * 60,
path: '/', path: '/',
}); });
return await getUserInfo(); await getUserInfo();
}
return result;
}, },
userLogout: async () => { userLogout: async () => {
apiUserLogout(); apiUserLogout();

@ -6,7 +6,7 @@ const logout = () => {
apiUserLogout(); apiUserLogout();
localStorage.clear(); localStorage.clear();
destroyCookie(null, 'token'); destroyCookie(null, 'token');
window.location.href = '/'; window.location.reload();
}; };
export default logout; export default logout;

Loading…
Cancel
Save