package com.weikou.beibeivideo.util;
|
|
import android.content.Context;
|
import android.content.SharedPreferences;
|
import android.view.View;
|
|
import com.bumptech.glide.Glide;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
|
import com.weikou.beibeivideo.BeibeiVideoAPI;
|
import com.weikou.beibeivideo.R;
|
import com.weikou.beibeivideo.entity.UserInfo;
|
import com.weikou.beibeivideo.entity.vo.UserInfoVO;
|
import com.weikou.beibeivideo.util.downutil.StringUtils;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
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", "");
|
if (StringUtils.isNullOrEmpty(protocol))
|
return false;
|
else
|
return true;
|
}
|
|
|
/**
|
* 同意用户协议
|
*
|
* @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) {
|
SharedPreferences preferences = context.getSharedPreferences("user", MODE_PRIVATE);
|
String uid = preferences.getString("LoginUid", "");
|
return uid;
|
}
|
|
|
public static boolean isLogin(Context context) {
|
String uid = getLoginUid(context);
|
return !StringUtils.isNullOrEmpty(uid);
|
}
|
|
public static UserInfo getLoginUserInfo(Context context) {
|
|
UserInfoVO vo = getLoginUserInfoDetail(context);
|
if (vo != null) {
|
UserInfo userInfo = new UserInfo();
|
userInfo.setId(vo.getId());
|
userInfo.setNickname(vo.getNickName());
|
userInfo.setPortrait(vo.getPortrait());
|
return userInfo;
|
}
|
|
SharedPreferences sp_user = context.getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String uid = sp_user.getString("LoginUid", "");
|
boolean isLogin = !com.lcjian.library.util.common.StringUtils.isEmpty(uid);// 获取登录状态
|
if (isLogin) {
|
UserInfo userInfo = new UserInfo();
|
userInfo.setId(uid);
|
String portrait = sp_user.getString("portrait", "");// 用户头像
|
String name = sp_user.getString("name", "");// 用户名称
|
userInfo.setNickname(name);
|
userInfo.setPortrait(portrait);
|
return userInfo;
|
} else {
|
return null;
|
}
|
}
|
|
|
/**
|
* 获取用户详情(新版本)
|
*
|
* @param context
|
* @return
|
*/
|
public static UserInfoVO getLoginUserInfoDetail(Context context) {
|
SharedPreferences sp_user = context.getSharedPreferences("user",
|
Context.MODE_PRIVATE);
|
String detail = sp_user.getString("detail", "");
|
|
if (StringUtils.isNullOrEmpty(detail)) {
|
return null;
|
} else {
|
return new Gson().fromJson(detail, UserInfoVO.class);
|
}
|
}
|
|
/**
|
* 保存用户详情
|
*
|
* @param context
|
* @param vo
|
*/
|
public static void saveLoginUserDetail(Context context, UserInfoVO vo) {
|
if (context == null || vo == null)
|
return;
|
SharedPreferences.Editor editor = context.getSharedPreferences("user",
|
Context.MODE_PRIVATE).edit();
|
editor.putString("detail", new Gson().toJson(vo));
|
editor.commit();
|
}
|
|
|
/**
|
* 退出登录
|
*
|
* @param context
|
*/
|
public static void logout(Context context) {
|
SharedPreferences.Editor editor = context.getSharedPreferences("user",
|
Context.MODE_PRIVATE).edit();
|
editor.remove("detail");
|
editor.remove("LoginUid");
|
editor.remove("portrait");
|
editor.remove("name");
|
editor.putBoolean("PushType", false);
|
editor.commit();
|
}
|
|
|
public static void updateUserInfo(Context context, IUserInfoUpdateListener listener) {
|
if (context == null)
|
return;
|
String loginUid = UserUtil.getLoginUid(context);
|
if (com.lcjian.library.util.common.StringUtils.isEmpty(loginUid)) {
|
if (listener != null)
|
listener.noLogin();
|
return;
|
}
|
BeibeiVideoAPI.getPersonInfo(context, UserUtil.getUid(context), loginUid, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optBoolean("IsPost")) {
|
JSONObject data = jsonObject.optJSONObject("Data");
|
UserInfoVO userInfoVO = new Gson().fromJson(data.toString(), UserInfoVO.class);
|
//缓存起来
|
if (userInfoVO != null && userInfoVO.getId() != null) {
|
UserUtil.saveLoginUserDetail(context, userInfoVO);
|
}
|
if (listener != null)
|
listener.onSuccess();
|
} else {
|
if (listener != null)
|
listener.onFail(jsonObject.optString("Error"));
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
if (listener != null)
|
listener.onFail(responseString);
|
}
|
});
|
|
}
|
|
public interface IUserInfoUpdateListener {
|
public void noLogin();
|
|
public void onSuccess();
|
|
public void onFail(String msg);
|
|
}
|
|
public static boolean isVIP(Context context) {
|
|
UserInfoVO vo = getLoginUserInfoDetail(context);
|
if (vo == null) {
|
return false;
|
}
|
if (vo.getVipExpireTime() == null) {
|
return false;
|
}
|
|
if (System.currentTimeMillis() > vo.getVipExpireTime().longValue()) {
|
return false;
|
}
|
|
return true;
|
}
|
|
|
}
|