diff --git a/lib/common/request/apis.dart b/lib/common/request/apis.dart index 4429056..3917e32 100644 --- a/lib/common/request/apis.dart +++ b/lib/common/request/apis.dart @@ -1,3 +1,5 @@ +part of 'request_client.dart'; + class Apis { /// app初始化配置信息 // GET /system/app/config @@ -19,6 +21,15 @@ class Apis { /// 发送验证码 static const String sendSmsCode = 'system/send/code'; + /// 初始化密码 + static const String initPassword = 'student/init/password'; + + /// 修改密码 + static const String changePassword = 'student/init/password'; + + /// 忘记密码 + static const String resetPassword = 'student/change/password'; + /// 课程模块 // GET /home/courseModule // 接口地址:https://app.apifox.com/link/project/2684751/apis/api-89897663 diff --git a/lib/common/request/dao/home_dao.dart b/lib/common/request/dao/home_dao.dart index 8ed0f60..3bea2fb 100644 --- a/lib/common/request/dao/home_dao.dart +++ b/lib/common/request/dao/home_dao.dart @@ -1,5 +1,3 @@ -import 'package:flutter/foundation.dart'; -import 'package:wow_english/common/request/apis.dart'; import 'package:wow_english/common/request/request_client.dart'; import 'package:wow_english/models/course_entity.dart'; @@ -18,10 +16,7 @@ class HomeDao { if (moduleId.isNotEmpty) { mapData['moduleId'] = moduleId; } - var data = await requestClient.get( - Apis.courseLesson, - queryParameters: mapData - ); + var data = await requestClient.get(Apis.courseLesson, queryParameters: mapData); return data; } } diff --git a/lib/common/request/dao/listen_dao.dart b/lib/common/request/dao/listen_dao.dart index 8a246e3..5d6e8dd 100644 --- a/lib/common/request/dao/listen_dao.dart +++ b/lib/common/request/dao/listen_dao.dart @@ -1,4 +1,3 @@ -import 'package:wow_english/common/request/apis.dart'; import 'package:wow_english/common/request/request_client.dart'; import 'package:wow_english/models/follow_read_entity.dart'; import 'package:wow_english/models/listen_entity.dart'; @@ -15,4 +14,4 @@ class ListenDao { var data = await requestClient.get>(Apis.followRead); return data; } -} \ No newline at end of file +} diff --git a/lib/common/request/dao/user_dao.dart b/lib/common/request/dao/user_dao.dart index 88e7625..b5f4f66 100644 --- a/lib/common/request/dao/user_dao.dart +++ b/lib/common/request/dao/user_dao.dart @@ -1,7 +1,6 @@ import 'package:wow_english/common/core/user_util.dart'; import 'package:wow_english/models/user_entity.dart'; -import '../apis.dart'; import '../request_client.dart'; class UserDao { @@ -32,6 +31,27 @@ class UserDao { await requestClient.post(Apis.sendSmsCode, data: params); } + /// 设置密码,初始化密码 + /// [password] 密码 + static Future initPassword(String password) async { + final params = {'password': password}; + await requestClient.post(Apis.initPassword, data: params); + } + + /// 修改密码 + /// [password] 密码 + static Future changePassword(String password) async { + final params = {'password': password}; + await requestClient.post(Apis.changePassword, data: params); + } + + /// 忘记密码 + /// [password] 密码 + static Future resetPassword(String phoneNum, String password, String smsCode) async { + final params = {'phoneNum': phoneNum, 'password': password, 'code': smsCode}; + await requestClient.post(Apis.resetPassword, data: params); + } + /// 获取用户信息 static Future getUserInfo() async { return await requestClient.post(Apis.getUserInfo); diff --git a/lib/common/request/exception.dart b/lib/common/request/exception.dart index 855902c..ae0c422 100644 --- a/lib/common/request/exception.dart +++ b/lib/common/request/exception.dart @@ -84,6 +84,11 @@ class ApiException implements Exception { apiException.stackInfo = exception?.toString(); return apiException; } + + @override + String toString() { + return 'ApiException{code: $code, message: $message, stackInfo: $stackInfo}'; + } } /// 请求错误 diff --git a/lib/common/request/request_client.dart b/lib/common/request/request_client.dart index b7721ee..bf67733 100644 --- a/lib/common/request/request_client.dart +++ b/lib/common/request/request_client.dart @@ -11,6 +11,8 @@ import 'config.dart'; import 'exception.dart'; import 'token_interceptor.dart'; +part 'apis.dart'; + RequestClient requestClient = RequestClient(); class RequestClient { diff --git a/lib/pages/login/setpwd/bloc/set_pwd_bloc.dart b/lib/pages/login/setpwd/bloc/set_pwd_bloc.dart index cf37538..c649bff 100644 --- a/lib/pages/login/setpwd/bloc/set_pwd_bloc.dart +++ b/lib/pages/login/setpwd/bloc/set_pwd_bloc.dart @@ -1,5 +1,8 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:wow_english/common/request/dao/user_dao.dart'; +import 'package:wow_english/common/request/exception.dart'; +import 'package:wow_english/utils/loading.dart'; import '../set_pwd_page.dart'; @@ -7,21 +10,25 @@ part 'set_pwd_event.dart'; part 'set_pwd_state.dart'; class SetPwdBloc extends Bloc { - final String? phoneNumber; final String? smsCode; late final SetPwdPageType pageType; final TextEditingController passWordFirstController = TextEditingController(); final TextEditingController passWordSecondController = TextEditingController(); + ///密码是否符合规则(第一个输入框) bool _passwordEnsure = false; + ///密码是否超过16位(第一个输入框) bool _passwordLarger = false; + ///是否显示Icon(第一个输入框) bool _showPwdIcon = false; + ///是否显示Icon(第二个输入框) bool _showCheckPwdIcon = false; + ///密码是否一致 bool _passwordCheck = true; @@ -37,14 +44,16 @@ class SetPwdBloc extends Bloc { bool get ensure => _passwordCheck && _passwordEnsure; + String get passwordText => passWordFirstController.text; + SetPwdBloc(this.phoneNumber, this.smsCode, this.pageType) : super(SetPwdInitial()) { on(_pwdEnsureTextChange); on(_pwdCheckTextChange); on(_setPassword); } - void _pwdCheckTextChange(PwdCheckEvent event,Emitter emitter) async { - if(passWordSecondController.text.isEmpty) { + void _pwdCheckTextChange(PwdCheckEvent event, Emitter emitter) async { + if (passWordSecondController.text.isEmpty) { _showCheckPwdIcon = false; emitter(PasswordCheckIconShowState()); } else { @@ -53,7 +62,7 @@ class SetPwdBloc extends Bloc { emitter(PasswordCheckIconShowState()); } } - if(passWordFirstController.text == passWordSecondController.text) { + if (passWordFirstController.text == passWordSecondController.text) { _passwordCheck = true; emitter(SetCheckPwdState()); } else { @@ -62,7 +71,7 @@ class SetPwdBloc extends Bloc { } } - void _pwdEnsureTextChange(PwdEnsureEvent event,Emitter emitter) async { + void _pwdEnsureTextChange(PwdEnsureEvent event, Emitter emitter) async { if (passWordFirstController.text.isEmpty) { if (_showPwdIcon) { _showPwdIcon = false; @@ -73,32 +82,29 @@ class SetPwdBloc extends Bloc { _showPwdIcon = true; emitter(PasswordIconShowState()); } - if(passWordSecondController.text.isNotEmpty) { + if (passWordSecondController.text.isNotEmpty) { if (passWordFirstController.text == passWordSecondController.text) { _passwordCheck = true; - } else { _passwordCheck = false; } emitter(SetCheckPwdState()); } - if (passWordFirstController.text.length >= 8 && passWordFirstController.text.length <=16) { + if (passWordFirstController.text.length >= 8 && passWordFirstController.text.length <= 16) { ///符合密码要求 - if(!_passwordEnsure) { + if (!_passwordEnsure) { _passwordEnsure = true; emitter(SetEnsurePwdState()); } - if(passWordSecondController.text.isNotEmpty) { - if (passWordFirstController.text == passWordSecondController.text) { - - } + if (passWordSecondController.text.isNotEmpty) { + if (passWordFirstController.text == passWordSecondController.text) {} } if (_passwordLarger) { _passwordLarger = false; emitter(PasswordLargeState()); } } else { - if(passWordFirstController.text.length > 16) { + if (passWordFirstController.text.length > 16) { ///超过十六位 if (!_passwordLarger) { _passwordLarger = true; @@ -110,8 +116,9 @@ class SetPwdBloc extends Bloc { emitter(PasswordLargeState()); } } + ///密码不符合要求 - if(_passwordEnsure) { + if (_passwordEnsure) { _passwordEnsure = false; emitter(SetEnsurePwdState()); } @@ -119,7 +126,37 @@ class SetPwdBloc extends Bloc { } } - void _setPassword(SetPasswordEvent event,Emitter emitter) async { - emitter(PasswordSetSuccessState()); + /// 设置密码,调接口 + void _setPassword(SetPasswordEvent event, Emitter emitter) async { + try { + await loading(() async { + switch (pageType) { + case SetPwdPageType.changePwd: + await UserDao.changePassword(passwordText); + break; + case SetPwdPageType.initPwd: + await UserDao.initPassword(passwordText); + break; + case SetPwdPageType.resetPwd: + if (phoneNumber == null || phoneNumber!.isEmpty) { + throw ApiException(ApiException.customErrorCode, '手机号为空'); + } + if (smsCode == null || smsCode!.isEmpty) { + throw ApiException(ApiException.customErrorCode, '验证码为空'); + } + await UserDao.resetPassword(phoneNumber!, passwordText, smsCode!); + break; + } + }); + emitter(PasswordSetSuccessState()); + } catch (e) { + String msg; + if (e is ApiException) { + msg = e.message ?? '未知错误'; + } else { + msg = e.toString(); + } + emitter(PasswordSetFailedState(msg)); + } } } diff --git a/lib/pages/login/setpwd/bloc/set_pwd_state.dart b/lib/pages/login/setpwd/bloc/set_pwd_state.dart index a9f5c1e..6badd9b 100644 --- a/lib/pages/login/setpwd/bloc/set_pwd_state.dart +++ b/lib/pages/login/setpwd/bloc/set_pwd_state.dart @@ -16,3 +16,9 @@ class PasswordIconShowState extends SetPwdState {} class PasswordCheckIconShowState extends SetPwdState {} class PasswordSetSuccessState extends SetPwdState {} + +class PasswordSetFailedState extends SetPwdState { + final String message; + + PasswordSetFailedState(this.message); +} diff --git a/lib/pages/login/setpwd/set_pwd_page.dart b/lib/pages/login/setpwd/set_pwd_page.dart index bf02fef..6c95065 100644 --- a/lib/pages/login/setpwd/set_pwd_page.dart +++ b/lib/pages/login/setpwd/set_pwd_page.dart @@ -4,12 +4,13 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:wow_english/common/extension/string_extension.dart'; import 'package:wow_english/common/widgets/textfield_customer_widget.dart'; import 'package:wow_english/route/route.dart'; +import 'package:wow_english/utils/toast_util.dart'; import 'bloc/set_pwd_bloc.dart'; enum SetPwdPageType { /// 第一次设置密码 - setPwd, + initPwd, /// 忘记重设密码,必传手机号和验证码 resetPwd, @@ -49,9 +50,16 @@ class _SetPassWordPageView extends StatelessWidget { Widget build(BuildContext context) { bloc = BlocProvider.of(context); return BlocListener( - listener: (context, s) { - if (s is PasswordSetSuccessState) { + listener: (context, state) { + if (state is PasswordSetSuccessState) { + if (bloc.pageType == SetPwdPageType.initPwd) { + showToast('密码设置成功'); + } else { + showToast('密码修改成功'); + } Navigator.of(context).pushNamedAndRemoveUntil(AppRouteName.home, (route) => false); + } else if (state is PasswordSetFailedState) { + state.message.toast(); } }, child: _buildSetPwdView(), @@ -62,7 +70,7 @@ class _SetPassWordPageView extends StatelessWidget { String text = ''; switch (bloc.pageType) { - case SetPwdPageType.setPwd: + case SetPwdPageType.initPwd: text = '欢迎登录wow english\n接下来请设置一下您的密码吧!'; break; case SetPwdPageType.resetPwd: diff --git a/lib/utils/loading.dart b/lib/utils/loading.dart index 94d57d1..366b04f 100644 --- a/lib/utils/loading.dart +++ b/lib/utils/loading.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; Future loading(Function block, {String loadingText = '请稍后...'}) async { @@ -7,6 +8,9 @@ Future loading(Function block, {String loadingText = '请稍后...'}) asy try { return await block(); } catch (e) { + if (kDebugMode) { + print("type=${e.runtimeType}, e=${e.toString()}"); + } rethrow; } finally { dismissLoading();