import { signIn, useAuthContext } from '@/utils/auth';
import { Toast } from '@ant-design/react-native';
import { Ionicons } from '@expo/vector-icons';
import { router, useLocalSearchParams } from 'expo-router';
import React, { useState } from 'react';
import {
KeyboardAvoidingView,
Platform,
Pressable,
ScrollView,
StyleSheet,
Text,
TextInput,
View,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
function FieldLabel({ children }: { children: React.ReactNode }) {
return (
{children}
);
}
export default function SignInScreen({
callback,
}: {
callback?: (signin: boolean) => void;
}) {
const [authMode, setAuthMode] = useState<'sms' | 'password'>('sms');
const [agreed, setAgreed] = useState(false);
const [loading, setLoading] = useState(false);
const [phone, setPhone] = useState('');
const [code, setCode] = useState('');
const [password, setPassword] = useState('');
const { redirectTo } = useLocalSearchParams<{ redirectTo?: string }>();
const { setToken } = useAuthContext();
const handleSendCode = () => {
if (phone.trim().length !== 11) {
Toast.fail('请先输入 11 位手机号');
return;
}
Toast.success('验证码已发送');
};
const handleLogin = async () => {
if (!agreed) {
Toast.fail('请先阅读并同意协议');
callback?.(false);
return;
}
if (phone.trim().length !== 11) {
Toast.fail('请输入正确的手机号');
callback?.(false);
return;
}
if (authMode === 'sms' && code.trim().length !== 6) {
Toast.fail('请输入 6 位验证码');
callback?.(false);
return;
}
if (authMode === 'password' && password.trim().length < 6) {
Toast.fail('请输入不少于 6 位的密码');
callback?.(false);
return;
}
setLoading(true);
const toastKey = Toast.loading('正在登录...');
try {
const token = await signIn(1);
setToken(token);
Toast.success('登录成功');
callback?.(true);
const nextRoute =
typeof redirectTo === 'string' && redirectTo.startsWith('/')
? redirectTo
: '/';
router.replace(nextRoute as never);
} catch (error) {
console.error('登录失败:', error);
Toast.fail('登录失败,请稍后重试');
callback?.(false);
} finally {
setLoading(false);
Toast.remove(toastKey);
}
};
return (
欢迎回来
登录贷款助手,开启您的财务管理之旅
{[
['sms', '验证码登录'],
['password', '密码登录'],
].map(([mode, label]) => {
const active = authMode === mode;
return (
setAuthMode(mode as 'sms' | 'password')}
className={`flex-1 rounded-xl px-4 py-3 ${
active ? 'bg-surface-container-lowest' : ''
}`}
style={({ pressed }) => ({
opacity: loading ? 0.5 : pressed ? 0.86 : 1,
transform: [{ scale: pressed ? 0.99 : 1 }],
})}
>
{label}
);
})}
手机号码
+86
{authMode === 'sms' ? '验证码' : '密码'}
{authMode === 'sms' ? (
({
opacity: loading ? 0.5 : pressed ? 0.72 : 1,
})}
>
获取验证码
) : (
)}
({
opacity: loading ? 0.6 : pressed ? 0.88 : 1,
transform: [{ scale: pressed ? 0.985 : 1 }],
})}
>
{loading ? '登录中...' : '立即登录'}
注册账号
遇到问题?
其他方式登录
{[
['chatbubble-ellipses-outline', '#6b7280'],
['logo-apple', '#6b7280'],
].map(([icon, color]) => (
({
opacity: pressed ? 0.8 : 1,
transform: [{ scale: pressed ? 0.94 : 1 }],
})}
>
))}
setAgreed((value) => !value)}
hitSlop={8}
className="pt-1"
>
{agreed ? (
) : null}
登录即代表您已阅读并同意
《用户服务协议》
、
《隐私政策》
,以及授权该应用获取您的公开信息。
);
}