import { useLocalSearchParams, useRouter } from 'expo-router'; import React, { useEffect, useState } from 'react'; import { ActivityIndicator, ScrollView, StatusBar, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { getParamConfig } from '@/services/user'; // 简单的HTML标签清理函数 const stripHtmlTags = (html: string): string => { if (!html) return ''; // 移除HTML标签,保留文本内容 return html .replace(/]*>[\s\S]*?<\/style>/gi, '') .replace(/]*>[\s\S]*?<\/script>/gi, '') .replace(/<[^>]+>/g, '\n') .replace(/ /g, ' ') .replace(/</g, '<') .replace(/>/g, '>') .replace(/&/g, '&') .replace(/"/g, '"') .replace(/\n\s*\n/g, '\n\n') .trim(); }; export default function AgreementScreen() { const router = useRouter(); const insets = useSafeAreaInsets(); const params = useLocalSearchParams<{ type: string }>(); const [loading, setLoading] = useState(true); const [title, setTitle] = useState(''); const [content, setContent] = useState(''); useEffect(() => { loadContent(); }, [params.type]); const loadContent = async () => { try { setLoading(true); const type = params.type || 'user.html'; console.log('加载协议类型:', type); const res = await getParamConfig(type) as any; console.log('协议内容返回:', res); if (res) { // API返回的数据结构: { title: string, data: string (HTML内容) } const titleText = res.title || (type === 'user.html' ? '用户协议' : '隐私协议'); setTitle(titleText); // 清理HTML标签,只保留纯文本 const rawContent = res.data || res || ''; setContent(stripHtmlTags(typeof rawContent === 'string' ? rawContent : JSON.stringify(rawContent))); } } catch (error) { console.error('获取协议内容失败:', error); } finally { setLoading(false); } }; const handleBack = () => { router.back(); }; return ( {/* 顶部导航 */} {title || '协议'} {loading ? ( ) : content ? ( {content} ) : ( 暂无内容 )} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#333', }, header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 10, height: 80, backgroundColor: '#333', }, 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, }, loadingBox: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', }, scrollView: { flex: 1, backgroundColor: '#fff', }, scrollContent: { padding: 15, paddingBottom: 50, }, contentText: { fontSize: 14, lineHeight: 22, color: '#333', }, emptyBox: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', }, emptyText: { fontSize: 14, color: '#999', }, });