diff --git a/example/lib/main.dart b/example/lib/main.dart index c7584a8..e594288 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -107,9 +107,34 @@ class HomePage extends StatelessWidget { child: Text('打开日志视图'), ), + RaisedButton( + onPressed: (){ +// NetUtil.request('/api/v1/mod', params: {'ids': [6] } ,method: NetMethod.POST, +// onSuccess: (params){ +// Logger.log("onSuccess#$params"); +// }, +// onCache: (params){ +// Logger.log("onCache#$params"); +// }); + + testPost(); + + }, + child: Text('测试接口'), + ), + + ], ), ), ); } + + void testPost() async{ + var cached = await NetUtil.getRequestCachedData( '/api/v1/mod', params: {'ids': [7] }); + print("cahced ${cached?.toString()}"); + var param = await NetUtil.post('/api/v1/mod', params: {'ids': [7] }, method: NetMethod.POST); + print('apapapsdjfdsjf: ${param?.toString()}'); + } + } diff --git a/lib/models/user/user_info_model.dart b/lib/models/user/user_info_model.dart index 7a8e292..a76547c 100644 --- a/lib/models/user/user_info_model.dart +++ b/lib/models/user/user_info_model.dart @@ -1,3 +1,4 @@ +import 'package:zhiying_comm/util/empty_util.dart'; /// /// 用户信息类 @@ -10,12 +11,10 @@ class UserInfoModel { List perms; String registerInviteCodeEnable; - UserInfoModel( - {this.token, - this.userId, - this.username, - this.perms, - this.registerInviteCodeEnable}); + // 获取模糊手机号码 + get blurMobile => !EmptyUtil.isEmpty(mobile) ? mobile.length == 11 ? '${mobile.substring(0, 3)}****${mobile.substring(7, mobile.length)}' : mobile : mobile; + + UserInfoModel({this.token, this.userId, this.username, this.perms, this.registerInviteCodeEnable}); UserInfoModel.fromJson(Map json) { token = json['token']; diff --git a/lib/models/user/user_info_model_notifier.dart b/lib/models/user/user_info_model_notifier.dart index 04ef29a..e373d54 100644 --- a/lib/models/user/user_info_model_notifier.dart +++ b/lib/models/user/user_info_model_notifier.dart @@ -1,35 +1,50 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:zhiying_comm/models/user/user_info_model.dart'; +import 'package:zhiying_comm/util/global_config.dart'; +import 'package:zhiying_comm/util/log/let_log.dart'; +import 'package:zhiying_comm/util/shared_prefe_util.dart'; /// -/// 用户信息 +/// 用户信息Provider /// class UserInfoNotifier with ChangeNotifier { + /// 用户信息 UserInfoModel _userInfo; - /// 初始化,是否需要读取缓存信息? - UserInfoNotifier(); - /// 更新用户数据 void setUserInfo(UserInfoModel loginUser) async { print('${loginUser.toString()}'); this._userInfo = loginUser; - // 缓存数据 TODO - /// token + // 缓存数据 + await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_TOKEN, loginUser.token); + await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_MOBILE, loginUser.mobile); + await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_USER_INFO, jsonEncode(loginUser.toJson())); + notifyListeners(); } /// 退出登陆 - void unLogin() { + void unLogin() async { this._userInfo = null; - // 清除缓存数据 TODO + // 清除缓存数据 + await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_TOKEN, ''); + await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_MOBILE, ''); + await SharedPreferencesUtil.setStringValue(GlobalConfig.SHARED_KEY_USER_INFO, ''); + notifyListeners(); } /// 获取登陆数据 - UserInfoModel getUserInfoModel() { + Future getUserInfoModel() async { if (null != _userInfo) { return _userInfo; } - // TODO 需要读取缓存的数据? - return null; + try { + String userInfoJson = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_USER_INFO); + _userInfo = UserInfoModel.fromJson(jsonDecode(userInfoJson)); + } catch (e) { + Logger.log(e); + } + return _userInfo; } } diff --git a/lib/util/global_config.dart b/lib/util/global_config.dart index 7c99c19..3d4bcd2 100644 --- a/lib/util/global_config.dart +++ b/lib/util/global_config.dart @@ -3,6 +3,17 @@ class GlobalConfig { static final String HTTP_RESPONSE_KEY_CODE = 'code'; static final String HTTP_RESPONSE_KEY_MSG = 'msg'; static final String HTTP_RESPONSE_KEY_DATA = 'data'; + /// 成功返回的CODE值 static final int RESPONSE_SUCCESS_CODE = 1; + + /// ====================== Shared_prefe_key ====================== /// + /// 用户token + static final String SHARED_KEY_TOKEN = 'sp_token'; + + /// 用户手机号码 + static final String SHARED_KEY_MOBILE = 'sp_mobile'; + + /// 用户json + static final String SHARED_KEY_USER_INFO = 'sp_userinfo'; } diff --git a/lib/util/net_util.dart b/lib/util/net_util.dart index 156efd7..8d2d6ee 100644 --- a/lib/util/net_util.dart +++ b/lib/util/net_util.dart @@ -1,5 +1,6 @@ import 'package:dio/dio.dart'; import 'package:dio/adapter.dart'; +import 'package:provider/provider.dart'; import 'package:zhiying_comm/util/empty_util.dart'; import 'dart:io'; import 'dart:ui'; @@ -235,11 +236,9 @@ class NetUtil { // secret_key params['secret_key'] = setting['secret_key']; - // token - String token = setting['token']; - if (token != null && - token != '' && - (!params.containsKey('token') || params['token'] == '')) { + // token 读取SP缓存中的用户token + String token = await SharedPreferencesUtil.getStringValue(GlobalConfig.SHARED_KEY_TOKEN); + if(!EmptyUtil.isEmpty(token)){ params['token'] = token; } diff --git a/lib/util/shared_prefe_util.dart b/lib/util/shared_prefe_util.dart index ef876e6..9aa1aa5 100644 --- a/lib/util/shared_prefe_util.dart +++ b/lib/util/shared_prefe_util.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:zhiying_comm/util/empty_util.dart'; +import 'package:zhiying_comm/util/log/let_log.dart'; class SharedPreferencesUtil { static Future> getNetCacheResult(String key) async { @@ -11,7 +12,7 @@ class SharedPreferencesUtil { // TODO need解密? if (!EmptyUtil.isEmpty(cacheResult)) { Map map = json.decode(cacheResult); - if(!EmptyUtil.isEmpty(map)) { + if (!EmptyUtil.isEmpty(map)) { return map; } } @@ -27,4 +28,25 @@ class SharedPreferencesUtil { } return; } + + static Future setStringValue(String key, String value) async { + if (!EmptyUtil.isEmpty(key) && !EmptyUtil.isEmpty(value)) { + Logger.log('key = ${key}, value = $value'); + SharedPreferences prefs = await SharedPreferences.getInstance(); + if (null != prefs) { + return prefs.setString(key, value); + } + } + return false; + } + + static Future getStringValue(String key, {String defaultVal = ''}) async { + if (!EmptyUtil.isEmpty(key)) { + SharedPreferences prefs = await SharedPreferences.getInstance(); + if (null != prefs && prefs.containsKey(key)) { + return prefs.getString(key); + } + } + return defaultVal; + } } diff --git a/lib/zhiying_comm.dart b/lib/zhiying_comm.dart index d4f07d0..8f6ba8b 100644 --- a/lib/zhiying_comm.dart +++ b/lib/zhiying_comm.dart @@ -16,6 +16,7 @@ export 'util/widget_factory.dart'; export 'util/router_util.dart'; export 'util/log/let_log.dart'; export 'util/empty_util.dart'; +export 'util/global_config.dart'; // 用户信息 export 'models/user/user_info_model.dart';