1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import 'package:domain/entity/response/user_login_entity.dart';
- import 'package:domain/repository/auth_repository.dart';
- import 'package:get/get.dart';
- import 'package:plugin_basic/service/user_service.dart';
- import 'package:plugin_platform/engine/toast/toast_engine.dart';
- import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
- import 'package:router/componentRouter/component_router_service.dart';
- import 'package:shared/utils/log_utils.dart';
- import 'package:shared/utils/util.dart';
- import 'login_state.dart';
- class LoginController extends GetxController with DioCancelableMixin {
- final AuthRepository authRepository = Get.find();
- final LoginState state = LoginState();
- bool pwdVisibility = false; //是否明文展示密码
- String? codeErrorText; //表单的错误信息展示
- String? passwordErrorText;
- String? code; //待提交的表单数据
- String? password;
- // 切换隐藏密码框
- void switchPwdVisibility() {
- pwdVisibility = !pwdVisibility;
- update();
- }
- // 重置电话的错误文本
- void resetCodeErrorText() {
- codeErrorText = null;
- update();
- }
- /// 执行账号密码的普通登录
- void doInputLogin() {
- codeErrorText = null;
- passwordErrorText = null;
- update();
- var codeController = state.formData['code']!['controller'];
- var passwordController = state.formData['password']!['controller'];
- code = codeController.text;
- password = passwordController.text;
- Log.d('当前待提交的 code:$code password:$password');
- if (Utils.isEmpty(code)) {
- codeErrorText = "The login code cannot be empty!".tr;
- update();
- } else if (Utils.isEmpty(password)) {
- passwordErrorText = "The password cannot be empty!".tr;
- update();
- } else {
- //执行密码登录
- _request2LoginPassword();
- }
- }
- /// 请求接口执行登录
- void _request2LoginPassword() async {
- var result = await authRepository.userLogin(code, password, registerId: "", cancelToken: cancelToken);
- //处理数据
- if (result.isSuccess) {
- //保存Token,去首页
- _handleLoginSuccess(result.data!);
- } else {
- ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
- }
- }
- /// 登录成功的统一处理 - 去首页
- void _handleLoginSuccess(UserLoginEntity result) {
- //保存Token
- final token = result.token;
- UserService.to.setToken(token);
- //发送通知请求用户详情数据
- // bus.emit(AppConstant.eventProfile2Refresh, true);
- //使用SingleTask启动模式去首页
- ComponentRouterServices.jobService.startWithPopSignInSignOutPage();
- }
- @override
- void onReady() async {
- super.onReady();
- }
- }
|