|
|
package com.rehome.zhdcoa.utils;
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
import android.content.Context;
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
import com.rehome.zhdcoa.Contans;
|
|
|
import com.rehome.zhdcoa.bean.UserAuthenticationAIBean;
|
|
|
import com.yolanda.nohttp.NoHttp;
|
|
|
import com.yolanda.nohttp.RequestMethod;
|
|
|
import com.yolanda.nohttp.rest.Request;
|
|
|
import com.yolanda.nohttp.rest.Response;
|
|
|
import static com.rehome.zhdcoa.utils.GsonUtils.GsonToBean;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
|
|
|
public class AuthenticationLoginAIUtils {
|
|
|
/**
|
|
|
* @param mActivity Activity
|
|
|
* @param account 工号
|
|
|
* @param password 密码
|
|
|
* @param listener 监听回调
|
|
|
*/
|
|
|
public static void authenticationAILogin(Activity mActivity, String account, String password, OnAuthenticationLoginListener listener) {
|
|
|
|
|
|
Request<String> request = NoHttp.createStringRequest(Contans.BASE_URL_AI_3D_SERVER + Contans.AUTHENTICATIONLOGINAI3D, RequestMethod.POST);
|
|
|
// request.add("account", account);
|
|
|
// request.add("password", password);
|
|
|
// request.addHeader("Content-Type", Headers.HEAD_VALUE_ACCEPT_APPLICATION_X_WWW_FORM_URLENCODED);
|
|
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
params.put("account", account);
|
|
|
params.put("password", password);
|
|
|
|
|
|
String json = GsonUtils.GsonString(params);
|
|
|
showLog(json,mActivity);
|
|
|
String jsonEncrypt = RSAUtils.encryptBASE64Str(json);
|
|
|
//showLog(jsonEncrypt);
|
|
|
request.setDefineRequestBodyForJson(jsonEncrypt);
|
|
|
|
|
|
if (Contans.BASE_URL_AI_3D_SERVER.equals(Contans.BASE_URL_AI_3D_SERVER_EXTRANET)) {
|
|
|
SSLSocketFactory socketFactory = NohttpUtils.getSSLSocketFactory(mActivity);
|
|
|
if (socketFactory != null) {
|
|
|
request.setSSLSocketFactory(socketFactory);
|
|
|
request.setHostnameVerifier(new HostnameVerifier() {
|
|
|
@Override
|
|
|
public boolean verify(String s, SSLSession sslSession) {
|
|
|
return true;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
NohttpUtils.getInstance().add(mActivity, 0, request, new HttpListener<String>() {
|
|
|
|
|
|
@Override
|
|
|
public void onSucceed(int what, Response<String> response) {
|
|
|
|
|
|
showLog("----------------",mActivity);
|
|
|
String result = response.get();
|
|
|
showLog(result,mActivity);
|
|
|
|
|
|
String jsonDecode = RSAUtils.decryptBASE64StrClient(result);
|
|
|
if (TextUtils.isEmpty(jsonDecode)) {
|
|
|
//解密失败
|
|
|
showLog("APP解密失败",mActivity);
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
} else {
|
|
|
showLog(jsonDecode,mActivity);
|
|
|
UserAuthenticationAIBean bean = GsonToBean(jsonDecode, UserAuthenticationAIBean.class);
|
|
|
if (bean != null) {
|
|
|
if (bean.isSuccess()) {//登录成功
|
|
|
SPUtils.put(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, bean.getData().getToken());
|
|
|
listener.onAuthenticationSuccess(true, bean.getData().getToken());
|
|
|
} else {
|
|
|
if (bean.getFlag() == -1) {
|
|
|
if (bean.getMsg() != null && bean.getMsg().equals("用户未注销")) {
|
|
|
String token = (String) SPUtils.get(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, String.valueOf(""));
|
|
|
listener.onAuthenticationSuccess(true, token);
|
|
|
}
|
|
|
} else {
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
if (bean.getFlag() == -2) {
|
|
|
showLog("没有传参",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -3) {
|
|
|
showLog("服务器解密失败",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -4) {
|
|
|
showLog("Json 格式不正确",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -5) {
|
|
|
showLog("账号密码不能为空",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -6) {
|
|
|
showLog("账号或密码错误",mActivity);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 状态码 flag:
|
|
|
// -1:已登录过
|
|
|
// -2:没有传参
|
|
|
// -3:解密失败
|
|
|
// -4:Json 格式不正确
|
|
|
// -5:账号密码不能为空
|
|
|
// -6:账号或密码错误
|
|
|
|
|
|
// String cookieId = "";
|
|
|
// // 响应头
|
|
|
// Headers headers = response.getHeaders();
|
|
|
// List<HttpCookie> cookies = headers.getCookies();
|
|
|
// Log.i("app", String.valueOf(cookies.size()));
|
|
|
// for (HttpCookie httpCookie : cookies) {
|
|
|
// String cookieName = httpCookie.getName();
|
|
|
// if ("JSESSIONID".equals(cookieName)) {
|
|
|
// // 这里就拿到了你想那的cookie
|
|
|
// cookieId = httpCookie.getValue();
|
|
|
// }
|
|
|
// }
|
|
|
// showLog("--------");
|
|
|
// showLog(cookieId);
|
|
|
// if (cookies.size() > 0 && !(cookieId.equals(""))) {
|
|
|
// SPUtils.put(context, Contans.AUTHENTICATIONLOGINTOKENAI, cookieId);
|
|
|
// }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onFailed(int what, Response<String> response) {
|
|
|
showLog("onFailed",mActivity);
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public static void authenticationAILoginShowProgress(Activity mActivity, String account, String password, OnAuthenticationLoginListener listener) {
|
|
|
|
|
|
Request<String> request = NoHttp.createStringRequest(Contans.BASE_URL_AI_3D_SERVER + Contans.AUTHENTICATIONLOGINAI3D, RequestMethod.POST);
|
|
|
// request.add("account", account);
|
|
|
// request.add("password", password);
|
|
|
// request.addHeader("Content-Type", Headers.HEAD_VALUE_ACCEPT_APPLICATION_X_WWW_FORM_URLENCODED);
|
|
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
params.put("account", account);
|
|
|
params.put("password", password);
|
|
|
|
|
|
String json = GsonUtils.GsonString(params);
|
|
|
//showLog(json);
|
|
|
String jsonEncrypt = RSAUtils.encryptBASE64Str(json);
|
|
|
//showLog(jsonEncrypt);
|
|
|
request.setDefineRequestBodyForJson(jsonEncrypt);
|
|
|
|
|
|
if (Contans.BASE_URL_AI_3D_SERVER.equals(Contans.BASE_URL_AI_3D_SERVER_EXTRANET)) {
|
|
|
SSLSocketFactory socketFactory = NohttpUtils.getSSLSocketFactory(mActivity);
|
|
|
if (socketFactory != null) {
|
|
|
request.setSSLSocketFactory(socketFactory);
|
|
|
request.setHostnameVerifier(new HostnameVerifier() {
|
|
|
@Override
|
|
|
public boolean verify(String s, SSLSession sslSession) {
|
|
|
return true;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
NohttpUtils.getInstance().add(mActivity, 0, request, new HttpListener<String>() {
|
|
|
|
|
|
@Override
|
|
|
public void onSucceed(int what, Response<String> response) {
|
|
|
|
|
|
showLog("----------------",mActivity);
|
|
|
String result = response.get();
|
|
|
showLog(result,mActivity);
|
|
|
|
|
|
String jsonDecode = RSAUtils.decryptBASE64StrClient(result);
|
|
|
if (TextUtils.isEmpty(jsonDecode)) {
|
|
|
//解密失败
|
|
|
showLog("APP解密失败",mActivity);
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
} else {
|
|
|
showLog(jsonDecode,mActivity);
|
|
|
UserAuthenticationAIBean bean = GsonToBean(jsonDecode, UserAuthenticationAIBean.class);
|
|
|
if (bean != null) {
|
|
|
if (bean.isSuccess()) {//登录成功
|
|
|
SPUtils.put(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, bean.getData().getToken());
|
|
|
listener.onAuthenticationSuccess(true, bean.getData().getToken());
|
|
|
} else {
|
|
|
if (bean.getFlag() == -1) {
|
|
|
if (bean.getMsg() != null && bean.getMsg().equals("用户未注销")) {
|
|
|
String token = (String) SPUtils.get(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, String.valueOf(""));
|
|
|
listener.onAuthenticationSuccess(true, token);
|
|
|
}
|
|
|
} else {
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
if (bean.getFlag() == -2) {
|
|
|
showLog("没有传参",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -3) {
|
|
|
showLog("服务器解密失败",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -4) {
|
|
|
showLog("Json 格式不正确",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -5) {
|
|
|
showLog("账号密码不能为空",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -6) {
|
|
|
showLog("账号或密码错误",mActivity);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 状态码 flag:
|
|
|
// -1:已登录过
|
|
|
// -2:没有传参
|
|
|
// -3:解密失败
|
|
|
// -4:Json 格式不正确
|
|
|
// -5:账号密码不能为空
|
|
|
// -6:账号或密码错误
|
|
|
|
|
|
// String cookieId = "";
|
|
|
// // 响应头
|
|
|
// Headers headers = response.getHeaders();
|
|
|
// List<HttpCookie> cookies = headers.getCookies();
|
|
|
// Log.i("app", String.valueOf(cookies.size()));
|
|
|
// for (HttpCookie httpCookie : cookies) {
|
|
|
// String cookieName = httpCookie.getName();
|
|
|
// if ("JSESSIONID".equals(cookieName)) {
|
|
|
// // 这里就拿到了你想那的cookie
|
|
|
// cookieId = httpCookie.getValue();
|
|
|
// }
|
|
|
// }
|
|
|
// showLog("--------");
|
|
|
// showLog(cookieId);
|
|
|
// if (cookies.size() > 0 && !(cookieId.equals(""))) {
|
|
|
// SPUtils.put(context, Contans.AUTHENTICATIONLOGINTOKENAI, cookieId);
|
|
|
// }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onFailed(int what, Response<String> response) {
|
|
|
showLog("onFailed",mActivity);
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public static void authenticationDeviceAlermInfoShowProgress(Activity mActivity, String account, String password, OnAuthenticationLoginListener listener) {
|
|
|
|
|
|
Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.DeviceAlermInfoLoginUrl, RequestMethod.POST);
|
|
|
|
|
|
// Map<String, String> params = new HashMap<>();
|
|
|
// params.put("username", account);
|
|
|
// params.put("password", password);
|
|
|
//
|
|
|
// String json = GsonUtils.GsonString(params);
|
|
|
//showLog(json);
|
|
|
//String jsonEncrypt = RSAUtils.encryptBASE64Str(json);
|
|
|
//showLog(jsonEncrypt);
|
|
|
//request.setDefineRequestBodyForJson(json);
|
|
|
|
|
|
request.add("username", account);
|
|
|
request.add("password", password);
|
|
|
|
|
|
if (Contans.IP.equals(Contans.IP_EXTRANET)) {
|
|
|
SSLSocketFactory socketFactory = NohttpUtils.getSSLSocketFactory(mActivity);
|
|
|
if (socketFactory != null) {
|
|
|
request.setSSLSocketFactory(socketFactory);
|
|
|
request.setHostnameVerifier(new HostnameVerifier() {
|
|
|
@Override
|
|
|
public boolean verify(String s, SSLSession sslSession) {
|
|
|
return true;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
NohttpUtils.getInstance().add(mActivity, 0, request, new HttpListener<String>() {
|
|
|
|
|
|
@Override
|
|
|
public void onSucceed(int what, Response<String> response) {
|
|
|
|
|
|
showLog("----------------",mActivity);
|
|
|
String result = response.get();
|
|
|
showLog(result,mActivity);
|
|
|
|
|
|
if(!TextUtils.isEmpty(result)){
|
|
|
UserAuthenticationAIBean bean = GsonToBean(result, UserAuthenticationAIBean.class);
|
|
|
if (bean != null) {
|
|
|
if (bean.isSuccess()&&bean.getCode()==20000) {//登录成功
|
|
|
SPUtils.put(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, bean.getData().getToken());
|
|
|
listener.onAuthenticationSuccess(true, bean.getData().getToken());
|
|
|
} else {
|
|
|
if (bean.getFlag() == -1) {
|
|
|
if (bean.getMsg() != null && bean.getMsg().equals("用户未注销")) {
|
|
|
String token = (String) SPUtils.get(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, String.valueOf(""));
|
|
|
listener.onAuthenticationSuccess(true, token);
|
|
|
}
|
|
|
} else {
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
if (bean.getFlag() == -2) {
|
|
|
showLog("没有传参",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -3) {
|
|
|
showLog("服务器解密失败",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -4) {
|
|
|
showLog("Json 格式不正确",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -5) {
|
|
|
showLog("账号密码不能为空",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -6) {
|
|
|
showLog("账号或密码错误",mActivity);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onFailed(int what, Response<String> response) {
|
|
|
showLog("onFailed",mActivity);
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public static void authenticationDeviceAlermInfoRsaShowProgress(Activity mActivity, String account, String password, OnAuthenticationLoginListener listener) {
|
|
|
|
|
|
Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.DeviceAlermInfoLoginUrl, RequestMethod.POST);
|
|
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
params.put("username", account);
|
|
|
params.put("password", password);
|
|
|
|
|
|
String json = GsonUtils.GsonString(params);
|
|
|
//showLog(json);
|
|
|
String jsonEncrypt = RSAUtils.encryptBASE64Str(json);
|
|
|
//showLog(jsonEncrypt);
|
|
|
request.setDefineRequestBodyForJson(jsonEncrypt);
|
|
|
|
|
|
if (Contans.IP.equals(Contans.IP_EXTRANET)) {
|
|
|
SSLSocketFactory socketFactory = NohttpUtils.getSSLSocketFactory(mActivity);
|
|
|
if (socketFactory != null) {
|
|
|
request.setSSLSocketFactory(socketFactory);
|
|
|
request.setHostnameVerifier(new HostnameVerifier() {
|
|
|
@Override
|
|
|
public boolean verify(String s, SSLSession sslSession) {
|
|
|
return true;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
NohttpUtils.getInstance().add(mActivity, 0, request, new HttpListener<String>() {
|
|
|
|
|
|
@Override
|
|
|
public void onSucceed(int what, Response<String> response) {
|
|
|
|
|
|
showLog("----------------",mActivity);
|
|
|
String result = response.get();
|
|
|
showLog(result,mActivity);
|
|
|
|
|
|
if(!TextUtils.isEmpty(result)){
|
|
|
UserAuthenticationAIBean bean = GsonToBean(result, UserAuthenticationAIBean.class);
|
|
|
if (bean != null) {
|
|
|
if (bean.isSuccess()&&bean.getCode()==20000) {//登录成功
|
|
|
SPUtils.put(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, bean.getData().getToken());
|
|
|
listener.onAuthenticationSuccess(true, bean.getData().getToken());
|
|
|
} else {
|
|
|
if (bean.getFlag() == -1) {
|
|
|
if (bean.getMsg() != null && bean.getMsg().equals("用户未注销")) {
|
|
|
String token = (String) SPUtils.get(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, String.valueOf(""));
|
|
|
listener.onAuthenticationSuccess(true, token);
|
|
|
}
|
|
|
} else {
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
if (bean.getFlag() == -2) {
|
|
|
showLog("没有传参",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -3) {
|
|
|
showLog("服务器解密失败",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -4) {
|
|
|
showLog("Json 格式不正确",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -5) {
|
|
|
showLog("账号密码不能为空",mActivity);
|
|
|
}
|
|
|
if (bean.getFlag() == -6) {
|
|
|
showLog("账号或密码错误",mActivity);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onFailed(int what, Response<String> response) {
|
|
|
showLog("onFailed",mActivity);
|
|
|
listener.onAuthenticationSuccess(false, "");
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
public static void showLog(String logText,Context context) {
|
|
|
if (isApkInDebug(context)) {
|
|
|
if (TextUtils.isEmpty(logText)) {
|
|
|
Log.i("app", "logText is null");
|
|
|
} else {
|
|
|
Log.i("app", logText);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断当前应用是否是debug状态
|
|
|
*/
|
|
|
|
|
|
public static boolean isApkInDebug(Context context) {
|
|
|
try {
|
|
|
ApplicationInfo info = context.getApplicationInfo();
|
|
|
return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
|
|
|
} catch (Exception e) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
} |