RuleModal.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React, { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
  2. import {
  3. Modal,
  4. ScrollView,
  5. StyleSheet,
  6. Text,
  7. TouchableOpacity,
  8. View
  9. } from 'react-native';
  10. import { getParamConfig } from '@/services/user';
  11. export interface RuleModalRef {
  12. show: () => void;
  13. close: () => void;
  14. }
  15. export const RuleModal = forwardRef<RuleModalRef>((_, ref) => {
  16. const [visible, setVisible] = useState(false);
  17. const [rules, setRules] = useState('');
  18. useImperativeHandle(ref, () => ({
  19. show: () => {
  20. setVisible(true);
  21. loadRules();
  22. },
  23. close: () => setVisible(false),
  24. }));
  25. const loadRules = async () => {
  26. try {
  27. const res = await getParamConfig('show_rule');
  28. setRules(res?.data || '暂无规则说明');
  29. } catch (error) {
  30. console.error('加载规则失败:', error);
  31. }
  32. };
  33. useEffect(() => {
  34. if (visible && !rules) loadRules();
  35. }, [visible]);
  36. return (
  37. <Modal visible={visible} transparent animationType="fade" onRequestClose={() => setVisible(false)}>
  38. <View style={styles.overlay}>
  39. <View style={styles.container}>
  40. <View style={styles.header}>
  41. <Text style={styles.title}>玩法规则</Text>
  42. <TouchableOpacity onPress={() => setVisible(false)} style={styles.closeBtn}>
  43. <Text style={styles.closeText}>×</Text>
  44. </TouchableOpacity>
  45. </View>
  46. <ScrollView style={styles.content} showsVerticalScrollIndicator={false}>
  47. <Text style={styles.ruleText}>{rules}</Text>
  48. </ScrollView>
  49. </View>
  50. </View>
  51. </Modal>
  52. );
  53. });
  54. const styles = StyleSheet.create({
  55. overlay: {
  56. flex: 1,
  57. backgroundColor: 'rgba(0,0,0,0.6)',
  58. justifyContent: 'center',
  59. alignItems: 'center',
  60. },
  61. container: {
  62. width: '85%',
  63. maxHeight: '70%',
  64. backgroundColor: '#fff',
  65. borderRadius: 15,
  66. overflow: 'hidden',
  67. },
  68. header: {
  69. flexDirection: 'row',
  70. alignItems: 'center',
  71. justifyContent: 'center',
  72. padding: 15,
  73. borderBottomWidth: 1,
  74. borderBottomColor: '#eee',
  75. backgroundColor: '#ff6600',
  76. },
  77. title: { fontSize: 16, fontWeight: '600', color: '#fff' },
  78. closeBtn: { position: 'absolute', right: 15, top: 10 },
  79. closeText: { fontSize: 24, color: '#fff' },
  80. content: { padding: 15 },
  81. ruleText: { fontSize: 14, color: '#333', lineHeight: 22 },
  82. });