DataUtils.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import 'package:flutter_osc/model/user_info.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'dart:async';
  4. class DataUtils {
  5. static final String SP_AC_TOKEN = "accessToken";
  6. static final String SP_RE_TOKEN = "refreshToken";
  7. static final String SP_UID = "uid";
  8. static final String SP_IS_LOGIN = "isLogin";
  9. static final String SP_EXPIRES_IN = "expiresIn";
  10. static final String SP_TOKEN_TYPE = "tokenType";
  11. static final String SP_USER_NAME = "name";
  12. static final String SP_USER_ID = "id";
  13. static final String SP_USER_LOC = "location";
  14. static final String SP_USER_GENDER = "gender";
  15. static final String SP_USER_AVATAR = "avatar";
  16. static final String SP_USER_EMAIL = "email";
  17. static final String SP_USER_URL = "url";
  18. // 保存用户登录信息,data中包含了token等信息
  19. static saveLoginInfo(Map data) async {
  20. if (data != null) {
  21. SharedPreferences sp = await SharedPreferences.getInstance();
  22. String accessToken = data['access_token'];
  23. await sp.setString(SP_AC_TOKEN, accessToken);
  24. String refreshToken = data['refresh_token'];
  25. await sp.setString(SP_RE_TOKEN, refreshToken);
  26. num uid = data['uid'];
  27. await sp.setInt(SP_UID, uid as int);
  28. String tokenType = data['tokenType'];
  29. await sp.setString(SP_TOKEN_TYPE, tokenType);
  30. num expiresIn = data['expires_in'];
  31. await sp.setInt(SP_EXPIRES_IN, expiresIn as int);
  32. await sp.setBool(SP_IS_LOGIN, true);
  33. }
  34. }
  35. static clearLoginInfo() async {
  36. SharedPreferences sp = await SharedPreferences.getInstance();
  37. await sp.setString(SP_AC_TOKEN, "");
  38. await sp.setString(SP_RE_TOKEN, "");
  39. await sp.setInt(SP_UID, -1);
  40. await sp.setString(SP_TOKEN_TYPE, "");
  41. await sp.setInt(SP_EXPIRES_IN, -1);
  42. await sp.setBool(SP_IS_LOGIN, false);
  43. }
  44. // 保存用户个人信息
  45. static Future<UserInfo?> saveUserInfo(Map? data) async {
  46. if (data != null) {
  47. SharedPreferences sp = await SharedPreferences.getInstance();
  48. String name = data['name'];
  49. num id = data['id'];
  50. String gender = data['gender'];
  51. String location = data['location'];
  52. String avatar = data['avatar'];
  53. String email = data['email'];
  54. String url = data['url'];
  55. await sp.setString(SP_USER_NAME, name);
  56. await sp.setInt(SP_USER_ID, id as int);
  57. await sp.setString(SP_USER_GENDER, gender);
  58. await sp.setString(SP_USER_AVATAR, avatar);
  59. await sp.setString(SP_USER_LOC, location);
  60. await sp.setString(SP_USER_EMAIL, email);
  61. await sp.setString(SP_USER_URL, url);
  62. UserInfo userInfo = UserInfo(
  63. id: id,
  64. name: name,
  65. gender: gender,
  66. avatar: avatar,
  67. email: email,
  68. location: location,
  69. url: url
  70. );
  71. return userInfo;
  72. }
  73. return null;
  74. }
  75. // 获取用户信息
  76. static Future<UserInfo?> getUserInfo() async {
  77. SharedPreferences sp = await SharedPreferences.getInstance();
  78. bool? isLogin = sp.getBool(SP_IS_LOGIN);
  79. if (isLogin == null || !isLogin) {
  80. return null;
  81. }
  82. UserInfo userInfo = UserInfo();
  83. userInfo.id = sp.getInt(SP_USER_ID);
  84. userInfo.name = sp.getString(SP_USER_NAME);
  85. userInfo.avatar = sp.getString(SP_USER_AVATAR);
  86. userInfo.email = sp.getString(SP_USER_EMAIL);
  87. userInfo.location = sp.getString(SP_USER_LOC);
  88. userInfo.gender = sp.getString(SP_USER_GENDER);
  89. userInfo.url = sp.getString(SP_USER_URL);
  90. return userInfo;
  91. }
  92. static Future<bool> isLogin() async {
  93. SharedPreferences sp = await SharedPreferences.getInstance();
  94. bool? b = sp.getBool(SP_IS_LOGIN);
  95. return b != null && b;
  96. }
  97. static Future<String?> getAccessToken() async {
  98. SharedPreferences sp = await SharedPreferences.getInstance();
  99. return sp.getString(SP_AC_TOKEN);
  100. }
  101. }