flutter_integrated_whb/lib/pages/mine/mine_set_pwd_page.dart

248 lines
8.3 KiB
Dart
Raw Normal View History

2025-07-16 08:38:10 +08:00
import 'dart:convert';
2025-07-11 11:03:21 +08:00
import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/custom_button.dart';
2025-09-05 09:27:46 +08:00
import 'package:qhd_prevention/customWidget/toast_util.dart';
2025-07-11 11:03:21 +08:00
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:flutter/material.dart';
import 'package:qhd_prevention/tools/h_colors.dart';
2025-07-16 08:38:10 +08:00
import 'package:shared_preferences/shared_preferences.dart';
import '../../http/ApiService.dart';
import '../login_page.dart';
2025-07-11 11:03:21 +08:00
class MineSetPwdPage extends StatefulWidget {
2025-09-08 17:53:44 +08:00
const MineSetPwdPage(this.type, {super.key});
2025-07-11 11:03:21 +08:00
2025-09-08 17:53:44 +08:00
final String type;
2025-07-11 11:03:21 +08:00
@override
State<MineSetPwdPage> createState() => _MineSetPwdPageState();
}
class _MineSetPwdPageState extends State<MineSetPwdPage> {
final _oldPwdController = TextEditingController();
final _newPwdController = TextEditingController();
final _confirmPwdController = TextEditingController();
2025-09-09 20:08:10 +08:00
String textString="为了您的账户安全,请确保密码长度为 8-18 位,必须包含大小写字母+数字+特殊字符,例如:Aa@123456";
@override
void initState() {
// TODO: implement initState
super.initState();
switch(widget.type){
case "0":
textString="为了您的账户安全,请确保密码长度为 8-18 位,必须包含大小写字母+数字+特殊字符,例如:Aa@123456";
break;
case "1":
textString="检测到您的密码为弱密码,请修改密码后重新登录。为了您的账户安全,请确保密码长度为 8-18 位,必须包含大小写字母+数字+特殊字符,例如:Aa@123456";
break;
case "2":
textString="检测到您30天内未修改密码请修改密码后重新登录。为了您的账户安全请确保密码长度为 8-18 位,必须包含大小写字母+数字+特殊字符,例如:Aa@123456";
break;
case "3":
textString="检测到您的密码为弱密码,请修改密码后重新登录。为了您的账户安全,请确保密码长度为 8-18 位,必须包含大小写字母+数字+特殊字符,例如:Aa@123456";
break;
case "4":
textString="检测到您30天内未修改密码请修改密码后重新登录。为了您的账户安全请确保密码长度为 8-18 位,必须包含大小写字母+数字+特殊字符,例如:Aa@123456";
break;
}
}
2025-07-11 11:03:21 +08:00
@override
void dispose() {
_oldPwdController.dispose();
_newPwdController.dispose();
_confirmPwdController.dispose();
super.dispose();
}
void _handleSubmit() {
final oldPwd = _oldPwdController.text.trim();
final newPwd = _newPwdController.text.trim();
final confirmPwd = _confirmPwdController.text.trim();
2025-09-05 09:27:46 +08:00
if (oldPwd.isEmpty) {
2025-09-08 17:53:44 +08:00
ToastUtil.showNormal(context, '请输入当前密码');
// _showMessage('请填写当前密码');
2025-07-11 11:03:21 +08:00
return;
}
2025-09-05 09:27:46 +08:00
if (newPwd.isEmpty) {
2025-09-08 17:53:44 +08:00
ToastUtil.showNormal(context, '请输入新密码');
2025-09-05 09:27:46 +08:00
// _showMessage('请填写新密码');
return;
}
if (confirmPwd.isEmpty) {
2025-09-08 17:53:44 +08:00
ToastUtil.showNormal(context, '请输入确认密码');
2025-09-05 09:27:46 +08:00
// _showMessage('请填写确认新密码');
return;
}
// if (oldPwd.isEmpty || newPwd.isEmpty || confirmPwd.isEmpty) {
// _showMessage('请填写完整所有字段');
// return;
// }
2025-07-11 11:03:21 +08:00
if (newPwd != confirmPwd) {
2025-09-09 20:08:10 +08:00
ToastUtil.showNormal(context, '新密码和确认密码两次输入的密码不一致');
2025-09-05 09:27:46 +08:00
// _showMessage('新密码与确认密码不一致');
2025-07-11 11:03:21 +08:00
return;
}
// 示例验证:密码复杂度(实际可用正则加强)
2025-09-08 17:53:44 +08:00
if (newPwd.length < 8 ) {
2025-09-09 20:08:10 +08:00
ToastUtil.showNormal(context, '新密码需要大于8位');
2025-09-08 17:53:44 +08:00
// _showMessage('密码长度需在8-18位之间');
return;
}
if ( newPwd.length > 18) {
2025-09-09 20:08:10 +08:00
ToastUtil.showNormal(context, '新密码需要小于18位');
2025-09-05 09:27:46 +08:00
// _showMessage('密码长度需在8-18位之间');
2025-07-11 11:03:21 +08:00
return;
}
2025-09-08 15:11:24 +08:00
// final RegExp regex = RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,18}$');
if (!isPasswordValid(newPwd)) {
2025-09-09 20:08:10 +08:00
ToastUtil.showNormal(context, '新密码必须包含大小字母,小写字母数字和特殊符号。');
2025-09-08 15:11:24 +08:00
// _showMessage('密码长度为6-18位必须包含大小字母小写字母数字和特殊符号。');
return;
}
2025-07-16 08:38:10 +08:00
_changePass(oldPwd,newPwd);
2025-07-11 11:03:21 +08:00
}
2025-09-08 15:11:24 +08:00
bool isPasswordValid(String password) {
// 定义各个规则的正则表达式
final hasUpperCase = RegExp(r'[A-Z]');
final hasLowerCase = RegExp(r'[a-z]');
final hasNumber = RegExp(r'[0-9]');
final hasSpecialChar = RegExp(r'[!@#$%^&*(),.?":{}|<>]'); // 根据需要调整特殊符号
// 检查是否同时满足所有条件
return hasUpperCase.hasMatch(password) &&
hasLowerCase.hasMatch(password) &&
hasNumber.hasMatch(password) &&
hasSpecialChar.hasMatch(password);
}
2025-07-11 11:03:21 +08:00
void _showMessage(String msg) {
2025-09-15 15:54:03 +08:00
ToastUtil.showNormal(context, msg);
2025-07-11 11:03:21 +08:00
}
Widget _buildPwdField(String hintText, TextEditingController controller) {
return Column(
children: [
TextField(
controller: controller,
obscureText: true,
decoration: InputDecoration(
hintText: hintText,
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(vertical: 10),
),
),
const Divider(height: 1, color: Colors.grey),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: MyAppbar(title: '修改密码'),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'修改密码',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 30),
2025-09-08 17:53:44 +08:00
_buildPwdField('当前密码', _oldPwdController),
2025-07-11 11:03:21 +08:00
const SizedBox(height: 20),
_buildPwdField('新密码', _newPwdController),
const SizedBox(height: 20),
_buildPwdField('确认新密码', _confirmPwdController),
const SizedBox(height: 15),
2025-09-09 20:08:10 +08:00
Text(
textString,
2025-07-11 11:03:21 +08:00
style: TextStyle(color: Colors.grey, fontSize: 13),
),
const SizedBox(height: 30,),
SizedBox(
width: double.infinity,
height: 48,
2025-07-16 08:38:10 +08:00
child: CustomButton(
onPressed: () {
_handleSubmit();
},
text: "提交", backgroundColor: Colors.blue),
2025-07-11 11:03:21 +08:00
),
],
),
),
),
);
}
2025-07-16 08:38:10 +08:00
Future<void> _changePass(String oldPwd, String newPwd) async {
try {
// “我的工作” 数量
final raw = await ApiService.changePassWord(oldPwd, newPwd);
// final hidCount = raw['hidCount'] as Map<String, dynamic>;
// print(hidCount);
if (raw['result'] == 'success') {
2025-09-08 17:53:44 +08:00
ToastUtil.showNormal(context, '新密码修改成功!');
2025-09-05 09:27:46 +08:00
// _showMessage('密码修改成功'); // 这里换成实际调用接口逻辑
2025-07-16 08:38:10 +08:00
Navigator.pop(context,true);
// 清除用户登录状态
await _clearUserSession();
// 跳转到登录页并清除所有历史路由
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => LoginPage()),
(Route<dynamic> route) => false, // 移除所有历史路由
);
2025-09-08 15:11:24 +08:00
}else if (raw['result'] == 'usererror'){
2025-09-08 17:53:44 +08:00
ToastUtil.showNormal(context, '当前密码密码有误');
2025-09-08 15:11:24 +08:00
// _showMessage('密码修改失败');
2025-07-16 08:38:10 +08:00
}else{
2025-09-08 17:53:44 +08:00
ToastUtil.showNormal(context, '登录错误!请联系管理员');
2025-09-05 09:27:46 +08:00
// _showMessage('密码修改失败');
2025-07-16 08:38:10 +08:00
}
} catch (e) {
// 出错时可以 Toast 或者在页面上显示错误状态
print('加载首页数据失败:$e');
2025-09-08 17:53:44 +08:00
ToastUtil.showNormal(context, '登录错误!请联系管理员');
2025-09-05 09:27:46 +08:00
// _showMessage('密码修改失败');
2025-07-16 08:38:10 +08:00
}
}
Future<void> _clearUserSession() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove('isLoggedIn'); // 清除登录状态
}
2025-07-11 11:03:21 +08:00
}
2025-07-16 08:38:10 +08:00