import { Images } from "@/constants/images"; import { getUserInfo, realNameAuth, UserInfo } from "@/services/user"; import { useFocusEffect, useRouter } from "expo-router"; import React, { useCallback, useState } from "react"; import { Alert, ImageBackground, KeyboardAvoidingView, Platform, ScrollView, StatusBar, StyleSheet, Text, TextInput, TouchableOpacity, View, } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; const NAME_REG = /^[\u4e00-\u9fa5]{2,10}$/; const ID_REG = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; const maskName = (name: string) => { if (!name) return ""; if (name.length <= 1) return name; return name[0] + "*".repeat(name.length - 1); }; const maskIdNum = (id: string) => { if (!id) return ""; if (id.length <= 8) return id; return id.slice(0, 3) + "*".repeat(id.length - 7) + id.slice(-4); }; export default function CertificationScreen() { const router = useRouter(); const insets = useSafeAreaInsets(); const [user, setUser] = useState(null); const [idName, setIdName] = useState(""); const [idNum, setIdNum] = useState(""); const [loading, setLoading] = useState(false); const [submitting, setSubmitting] = useState(false); const loadUser = useCallback(async () => { setLoading(true); try { const info = await getUserInfo(); setUser(info); } catch (e) { console.error("获取用户信息失败:", e); } finally { setLoading(false); } }, []); useFocusEffect( useCallback(() => { loadUser(); }, [loadUser]), ); const isVerified = user?.realNameFlag === 1; const handleBack = () => router.back(); const handleSubmit = async () => { const trimName = idName.trim(); const trimId = idNum.trim(); if (!trimName) { Alert.alert("提示", "请输入真实姓名"); return; } if (!NAME_REG.test(trimName)) { Alert.alert("提示", "请输入正确的姓名格式(2-10个中文字符)"); return; } if (!trimId) { Alert.alert("提示", "请输入身份证号"); return; } if (!ID_REG.test(trimId)) { Alert.alert("提示", "请输入正确的身份证号码"); return; } try { setSubmitting(true); const ok = await realNameAuth(trimName, trimId); if (ok) { Alert.alert("提示", "提交成功", [ { text: "确定", onPress: () => router.back() }, ]); } } catch (e) { console.error("提交实名认证失败:", e); } finally { setSubmitting(false); } }; return ( 实名认证 {loading ? ( 加载中... ) : isVerified ? ( <> ✓ 已完成实名认证 真实姓名 {maskName(user?.idName || "")} 身份证号 {maskIdNum(user?.idNum || "")} 实名信息已提交,不可修改 ) : ( <> 真实姓名 身份证号 ⚠️ 请确保您填写的信息真实有效,提交后将无法修改 {submitting ? "提交中..." : "提交"} )} ); } const styles = StyleSheet.create({ container: { flex: 1, }, header: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 10, height: 80, }, backBtn: { width: 40, height: 40, justifyContent: "center", alignItems: "center", }, backIcon: { fontSize: 32, color: "#fff", fontWeight: "bold", }, headerTitle: { fontSize: 16, fontWeight: "bold", color: "#fff", }, placeholder: { width: 40, }, scrollView: { flex: 1, }, content: { paddingHorizontal: 15, paddingTop: 10, paddingBottom: 30, }, card: { backgroundColor: "#fff", borderRadius: 15, padding: 20, }, loadingText: { textAlign: "center", color: "#999", paddingVertical: 40, }, verifiedBanner: { backgroundColor: "#e6f9ef", borderRadius: 8, paddingVertical: 10, paddingHorizontal: 14, marginBottom: 20, alignItems: "center", }, verifiedBannerText: { color: "#1aad5a", fontSize: 14, fontWeight: "600", }, formItem: { marginBottom: 20, }, label: { fontSize: 14, color: "#333", fontWeight: "500", marginBottom: 10, }, input: { width: "100%", height: 40, paddingHorizontal: 10, borderWidth: 1, borderColor: "#e5e5e5", borderRadius: 5, fontSize: 14, color: "#333", backgroundColor: "#f8f8f8", }, readonly: { width: "100%", height: 40, paddingHorizontal: 10, borderWidth: 1, borderColor: "#e5e5e5", borderRadius: 5, backgroundColor: "#f5f5f5", justifyContent: "center", }, readonlyText: { fontSize: 14, color: "#666", letterSpacing: 1, }, tips: { flexDirection: "row", alignItems: "flex-start", padding: 10, marginBottom: 20, backgroundColor: "#fff9e6", borderRadius: 5, borderLeftWidth: 2, borderLeftColor: "#ffdd02", }, tipsIcon: { fontSize: 14, marginRight: 6, }, tipsText: { flex: 1, fontSize: 12, color: "#ff9900", lineHeight: 18, }, submitBtn: { backgroundColor: "#FC7D2E", borderRadius: 8, paddingVertical: 13, alignItems: "center", }, submitBtnDisabled: { opacity: 0.6, }, submitBtnText: { color: "#fff", fontSize: 16, fontWeight: "600", }, });