387 lines
15 KiB
Java
387 lines
15 KiB
Java
package com.gh.common.util;
|
||
|
||
import android.app.Dialog;
|
||
import android.content.Context;
|
||
import android.content.SharedPreferences;
|
||
import android.preference.PreferenceManager;
|
||
import android.text.TextUtils;
|
||
|
||
import com.gh.gamecenter.entity.LoginResponseEntity;
|
||
import com.gh.gamecenter.entity.UserInfoEntity;
|
||
import com.gh.gamecenter.retrofit.JSONObjectResponse;
|
||
import com.gh.gamecenter.retrofit.Response;
|
||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||
import com.google.gson.Gson;
|
||
import com.google.gson.reflect.TypeToken;
|
||
import com.lightgame.utils.Utils;
|
||
|
||
import org.json.JSONException;
|
||
import org.json.JSONObject;
|
||
|
||
import java.lang.reflect.Type;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
import okhttp3.MediaType;
|
||
import okhttp3.RequestBody;
|
||
import okhttp3.ResponseBody;
|
||
import retrofit2.HttpException;
|
||
import rx.Observable;
|
||
import rx.android.schedulers.AndroidSchedulers;
|
||
import rx.schedulers.Schedulers;
|
||
|
||
/**
|
||
* Created by khy on 7/07/17.
|
||
*/
|
||
|
||
public class LoginUtils {
|
||
|
||
public enum LoginTag {
|
||
qq, wechat, weibo, phone, refresh
|
||
}
|
||
|
||
// 获取验证码
|
||
public static void getPhoneCaptcha(final Context context, String phoneNum, final onCaptchaCallBackListener listener) {
|
||
Map<String, String> params = new HashMap<>();
|
||
params.put("mobile", phoneNum);
|
||
RequestBody body = RequestBody.create(MediaType.parse("application/json"),
|
||
new JSONObject(params).toString());
|
||
RetrofitManager
|
||
.getUsersea()
|
||
.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"));
|
||
} catch (JSONException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(HttpException e) {
|
||
super.onFailure(e);
|
||
int code = e.code();
|
||
switch (code) {
|
||
case 40001:
|
||
Utils.toast(context, "缺少mobile参数");
|
||
break;
|
||
case 40002:
|
||
Utils.toast(context, "mobile格式错误");
|
||
break;
|
||
case 40003:
|
||
Utils.toast(context, "一分钟内已发送过验证码");
|
||
break;
|
||
default:
|
||
Utils.toast(context, "未知错误");
|
||
break;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
// 登录前,做好body判断
|
||
public static void login(final Context context, final JSONObject content, final LoginTag loginTag,
|
||
final onLoginCallBackListener listener) {
|
||
RequestBody body = RequestBody.create(MediaType.parse("application/json"), content.toString());
|
||
|
||
Observable<LoginResponseEntity> observable;
|
||
|
||
if (loginTag == LoginTag.weibo) {
|
||
observable = RetrofitManager.getUsersea().loginByWeibo(body);
|
||
} else if (loginTag == LoginTag.qq) {
|
||
observable = RetrofitManager.getUsersea().loginByQQ(body);
|
||
} else if (loginTag == LoginTag.wechat) {
|
||
observable = RetrofitManager.getUsersea().loginByWechat(body);
|
||
} else if (loginTag == LoginTag.phone) {
|
||
observable = RetrofitManager.getUsersea().loginByMobile(body);
|
||
} else if (loginTag == LoginTag.refresh) {
|
||
observable = RetrofitManager.getUsersea().refreshToken(body);
|
||
} else {
|
||
return;
|
||
}
|
||
|
||
observable
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<LoginResponseEntity>() {
|
||
@Override
|
||
public void onResponse(LoginResponseEntity response) {
|
||
super.onResponse(response);
|
||
if (loginTag.equals(LoginTag.refresh)) {
|
||
LoginResponseEntity loginToken = getLoginToken(context);
|
||
if (loginToken != null) {
|
||
response.setLoginType(loginToken.getLoginType());
|
||
}
|
||
} else {
|
||
if (loginTag.equals(LoginTag.phone)) {
|
||
try {
|
||
response.setLoginType(content.getString("mobile"));
|
||
} catch (JSONException e) {
|
||
e.printStackTrace();
|
||
}
|
||
} else {
|
||
response.setLoginType(loginTag.name());
|
||
}
|
||
|
||
}
|
||
saveLoginToken(context, response);
|
||
getUserData(context, response.getAccessToken().getValue(), listener);
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(HttpException e) {
|
||
super.onFailure(e);
|
||
|
||
if (e == null) return;
|
||
|
||
ResponseBody responseBody = e.response().errorBody();
|
||
try {
|
||
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", "error_message::" + content.getString("message") + "\n error_code::" + code);
|
||
} catch (Exception e1) {
|
||
e1.printStackTrace();
|
||
}
|
||
|
||
cleanUserData(context);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 注销登录
|
||
public static void logout(Context context) {
|
||
cleanUserData(context);
|
||
|
||
LoginResponseEntity loginToken = getLoginToken(context);
|
||
if (loginToken == null || loginToken.getAccessToken() == null) return;
|
||
|
||
RetrofitManager
|
||
.getUsersea()
|
||
.logout(loginToken.getAccessToken().getValue())
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<ResponseBody>(){});
|
||
}
|
||
|
||
// 清除本地用户相关信息
|
||
public static void cleanUserData(Context context) {
|
||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||
SharedPreferences.Editor edit = sp.edit();
|
||
edit.putString("user_info", null);
|
||
edit.putString("login_token", null);
|
||
edit.apply();
|
||
|
||
GetLoginDataUtils.getInstance(context).QQLogout();
|
||
}
|
||
|
||
// 检查本地是否有缓存token,有则马上登录
|
||
public static void checkLogin(Context context, onLoginCallBackListener listener) {
|
||
LoginResponseEntity loginToken = getLoginToken(context);
|
||
|
||
if (loginToken != null && loginToken.getAccessToken() != null) {
|
||
LoginResponseEntity.AccessToken accessToken = loginToken.getAccessToken();
|
||
Long accessExpire = accessToken.getExpire();
|
||
if (accessExpire != null && accessExpire > Utils.getTime(context)) {
|
||
getUserData(context, accessToken.getValue(), listener);
|
||
} else {
|
||
LoginResponseEntity.RefreshToken refreshToken = loginToken.getRefreshToken();
|
||
Long refreshExpire = refreshToken.getExpire();
|
||
if (refreshExpire != null && refreshExpire > Utils.getTime(context)) {
|
||
Map<String, String> params = new HashMap<>();
|
||
params.put("refresh_token", refreshToken.getValue());
|
||
login(context, new JSONObject(params), LoginTag.refresh, listener); // TODO 刷新accessToken
|
||
} else {
|
||
// TODO 重新登录
|
||
cleanUserData(context);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 获取用户信息
|
||
public static void getUserData(final Context context, String token, final onLoginCallBackListener listener) {
|
||
RetrofitManager
|
||
.getUsersea()
|
||
.getUserInfo(token)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Response<UserInfoEntity>() {
|
||
@Override
|
||
public void onResponse(UserInfoEntity response) {
|
||
super.onResponse(response);
|
||
saveUserInfo(context, response);
|
||
listener.onLogin(response, null);
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(HttpException e) {
|
||
super.onFailure(e);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 获取本地缓存用户信息
|
||
public static UserInfoEntity getUserInfo(Context context) {
|
||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||
String loginData = sp.getString("user_info", null);
|
||
if (!TextUtils.isEmpty(loginData)) {
|
||
Type listType = new TypeToken<UserInfoEntity>() {
|
||
}.getType();
|
||
return new Gson().fromJson(loginData, listType);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
// 在本地缓存用户信息
|
||
public static void saveUserInfo(Context context, UserInfoEntity entity) {
|
||
if (entity != null) {
|
||
String s = new Gson().toJson(entity);
|
||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||
SharedPreferences.Editor edit = sp.edit();
|
||
edit.putString("user_info", s);
|
||
edit.apply();
|
||
}
|
||
}
|
||
|
||
//获取本地缓存用户token
|
||
public static LoginResponseEntity getLoginToken(Context context) {
|
||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||
String loginData = sp.getString("login_token", null);
|
||
if (!TextUtils.isEmpty(loginData)) {
|
||
Type listType = new TypeToken<LoginResponseEntity>() {
|
||
}.getType();
|
||
return new Gson().fromJson(loginData, listType);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// 在本地缓存用户token
|
||
public static void saveLoginToken(Context context, LoginResponseEntity entity) {
|
||
if (entity != null) {
|
||
String s = new Gson().toJson(entity);
|
||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||
SharedPreferences.Editor edit = sp.edit();
|
||
edit.putString("login_token", s);
|
||
edit.apply();
|
||
}
|
||
}
|
||
|
||
//更改用户信息
|
||
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;
|
||
case "userIcon":
|
||
entity.setIcon(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);
|
||
}
|
||
|
||
// 登录回调
|
||
public interface onLoginCallBackListener {
|
||
void onLogin(UserInfoEntity entity, LoginTag loginTag);
|
||
}
|
||
}
|