You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

441 lines
20 KiB
Java

3 years ago
package com.rehome.zhdcoa.utils;
1 year ago
import android.app.Activity;
3 years ago
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.text.TextUtils;
3 years ago
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;
3 years ago
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
3 years ago
public class AuthenticationLoginAIUtils {
/**
1 year ago
* @param mActivity Activity
* @param account
3 years ago
* @param password
* @param listener
*/
1 year ago
public static void authenticationAILogin(Activity mActivity, String account, String password, OnAuthenticationLoginListener listener) {
3 years ago
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);
3 years ago
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)) {
1 year ago
SSLSocketFactory socketFactory = NohttpUtils.getSSLSocketFactory(mActivity);
3 years ago
if (socketFactory != null) {
request.setSSLSocketFactory(socketFactory);
request.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
});
}
}
1 year ago
NohttpUtils.getInstance().add(mActivity, 0, request, new HttpListener<String>() {
3 years ago
@Override
public void onSucceed(int what, Response<String> response) {
1 year ago
showLog("----------------",mActivity);
3 years ago
String result = response.get();
1 year ago
showLog(result,mActivity);
String jsonDecode = RSAUtils.decryptBASE64StrClient(result);
if (TextUtils.isEmpty(jsonDecode)) {
//解密失败
1 year ago
showLog("APP解密失败",mActivity);
listener.onAuthenticationSuccess(false, "");
} else {
1 year ago
showLog(jsonDecode,mActivity);
UserAuthenticationAIBean bean = GsonToBean(jsonDecode, UserAuthenticationAIBean.class);
if (bean != null) {
if (bean.isSuccess()) {//登录成功
1 year ago
SPUtils.put(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, bean.getData().getToken());
listener.onAuthenticationSuccess(true, bean.getData().getToken());
3 years ago
} else {
if (bean.getFlag() == -1) {
if (bean.getMsg() != null && bean.getMsg().equals("用户未注销")) {
1 year ago
String token = (String) SPUtils.get(mActivity, Contans.AUTHENTICATIONLOGINTOKENAI, String.valueOf(""));
listener.onAuthenticationSuccess(true, token);
}
} else {
listener.onAuthenticationSuccess(false, "");
if (bean.getFlag() == -2) {
1 year ago
showLog("没有传参",mActivity);
}
if (bean.getFlag() == -3) {
1 year ago
showLog("服务器解密失败",mActivity);
}
if (bean.getFlag() == -4) {
1 year ago
showLog("Json 格式不正确",mActivity);
}
if (bean.getFlag() == -5) {
1 year ago
showLog("账号密码不能为空",mActivity);
}
if (bean.getFlag() == -6) {
1 year ago
showLog("账号或密码错误",mActivity);
}
}
3 years ago
}
// 状态码 flag
// -1已登录过
// -2没有传参
// -3解密失败
// -4Json 格式不正确
// -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);
3 years ago
// }
}
3 years ago
}
}
@Override
public void onFailed(int what, Response<String> response) {
1 year ago
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解密失败
// -4Json 格式不正确
// -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);
3 years ago
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;
}
}
3 years ago
}