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.

164 lines
7.0 KiB
Java

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