基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

54 lines
1.6 KiB

  1. import 'package:zhiying_comm/zhiying_comm.dart';
  2. import 'package:zhiying_comm/util/shared_prefe_util.dart';
  3. import 'package:zhiying_comm/util/empty_util.dart';
  4. import 'package:zhiying_comm/util/global_config.dart';
  5. import 'model/login_style_model.dart';
  6. /// 登陆数据管理工具类
  7. class LoginStyleUtil {
  8. static final String _URL = '/api/v1/sign/in';
  9. static void init() async {
  10. String key = _getCacheKey();
  11. /// 清除数据缓存
  12. await SharedPreferencesUtil.setNetCacheResult(key, '');
  13. }
  14. /// 获取数据
  15. static Future<LoginStyleModel> getLoginModel() async {
  16. var cache = await fetchCachePageData();
  17. if (!EmptyUtil.isEmpty(cache)) return cache;
  18. var result = await fetchNetPageData();
  19. if (!EmptyUtil.isEmpty(result)) return result;
  20. return null;
  21. }
  22. /// 获取缓存的key
  23. static String _getCacheKey() {
  24. return NetUtil.getRequestParamsCachedKey(null, _URL);
  25. }
  26. /// 获取缓存的页面数据
  27. static Future<LoginStyleModel> fetchCachePageData() async {
  28. var result = await NetUtil.getRequestCachedData(_URL);
  29. if (!EmptyUtil.isEmpty(result)) {
  30. LoginStyleModel model = LoginStyleModel.fromJson(result);
  31. return model;
  32. }
  33. return null;
  34. }
  35. /// 获取页面数据
  36. static Future<LoginStyleModel> fetchNetPageData() async {
  37. var result = await NetUtil.post(_URL, method: NetMethod.GET, cache: true);
  38. if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) {
  39. LoginStyleModel model = LoginStyleModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]);
  40. return model;
  41. }
  42. return null;
  43. }
  44. }