更新登录相关错误码 多设备登录提示优化(没效果)

This commit is contained in:
kehaoyuan
2017-08-25 10:47:15 +08:00
parent e4be47e7e7
commit 82e2aa45c6
14 changed files with 393 additions and 104 deletions

View File

@ -66,7 +66,19 @@ public class LoginUtils {
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
Utils.toast(context, "请检查网络是否可用");
if (e == null) {
Utils.toast(context, "请检查网络是否可用");
return;
}
try {
ResponseBody responseBody = e.response().errorBody();
String string = responseBody.string();
JSONObject content = new JSONObject(string);
int code = content.getInt("code");
outputErrorHint(context, code);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
@ -97,28 +109,16 @@ public class LoginUtils {
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
if (e == null) return;
ResponseBody responseBody = e.response().errorBody();
if (e == null) {
Utils.toast(context, "请检查网络是否可用");
return;
}
try {
ResponseBody responseBody = e.response().errorBody();
String string = responseBody.string();
JSONObject content = new JSONObject(string);
int code = content.getInt("code");
switch (code) {
case 40001:
Utils.toast(context, "无效的手机号码,手机号码格式错误");
break;
case 40002:
Utils.toast(context, "mobile格式错误");
break;
case 40003:
Utils.toast(context, "一分钟内已发送过验证码");
break;
default:
Utils.toast(context, "未知错误");
break;
}
outputErrorHint(context, code);
} catch (Exception e1) {
e1.printStackTrace();
}
@ -188,71 +188,54 @@ public class LoginUtils {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String syncDeviceID = sp.getString("syncDeviceID", null);
if (!TextUtils.isEmpty(syncDeviceID)) {
syncUserData(context, syncDeviceID, token, listener);
syncUserData(context, syncDeviceID, token, listener, loginTag);
return;
}
}
getUserData(context, token, listener);
getUserData(context, token, listener, loginTag);
}
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
if (e == null) {
Utils.toast(context, "请检查网络是否可用");
return;
}
if (e == null) return;
ResponseBody responseBody = e.response().errorBody();
try {
ResponseBody responseBody = e.response().errorBody();
String string = responseBody.string();
JSONObject content = new JSONObject(string);
int code = content.getInt("code");
switch (code) {
case 40000:
Utils.toast(context, "参数错误或不完整");
case 40004:
Utils.toast(context, "缺少mobile参数");
break;
case 40005:
Utils.toast(context, "缺少code参数");
break;
case 40006:
Utils.toast(context, "缺少service_id参数");
break;
case 40007:
Utils.toast(context, "服务已超时");
break;
case 40008:
Utils.toast(context, "验证码已超时");
break;
case 40009:
Utils.toast(context, "验证码错误");
break;
case 40010:
Utils.toast(context, "登录授权失败");
break;
case 40011:
Utils.toast(context, "缺少token参数");
break;
case 40012:
Utils.toast(context, "令牌已过期");
break;
default:
Utils.toast(context, "未知错误");
break;
}
Utils.log("login::" + loginTag, "error_message::" + content.getString("message") + "\n error_code::" + code);
if (loginTag == LoginTag.refresh && code == 40802) {
Utils.log("=======40802::" + string);
JSONObject device = content.getJSONObject("device");
String manufacturer = device.getString("manufacturer");
String model = device.getString("model");
DialogUtils.showAlertDialog(context, "你的账号已在另外一台设备登录"
, StringUtils.buildString("", manufacturer, "", model, "")
, "知道了", null, null, null);
LoginUtils.cleanUserData(context);
// TODO 要不要调用退出登录接口
} else {
outputErrorHint(context, code);
}
} catch (Exception e1) {
e1.printStackTrace();
}
cleanUserData(context);
}
});
}
private static void syncUserData(final Context context, String syncDeviceID, final String token
, final onLoginCallBackListener listener) {
, final onLoginCallBackListener listener, final LoginTag loginTag) {
HashMap<String, String> map = new HashMap<>();
map.put("device_id", syncDeviceID);
@ -264,19 +247,32 @@ public class LoginUtils {
@Override
public void onResponse(ResponseBody response) {
super.onResponse(response);
getUserData(context, token, listener);
getUserData(context, token, listener, loginTag);
}
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
getUserData(context, token, listener);
if (e == null) {
Utils.toast(context, "请检查网络是否可用");
return;
}
try {
ResponseBody responseBody = e.response().errorBody();
String string = responseBody.string();
JSONObject content = new JSONObject(string);
int code = content.getInt("code");
outputErrorHint(context, code);
} catch (Exception e1) {
e1.printStackTrace();
}
getUserData(context, token, listener, loginTag);
}
});
}
// 注销登录
public static void logout(Context context) {
public static void logout(final Context context) {
LoginResponseEntity loginToken = getLoginToken(context);
if (loginToken == null || loginToken.getAccessToken() == null) return;
@ -286,6 +282,23 @@ public class LoginUtils {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<ResponseBody>() {
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
if (e == null) {
Utils.toast(context, "请检查网络是否可用");
return;
}
try {
ResponseBody responseBody = e.response().errorBody();
String string = responseBody.string();
JSONObject content = new JSONObject(string);
int code = content.getInt("code");
outputErrorHint(context, code);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
cleanUserData(context);
@ -310,7 +323,7 @@ public class LoginUtils {
LoginResponseEntity.AccessToken accessToken = loginToken.getAccessToken();
Long accessExpire = accessToken.getExpire();
if (accessExpire != null && accessExpire > Utils.getTime(context)) {
getUserData(context, accessToken.getValue(), listener);
getUserData(context, accessToken.getValue(), listener, null);
} else {
LoginResponseEntity.RefreshToken refreshToken = loginToken.getRefreshToken();
Long refreshExpire = refreshToken.getExpire();
@ -327,7 +340,7 @@ public class LoginUtils {
}
// 获取用户信息
public static void getUserData(final Context context, String token, final onLoginCallBackListener listener) {
public static void getUserData(final Context context, String token, final onLoginCallBackListener listener, final LoginTag loginTag) {
RetrofitManager
.getApi()
.getUserInfo(token)
@ -339,13 +352,26 @@ public class LoginUtils {
super.onResponse(response);
saveUserInfo(context, response);
if (listener != null) {
listener.onLogin(response, null);
listener.onLogin(response, loginTag);
}
}
@Override
public void onFailure(HttpException e) {
super.onFailure(e);
if (e == null) {
Utils.toast(context, "请检查网络是否可用");
return;
}
try {
ResponseBody responseBody = e.response().errorBody();
String string = responseBody.string();
JSONObject content = new JSONObject(string);
int code = content.getInt("code");
outputErrorHint(context, code);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
@ -478,10 +504,157 @@ public class LoginUtils {
loadingDialog.dismiss();
}
Utils.toast(context, "修改失败");
if (e == null) {
Utils.toast(context, "请检查网络是否可用");
return;
}
try {
ResponseBody responseBody = e.response().errorBody();
String string = responseBody.string();
JSONObject content = new JSONObject(string);
int code = content.getInt("code");
outputErrorHint(context, code);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
private static void outputErrorHint(Context context, int code) {
switch (code) {
case 40000:
Utils.toast(context, "参数不全");
break;
case 40001:
Utils.toast(context, "已经发送过短信");
break;
case 40002:
Utils.toast(context, "请求第三方开放平台时发生错误");
break;
case 40003:
Utils.toast(context, "上传用户头像时发生错误");
break;
case 40101:
Utils.toast(context, "缺少参数 app_id");
break;
case 40102:
Utils.toast(context, "缺少签名验证的头信息");
break;
case 40104:
Utils.toast(context, "缺少token");
break;
case 40105:
Utils.toast(context, "缺少手机号码");
break;
case 40106:
Utils.toast(context, "缺少用户名");
break;
case 40107:
Utils.toast(context, "缺少密码参数");
break;
case 40202:
Utils.toast(context, "无效的手机号码");
break;
case 40203:
Utils.toast(context, "无效的用户名");
break;
case 40204:
Utils.toast(context, "无效的头像地址");
break;
case 40205:
Utils.toast(context, "无效的性别参数");
break;
case 40206:
Utils.toast(context, "无效的地区参数");
break;
case 40208:
Utils.toast(context, "无效的密码");
break;
case 40209:
Utils.toast(context, "无效的URL 地址");
break;
case 42000:
Utils.toast(context, "无效的app_id");
break;
case 42001:
Utils.toast(context, "无效的app_secret");
break;
case 42002:
Utils.toast(context, "无效的Union_id");
break;
case 42003:
Utils.toast(context, "无效的设备信息");
break;
case 42004:
Utils.toast(context, "无效的请求");
break;
case 40301:
Utils.toast(context, "签名验证失败");
break;
case 40302:
Utils.toast(context, "验证码错误");
break;
case 40303:
Utils.toast(context, "密码错误");
break;
case 40304:
Utils.toast(context, "不支持该种方式登录");
break;
case 40305:
Utils.toast(context, "错误的状态值(应用只有两种状态: working / stop)");
break;
case 40306:
Utils.toast(context, "传递了无法识别的参数");
break;
case 40401:
Utils.toast(context, "token过期");
break;
case 40402:
Utils.toast(context, "Service_id过期,主要原因是:收到手机短信验证码后长时间没有进行登录操作");
break;
case 40403:
Utils.toast(context, "验证码已过期");
break;
case 40501:
Utils.toast(context, "同名应用已经存在");
break;
case 40502:
Utils.toast(context, "用户名已存在");
break;
case 40503:
Utils.toast(context, "名称已经存在");
break;
case 40601:
Utils.toast(context, "应用不存在");
break;
case 40602:
Utils.toast(context, "用户不存在");
break;
case 40603:
Utils.toast(context, "用户系统不存在");
break;
case 40604:
Utils.toast(context, "用户已被冻结");
break;
case 40605:
Utils.toast(context, "用户没有冻结");
break;
case 40606:
Utils.toast(context, "该应用被停止运行了");
break;
case 40801:
Utils.toast(context, "访问过于频繁");
break;
default:
Utils.toast(context, "未知错误");
break;
}
}
// 更改用户信息回调
public interface onChangeUserInfoListener {
void onChange();