import 'dart:convert'; import 'package:lszlgl/model/rsp/login_rsp.dart'; import 'package:lszlgl/model/rsp/user_rsp.dart'; import 'package:lszlgl/router/my_router.dart'; import 'package:lszlgl/utils/sp_utils.dart'; class UserService { UserService._(); static UserService? _instance; static UserService get() => _instance ??= UserService._(); LoginRsp? _login; UserRsp? _user; /// 存储登录信息 Future saveLogin(LoginRsp? value) async { if (value == null) return; _login = value; await SPUtils.getInstance().saveString('login', jsonEncode(value.toJson())); } /// 获取登录信息 LoginRsp? getLogin() { if (_login != null) return _login; String? login = SPUtils.getInstance().getString('login'); if (login == null) return null; return LoginRsp.fromJson(jsonDecode(login)); } /// 退出登录 Future logout({bool goLogin = true}) async { _login = null; await SPUtils.getInstance().remove('login'); _user = null; await SPUtils.getInstance().remove('user'); // 跳转登录页 if (goLogin) MyRouter.startLogin(popAll: true); } /// 存储用户信息 Future saveUser(UserRsp? value) async { if (value == null) return; _user = value; await SPUtils.getInstance().saveString('user', jsonEncode(value.toJson())); } /// 获取用户信息 UserRsp? getUser() { if (_user != null) return _user; String? user = SPUtils.getInstance().getString('user'); if (user == null) return null; return UserRsp.fromJson(jsonDecode(user)); } /// 存储数据库DeviceInfoTable表的id号,用于记录同步的位置 Future saveDeviceInfoTableTime(int timeId) async { await SPUtils.getInstance().saveInt('DeviceInfoTableTimeInt', timeId); } /// 获取登录信息 int getDeviceInfoTableTime() { return SPUtils.getInstance().getInt('DeviceInfoTableTimeInt') ?? -1; } }