220 lines
7.0 KiB
Dart
220 lines
7.0 KiB
Dart
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:flutter/material.dart';
|
||
import 'package:qhd_prevention/tools/h_colors.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
|
||
import '../../http/ApiService.dart';
|
||
import '../login_page.dart';
|
||
|
||
class MineSetPwdPage extends StatefulWidget {
|
||
const MineSetPwdPage(this.type, {super.key});
|
||
|
||
final String type;
|
||
@override
|
||
State<MineSetPwdPage> createState() => _MineSetPwdPageState();
|
||
}
|
||
|
||
class _MineSetPwdPageState extends State<MineSetPwdPage> {
|
||
final _oldPwdController = TextEditingController();
|
||
final _newPwdController = TextEditingController();
|
||
final _confirmPwdController = TextEditingController();
|
||
|
||
@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();
|
||
|
||
if (oldPwd.isEmpty) {
|
||
ToastUtil.showNormal(context, '请输入当前密码');
|
||
// _showMessage('请填写当前密码');
|
||
return;
|
||
}
|
||
|
||
if (newPwd.isEmpty) {
|
||
ToastUtil.showNormal(context, '请输入新密码');
|
||
// _showMessage('请填写新密码');
|
||
return;
|
||
}
|
||
|
||
if (confirmPwd.isEmpty) {
|
||
ToastUtil.showNormal(context, '请输入确认密码');
|
||
// _showMessage('请填写确认新密码');
|
||
return;
|
||
}
|
||
|
||
|
||
// if (oldPwd.isEmpty || newPwd.isEmpty || confirmPwd.isEmpty) {
|
||
// _showMessage('请填写完整所有字段');
|
||
// return;
|
||
// }
|
||
|
||
if (newPwd != confirmPwd) {
|
||
ToastUtil.showNormal(context, '两次输入的密码不一致');
|
||
// _showMessage('新密码与确认密码不一致');
|
||
return;
|
||
}
|
||
|
||
// 示例验证:密码复杂度(实际可用正则加强)
|
||
if (newPwd.length < 8 ) {
|
||
ToastUtil.showNormal(context, '密码需要大于8位');
|
||
// _showMessage('密码长度需在8-18位之间');
|
||
return;
|
||
}
|
||
|
||
if ( newPwd.length > 18) {
|
||
ToastUtil.showNormal(context, '密码需要小于18位');
|
||
// _showMessage('密码长度需在8-18位之间');
|
||
return;
|
||
}
|
||
|
||
// final RegExp regex = RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,18}$');
|
||
if (!isPasswordValid(newPwd)) {
|
||
ToastUtil.showNormal(context, '必须包含大小字母,小写字母数字和特殊符号。');
|
||
// _showMessage('密码长度为6-18位,必须包含大小字母,小写字母数字和特殊符号。');
|
||
return;
|
||
}
|
||
|
||
_changePass(oldPwd,newPwd);
|
||
|
||
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
void _showMessage(String msg) {
|
||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
|
||
}
|
||
|
||
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),
|
||
_buildPwdField('当前密码', _oldPwdController),
|
||
const SizedBox(height: 20),
|
||
_buildPwdField('新密码', _newPwdController),
|
||
const SizedBox(height: 20),
|
||
_buildPwdField('确认新密码', _confirmPwdController),
|
||
const SizedBox(height: 15),
|
||
const Text(
|
||
'为了您的账户安全,请确保密码长度为 8-18 位,必须包含大小写字母+数字+特殊字符,例如:Aa@123456',
|
||
style: TextStyle(color: Colors.grey, fontSize: 13),
|
||
),
|
||
const SizedBox(height: 30,),
|
||
SizedBox(
|
||
width: double.infinity,
|
||
height: 48,
|
||
child: CustomButton(
|
||
onPressed: () {
|
||
_handleSubmit();
|
||
},
|
||
text: "提交", backgroundColor: Colors.blue),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
|
||
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') {
|
||
ToastUtil.showNormal(context, '新密码修改成功!');
|
||
// _showMessage('密码修改成功'); // 这里换成实际调用接口逻辑
|
||
Navigator.pop(context,true);
|
||
|
||
// 清除用户登录状态
|
||
await _clearUserSession();
|
||
// 跳转到登录页并清除所有历史路由
|
||
Navigator.pushAndRemoveUntil(
|
||
context,
|
||
MaterialPageRoute(builder: (context) => LoginPage()),
|
||
(Route<dynamic> route) => false, // 移除所有历史路由
|
||
);
|
||
|
||
}else if (raw['result'] == 'usererror'){
|
||
ToastUtil.showNormal(context, '当前密码密码有误');
|
||
// _showMessage('密码修改失败');
|
||
}else{
|
||
ToastUtil.showNormal(context, '登录错误!请联系管理员');
|
||
// _showMessage('密码修改失败');
|
||
}
|
||
|
||
} catch (e) {
|
||
// 出错时可以 Toast 或者在页面上显示错误状态
|
||
print('加载首页数据失败:$e');
|
||
ToastUtil.showNormal(context, '登录错误!请联系管理员');
|
||
// _showMessage('密码修改失败');
|
||
}
|
||
}
|
||
|
||
Future<void> _clearUserSession() async {
|
||
final prefs = await SharedPreferences.getInstance();
|
||
await prefs.remove('isLoggedIn'); // 清除登录状态
|
||
}
|
||
|
||
}
|
||
|
||
|