import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:qhd_prevention/customWidget/custom_button.dart'; import 'package:qhd_prevention/customWidget/toast_util.dart'; import 'package:qhd_prevention/pages/my_appbar.dart'; import 'package:qhd_prevention/pages/user/login_page.dart'; import 'package:qhd_prevention/services/SessionService.dart'; import 'package:qhd_prevention/services/auth_service.dart'; import 'package:qhd_prevention/services/password_policy.dart'; import 'package:qhd_prevention/tools/tools.dart'; import '../../http/ApiService.dart'; class MineSetPwdPage extends StatefulWidget { const MineSetPwdPage(this.type, {super.key}); final String type; @override State createState() => _MineSetPwdPageState(); } class _MineSetPwdPageState extends State { final _formKey = GlobalKey(); final _oldPwdController = TextEditingController(); final _newPwdController = TextEditingController(); final _confirmPwdController = TextEditingController(); bool _obscureOld = true; bool _obscureNew = true; bool _obscureConfirm = true; String textString = "为了您的账户安全,请确保密码长度为 8-16 位,必须包含大小写字母、数字和特殊字符(!#@*&),例如:Aa@12345"; Map passData = {"id": '', "password": "", "newPassword": ""}; @override void initState() { super.initState(); switch (widget.type) { case "0": textString = "密码长度8-16位,且必须包含大小写字母、数字和特殊符号(!#@*&)"; break; case "1": textString = "检测到您的密码为弱密码,请修改密码后重新登录。密码长度为8-16位,且必须包含大小写字母、数字和特殊符号(!#@*&)。"; break; case "2": textString = "检测到您30天内未修改密码,请修改密码后重新登录。密码长度为8-16位,且必须包含大小写字母、数字和特殊符号(!#@*&)。"; break; case "3": textString = "检测到您的密码为弱密码,请修改密码后重新登录。密码长度为8-16位,且必须包含大小写字母、数字和特殊符号(!#@*&)。"; break; case "4": textString = "检测到您30天内未修改密码,请修改密码后重新登录。密码长度为8-16位,且必须包含大小写字母、数字和特殊符号(!#@*&)。"; break; } } @override void dispose() { _oldPwdController.dispose(); _newPwdController.dispose(); _confirmPwdController.dispose(); super.dispose(); } // 与登录页一致的输入框样式 Widget _buildRoundedInput({ required TextEditingController controller, required String hint, bool obscure = false, VoidCallback? toggleObscure, FormFieldValidator? validator, }) { return TextFormField( controller: controller, obscureText: obscure, validator: validator, decoration: InputDecoration( hintText: hint, hintStyle: const TextStyle(color: Colors.grey), filled: true, fillColor: const Color(0xFFE2EBF4), contentPadding: const EdgeInsets.symmetric( horizontal: 20, vertical: 14, ), suffixIcon: toggleObscure == null ? null : IconButton( icon: Icon( obscure ? Icons.visibility_off : Icons.visibility, color: Colors.grey, ), onPressed: toggleObscure, ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(30), borderSide: BorderSide.none, ), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(30), borderSide: BorderSide.none, ), ), style: const TextStyle(color: Colors.black), ); } void _handleSubmit() async { // 使用 Form 验证必填 if (!(_formKey.currentState?.validate() ?? false)) return; final oldPwd = _oldPwdController.text.trim(); final newPwd = _newPwdController.text.trim(); final confirmPwd = _confirmPwdController.text.trim(); // if (oldPwd == newPwd) { // ToastUtil.showNormal(context, '新密码不能喝旧密码相同'); // return; // } if (newPwd != confirmPwd) { ToastUtil.showNormal(context, '新密码和确认密码两次输入的密码不一致'); return; } final passwordError = PasswordPolicy.validate(newPwd); if (passwordError != null) { ToastUtil.showNormal(context, passwordError); return; } await _changePass(oldPwd, newPwd, confirmPwd); } Future _changePass( String oldPwd, String newPwd, String confirmPwd, ) async { LoadingDialogHelper.show(); try { passData['id'] = SessionService.instance.accountId ?? ""; passData['password'] = oldPwd; passData['newPassword'] = newPwd; passData['confirmPassword'] = newPwd; final raw = await AuthApi.changePassWord(passData); LoadingDialogHelper.hide(); if (raw['success'] == true) { ToastUtil.showNormal(context, '新密码修改成功!'); await AuthService.logout(); // 跳转到登录页并清除所有历史路由 Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (context) => const LoginPage()), (Route route) => false, ); } else { ToastUtil.showNormal(context, raw['errMessage'] ?? '请求失败,请重试'); } } catch (e) { LoadingDialogHelper.hide(); print('修改密码出错:$e'); ToastUtil.showNormal(context, '请求失败,请重试'); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: const MyAppbar(title: '修改密码'), body: SafeArea( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( '修改密码', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), ), const SizedBox(height: 30), // 当前密码 _buildRoundedInput( controller: _oldPwdController, hint: '当前密码', obscure: _obscureOld, toggleObscure: () => setState(() => _obscureOld = !_obscureOld), validator: (v) { if (v == null || v.isEmpty) return '请输入当前密码'; return null; }, ), const SizedBox(height: 20), // 新密码 _buildRoundedInput( controller: _newPwdController, hint: '新密码', obscure: _obscureNew, toggleObscure: () => setState(() => _obscureNew = !_obscureNew), validator: (v) { if (v == null || v.isEmpty) return '请输入新密码'; return null; }, ), const SizedBox(height: 20), // 确认新密码 _buildRoundedInput( controller: _confirmPwdController, hint: '确认新密码', obscure: _obscureConfirm, toggleObscure: () => setState(() => _obscureConfirm = !_obscureConfirm), validator: (v) { if (v == null || v.isEmpty) return '请输入确认密码'; return null; }, ), const SizedBox(height: 15), Text( textString, style: const TextStyle(color: Colors.red, fontSize: 13), ), const SizedBox(height: 30), SizedBox( width: double.infinity, height: 45, child: CustomButton( onPressed: _handleSubmit, text: "提交", backgroundColor: Colors.blue, ), ), ], ), ), ), ), ); } }