package com.rehome.zhdcoa.utils; import android.content.Context; import android.text.TextUtils; import android.util.Log; import com.rehome.zhdcoa.BuildConfig; import com.rehome.zhdcoa.Contans; import com.rehome.zhdcoa.bean.UserAuthenticationAIBean; import com.yolanda.nohttp.Headers; 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.net.HttpCookie; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; public class AuthenticationLoginAIUtils { /** * @param context Context * @param account 工号 * @param password 密码 * @param listener 监听回调 */ public static void authenticationAILogin(Context context, String account, String password, OnAuthenticationLoginListener listener) { Request 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 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(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() { @Override public void onSucceed(int what, Response response) { showLog("----------------"); String result = response.get(); showLog(result); String jsonDecode = RSAUtils.decryptBASE64StrClient(result); if (TextUtils.isEmpty(jsonDecode)) { //解密失败 showLog("APP解密失败"); listener.onAuthenticationSuccess(false, ""); } else { showLog(jsonDecode); 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()); } 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("没有传参"); } if (bean.getFlag() == -3) { showLog("服务器解密失败"); } if (bean.getFlag() == -4) { showLog("Json 格式不正确"); } if (bean.getFlag() == -5) { showLog("账号密码不能为空"); } if (bean.getFlag() == -6) { showLog("账号或密码错误"); } } } // 状态码 flag: // -1:已登录过 // -2:没有传参 // -3:解密失败 // -4:Json 格式不正确 // -5:账号密码不能为空 // -6:账号或密码错误 // String cookieId = ""; // // 响应头 // Headers headers = response.getHeaders(); // List 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 response) { showLog("onFailed"); listener.onAuthenticationSuccess(false, ""); } }); } public static void showLog(String logText) { if (BuildConfig.LOG_ERROR) { if (TextUtils.isEmpty(logText)) { Log.i("app", "logText is null"); } else { Log.i("app", logText); } } } }