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