package com.tejia.lijin.app.util.user;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.tejia.lijin.app.ui.mine.LoginSelectActivity;
|
import com.xiaomi.mipush.sdk.MiPushClient;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.entity.UserInfo;
|
import com.tejia.lijin.app.entity.user.UserLoginStatus;
|
import com.tejia.lijin.app.ui.mine.LoginActivity;
|
import com.tejia.lijin.app.util.Constant;
|
import com.tejia.lijin.app.util.downutil.StringUtils;
|
|
import org.json.JSONException;
|
import org.json.JSONObject;
|
|
import static android.content.Context.MODE_PRIVATE;
|
|
public class UserUtil {
|
|
private static Gson gson = new GsonBuilder().serializeNulls().create();
|
|
/**
|
* SharedPreferences中获取用户信息
|
*
|
* @param context
|
* @return
|
*/
|
public static UserInfo getUserInfo(Context context) {
|
SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
|
String userInfo = sp.getString("userinfo", "");
|
if (StringUtils.isNullOrEmpty(userInfo))
|
return null;
|
JSONObject jsonObject = null;
|
try {
|
jsonObject = new JSONObject(userInfo);
|
} catch (JSONException e) {
|
return null;
|
}
|
UserInfo info = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("user").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
return info;
|
}
|
|
|
/**
|
* 获取登录的uid
|
*
|
* @param context
|
* @return
|
*/
|
public static Long getUid(Context context) {
|
SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
|
String uid = sp.getString("uid", "");
|
if (StringUtils.isNullOrEmpty(uid) || uid.equalsIgnoreCase("0"))
|
return null;
|
return Long.parseLong(uid);
|
}
|
|
|
/**
|
* 是否同意了用户协议
|
*
|
* @param context
|
* @return
|
*/
|
public static boolean isAgreeUserProtocol(Context context) {
|
if (Constant.TEST)
|
return true;
|
|
SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
|
String protocol = sp.getString("protocol-agree", "");
|
if (StringUtils.isNullOrEmpty(protocol))
|
return false;
|
else
|
return true;
|
}
|
|
|
/**
|
* 是否已经显示了用户引导
|
*
|
* @param context
|
* @return
|
*/
|
public static boolean isShownUserGuide(Context context) {
|
return true;
|
// SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
|
// String protocol = sp.getString("shown-user-guide", "");
|
// 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 void shownUserGuide(Context context) {
|
SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putString("shown-user-guide", "1");
|
editor.commit();
|
}
|
|
/**
|
* 是否登录
|
*
|
* @param context
|
* @return
|
*/
|
public static boolean isLogin(Context context) {
|
SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
|
return sp.getBoolean("isLogin", false);
|
}
|
|
public static void setInviteCode(Context context, String inviteCode) {
|
SharedPreferences sp = context.getSharedPreferences("user-info", Context.MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putString("inviteCode", inviteCode);
|
editor.commit();
|
}
|
|
public static String getInviteCode(Context context) {
|
SharedPreferences sp = context.getSharedPreferences("user-info", Context.MODE_PRIVATE);
|
return sp.getString("inviteCode", "");
|
}
|
|
/**
|
* 获取登录状态
|
*
|
* @param context
|
* @return
|
*/
|
public static UserLoginStatus getLoginState(Context context) {
|
boolean isLogin = isLogin(context);
|
if (isLogin) {
|
String inviteCode = getInviteCode(context);
|
if (StringUtils.isNullOrEmpty(inviteCode))
|
return UserLoginStatus.LOINGED_NO_ACTIVE;
|
else
|
return UserLoginStatus.LOGINED_ACTIVED;
|
|
} else {
|
return UserLoginStatus.NOT_LOGIN;
|
}
|
}
|
|
public static void logout(Context context) {
|
SharedPreferences sp = context.getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean("isLogin", false);
|
editor.putBoolean("isWxBind", false);
|
editor.putString("uid", "0");
|
editor.remove("userinfo");
|
editor.commit();
|
|
//清除个人信息缓存
|
sp = context.getSharedPreferences("user-info", MODE_PRIVATE);
|
editor = sp.edit();
|
editor.clear();
|
editor.commit();
|
MiPushClient.setAlias(context, sp.getString("uid", "0"), null);
|
}
|
|
public static int getUserLevelIcon(String level) {
|
int resourceId = 0;
|
switch (level) {
|
case "noActive":
|
resourceId = R.drawable.noactive;
|
break;
|
case "daRen":
|
case "actived":
|
resourceId = R.drawable.actived;
|
break;
|
case "normalVIP":
|
case "vipPre1":
|
resourceId = R.drawable.vippre1;
|
break;
|
case "highVIP":
|
case "vipPre2":
|
resourceId = R.drawable.vippre2;
|
break;
|
case "vipApply":
|
resourceId = R.drawable.vipapply;
|
break;
|
case "superVIP":
|
case "vip":
|
resourceId = R.drawable.vip;
|
break;
|
case "tearcherApply":
|
resourceId = R.drawable.tearcherapply;
|
break;
|
case "tearcher":
|
resourceId = R.drawable.tearcher;
|
break;
|
}
|
|
return resourceId;
|
}
|
|
|
public static void jumpLogin(Context context) {
|
context.startActivity(new Intent(context, LoginSelectActivity.class));
|
}
|
|
|
}
|