2024-03-18 13:23:58 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-03-31 17:13:29 +08:00
|
|
|
import 'package:dio/dio.dart' as dio_package;
|
2024-03-18 13:23:58 +08:00
|
|
|
import 'package:get/get.dart';
|
2024-03-26 15:30:43 +08:00
|
|
|
import 'package:sk_base_mobile/apis/index.dart';
|
2024-03-26 16:02:49 +08:00
|
|
|
import 'package:sk_base_mobile/store/dict.store.dart';
|
2024-03-18 13:23:58 +08:00
|
|
|
import 'package:sk_base_mobile/util/logger_util.dart';
|
|
|
|
import 'package:sk_base_mobile/widgets/tap_to_dismiss_keyboard.dart';
|
|
|
|
import 'package:sk_base_mobile/models/auth.dart';
|
|
|
|
import 'package:sk_base_mobile/models/user_info.model.dart';
|
|
|
|
import 'package:sk_base_mobile/services/service.dart';
|
|
|
|
import '../constants/constants.dart';
|
|
|
|
import '../util/util.dart';
|
|
|
|
|
2024-03-26 16:02:49 +08:00
|
|
|
class AuthStore extends GetxService {
|
2024-03-18 13:23:58 +08:00
|
|
|
static AuthStore get to => Get.find();
|
|
|
|
final userInfo = UserInfoModel().obs;
|
|
|
|
|
|
|
|
Future<AuthStore> init() async {
|
|
|
|
try {
|
|
|
|
String? preUserInfo =
|
|
|
|
StorageService.to.getString(CacheKeys.userInfo, isWithUser: false);
|
|
|
|
String? token =
|
|
|
|
StorageService.to.getString(CacheKeys.token, isWithUser: false);
|
|
|
|
|
2024-03-19 13:27:42 +08:00
|
|
|
if (token != null) {
|
|
|
|
if (preUserInfo != null) {
|
2024-03-28 15:13:27 +08:00
|
|
|
await getUserInfo();
|
2024-03-19 13:27:42 +08:00
|
|
|
LoggerUtil().info('[Store-Auth] userId: ${userInfo.value.id}');
|
|
|
|
}
|
|
|
|
}
|
2024-03-18 13:23:58 +08:00
|
|
|
} catch (e) {
|
|
|
|
LoggerUtil().info(
|
2024-03-31 17:13:29 +08:00
|
|
|
'[Store-Auth]Init failed, please try again.$e',
|
2024-03-18 13:23:58 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteAccount() async {
|
2024-03-19 10:02:20 +08:00
|
|
|
await LoadingUtil.to.show(status: 'Deleting...');
|
2024-03-18 13:23:58 +08:00
|
|
|
try {
|
|
|
|
final response = await Api.deleteAccount();
|
|
|
|
if (response.data != null) {
|
|
|
|
await SnackBarUtil().success('Delete account succeed.');
|
|
|
|
await logout();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
await SnackBarUtil().error('Delete account faield. Please try again.');
|
|
|
|
} finally {
|
2024-03-19 10:02:20 +08:00
|
|
|
await LoadingUtil.to.dismiss();
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> logout({bool force = false}) async {
|
|
|
|
await StorageService.to.remove(CacheKeys.token, isWithUser: false);
|
|
|
|
await StorageService.to.remove(CacheKeys.userInfo, isWithUser: false);
|
2024-03-28 15:13:27 +08:00
|
|
|
LoggerUtil().info('[Store-Auth] Logout succeed.');
|
|
|
|
// 如果当前已经在login界面 不需要跳转
|
|
|
|
if (Get.currentRoute != RouteConfig.login) {
|
|
|
|
Get.offAllNamed(RouteConfig.login);
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 08:59:08 +08:00
|
|
|
Future<void> login(
|
|
|
|
{required String username, required String password}) async {
|
2024-03-31 17:13:29 +08:00
|
|
|
dio_package.Response response;
|
2024-03-19 08:59:08 +08:00
|
|
|
// if (type == LoginEnum.fastLogin &&
|
|
|
|
// StorageService.to.getString(CacheKeys.deviceUUID, isWithUser: false) ==
|
|
|
|
// null) {
|
|
|
|
// SnackBarUtil().error('Need DeviceUUID. Please restart app.');
|
|
|
|
// return;
|
|
|
|
// }
|
2024-03-18 13:23:58 +08:00
|
|
|
|
2024-03-19 10:02:20 +08:00
|
|
|
LoadingUtil.to.show();
|
2024-03-18 13:23:58 +08:00
|
|
|
TapToDismissKeyboard.dismissOf(context: Get.context!);
|
|
|
|
try {
|
2024-03-19 08:59:08 +08:00
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
|
|
response = await Api.login(username, password);
|
2024-03-18 13:23:58 +08:00
|
|
|
if (response.data != null) {
|
|
|
|
final auth = Auth.fromJson(response.data['data']);
|
2024-03-19 08:59:08 +08:00
|
|
|
|
2024-03-18 13:23:58 +08:00
|
|
|
if (auth.token != null) {
|
|
|
|
await StorageService.to
|
|
|
|
.setString(CacheKeys.token, auth.token!, isWithUser: false);
|
|
|
|
}
|
2024-03-19 08:59:08 +08:00
|
|
|
await getUserInfo();
|
2024-03-28 15:13:27 +08:00
|
|
|
await getCommonInfo();
|
2024-03-18 13:23:58 +08:00
|
|
|
Get.offNamed(RouteConfig.home);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-03-28 15:13:27 +08:00
|
|
|
await SnackBarUtil().error('账号密码错误$e');
|
2024-03-20 09:37:20 +08:00
|
|
|
LoggerUtil().error(e);
|
2024-03-18 13:23:58 +08:00
|
|
|
} finally {
|
2024-03-19 10:02:20 +08:00
|
|
|
LoadingUtil.to.dismiss();
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateUserInfoState(UserInfoModel newInfo) async {
|
|
|
|
userInfo(newInfo);
|
|
|
|
await StorageService.to
|
|
|
|
.setString(CacheKeys.userInfo, jsonEncode(newInfo), isWithUser: false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> getUserInfo() async {
|
|
|
|
try {
|
2024-03-19 08:59:08 +08:00
|
|
|
final response = await Api.getUserInfo();
|
2024-03-18 13:23:58 +08:00
|
|
|
if (response.data != null) {
|
2024-03-22 17:26:57 +08:00
|
|
|
UserInfoModel userInfo = UserInfoModel.fromJson(response.data);
|
2024-03-18 13:23:58 +08:00
|
|
|
await updateUserInfoState(userInfo);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
SnackBarUtil().error('$e');
|
2024-03-28 15:13:27 +08:00
|
|
|
} finally {}
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> saveUserInfo(Map<String, dynamic> data) async {
|
2024-03-19 10:02:20 +08:00
|
|
|
LoadingUtil.to.show(status: 'Saving...');
|
2024-03-18 13:23:58 +08:00
|
|
|
try {
|
|
|
|
final res = await Api.saveUserInfo(data);
|
|
|
|
if (res.data != null) {
|
|
|
|
await getUserInfo();
|
|
|
|
SnackBarUtil().success('Save successfully.');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
SnackBarUtil().error('$e');
|
|
|
|
} finally {
|
2024-03-19 10:02:20 +08:00
|
|
|
LoadingUtil.to.dismiss();
|
2024-03-18 13:23:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> getCommonInfo() async {
|
|
|
|
await AppInfoService.to.getAppConfig();
|
|
|
|
await Future.wait([
|
2024-03-26 16:02:49 +08:00
|
|
|
DictService.to.getDictTypes()
|
|
|
|
|
2024-03-18 13:23:58 +08:00
|
|
|
/// 依赖业务信息
|
|
|
|
// Get.putAsync<BlockStore>(() => BlockStore().init()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|