user_service.dart 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import 'dart:convert';
  2. import 'package:lszlgl/model/rsp/login_rsp.dart';
  3. import 'package:lszlgl/model/rsp/user_rsp.dart';
  4. import 'package:lszlgl/router/my_router.dart';
  5. import 'package:lszlgl/utils/sp_utils.dart';
  6. class UserService {
  7. UserService._();
  8. static UserService? _instance;
  9. static UserService get() => _instance ??= UserService._();
  10. LoginRsp? _login;
  11. UserRsp? _user;
  12. /// 存储登录信息
  13. Future<void> saveLogin(LoginRsp? value) async {
  14. if (value == null) return;
  15. _login = value;
  16. await SPUtils.getInstance().saveString('login', jsonEncode(value.toJson()));
  17. }
  18. /// 获取登录信息
  19. LoginRsp? getLogin() {
  20. if (_login != null) return _login;
  21. String? login = SPUtils.getInstance().getString('login');
  22. if (login == null) return null;
  23. return LoginRsp.fromJson(jsonDecode(login));
  24. }
  25. /// 退出登录
  26. Future<void> logout({bool goLogin = true}) async {
  27. _login = null;
  28. await SPUtils.getInstance().remove('login');
  29. _user = null;
  30. await SPUtils.getInstance().remove('user');
  31. // 跳转登录页
  32. if (goLogin) MyRouter.startLogin(popAll: true);
  33. }
  34. /// 存储用户信息
  35. Future<void> saveUser(UserRsp? value) async {
  36. if (value == null) return;
  37. _user = value;
  38. await SPUtils.getInstance().saveString('user', jsonEncode(value.toJson()));
  39. }
  40. /// 获取用户信息
  41. UserRsp? getUser() {
  42. if (_user != null) return _user;
  43. String? user = SPUtils.getInstance().getString('user');
  44. if (user == null) return null;
  45. return UserRsp.fromJson(jsonDecode(user));
  46. }
  47. /// 存储数据库DeviceInfoTable表的id号,用于记录同步的位置
  48. Future<void> saveDeviceInfoTableTime(int timeId) async {
  49. await SPUtils.getInstance().saveInt('DeviceInfoTableTimeInt', timeId);
  50. }
  51. /// 获取登录信息
  52. int getDeviceInfoTableTime() {
  53. return SPUtils.getInstance().getInt('DeviceInfoTableTimeInt') ?? -1;
  54. }
  55. }