RuleModal.tsx 3.6 KB

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