905086
2026-11-07 6697e53613d5ce562a19795ab41b19803c2bb93d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import 'dart:convert';
 
import 'package:flutter/material.dart';
import 'package:fluttergamebox/api/api_service.dart';
import 'package:fluttergamebox/common/theme.dart';
import 'package:fluttergamebox/pages/login_page.dart';
 
/// 注册账号弹窗
void showRegisterDialog(BuildContext context) {
  showDialog(
    context: context,
    barrierColor: Colors.black54,
    builder: (context) => const _AuthDialog(mode: 'register'),
  );
}
 
/// 找回密码弹窗
void showForgotDialog(BuildContext context) {
  showDialog(
    context: context,
    barrierColor: Colors.black54,
    builder: (context) => const _AuthDialog(mode: 'forgot'),
  );
}
 
class _AuthDialog extends StatefulWidget {
  final String mode;
  const _AuthDialog({required this.mode});
  @override
  State<_AuthDialog> createState() => _AuthDialogState();
}
 
class _AuthDialogState extends State<_AuthDialog> {
  final _formKey = GlobalKey<FormState>();
  final phone = TextEditingController();
  final pwd = TextEditingController();
  final pwd2 = TextEditingController();
  final captcha = TextEditingController();
  String captchaId = '';
  Image? captchaImage;
  bool _loading = false;
 
  bool get isRegister => widget.mode == 'register';
 
  @override
  void initState() {
    super.initState();
    _refreshCaptcha();
  }
 
  Future<void> _refreshCaptcha() async {
    try {
      final res = await ApiService.captcha();
      if (res['code'] == 0) {
        // 服务端返回 data URI(data:image/png;base64,xxx),解码后用 Image.memory 显示
        String b64 = res['data']['captcha_image'] as String;
        if (b64.contains(',')) {
          b64 = b64.substring(b64.indexOf(',') + 1);
        }
        final bytes = base64Decode(b64);
        setState(() {
          captchaId = res['data']['captcha_id'];
          captchaImage = Image.memory(
            bytes,
            width: double.infinity,
            height: double.infinity,
            fit: BoxFit.fill,
          );
        });
      }
    } catch (_) {}
  }
 
  Future<void> _submit() async {
    if (phone.text.trim().isEmpty ||
        pwd.text.isEmpty ||
        pwd2.text.isEmpty ||
        captcha.text.isEmpty) {
      showToast(context, '请填写完整信息', error: true);
      return;
    }
    if (pwd.text != pwd2.text) {
      showToast(context, '两次输入的密码不一致', error: true);
      return;
    }
    setState(() => _loading = true);
    try {
      final res = isRegister
          ? await ApiService.register({
              'phone': phone.text.trim(),
              'password': pwd.text,
              'confirm': pwd2.text,
              'captcha_id': captchaId,
              'captcha': captcha.text,
            })
          : await ApiService.resetPassword({
              'phone': phone.text.trim(),
              'password': pwd.text,
              'confirm': pwd2.text,
              'captcha_id': captchaId,
              'captcha': captcha.text,
            });
      if (!mounted) return;
      if (res['code'] == 0) {
        showToast(context, res['msg'] ?? '操作成功');
        Navigator.pop(context);
      } else {
        showToast(context, res['msg'] ?? '操作失败', error: true);
        _refreshCaptcha();
      }
    } catch (e) {
      if (mounted) showToast(context, '网络错误', error: true);
    } finally {
      if (mounted) setState(() => _loading = false);
    }
  }
 
  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: const Color(0xFF1B1E2E),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      child: Container(
        width: 380,
        padding: const EdgeInsets.all(0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 标题栏
            Container(
              height: 42,
              padding: const EdgeInsets.symmetric(horizontal: 14),
              decoration: BoxDecoration(
                color: const Color(0xFF151726),
                borderRadius:
                    const BorderRadius.vertical(top: Radius.circular(8)),
              ),
              child: Row(
                children: [
                  Icon(isRegister ? Icons.person_add_alt : Icons.lock_reset,
                      size: 16, color: AppColors.textGray),
                  const SizedBox(width: 8),
                  Text(isRegister ? '注册账号' : '找回密码',
                      style: const TextStyle(
                          color: Colors.white, fontSize: 14)),
                  const Spacer(),
                  GestureDetector(
                    onTap: () => Navigator.pop(context),
                    child: const Icon(Icons.close,
                        size: 16, color: AppColors.textGray),
                  ),
                ],
              ),
            ),
            const Divider(color: Color(0xFF2A2E45), height: 1),
            Padding(
              padding: const EdgeInsets.all(24),
              child: Column(
                children: [
                  // Logo
                  ClipOval(
                    child: Image.asset('assets/images/logo.png',
                        width: 60,
                        height: 60,
                        fit: BoxFit.cover,
                        errorBuilder: (_, __, ___) => Container(
                            width: 60,
                            height: 60,
                            color: AppColors.pink,
                            child: const Center(
                                child: Text('99',
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontWeight: FontWeight.bold))))),
                  ),
                  const SizedBox(height: 24),
                  _field(phone, '手机号', Icons.phone_iphone),
                  const SizedBox(height: 14),
                  _field(pwd, isRegister ? '密码' : '请输入新密码',
                      Icons.lock_outline,
                      obscure: true),
                  const SizedBox(height: 14),
                  _field(pwd2,
                      isRegister ? '确认密码' : '请再次输入新密码',
                      Icons.lock_outline,
                      obscure: true),
                  const SizedBox(height: 14),
                  // 验证码
                  Row(
                    children: [
                      Expanded(child: _field(captcha, '图形验证码', Icons.verified_outlined)),
                      const SizedBox(width: 10),
                      GestureDetector(
                        onTap: _refreshCaptcha,
                        child: Container(
                          width: 110,
                          height: 46,
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(6),
                          ),
                          padding: const EdgeInsets.symmetric(
                              horizontal: 6, vertical: 4),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(3),
                            child: captchaImage ??
                                const Center(
                                    child: Text('加载中...',
                                        style: TextStyle(
                                            color: Colors.grey,
                                            fontSize: 11))),
                          ),
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 24),
                  SizedBox(
                    width: double.infinity,
                    height: 46,
                    child: ElevatedButton(
                      onPressed: _loading ? null : _submit,
                      style: ElevatedButton.styleFrom(
                        backgroundColor: AppColors.accent,
                        foregroundColor: Colors.white,
                        elevation: 0,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(6)),
                      ),
                      child: _loading
                          ? const SizedBox(
                              width: 18,
                              height: 18,
                              child: CircularProgressIndicator(
                                  strokeWidth: 2, color: Colors.white))
                          : Text(isRegister ? '注册' : '找回密码',
                              style: const TextStyle(fontSize: 15)),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
 
  Widget _field(TextEditingController ctrl, String hint, IconData icon,
      {bool obscure = false}) {
    return Container(
      height: 46,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(6),
      ),
      child: Row(
        children: [
          const SizedBox(width: 14),
          Icon(icon, size: 18, color: const Color(0xFF9AA0B4)),
          const SizedBox(width: 10),
          Expanded(
            child: TextField(
              controller: ctrl,
              obscureText: obscure,
              style: const TextStyle(color: Color(0xFF333333), fontSize: 14),
              decoration: InputDecoration(
                hintText: hint,
                hintStyle: const TextStyle(
                    color: Color(0xFFAAAAAA), fontSize: 14),
                border: InputBorder.none,
                isDense: true,
              ),
            ),
          ),
        ],
      ),
    );
  }
}