RuleModal.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Image } from 'expo-image';
  2. import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
  3. import {
  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. export interface RuleModalRef {
  15. show: () => void;
  16. close: () => void;
  17. }
  18. export const RuleModal = forwardRef<RuleModalRef>((_, ref) => {
  19. const [visible, setVisible] = useState(false);
  20. const [rules, setRules] = useState('');
  21. useImperativeHandle(ref, () => ({
  22. show: () => {
  23. setVisible(true);
  24. loadRules();
  25. },
  26. close: () => setVisible(false),
  27. }));
  28. const loadRules = async () => {
  29. try {
  30. const res = await getParamConfig('show_rule');
  31. // 去除HTML标签
  32. const text = res?.data?.replace(/<[^>]+>/g, '') || '平台发货零门槛,全国统一运费15元/单,满五件享快递包邮服务,默认5个工作日内完成发货。';
  33. setRules(text);
  34. } catch (error) {
  35. console.error('加载规则失败:', error);
  36. setRules('平台发货零门槛,全国统一运费15元/单,满五件享快递包邮服务,默认5个工作日内完成发货。');
  37. }
  38. };
  39. useEffect(() => {
  40. if (visible && !rules) loadRules();
  41. }, [visible]);
  42. return (
  43. <Modal visible={visible} transparent animationType="fade" onRequestClose={() => setVisible(false)}>
  44. <View style={styles.overlay}>
  45. <View style={styles.container}>
  46. {/* 关闭按钮 */}
  47. <TouchableOpacity onPress={() => setVisible(false)} style={styles.closeBtn}>
  48. <Image source={{ uri: Images.common.closeBut }} style={styles.closeIcon} contentFit="contain" />
  49. </TouchableOpacity>
  50. {/* 内容区域 */}
  51. <ImageBackground
  52. source={{ uri: Images.mine.dialogContentBg }}
  53. style={styles.contentBg}
  54. resizeMode="stretch"
  55. >
  56. <Text style={styles.title}>玩法规则</Text>
  57. <ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
  58. <Text style={styles.ruleText}>{rules}</Text>
  59. </ScrollView>
  60. </ImageBackground>
  61. </View>
  62. </View>
  63. </Modal>
  64. );
  65. });
  66. const styles = StyleSheet.create({
  67. overlay: {
  68. flex: 1,
  69. backgroundColor: 'rgba(0,0,0,0.6)',
  70. justifyContent: 'center',
  71. alignItems: 'center',
  72. },
  73. container: {
  74. width: '90%',
  75. alignItems: 'center',
  76. },
  77. closeBtn: {
  78. position: 'absolute',
  79. right: 15,
  80. top: 20,
  81. zIndex: 10,
  82. width: 35,
  83. height: 35,
  84. },
  85. closeIcon: {
  86. width: 35,
  87. height: 35,
  88. },
  89. contentBg: {
  90. width: '100%',
  91. height: 400,
  92. paddingTop: 75,
  93. paddingHorizontal: 30,
  94. paddingBottom: 30,
  95. },
  96. title: {
  97. fontSize: 18,
  98. fontWeight: 'bold',
  99. color: '#333',
  100. textAlign: 'center',
  101. marginBottom: 15,
  102. },
  103. scrollView: {
  104. flex: 1,
  105. },
  106. ruleText: {
  107. fontSize: 14,
  108. color: '#666',
  109. lineHeight: 24,
  110. },
  111. });