RuleModal.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Image } from 'expo-image';
  2. import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
  3. import {
  4. Dimensions,
  5. ImageBackground,
  6. Modal,
  7. ScrollView,
  8. StyleSheet,
  9. Text,
  10. TouchableOpacity,
  11. View,
  12. } from 'react-native';
  13. import { Images } from '@/constants/images';
  14. import { getParamConfig } from '@/services/user';
  15. const { width: SCREEN_WIDTH } = Dimensions.get('window');
  16. export interface RuleModalRef {
  17. show: () => void;
  18. close: () => void;
  19. }
  20. export const RuleModal = forwardRef<RuleModalRef>((_, ref) => {
  21. const [visible, setVisible] = useState(false);
  22. const [rules, setRules] = useState('');
  23. useImperativeHandle(ref, () => ({
  24. show: () => {
  25. setVisible(true);
  26. loadRules();
  27. },
  28. close: () => setVisible(false),
  29. }));
  30. const loadRules = async () => {
  31. try {
  32. const res = await getParamConfig('baoxiang_rule');
  33. // 增强清洗:移除 style/script,去除 HTML 标签,并压缩多余空行
  34. const rawData = res?.data || '';
  35. const text = rawData
  36. .replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
  37. .replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
  38. .replace(/<[^>]+>/g, '')
  39. .replace(/&nbsp;/g, ' ')
  40. .replace(/\n\s*\n/g, '\n') // 将多个空行压缩为一个
  41. .trim() || '平台发货零门槛,全国统一运费15元/单,满五件享快递包邮服务,默认5个工作日内完成发货。';
  42. setRules(text);
  43. } catch (error) {
  44. console.error('加载规则失败:', error);
  45. setRules('平台发货零门槛,全国统一运费15元/单,满五件享快递包邮服务,默认5个工作日内完成发货。');
  46. }
  47. };
  48. useEffect(() => {
  49. if (visible && !rules) loadRules();
  50. }, [visible]);
  51. return (
  52. <Modal visible={visible} transparent animationType="fade" onRequestClose={() => setVisible(false)}>
  53. <View style={styles.overlay}>
  54. <View style={styles.container}>
  55. {/* 内容区域 */}
  56. <ImageBackground
  57. source={{ uri: Images.mine.dialogContentBg }}
  58. style={styles.contentBg}
  59. resizeMode="stretch"
  60. >
  61. <Text style={styles.title}>玩法规则</Text>
  62. <ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
  63. <Text style={styles.ruleText}>{rules}</Text>
  64. </ScrollView>
  65. </ImageBackground>
  66. {/* 关闭按钮移至下方 */}
  67. <TouchableOpacity onPress={() => setVisible(false)} style={styles.closeBtn}>
  68. <Image source={{ uri: Images.common.closeBut }} style={styles.closeIcon} contentFit="contain" />
  69. </TouchableOpacity>
  70. </View>
  71. </View>
  72. </Modal>
  73. );
  74. });
  75. const styles = StyleSheet.create({
  76. overlay: {
  77. flex: 1,
  78. backgroundColor: 'rgba(0,0,0,0.6)',
  79. justifyContent: 'center',
  80. alignItems: 'center',
  81. },
  82. container: {
  83. width: '100%',
  84. alignItems: 'center',
  85. justifyContent: 'center',
  86. },
  87. contentBg: {
  88. width: SCREEN_WIDTH * 0.85,
  89. height: SCREEN_WIDTH * 0.85 * 1.25,
  90. paddingTop: 85,
  91. paddingHorizontal: 35,
  92. paddingBottom: 50,
  93. justifyContent: 'flex-start',
  94. },
  95. closeBtn: {
  96. marginTop: 20,
  97. width: 45,
  98. height: 45,
  99. justifyContent: 'center',
  100. alignItems: 'center',
  101. },
  102. closeIcon: {
  103. width: 45,
  104. height: 45,
  105. },
  106. title: {
  107. fontSize: 18,
  108. fontWeight: 'bold',
  109. color: '#333',
  110. textAlign: 'center',
  111. marginBottom: 15,
  112. },
  113. scrollView: {
  114. flex: 1,
  115. },
  116. ruleText: {
  117. fontSize: 14,
  118. color: '#333',
  119. lineHeight: 20, // 降低行间距
  120. },
  121. });