import 'dart:convert'; import 'package:dio/dio.dart' as Dio; import 'package:get/get.dart'; import 'package:sk_base_mobile/apis/api.dart' as Api; 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/config.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'; class AuthStore extends GetxController { static AuthStore get to => Get.find(); final userInfo = UserInfoModel().obs; Future init() async { try { String? preUserInfo = StorageService.to.getString(CacheKeys.userInfo, isWithUser: false); String? token = StorageService.to.getString(CacheKeys.token, isWithUser: false); // if (token != null) { // final response = await Api.isValidToken(token); // if (response.data != null) { // // Token有效无需登录 // if (response.data['data'] == true) { // } else { // LoggerUtil().info('[Store-Auth] Token invalid, need to login.'); // await StorageService.to.remove(CacheKeys.token, isWithUser: false); // } // if (preUserInfo != null) { // userInfo(UserInfoModel.fromJson(jsonDecode(preUserInfo))); // LoggerUtil().info('[Store-Auth] userId: ${userInfo.value.userId}'); // } // } // } } catch (e) { LoggerUtil().info( '[Store-Auth]Init failed, please try again.${e}', ); } return this; } Future deleteAccount() async { await LoadingUtil.show(status: 'Deleting...'); 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 { await LoadingUtil.dismiss(); ; } } Future logout({bool force = false}) async { await LoadingUtil.show(status: 'Logout...'); await StorageService.to.remove(CacheKeys.token, isWithUser: false); await StorageService.to.remove(CacheKeys.userInfo, isWithUser: false); try { final response = await Api.logout(); if (response.data != null) { LoggerUtil().info('[Store-Auth] Logout succeed.'); if (Get.context != null) Get.offAllNamed(RouteConfig.login); } } catch (e) { } finally { LoadingUtil.dismiss(); } } Future login(LoginEnum type, {String? identityToken}) async { Dio.Response response; if (type == LoginEnum.fastLogin && StorageService.to.getString(CacheKeys.deviceUUID, isWithUser: false) == null) { SnackBarUtil().error('Need DeviceUUID. Please restart app.'); return; } LoadingUtil.show(status: 'Login...'); // Hide keyboard TapToDismissKeyboard.dismissOf(context: Get.context!); try { response = await Api.login( '4', type == LoginEnum.fastLogin ? (StorageService.to .getString(CacheKeys.deviceUUID, isWithUser: false)) : identityToken); if (response.data != null) { final auth = Auth.fromJson(response.data['data']); if (auth.token != null) { await StorageService.to .setString(CacheKeys.token, auth.token!, isWithUser: false); } if (auth.userInfo != null) { userInfo(auth.userInfo!); await StorageService.to.setString( CacheKeys.userInfo, jsonEncode(auth.userInfo), isWithUser: false); LoggerUtil().info('[Store-Auth] Login succeed.'); } Get.offNamed(RouteConfig.home); getCommonInfo(); } } catch (e) { SnackBarUtil().error('${e}'); } finally { LoadingUtil.dismiss(); } } Future updateUserInfoState(UserInfoModel newInfo) async { userInfo(newInfo); await StorageService.to .setString(CacheKeys.userInfo, jsonEncode(newInfo), isWithUser: false); } Future getUserInfo() async { await LoadingUtil.show(status: 'Loading...'); try { final response = await Api.getUserInfo(userInfo.value.userId!); if (response.data != null) { UserInfoModel userInfo = UserInfoModel.fromJson(response.data['data']); await updateUserInfoState(userInfo); } } catch (e) { SnackBarUtil().error('$e'); } finally { LoadingUtil.dismiss(); } } Future saveUserInfo(Map data) async { LoadingUtil.show(status: 'Saving...'); try { final res = await Api.saveUserInfo(data); if (res.data != null) { await getUserInfo(); SnackBarUtil().success('Save successfully.'); } } catch (e) { SnackBarUtil().error('$e'); } finally { LoadingUtil.dismiss(); } } Future getCommonInfo() async { await AppInfoService.to.getAppConfig(); await Future.wait([ /// 依赖业务信息 // Get.putAsync(() => BlockStore().init()), ]); } }