You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.8 KiB
92 lines
2.8 KiB
2 weeks ago
|
import 'package:flutter/material.dart';
|
||
2 weeks ago
|
import 'package:get/get.dart';
|
||
2 weeks ago
|
import 'package:problem_check_system/data/models/auth_model.dart';
|
||
2 weeks ago
|
import 'package:problem_check_system/app/routes/app_routes.dart';
|
||
2 weeks ago
|
import 'package:problem_check_system/data/repositories/auth_repository.dart';
|
||
2 weeks ago
|
|
||
2 weeks ago
|
class LoginController extends GetxController {
|
||
2 weeks ago
|
final AuthRepository _authRepository;
|
||
|
final TextEditingController usernameController = TextEditingController();
|
||
|
final TextEditingController passwordController = TextEditingController();
|
||
2 weeks ago
|
|
||
|
final isLoading = false.obs;
|
||
|
final rememberPassword = false.obs;
|
||
|
|
||
2 weeks ago
|
LoginController({required AuthRepository authRepository})
|
||
2 weeks ago
|
: _authRepository = authRepository;
|
||
2 weeks ago
|
|
||
2 weeks ago
|
@override
|
||
|
void onInit() {
|
||
|
super.onInit();
|
||
2 weeks ago
|
_loadRememberedUser();
|
||
2 weeks ago
|
}
|
||
|
|
||
2 weeks ago
|
void _loadRememberedUser() {
|
||
|
final remember = _authRepository.getRememberPassword();
|
||
|
rememberPassword.value = remember;
|
||
|
if (remember) {
|
||
|
final loginData = _authRepository.getLoginKey();
|
||
|
usernameController.text = loginData.username;
|
||
|
passwordController.text = loginData.password;
|
||
2 weeks ago
|
}
|
||
|
}
|
||
|
|
||
2 weeks ago
|
/// Check if the user is already logged in by delegating to the repository.
|
||
|
bool isLoggedIn() {
|
||
|
return _authRepository.isLoggedIn();
|
||
2 weeks ago
|
}
|
||
|
|
||
2 weeks ago
|
/// 登录逻辑
|
||
2 weeks ago
|
Future<void> login() async {
|
||
2 weeks ago
|
final username = usernameController.text.trim();
|
||
|
final password = passwordController.text.trim();
|
||
2 weeks ago
|
|
||
|
if (username.isEmpty || password.isEmpty) {
|
||
2 weeks ago
|
Get.snackbar('输入错误', '用户名和密码不能为空');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
isLoading.value = true;
|
||
2 weeks ago
|
final loginData = LoginRequest(username: username, password: password);
|
||
2 weeks ago
|
|
||
2 weeks ago
|
if (_authRepository.isOnline) {
|
||
|
await _onlineLogin(loginData);
|
||
2 weeks ago
|
} else {
|
||
2 weeks ago
|
_offlineLogin(loginData);
|
||
2 weeks ago
|
}
|
||
|
isLoading.value = false;
|
||
|
}
|
||
|
|
||
2 weeks ago
|
/// 在线登录
|
||
|
Future<void> _onlineLogin(LoginRequest loginRequest) async {
|
||
2 weeks ago
|
try {
|
||
2 weeks ago
|
var loginResponse = await _authRepository.login(loginRequest);
|
||
|
// 登录成功后
|
||
|
_authRepository.saveToken(loginResponse.token);
|
||
|
_authRepository.saveRefreshToken(loginResponse.refreshToken);
|
||
|
_authRepository.addRememberPassword(rememberPassword.value);
|
||
|
|
||
|
if (rememberPassword.value) {
|
||
|
_authRepository.addLoginKey(loginRequest);
|
||
2 weeks ago
|
} else {
|
||
2 weeks ago
|
_authRepository.removeLoginKey();
|
||
2 weeks ago
|
}
|
||
2 weeks ago
|
Get.offAllNamed(AppRoutes.home);
|
||
2 weeks ago
|
} catch (e) {
|
||
2 weeks ago
|
throw Exception(e);
|
||
2 weeks ago
|
}
|
||
|
}
|
||
|
|
||
2 weeks ago
|
void _offlineLogin(LoginRequest loginRequest) {
|
||
|
final loginData = _authRepository.getLoginKey();
|
||
2 weeks ago
|
|
||
2 weeks ago
|
if (loginData.username == loginRequest.username &&
|
||
|
loginData.password == loginRequest.password) {
|
||
2 weeks ago
|
Get.offAllNamed(AppRoutes.home);
|
||
|
Get.snackbar('离线登录成功', '您已离线登录到系统');
|
||
|
} else {
|
||
2 weeks ago
|
Get.snackbar('登录失败', '无网络连接,且无法验证本地凭证');
|
||
2 weeks ago
|
}
|
||
2 weeks ago
|
}
|
||
2 weeks ago
|
}
|