Weller hace 4 años
padre
commit
dc2b538c90
Se han modificado 7 ficheros con 95 adiciones y 23 borrados
  1. +25
    -0
      example/lib/main.dart
  2. +5
    -6
      lib/models/user/user_info_model.dart
  3. +26
    -11
      lib/models/user/user_info_model_notifier.dart
  4. +11
    -0
      lib/util/global_config.dart
  5. +4
    -5
      lib/util/net_util.dart
  6. +23
    -1
      lib/util/shared_prefe_util.dart
  7. +1
    -0
      lib/zhiying_comm.dart

+ 25
- 0
example/lib/main.dart Ver fichero

@@ -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()}');
}

}

+ 5
- 6
lib/models/user/user_info_model.dart Ver fichero

@@ -1,3 +1,4 @@
import 'package:zhiying_comm/util/empty_util.dart';

///
/// 用户信息类
@@ -10,12 +11,10 @@ class UserInfoModel {
List<String> 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<String, dynamic> json) {
token = json['token'];


+ 26
- 11
lib/models/user/user_info_model_notifier.dart Ver fichero

@@ -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<UserInfoModel> 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;
}
}

+ 11
- 0
lib/util/global_config.dart Ver fichero

@@ -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';
}

+ 4
- 5
lib/util/net_util.dart Ver fichero

@@ -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;
}



+ 23
- 1
lib/util/shared_prefe_util.dart Ver fichero

@@ -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<Map<String, dynamic>> getNetCacheResult(String key) async {
@@ -11,7 +12,7 @@ class SharedPreferencesUtil {
// TODO need解密?
if (!EmptyUtil.isEmpty(cacheResult)) {
Map<String, dynamic> map = json.decode(cacheResult);
if(!EmptyUtil.isEmpty(map)) {
if (!EmptyUtil.isEmpty(map)) {
return map;
}
}
@@ -27,4 +28,25 @@ class SharedPreferencesUtil {
}
return;
}

static Future<bool> 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<String> 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;
}
}

+ 1
- 0
lib/zhiying_comm.dart Ver fichero

@@ -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';


Cargando…
Cancelar
Guardar