admin
2021-02-03 1981dee5aec45793d3c4ebdbc4e637528c71b3c5
BuWanVideo/src/com/weikou/beibeivideo/util/UserUtil.java
@@ -2,10 +2,20 @@
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;
@@ -53,4 +63,138 @@
        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);
    }
}