package com.ysvideo.zhibo.app.util;
|
|
import android.content.Context;
|
import android.content.SharedPreferences;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.ysvideo.zhibo.app.entity.user.UserInfo;
|
import com.ysvideo.zhibo.lib.common.util.common.StringUtils;
|
|
import static android.content.Context.MODE_PRIVATE;
|
|
public class UserUtil {
|
|
private static Gson gson = new GsonBuilder().serializeNulls().create();
|
|
|
/**
|
* 是否同意了用户协议
|
*
|
* @param context
|
* @return
|
*/
|
public static boolean isAgreeUserProtocol(Context context) {
|
SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
|
String protocol = sp.getString("protocol-agree", "");
|
return !StringUtils.isEmpty(protocol);
|
}
|
|
|
/**
|
* 同意用户协议
|
*
|
* @param context
|
*/
|
public static void agreeUserProtocol(Context context) {
|
SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putString("protocol-agree", "1");
|
editor.commit();
|
}
|
|
public static String getUid(Context context) {
|
SharedPreferences preferences = context.getSharedPreferences("user", MODE_PRIVATE);
|
String uid = preferences.getString("uid", "");
|
return uid;
|
}
|
|
public static String getLoginUid(Context context) {
|
UserInfo user = getLoginUser(context);
|
if (user == null)
|
return null;
|
return user.getId();
|
}
|
|
public static UserInfo getLoginUser(Context context) {
|
SharedPreferences preferences = context.getSharedPreferences("user", MODE_PRIVATE);
|
String loginUserStr = preferences.getString("loginUser", null);
|
// loginUserStr="{\"id\":\"123123\",\"portrait\":\"http://\",\"nickName\":\"测试\"}";
|
if (loginUserStr == null)
|
return null;
|
return new Gson().fromJson(loginUserStr, UserInfo.class);
|
}
|
|
public static boolean isLogin(Context context) {
|
return getLoginUser(context) != null;
|
// return true;
|
}
|
|
|
/**
|
* 退出登录
|
*
|
* @param context
|
*/
|
public static void logout(Context context) {
|
SharedPreferences preferences = context.getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = preferences.edit();
|
editor.remove("loginUser");
|
editor.commit();
|
}
|
|
/**
|
* 登录成功
|
*
|
* @param context
|
* @param userInfo
|
*/
|
public static void loginSuccess(Context context, UserInfo userInfo) {
|
SharedPreferences preferences = context.getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = preferences.edit();
|
editor.putString("loginUser", new Gson().toJson(userInfo));
|
editor.commit();
|
}
|
}
|