WGShare.Mobile.Flutter/wgshare/lib/common/store/user_store.dart

70 lines
1.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:wgshare/common/mixins/request_tool_mixin.dart';
import 'package:wgshare/common/store/app_storage_key.dart';
import 'package:wgshare/utils/storage.dart';
import '../models/user_info_entity.dart';
class UserStore extends GetxController with RequestToolMixin {
static UserStore get to => Get.find();
/// 是否登录
String? token;
/// 用户信息
Rx<UserInfoEntity?> userInfoEntity = Rx(null);
UserStore init() {
token = StorageService.to.read(AppStorageKey.token.value);
try {
var userDetail = StorageService.to.read(AppStorageKey.userInfo.value);
if (userDetail != null) {
userInfoEntity.value = UserInfoEntity.fromJson(userDetail);
}
} catch (err) {
debugPrint('wgs输出===$err');
StorageService.to.remove(AppStorageKey.userInfo.value);
}
if ((token?.isNotEmpty ?? false) && userInfoEntity.value != null) {
} else {
/// TODO 返回登录页面
// Get.offAllNamed(Routes.login);
}
return this;
}
/// 保存token
void setToken(String token) {
this.token = token;
StorageService.to.write(AppStorageKey.token.value, token);
}
/// 保存用户信息
void setUserDetailInfo(UserInfoEntity info) {
userInfoEntity.value = info;
StorageService.to.write(AppStorageKey.userInfo.value, info);
}
/// 清除数据
void erase() {
userInfoEntity.value = null;
token = null;
StorageService.to.erase();
}
// 用户信息更新
Future<UserInfoEntity?> updateUserInfo() async {
// BaseStructureResult<UserInfoDetail> res = await getClient().getUser();
// var data = res.data;
// if (res.success && data != null) {
// setUserDetailInfo(data);
// return data;
// }
// return null;
}
}