login_controller.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'package:cpt_auth/modules/main/main_page.dart';
  2. import 'package:cpt_auth/modules/reset_psd/reset_psd_page.dart';
  3. import 'package:cpt_auth/modules/sign_up/sign_up_page.dart';
  4. import 'package:domain/entity/response/user_login_entity.dart';
  5. import 'package:domain/repository/auth_repository.dart';
  6. import 'package:get/get.dart';
  7. import 'package:plugin_basic/service/user_service.dart';
  8. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  9. import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
  10. import 'package:router/componentRouter/component_router_service.dart';
  11. import 'package:shared/utils/log_utils.dart';
  12. import 'package:shared/utils/util.dart';
  13. import 'login_state.dart';
  14. class LoginController extends GetxController with DioCancelableMixin {
  15. final AuthRepository authRepository = Get.find();
  16. final LoginState state = LoginState();
  17. // 切换隐藏密码框
  18. void switchPwdVisibility() {
  19. state.pwdVisibility = !state.pwdVisibility;
  20. update();
  21. }
  22. // 重置电话的错误文本
  23. void resetCodeErrorText() {
  24. state.codeErrorText = null;
  25. update();
  26. }
  27. /// 执行账号密码的普通登录
  28. void doInputLogin() {
  29. //todo 测试
  30. MainPage.startWithPopAll();
  31. return;
  32. state.codeErrorText = null;
  33. state.passwordErrorText = null;
  34. update();
  35. var codeController = state.formData['code']!['controller'];
  36. var passwordController = state.formData['password']!['controller'];
  37. state.code = codeController.text;
  38. state.password = passwordController.text;
  39. Log.d('当前待提交的 code:${state.code} password:${state.password}');
  40. if (Utils.isEmpty(state.code)) {
  41. state.codeErrorText = "The email cannot be empty!".tr;
  42. update();
  43. } else if (Utils.isEmpty(state.password)) {
  44. state.passwordErrorText = "The password cannot be empty!".tr;
  45. update();
  46. } else {
  47. //执行密码登录
  48. _request2LoginPassword();
  49. }
  50. }
  51. /// 请求接口执行登录
  52. void _request2LoginPassword() async {
  53. var result = await authRepository.userLogin(state.code, state.password, registerId: "", cancelToken: cancelToken);
  54. //处理数据
  55. if (result.isSuccess) {
  56. //保存Token,去首页
  57. _handleLoginSuccess(result.data!);
  58. } else {
  59. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  60. }
  61. }
  62. /// 登录成功的统一处理 - 去首页
  63. void _handleLoginSuccess(UserLoginEntity result) {
  64. //保存Token
  65. final token = result.token;
  66. UserService.to.setToken(token);
  67. //发送通知请求用户详情数据
  68. // bus.emit(AppConstant.eventProfile2Refresh, true);
  69. //使用SingleTask启动模式去首页
  70. ComponentRouterServices.jobService.startWithPopSignInSignOutPage();
  71. }
  72. @override
  73. void onReady() async {
  74. super.onReady();
  75. }
  76. /// 跳转登录页面
  77. void gotoSignUpPage() {
  78. // SignUpPage.startInstance();
  79. ResetPasswordPage.startInstance();
  80. }
  81. }