345 lines
12 KiB
Java
345 lines
12 KiB
Java
package com.gh.common.util;
|
|
|
|
import android.content.Context;
|
|
import android.text.TextUtils;
|
|
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.retrofit.JSONObjectResponse;
|
|
import com.gh.gamecenter.retrofit.Response;
|
|
import com.gh.gamecenter.retrofit.RetrofitManager;
|
|
import com.lightgame.utils.Utils;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
import io.reactivex.schedulers.Schedulers;
|
|
import okhttp3.MediaType;
|
|
import okhttp3.RequestBody;
|
|
import okhttp3.ResponseBody;
|
|
import retrofit2.HttpException;
|
|
|
|
/**
|
|
* Created by khy on 7/07/17.
|
|
*/
|
|
|
|
// TODO: 1/12/17 逐步整理 删除
|
|
public class LoginUtils {
|
|
|
|
// 获取验证码
|
|
public static void getPhoneCaptcha(final Context context, String phoneNum, final onCaptchaCallBackListener listener) {
|
|
JSONObject content = new JSONObject();
|
|
try {
|
|
content.put("mobile", phoneNum);
|
|
content.put("device", DeviceUtils.getLoginDevice(context.getApplicationContext()));
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
RequestBody body = RequestBody.create(MediaType.parse("application/json"), content.toString());
|
|
RetrofitManager.getInstance(context)
|
|
.getApi()
|
|
.loginByCaptcha(body)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new JSONObjectResponse() {
|
|
@Override
|
|
public void onResponse(JSONObject response) {
|
|
super.onResponse(response);
|
|
try {
|
|
listener.onCaptcha(response.getString("service_id"));
|
|
Utils.toast(context, "验证码短信已发送,请注意查收");
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@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");
|
|
if (code == 403202) {
|
|
captchaErrorHint(context, content);
|
|
} else {
|
|
outputErrorHint(context, code);
|
|
}
|
|
} catch (Exception e1) {
|
|
e1.printStackTrace();
|
|
Utils.toast(context, "无法获取验证码,请检查你的网络状态");
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
// 注销登录
|
|
public static void logout(final Context context, final OnLogoutListener listener) {
|
|
|
|
RetrofitManager.getInstance(context)
|
|
.getApi()
|
|
.logout()
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(new Response<ResponseBody>() {
|
|
|
|
@Override
|
|
public void onResponse(ResponseBody response) {
|
|
super.onResponse(response);
|
|
listener.onCompleted();
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(HttpException e) {
|
|
super.onFailure(e);
|
|
listener.onCompleted();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 请用 ErrorHelper.handleError 替代本方法
|
|
*/
|
|
@Deprecated
|
|
public static void userPostErrorToast(String errorString, Context context, boolean isQuestion) {
|
|
if (TextUtils.isEmpty(errorString)) {
|
|
Utils.toast(context, R.string.post_failure_hint);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
JSONObject errorJson = new JSONObject(errorString);
|
|
Iterator<String> iterator = errorJson.keys();
|
|
while (iterator.hasNext()) {
|
|
String key = iterator.next();
|
|
if ("toast".equals(key)) {
|
|
Utils.toast(context, errorJson.get(key).toString());
|
|
return;
|
|
}
|
|
}
|
|
|
|
int errorCode = errorJson.getInt("code");
|
|
switch (errorCode) {
|
|
case 403018:
|
|
Utils.toast(context, R.string.comment_failed_unable);
|
|
case 403050:
|
|
case 403053:
|
|
case 403048:
|
|
case 403049:
|
|
case 403057:
|
|
case 403045:
|
|
case 403046:
|
|
case 403054:
|
|
Utils.toast(context, R.string.comment_failed_userbanned);
|
|
break;
|
|
case 403051:
|
|
Utils.toast(context, R.string.comment_failed_userblocked);
|
|
break;
|
|
case 403020:
|
|
if (isQuestion) {
|
|
DialogUtils.showAlertDialog(context, "限制提醒"
|
|
, "提问过于频繁,请先休息一下哦", "知道了"
|
|
, null, null, null);
|
|
} else {
|
|
Utils.toast(context, R.string.comment_failed_toofrequent);
|
|
}
|
|
break;
|
|
case 403021:
|
|
Utils.toast(context, R.string.comment_failed_illegal);
|
|
break;
|
|
case 403047:
|
|
Utils.toast(context, R.string.deny_vote_answer);
|
|
break;
|
|
default:
|
|
Utils.toast(context, R.string.post_failure_hint);
|
|
break;
|
|
}
|
|
} catch (Exception e) {
|
|
Utils.toast(context, R.string.post_failure_hint);
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void captchaErrorHint(Context context, JSONObject content) {
|
|
try {
|
|
JSONObject detail = content.getJSONObject("detail");
|
|
if ("isv.BUSINESS_LIMIT_CONTROL".equals(detail.getString("sub_code"))) {
|
|
Utils.toast(context, "获取验证码太频繁,请稍后再试");
|
|
} else {
|
|
Utils.toast(context, detail.getString("sub_msg"));
|
|
}
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
public static void outputErrorHint(Context context, int code) {
|
|
switch (code) {
|
|
case 40000:
|
|
Utils.toast(context, "参数不全");
|
|
break;
|
|
case 400001:
|
|
Utils.toast(context, "验证码获取过快,请稍后重试");// 已经发送过短信
|
|
break;
|
|
case 400002:
|
|
Utils.toast(context, "请求第三方开放平台时发生错误");
|
|
break;
|
|
case 400003:
|
|
Utils.toast(context, "上传用户头像时发生错误");
|
|
break;
|
|
case 400101:
|
|
Utils.toast(context, "缺少参数 app_id");
|
|
break;
|
|
case 400102:
|
|
Utils.toast(context, "缺少签名验证的头信息");
|
|
break;
|
|
case 400104:
|
|
Utils.toast(context, "缺少token");
|
|
break;
|
|
case 400105:
|
|
Utils.toast(context, "缺少手机号码");
|
|
break;
|
|
case 400106:
|
|
Utils.toast(context, "缺少用户名");
|
|
break;
|
|
case 400107:
|
|
Utils.toast(context, "缺少密码参数");
|
|
break;
|
|
case 400202:
|
|
Utils.toast(context, "无效的手机号码");
|
|
break;
|
|
case 400203:
|
|
Utils.toast(context, "无效的用户名");
|
|
break;
|
|
case 400204:
|
|
Utils.toast(context, "无效的头像地址");
|
|
break;
|
|
case 400205:
|
|
Utils.toast(context, "无效的性别参数");
|
|
break;
|
|
case 400206:
|
|
Utils.toast(context, "无效的地区参数");
|
|
break;
|
|
case 400208:
|
|
Utils.toast(context, "无效的密码");
|
|
break;
|
|
case 400209:
|
|
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 400301:
|
|
Utils.toast(context, "签名验证失败");
|
|
break;
|
|
case 400302:
|
|
Utils.toast(context, "验证码错误");
|
|
break;
|
|
case 400303:
|
|
Utils.toast(context, "密码错误");
|
|
break;
|
|
case 400304:
|
|
Utils.toast(context, "不支持该种方式登录");
|
|
break;
|
|
case 400305:
|
|
Utils.toast(context, "错误的状态值(应用只有两种状态: working / stop)");
|
|
break;
|
|
case 400306:
|
|
Utils.toast(context, "传递了无法识别的参数");
|
|
break;
|
|
case 400401:
|
|
Utils.toast(context, "token过期");
|
|
break;
|
|
case 400402:
|
|
Utils.toast(context, "Service_id过期,主要原因是:收到手机短信验证码后长时间没有进行登录操作");
|
|
break;
|
|
case 400403:
|
|
Utils.toast(context, "验证码已过期");
|
|
break;
|
|
case 400501:
|
|
Utils.toast(context, "同名应用已经存在");
|
|
break;
|
|
case 400502:
|
|
Utils.toast(context, "昵称已存在");
|
|
break;
|
|
case 400503:
|
|
Utils.toast(context, "名称已经存在");
|
|
break;
|
|
case 400601:
|
|
Utils.toast(context, "应用不存在");
|
|
break;
|
|
case 400602:
|
|
Utils.toast(context, "用户不存在");
|
|
break;
|
|
case 400603:
|
|
Utils.toast(context, "用户系统不存在");
|
|
break;
|
|
case 400604:
|
|
Utils.toast(context, "用户已被冻结");
|
|
break;
|
|
case 400605:
|
|
Utils.toast(context, "用户没有冻结");
|
|
break;
|
|
case 400606:
|
|
Utils.toast(context, "该应用被停止运行了");
|
|
break;
|
|
case 400801:
|
|
Utils.toast(context, "访问过于频繁");
|
|
break;
|
|
case 403007:
|
|
Utils.toast(context, "设备异常,获取验证码失败,请更换登陆方式或明天再试");
|
|
break;
|
|
case 403202:
|
|
Utils.toast(context, "403202");
|
|
break;
|
|
case 400213:
|
|
case 403016:
|
|
Utils.toast(context, "内容违规,请修改后再保存");
|
|
break;
|
|
case 403801:
|
|
Utils.toast(context, "获取验证码太频繁,请稍后再试");
|
|
break;
|
|
case 403204:
|
|
Utils.toast(context, "获取验证码太频繁,请稍后再试");
|
|
break;
|
|
default:
|
|
Utils.toast(context, code + "");
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
// 更改用户信息回调
|
|
public interface OnLogoutListener {
|
|
void onCompleted();
|
|
}
|
|
|
|
// 获取验证码回调
|
|
public interface onCaptchaCallBackListener {
|
|
void onCaptcha(String serviceId);
|
|
}
|
|
|
|
}
|