12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- 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<void> 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<void> 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<void> 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<void> saveDeviceInfoTableTime(int timeId) async {
- await SPUtils.getInstance().saveInt('DeviceInfoTableTimeInt', timeId);
- }
- /// 获取登录信息
- int getDeviceInfoTableTime() {
- return SPUtils.getInstance().getInt('DeviceInfoTableTimeInt') ?? -1;
- }
- }
|