修改用户信息(更换头像未完成)
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
package com.gh.common.util;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
@ -36,7 +37,7 @@ import rx.schedulers.Schedulers;
|
||||
public class LoginUtils {
|
||||
|
||||
public enum LoginTag {
|
||||
qq, wechat, weibo, phone
|
||||
qq, wechat, weibo, phone, refresh
|
||||
}
|
||||
|
||||
// 获取验证码
|
||||
@ -92,12 +93,16 @@ public class LoginUtils {
|
||||
|
||||
if (loginTag == LoginTag.weibo) {
|
||||
observable = RetrofitManager.getUsersea().loginByWeibo(body);
|
||||
} else if (loginTag == LoginTag.qq){
|
||||
} else if (loginTag == LoginTag.qq) {
|
||||
observable = RetrofitManager.getUsersea().loginByQQ(body);
|
||||
} else if (loginTag == LoginTag.wechat) {
|
||||
observable = RetrofitManager.getUsersea().loginByWechat(body);
|
||||
} else {
|
||||
} else if (loginTag == LoginTag.phone) {
|
||||
observable = RetrofitManager.getUsersea().loginByMobile(body);
|
||||
} else if (loginTag == LoginTag.refresh) {
|
||||
observable = RetrofitManager.getUsersea().refreshToken(body);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
observable
|
||||
@ -121,36 +126,36 @@ public class LoginUtils {
|
||||
int code = content.getInt("code");
|
||||
switch (code) {
|
||||
case 40000:
|
||||
Utils.toast(context,"参数错误或不完整");
|
||||
Utils.toast(context, "参数错误或不完整");
|
||||
case 40004:
|
||||
Utils.toast(context,"缺少mobile参数");
|
||||
Utils.toast(context, "缺少mobile参数");
|
||||
break;
|
||||
case 40005:
|
||||
Utils.toast(context,"缺少code参数");
|
||||
Utils.toast(context, "缺少code参数");
|
||||
break;
|
||||
case 40006:
|
||||
Utils.toast(context,"缺少service_id参数");
|
||||
Utils.toast(context, "缺少service_id参数");
|
||||
break;
|
||||
case 40007:
|
||||
Utils.toast(context,"服务已超时");
|
||||
Utils.toast(context, "服务已超时");
|
||||
break;
|
||||
case 40008:
|
||||
Utils.toast(context,"验证码已超时");
|
||||
Utils.toast(context, "验证码已超时");
|
||||
break;
|
||||
case 40009:
|
||||
Utils.toast(context,"验证码错误");
|
||||
Utils.toast(context, "验证码错误");
|
||||
break;
|
||||
case 40010:
|
||||
Utils.toast(context,"登录授权失败");
|
||||
Utils.toast(context, "登录授权失败");
|
||||
break;
|
||||
case 40011:
|
||||
Utils.toast(context,"缺少token参数");
|
||||
Utils.toast(context, "缺少token参数");
|
||||
break;
|
||||
case 40012:
|
||||
Utils.toast(context,"令牌已过期");
|
||||
Utils.toast(context, "令牌已过期");
|
||||
break;
|
||||
default:
|
||||
Utils.toast(context,"未知错误");
|
||||
Utils.toast(context, "未知错误");
|
||||
break;
|
||||
}
|
||||
|
||||
@ -162,7 +167,7 @@ public class LoginUtils {
|
||||
});
|
||||
}
|
||||
|
||||
public static void logout(Context context) {
|
||||
public static void cleanUserData(Context context) {
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = sp.edit();
|
||||
edit.putString("user_info", null);
|
||||
@ -183,9 +188,14 @@ public class LoginUtils {
|
||||
LoginResponseEntity.RefreshToken refreshToken = loginToken.getRefreshToken();
|
||||
Long refreshExpire = refreshToken.getExpire();
|
||||
if (refreshExpire != null && refreshExpire > Utils.getTime(context)) {
|
||||
// TODO 刷新accessToken
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("refresh_token", refreshToken.getValue());
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||||
new JSONObject(params).toString());
|
||||
login(context, body, LoginTag.refresh, listener); // TODO 刷新accessToken
|
||||
} else {
|
||||
// TODO 重新登录
|
||||
cleanUserData(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -197,7 +207,7 @@ public class LoginUtils {
|
||||
.getUserInfo(token)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<UserInfoEntity>(){
|
||||
.subscribe(new Response<UserInfoEntity>() {
|
||||
@Override
|
||||
public void onResponse(UserInfoEntity response) {
|
||||
super.onResponse(response);
|
||||
@ -216,7 +226,8 @@ public class LoginUtils {
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String loginData = sp.getString("user_info", null);
|
||||
if (!TextUtils.isEmpty(loginData)) {
|
||||
Type listType = new TypeToken<UserInfoEntity>() {}.getType();
|
||||
Type listType = new TypeToken<UserInfoEntity>() {
|
||||
}.getType();
|
||||
return new Gson().fromJson(loginData, listType);
|
||||
}
|
||||
return null;
|
||||
@ -238,7 +249,8 @@ public class LoginUtils {
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String loginData = sp.getString("login_token", null);
|
||||
if (!TextUtils.isEmpty(loginData)) {
|
||||
Type listType = new TypeToken<LoginResponseEntity>() {}.getType();
|
||||
Type listType = new TypeToken<LoginResponseEntity>() {
|
||||
}.getType();
|
||||
return new Gson().fromJson(loginData, listType);
|
||||
}
|
||||
return null;
|
||||
@ -254,6 +266,65 @@ public class LoginUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void changeUserInfo(final onChangeUserInfoListener listener, final Context context, String content, String editType) {
|
||||
final UserInfoEntity entity = getUserInfo(context);
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (editType) {
|
||||
case "nickName":
|
||||
entity.setName(content);
|
||||
break;
|
||||
case "contact":
|
||||
entity.setContact(content);
|
||||
break;
|
||||
case "sex":
|
||||
entity.setGender(content);
|
||||
break;
|
||||
case "area":
|
||||
entity.setRegion(content);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
final Dialog loadingDialog = DialogUtils.showWaitDialog(context, "正在修改信息...");
|
||||
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||||
new Gson().toJson(entity));
|
||||
RetrofitManager
|
||||
.getUsersea()
|
||||
.changeUserInfo(body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(ResponseBody response) {
|
||||
super.onResponse(response);
|
||||
if (loadingDialog != null) {
|
||||
loadingDialog.dismiss();
|
||||
}
|
||||
|
||||
saveUserInfo(context, entity);
|
||||
listener.onChange();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (loadingDialog != null) {
|
||||
loadingDialog.dismiss();
|
||||
}
|
||||
Utils.toast(context, "修改失败");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public interface onChangeUserInfoListener {
|
||||
void onChange();
|
||||
}
|
||||
|
||||
public interface onCaptchaCallBackListener {
|
||||
void onCaptcha(String serviceId);
|
||||
|
||||
Reference in New Issue
Block a user