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(); 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 _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 _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) { if (!isRegister) { // 弹出提示:联系管理员重置密码 _showContactAdminDialog(); } else { 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); } } /// 找回密码申请提交成功后的提示弹窗 void _showContactAdminDialog() { showDialog( context: context, builder: (ctx) => AlertDialog( backgroundColor: const Color(0xFF1B1E2E), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)), title: const Row( children: [ Icon(Icons.info_outline, color: AppColors.yellow, size: 20), SizedBox(width: 8), Text('申请已提交', style: TextStyle(color: Colors.white, fontSize: 15)), ], ), content: const Text('找回密码申请已提交,请联系管理员重置密码!', style: TextStyle(color: AppColors.textGray, fontSize: 13, height: 1.6)), actions: [ TextButton( onPressed: () { Navigator.pop(ctx); // 关闭提示 Navigator.pop(context, true); // 关闭找回密码弹窗 }, child: const Text('知道了', style: TextStyle(color: AppColors.accent, fontSize: 13)), ), ], ), ); } @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.person_outline, size: 16, color: Colors.white), 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: 72, height: 72, fit: BoxFit.cover, errorBuilder: (_, __, ___) => Container( width: 72, height: 72, color: AppColors.pink, child: const Center( child: Text('99', style: TextStyle( color: Colors.white, fontWeight: FontWeight.bold))))), ), const SizedBox(height: 26), _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), // 图形验证码(输入框内右侧嵌验证码图,点击图片可刷新) _field(captcha, '图形验证码', Icons.verified_outlined, suffix: GestureDetector( onTap: _refreshCaptcha, child: Container( width: 90, height: 32, margin: const EdgeInsets.only(right: 7), decoration: BoxDecoration( color: const Color(0xFF14161F), borderRadius: BorderRadius.circular(4), ), padding: const EdgeInsets.symmetric(horizontal: 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, Widget? suffix}) { 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, ), ), ), if (suffix != null) suffix, ], ), ); } }