Browse Source

change dev

dev
赵明涛 5 days ago
parent
commit
26c719207d
  1. 442
      app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java
  2. 307
      app/src/main/java/com/ouxuan/oxface/network/OrderVerificationManager.java
  3. 268
      app/src/main/java/com/ouxuan/oxface/network/OrderVerificationResultHandler.java
  4. 143
      网络请求模块重构说明.md

442
app/src/main/java/com/ouxuan/oxface/OXFaceOnlineActivity.java

@ -45,6 +45,8 @@ import com.ouxuan.oxface.data.DeviceSelectDataManager;
import com.ouxuan.oxface.orderOX.OrderVerificationResultActivity; import com.ouxuan.oxface.orderOX.OrderVerificationResultActivity;
import com.ouxuan.oxface.orderOX.VerificationCodeActivity; import com.ouxuan.oxface.orderOX.VerificationCodeActivity;
import com.ouxuan.oxface.utils.LogManager; import com.ouxuan.oxface.utils.LogManager;
import com.ouxuan.oxface.network.OrderVerificationManager;
import com.ouxuan.oxface.network.OrderVerificationResultHandler;
import java.util.List; import java.util.List;
@ -169,6 +171,10 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi
private com.ouxuan.oxface.data.LoginDataManager loginDataManager; // 新增LoginDataManager实例 private com.ouxuan.oxface.data.LoginDataManager loginDataManager; // 新增LoginDataManager实例
private CameraControlReceiver cameraControlReceiver; // 新增摄像头控制广播接收器实例 private CameraControlReceiver cameraControlReceiver; // 新增摄像头控制广播接收器实例
// 网络请求管理器
private OrderVerificationManager orderVerificationManager;
private OrderVerificationResultHandler orderVerificationResultHandler;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -180,6 +186,10 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi
mContext = this; mContext = this;
// 初始化LoginDataManager // 初始化LoginDataManager
loginDataManager = com.ouxuan.oxface.data.LoginDataManager.getInstance(this); loginDataManager = com.ouxuan.oxface.data.LoginDataManager.getInstance(this);
// 初始化网络请求管理器
initNetworkManagers();
initView(); initView();
// 初始化并注册广播接收器 // 初始化并注册广播接收器
@ -213,6 +223,64 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi
} }
/** /**
* 初始化网络请求管理器
*/
private void initNetworkManagers() {
// 初始化订单验证管理器
orderVerificationManager = new OrderVerificationManager(this, new OrderVerificationManager.OrderVerificationListener() {
@Override
public void showLoadingStatus(String message) {
OXFaceOnlineActivity.this.showLoadingStatus(message);
}
@Override
public void hideLoadingStatus() {
OXFaceOnlineActivity.this.hideLoadingStatus();
}
@Override
public void showToast(String message) {
OXFaceOnlineActivity.this.showToast(message);
}
@Override
public void onVerificationSuccess(com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult data, int verificationType) {
orderVerificationResultHandler.handleVerificationSuccess(data, verificationType);
}
@Override
public void onVerificationError(int errorCode, String errorMessage, int verificationType) {
orderVerificationResultHandler.handleVerificationError(errorCode, errorMessage, verificationType);
}
@Override
public void onVerificationException(Throwable throwable, int verificationType) {
orderVerificationResultHandler.handleVerificationException(throwable, verificationType);
}
});
// 初始化订单验证结果处理器
orderVerificationResultHandler = new OrderVerificationResultHandler(this, new OrderVerificationResultHandler.OrderVerificationResultListener() {
@Override
public void showToast(String message) {
OXFaceOnlineActivity.this.showToast(message);
}
@Override
public void navigateToResultPage(Intent intent) {
startActivity(intent);
}
@Override
public void navigateToOrderSelectionPage(java.util.Map<String, Object> queryData) {
// 这里可以根据实际情况实现跳转到订单选择页面的逻辑
LogManager.logInfo(TAG, "准备跳转到订单选择页面: " + queryData.toString());
// 这里可以添加实际的页面跳转逻辑
}
});
}
/**
* View * View
*/ */
private void initView() { private void initView() {
@ -1048,7 +1116,7 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi
verifyCode = base64img; verifyCode = base64img;
// 设置模式为2人脸验证 // 设置模式为2人脸验证
modeType = 2;
modeType = OrderVerificationManager.TYPE_FACE_VERIFICATION;
// 自动调用订单核销方法 // 自动调用订单核销方法
getCheckOrder(); getCheckOrder();
@ -1387,16 +1455,9 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi
*/ */
private void handleVerificationCodeSubmit(String verificationCode) { private void handleVerificationCodeSubmit(String verificationCode) {
// 设置验证码验证模式 // 设置验证码验证模式
modeType = 1;
modeType = OrderVerificationManager.TYPE_VERIFICATION_CODE;
verifyCode = verificationCode; verifyCode = verificationCode;
// 显示验证状态
if (layoutCompareStatus != null) {
layoutCompareStatus.setVisibility(View.VISIBLE);
textCompareStatus.setTextColor(Color.parseColor("#009874"));
textCompareStatus.setText("正在验证验证码...");
}
// 调用订单查询方法进行验证码验证 // 调用订单查询方法进行验证码验证
getCheckOrder(); getCheckOrder();
} }
@ -1473,367 +1534,8 @@ public class OXFaceOnlineActivity extends BaseActivity implements View.OnClickLi
private void getCheckOrder() { private void getCheckOrder() {
LogManager.logInfo(TAG, "开始查验订单列表,modeType: " + modeType); LogManager.logInfo(TAG, "开始查验订单列表,modeType: " + modeType);
// 获取设备信息和配置信息
com.ouxuan.oxface.data.DeviceSelectDataManager deviceDataManager =
com.ouxuan.oxface.data.DeviceSelectDataManager.getInstance(this);
// 获取token和hardware_id
String token = getAuthToken(); // 修改为使用getAuthToken()方法获取token
int hardwareId = deviceDataManager.getSelectedHardwareId();
// 参数校验
if (token == null || token.isEmpty()) {
LogManager.logError(TAG, "获取token失败");
showToast("获取token失败");
return;
}
if (hardwareId <= 0) {
LogManager.logError(TAG, "获取hardwareId失败");
showToast("获取设备ID失败");
return;
}
// 根据不同的验证类型调用相应的接口
if (modeType == 1) {
// 验证码验证
handleVerificationCodeCheck(token, hardwareId);
} else if (modeType == 2) {
// 人脸验证
handleFaceCheck(token, hardwareId);
} else if (modeType == 3) {
// 扫码验证
handleScanCheck(token, hardwareId);
} else if (modeType == 4) {
// 扫码器验证
handleScannerCheck(token, hardwareId);
} else {
LogManager.logError(TAG, "不支持的验证类型: " + modeType);
showToast("不支持的验证类型");
}
}
/**
* 处理验证码验证
*/
private void handleVerificationCodeCheck(String token, int hardwareId) {
LogManager.logInfo(TAG, "验证码验证,验证码: " + verifyCode);
if (verifyCode == null || verifyCode.isEmpty()) {
LogManager.logError(TAG, "验证码为空");
showToast("请输入验证码");
return;
}
// 显示加载状态
showLoadingStatus("正在验证验证码...");
// 调用网络请求
com.ouxuan.oxface.network.utils.NetworkUtils.checkOrder(
token, modeType, hardwareId, verifyCode, null, null, null,
new com.ouxuan.oxface.network.callback.NetworkCallback<com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult>() {
@Override
public void onSuccess(com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult data) {
LogManager.logInfo(TAG, "验证码验证成功");
handleCheckOrderSuccess(data, modeType);
}
@Override
public void onError(int errorCode, String errorMessage) {
LogManager.logError(TAG, "验证码验证失败: " + errorMessage);
handleCheckOrderError(errorCode, errorMessage, modeType);
}
@Override
public void onException(Throwable throwable) {
LogManager.logError(TAG, "验证码验证异常", throwable);
showToast("网络请求异常: " + throwable.getMessage());
}
@Override
public void onComplete() {
hideLoadingStatus();
}
});
}
/**
* 处理人脸验证
*/
private void handleFaceCheck(String token, int hardwareId) {
LogManager.logInfo(TAG, "人脸验证");
// 从verifyCode变量中获取人脸base64数据
String faceBase64 = verifyCode; // 使用verifyCode变量中保存的人脸base64数据
if (faceBase64 == null || faceBase64.isEmpty()) {
LogManager.logError(TAG, "人脸数据为空");
showToast("未检测到有效的人脸数据");
return;
}
// 显示加载状态
showLoadingStatus("正在进行人脸验证...");
// 调用网络请求
com.ouxuan.oxface.network.utils.NetworkUtils.checkOrder(
token, modeType, hardwareId, null, faceBase64, "face_base64", null,
new com.ouxuan.oxface.network.callback.NetworkCallback<com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult>() {
@Override
public void onSuccess(com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult data) {
LogManager.logInfo(TAG, "人脸验证成功");
LogManager.logInfo(TAG, "人脸验证接口返回数据: " + (data != null ? data.toString() : "null"));
handleCheckOrderSuccess(data, modeType);
}
@Override
public void onError(int errorCode, String errorMessage) {
LogManager.logError(TAG, "人脸验证失败: " + errorMessage);
LogManager.logError(TAG, "人脸验证错误码: " + errorCode + ", 错误信息: " + errorMessage);
handleCheckOrderError(errorCode, errorMessage, modeType);
}
@Override
public void onException(Throwable throwable) {
LogManager.logError(TAG, "人脸验证异常", throwable);
showToast("网络请求异常: " + throwable.getMessage());
}
@Override
public void onComplete() {
hideLoadingStatus();
}
});
}
/**
* 处理扫码验证
*/
private void handleScanCheck(String token, int hardwareId) {
LogManager.logInfo(TAG, "扫码验证");
// 扫码验证通常由用户扫描二维码触发这里作为示例
// 显示加载状态
showLoadingStatus("正在进行扫码验证...");
// 调用网络请求
com.ouxuan.oxface.network.utils.NetworkUtils.checkOrder(
token, modeType, hardwareId, null, null, null, null,
new com.ouxuan.oxface.network.callback.NetworkCallback<com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult>() {
@Override
public void onSuccess(com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult data) {
LogManager.logInfo(TAG, "扫码验证成功");
handleCheckOrderSuccess(data, modeType);
}
@Override
public void onError(int errorCode, String errorMessage) {
LogManager.logError(TAG, "扫码验证失败: " + errorMessage);
handleCheckOrderError(errorCode, errorMessage, modeType);
}
@Override
public void onException(Throwable throwable) {
LogManager.logError(TAG, "扫码验证异常", throwable);
showToast("网络请求异常: " + throwable.getMessage());
}
@Override
public void onComplete() {
hideLoadingStatus();
}
});
}
/**
* 处理扫码器验证
*/
private void handleScannerCheck(String token, int hardwareId) {
LogManager.logInfo(TAG, "扫码器验证");
// 注意在实际实现中需要传入解密文本
String decryptText = ""; // 需要从扫码器获取解密文本
// 显示加载状态
showLoadingStatus("正在进行扫码器验证...");
// 调用网络请求
com.ouxuan.oxface.network.utils.NetworkUtils.checkOrder(
token, modeType, hardwareId, null, null, null, decryptText,
new com.ouxuan.oxface.network.callback.NetworkCallback<com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult>() {
@Override
public void onSuccess(com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult data) {
LogManager.logInfo(TAG, "扫码器验证成功");
handleCheckOrderSuccess(data, modeType);
}
@Override
public void onError(int errorCode, String errorMessage) {
LogManager.logError(TAG, "扫码器验证失败: " + errorMessage);
handleCheckOrderError(errorCode, errorMessage, modeType);
}
@Override
public void onException(Throwable throwable) {
LogManager.logError(TAG, "扫码器验证异常", throwable);
showToast("网络请求异常: " + throwable.getMessage());
}
@Override
public void onComplete() {
hideLoadingStatus();
}
});
}
/**
* 处理订单查验成功响应
*/
private void handleCheckOrderSuccess(com.ouxuan.oxface.network.api.PadApiService.CheckOrderResult data, int type) {
LogManager.logInfo(TAG, "订单查验成功,类型: " + type);
if (data == null || data.getResult() == null || data.getResult().isEmpty()) {
LogManager.logWarning(TAG, "返回数据为空");
showToast("未找到可核销的订单");
return;
}
// 清除定时器
// clearAllTimer(); // 需要实现此方法
if (type == 1 || type == 4) { // 验证码验证或扫码器验证
showToast("操作成功,正在跳转...");
com.ouxuan.oxface.network.api.PadApiService.CheckOrderItem firstItem = data.getResult().get(0);
// 准备订单核销结果页信息
java.util.Map<String, Object> resultData = new java.util.HashMap<>();
resultData.put("type", type);
resultData.put("order_no", firstItem.getOrderNo());
resultData.put("v_code", firstItem.getVCode() != null && !firstItem.getVCode().isEmpty() ?
firstItem.getVCode().get(0) : "");
resultData.put("order_type", firstItem.getOrderType());
// 打印resultData
LogManager.logInfo(TAG, "resultData: " + resultData);
// 使用辅助方法处理info字段因为info可能是字符串或对象
String cardNo = "";
Object info = firstItem.getInfo();
if (info != null) {
if (info instanceof String) {
// 如果info是字符串直接使用
cardNo = (String) info;
} else if (info instanceof com.ouxuan.oxface.network.api.PadApiService.OrderInfo) {
// 如果info是OrderInfo对象获取userId
com.ouxuan.oxface.network.api.PadApiService.OrderInfo orderInfo =
(com.ouxuan.oxface.network.api.PadApiService.OrderInfo) info;
cardNo = orderInfo.getUserId() != null ? orderInfo.getUserId() : "";
} else if (info instanceof com.google.gson.JsonObject) {
// 如果info是JsonObject尝试获取card_no字段
com.google.gson.JsonObject jsonObject = (com.google.gson.JsonObject) info;
if (jsonObject.has("card_no")) {
cardNo = jsonObject.get("card_no").getAsString();
}
}
}
resultData.put("card_no", cardNo);
// 模拟跳转到验证结果页面
LogManager.logInfo(TAG, "准备跳转到验证结果页面: " + resultData.toString());
// 这里需要实现实际的页面跳转逻辑
showToast("验证成功,准备跳转到结果页面");
} else if (type == 2) { // 人脸验证
// 准备订单核销选择页信息
java.util.Map<String, Object> queryData = new java.util.HashMap<>();
queryData.put("type", 2);
// queryData.put("face_base64", ret.face_base64); // 需要实际的人脸base64数据
queryData.put("check_type", "face_base64");
// if(isOrder.length>0) queryData.put("check_order_res", isOrder); // 需要实际的识别数据
queryData.put("result", data.getResult());
// 模拟跳转到订单验证页面
LogManager.logInfo(TAG, "准备跳转到订单验证页面: " + queryData.toString());
// 这里需要实现实际的页面跳转逻辑
showToast("人脸验证成功,准备跳转到订单选择页面");
} else if (type == 3) { // 扫码验证
showToast("操作成功,正在跳转...");
// 准备订单核销选择页信息
java.util.Map<String, Object> queryData = new java.util.HashMap<>();
queryData.put("type", type);
queryData.put("result", data.getResult());
// 模拟跳转到订单验证页面
LogManager.logInfo(TAG, "准备跳转到订单验证页面: " + queryData.toString());
// 这里需要实现实际的页面跳转逻辑
showToast("扫码验证成功,准备跳转到订单选择页面");
}
}
/**
* 处理订单查验错误响应
*/
private void handleCheckOrderError(int errorCode, String errorMessage, int type) {
LogManager.logError(TAG, "订单查验失败,类型: " + type + ", 错误码: " + errorCode + ", 错误信息: " + errorMessage);
// 当人脸验证返回303错误码时不显示任何提示
if (type == 2 && errorCode == 303) {
LogManager.logInfo(TAG, "人脸验证返回303,不显示提示");
return;
}
// 当服务器有正确的返回时如果code不为0应该显示服务器返回的message而不是使用自定义的错误信息
String displayMessage = errorMessage;
// 只有在错误信息为空或null时才使用默认的错误信息
if (errorMessage == null || errorMessage.isEmpty()) {
// 如果没有错误信息根据错误码显示默认信息
switch (errorCode) {
case 404:
displayMessage = "请求的资源未找到";
break;
case 500:
displayMessage = "服务器内部错误";
break;
case 401:
displayMessage = "认证失败,请重新登录";
break;
case 403:
displayMessage = "权限不足,无法访问";
break;
default:
displayMessage = "请求失败,错误码: " + errorCode;
break;
}
}
if (type == 4) { // 扫码器验证
showToast(displayMessage != null && !displayMessage.isEmpty() ? displayMessage : "核销码不可用!");
// 模拟返回上一页
LogManager.logInfo(TAG, "扫码器验证失败,准备返回上一页");
// 这里需要实现实际的页面返回逻辑
} else if (type == 3 && "抱歉! 暂无可核销订单".equals(errorMessage)) {
// clearAllTimer(); // 需要实现此方法
showToast(displayMessage);
// 准备订单核销选择页信息
java.util.Map<String, Object> queryData = new java.util.HashMap<>();
queryData.put("type", type);
queryData.put("result", new java.util.ArrayList<>());
// 模拟跳转到订单验证页面
LogManager.logInfo(TAG, "暂无可核销订单,准备跳转到订单验证页面: " + queryData.toString());
// 这里需要实现实际的页面跳转逻辑
} else if (type == 1) { // 验证码验证
showToast(displayMessage != null && !displayMessage.isEmpty() ? displayMessage : "验证失败");
} else {
// 对于其他人脸验证等类型显示具体的错误信息而不是"网络请求异常"
showToast(displayMessage != null && !displayMessage.isEmpty() ? displayMessage : "验证失败");
}
// 使用新的网络请求管理器执行验证
orderVerificationManager.performVerification(modeType, verifyCode, null);
} }
/** /**

307
app/src/main/java/com/ouxuan/oxface/network/OrderVerificationManager.java

@ -0,0 +1,307 @@
package com.ouxuan.oxface.network;
import android.content.Context;
import com.ouxuan.oxface.data.DeviceSelectDataManager;
import com.ouxuan.oxface.data.LoginDataManager;
import com.ouxuan.oxface.network.api.PadApiService;
import com.ouxuan.oxface.network.callback.NetworkCallback;
import com.ouxuan.oxface.network.utils.NetworkUtils;
import com.ouxuan.oxface.utils.LogManager;
/**
* 订单验证管理器
* 封装所有订单验证相关的网络请求逻辑
*/
public class OrderVerificationManager {
private static final String TAG = "OrderVerificationManager";
// 验证类型常量
public static final int TYPE_VERIFICATION_CODE = 1; // 验证码验证
public static final int TYPE_FACE_VERIFICATION = 2; // 人脸验证
public static final int TYPE_SCAN_VERIFICATION = 3; // 扫码验证
public static final int TYPE_SCANNER_VERIFICATION = 4; // 扫码器验证
private Context context;
private LoginDataManager loginDataManager;
private DeviceSelectDataManager deviceDataManager;
private OrderVerificationListener listener;
/**
* 订单验证结果监听器
*/
public interface OrderVerificationListener {
/**
* 显示加载状态
* @param message 加载消息
*/
void showLoadingStatus(String message);
/**
* 隐藏加载状态
*/
void hideLoadingStatus();
/**
* 显示Toast消息
* @param message 消息内容
*/
void showToast(String message);
/**
* 验证成功回调
* @param data 返回的数据
* @param verificationType 验证类型
*/
void onVerificationSuccess(PadApiService.CheckOrderResult data, int verificationType);
/**
* 验证失败回调
* @param errorCode 错误码
* @param errorMessage 错误信息
* @param verificationType 验证类型
*/
void onVerificationError(int errorCode, String errorMessage, int verificationType);
/**
* 验证异常回调
* @param throwable 异常信息
* @param verificationType 验证类型
*/
void onVerificationException(Throwable throwable, int verificationType);
}
/**
* 构造方法
* @param context 上下文
* @param listener 结果监听器
*/
public OrderVerificationManager(Context context, OrderVerificationListener listener) {
this.context = context;
this.listener = listener;
this.loginDataManager = LoginDataManager.getInstance(context);
this.deviceDataManager = DeviceSelectDataManager.getInstance(context);
}
/**
* 执行订单验证
* @param verificationType 验证类型
* @param verifyCode 验证码或人脸base64数据
* @param decryptText 解密文本仅扫码器验证使用
*/
public void performVerification(int verificationType, String verifyCode, String decryptText) {
LogManager.logInfo(TAG, "开始执行订单验证,类型: " + verificationType);
// 获取认证信息
String token = getAuthToken();
int hardwareId = getHardwareId();
// 参数校验
if (!validateParams(token, hardwareId)) {
return;
}
// 根据验证类型执行相应的验证
switch (verificationType) {
case TYPE_VERIFICATION_CODE:
performVerificationCodeCheck(token, hardwareId, verifyCode);
break;
case TYPE_FACE_VERIFICATION:
performFaceCheck(token, hardwareId, verifyCode);
break;
case TYPE_SCAN_VERIFICATION:
performScanCheck(token, hardwareId);
break;
case TYPE_SCANNER_VERIFICATION:
performScannerCheck(token, hardwareId, decryptText);
break;
default:
LogManager.logError(TAG, "不支持的验证类型: " + verificationType);
if (listener != null) {
listener.showToast("不支持的验证类型");
}
break;
}
}
/**
* 验证码验证
*/
private void performVerificationCodeCheck(String token, int hardwareId, String verifyCode) {
LogManager.logInfo(TAG, "执行验证码验证,验证码: " + verifyCode);
if (verifyCode == null || verifyCode.isEmpty()) {
LogManager.logError(TAG, "验证码为空");
if (listener != null) {
listener.showToast("请输入验证码");
}
return;
}
// 显示加载状态
if (listener != null) {
listener.showLoadingStatus("正在验证验证码...");
}
// 调用网络请求
NetworkUtils.checkOrder(
token, TYPE_VERIFICATION_CODE, hardwareId, verifyCode, null, null, null,
createNetworkCallback(TYPE_VERIFICATION_CODE)
);
}
/**
* 人脸验证
*/
private void performFaceCheck(String token, int hardwareId, String faceBase64) {
LogManager.logInfo(TAG, "执行人脸验证");
if (faceBase64 == null || faceBase64.isEmpty()) {
LogManager.logError(TAG, "人脸数据为空");
if (listener != null) {
listener.showToast("未检测到有效的人脸数据");
}
return;
}
// 显示加载状态
if (listener != null) {
listener.showLoadingStatus("正在进行人脸验证...");
}
// 调用网络请求
NetworkUtils.checkOrder(
token, TYPE_FACE_VERIFICATION, hardwareId, null, faceBase64, "face_base64", null,
createNetworkCallback(TYPE_FACE_VERIFICATION)
);
}
/**
* 扫码验证
*/
private void performScanCheck(String token, int hardwareId) {
LogManager.logInfo(TAG, "执行扫码验证");
// 显示加载状态
if (listener != null) {
listener.showLoadingStatus("正在进行扫码验证...");
}
// 调用网络请求
NetworkUtils.checkOrder(
token, TYPE_SCAN_VERIFICATION, hardwareId, null, null, null, null,
createNetworkCallback(TYPE_SCAN_VERIFICATION)
);
}
/**
* 扫码器验证
*/
private void performScannerCheck(String token, int hardwareId, String decryptText) {
LogManager.logInfo(TAG, "执行扫码器验证");
// 显示加载状态
if (listener != null) {
listener.showLoadingStatus("正在进行扫码器验证...");
}
// 调用网络请求
NetworkUtils.checkOrder(
token, TYPE_SCANNER_VERIFICATION, hardwareId, null, null, null, decryptText,
createNetworkCallback(TYPE_SCANNER_VERIFICATION)
);
}
/**
* 创建网络请求回调
*/
private NetworkCallback<PadApiService.CheckOrderResult> createNetworkCallback(int verificationType) {
return new NetworkCallback<PadApiService.CheckOrderResult>() {
@Override
public void onSuccess(PadApiService.CheckOrderResult data) {
LogManager.logInfo(TAG, "验证成功,类型: " + verificationType);
if (listener != null) {
listener.onVerificationSuccess(data, verificationType);
}
}
@Override
public void onError(int errorCode, String errorMessage) {
LogManager.logError(TAG, "验证失败,类型: " + verificationType + ", 错误码: " + errorCode + ", 错误信息: " + errorMessage);
if (listener != null) {
listener.onVerificationError(errorCode, errorMessage, verificationType);
}
}
@Override
public void onException(Throwable throwable) {
LogManager.logError(TAG, "验证异常,类型: " + verificationType, throwable);
if (listener != null) {
listener.onVerificationException(throwable, verificationType);
}
}
@Override
public void onComplete() {
if (listener != null) {
listener.hideLoadingStatus();
}
}
};
}
/**
* 获取认证Token
*/
private String getAuthToken() {
return loginDataManager.getAuthToken();
}
/**
* 获取硬件ID
*/
private int getHardwareId() {
return deviceDataManager.getSelectedHardwareId();
}
/**
* 验证参数
*/
private boolean validateParams(String token, int hardwareId) {
if (token == null || token.isEmpty()) {
LogManager.logError(TAG, "获取token失败");
if (listener != null) {
listener.showToast("获取token失败");
}
return false;
}
if (hardwareId <= 0) {
LogManager.logError(TAG, "获取hardwareId失败");
if (listener != null) {
listener.showToast("获取设备ID失败");
}
return false;
}
return true;
}
/**
* 获取验证类型描述
*/
public static String getVerificationTypeDescription(int verificationType) {
switch (verificationType) {
case TYPE_VERIFICATION_CODE:
return "验证码验证";
case TYPE_FACE_VERIFICATION:
return "人脸验证";
case TYPE_SCAN_VERIFICATION:
return "扫码验证";
case TYPE_SCANNER_VERIFICATION:
return "扫码器验证";
default:
return "未知验证类型";
}
}
}

268
app/src/main/java/com/ouxuan/oxface/network/OrderVerificationResultHandler.java

@ -0,0 +1,268 @@
package com.ouxuan.oxface.network;
import android.content.Intent;
import android.content.Context;
import com.ouxuan.oxface.network.api.PadApiService;
import com.ouxuan.oxface.orderOX.OrderVerificationResultActivity;
import com.ouxuan.oxface.utils.LogManager;
import com.google.gson.JsonObject;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
/**
* 订单验证结果处理器
* 处理不同类型验证的结果包括页面跳转和数据处理
*/
public class OrderVerificationResultHandler {
private static final String TAG = "OrderVerificationResultHandler";
private Context context;
private OrderVerificationResultListener listener;
/**
* 订单验证结果处理监听器
*/
public interface OrderVerificationResultListener {
/**
* 显示Toast消息
* @param message 消息内容
*/
void showToast(String message);
/**
* 跳转到订单验证结果页面
* @param intent 跳转意图
*/
void navigateToResultPage(Intent intent);
/**
* 跳转到订单选择页面
* @param queryData 查询数据
*/
void navigateToOrderSelectionPage(Map<String, Object> queryData);
}
/**
* 构造方法
* @param context 上下文
* @param listener 结果监听器
*/
public OrderVerificationResultHandler(Context context, OrderVerificationResultListener listener) {
this.context = context;
this.listener = listener;
}
/**
* 处理验证成功结果
* @param data 返回的数据
* @param verificationType 验证类型
*/
public void handleVerificationSuccess(PadApiService.CheckOrderResult data, int verificationType) {
LogManager.logInfo(TAG, "处理验证成功结果,类型: " + verificationType);
if (data == null || data.getResult() == null || data.getResult().isEmpty()) {
LogManager.logWarning(TAG, "返回数据为空");
if (listener != null) {
listener.showToast("未找到可核销的订单");
}
return;
}
switch (verificationType) {
case OrderVerificationManager.TYPE_VERIFICATION_CODE:
case OrderVerificationManager.TYPE_SCANNER_VERIFICATION:
handleDirectResultTypes(data, verificationType);
break;
case OrderVerificationManager.TYPE_FACE_VERIFICATION:
handleFaceVerificationResult(data);
break;
case OrderVerificationManager.TYPE_SCAN_VERIFICATION:
handleScanVerificationResult(data);
break;
default:
LogManager.logWarning(TAG, "未知的验证类型: " + verificationType);
break;
}
}
/**
* 处理验证失败结果
* @param errorCode 错误码
* @param errorMessage 错误信息
* @param verificationType 验证类型
*/
public void handleVerificationError(int errorCode, String errorMessage, int verificationType) {
LogManager.logError(TAG, "处理验证失败结果,类型: " + verificationType + ", 错误码: " + errorCode + ", 错误信息: " + errorMessage);
// 当人脸验证返回303错误码时不显示任何提示
if (verificationType == OrderVerificationManager.TYPE_FACE_VERIFICATION && errorCode == 303) {
LogManager.logInfo(TAG, "人脸验证返回303,不显示提示");
return;
}
String displayMessage = getDisplayErrorMessage(errorCode, errorMessage);
if (verificationType == OrderVerificationManager.TYPE_SCANNER_VERIFICATION) {
if (listener != null) {
listener.showToast(displayMessage != null && !displayMessage.isEmpty() ? displayMessage : "核销码不可用!");
}
LogManager.logInfo(TAG, "扫码器验证失败,准备返回上一页");
} else if (verificationType == OrderVerificationManager.TYPE_SCAN_VERIFICATION && "抱歉! 暂无可核销订单".equals(errorMessage)) {
if (listener != null) {
listener.showToast(displayMessage);
}
// 准备订单核销选择页信息
Map<String, Object> queryData = new HashMap<>();
queryData.put("type", verificationType);
queryData.put("result", new ArrayList<>());
LogManager.logInfo(TAG, "暂无可核销订单,准备跳转到订单验证页面: " + queryData.toString());
if (listener != null) {
listener.navigateToOrderSelectionPage(queryData);
}
} else {
if (listener != null) {
listener.showToast(displayMessage != null && !displayMessage.isEmpty() ? displayMessage : "验证失败");
}
}
}
/**
* 处理验证异常结果
* @param throwable 异常信息
* @param verificationType 验证类型
*/
public void handleVerificationException(Throwable throwable, int verificationType) {
LogManager.logError(TAG, "处理验证异常结果,类型: " + verificationType, throwable);
if (listener != null) {
listener.showToast("网络请求异常: " + throwable.getMessage());
}
}
/**
* 处理直接跳转结果页面的验证类型验证码验证扫码器验证
*/
private void handleDirectResultTypes(PadApiService.CheckOrderResult data, int verificationType) {
if (listener != null) {
listener.showToast("操作成功,正在跳转...");
}
PadApiService.CheckOrderItem firstItem = data.getResult().get(0);
// 创建跳转到OrderVerificationResultActivity的Intent
Intent intent = new Intent(context, OrderVerificationResultActivity.class);
intent.putExtra("verification_type", verificationType);
intent.putExtra("order_no", firstItem.getOrderNo());
intent.putExtra("verification_code", firstItem.getVCode() != null && !firstItem.getVCode().isEmpty() ?
firstItem.getVCode().get(0) : "");
intent.putExtra("order_type", firstItem.getOrderType());
// 处理info字段获取卡号
String cardNo = extractCardNumber(firstItem.getInfo());
intent.putExtra("card_no", cardNo);
intent.putExtra("status", "核销成功"); // 可以根据实际情况设置状态
LogManager.logInfo(TAG, "准备跳转到验证结果页面,订单号: " + firstItem.getOrderNo());
if (listener != null) {
listener.navigateToResultPage(intent);
}
}
/**
* 处理人脸验证结果
*/
private void handleFaceVerificationResult(PadApiService.CheckOrderResult data) {
// 准备订单核销选择页信息
Map<String, Object> queryData = new HashMap<>();
queryData.put("type", OrderVerificationManager.TYPE_FACE_VERIFICATION);
queryData.put("check_type", "face_base64");
queryData.put("result", data.getResult());
LogManager.logInfo(TAG, "准备跳转到订单验证页面: " + queryData.toString());
if (listener != null) {
listener.showToast("人脸验证成功,准备跳转到订单选择页面");
listener.navigateToOrderSelectionPage(queryData);
}
}
/**
* 处理扫码验证结果
*/
private void handleScanVerificationResult(PadApiService.CheckOrderResult data) {
if (listener != null) {
listener.showToast("操作成功,正在跳转...");
}
// 准备订单核销选择页信息
Map<String, Object> queryData = new HashMap<>();
queryData.put("type", OrderVerificationManager.TYPE_SCAN_VERIFICATION);
queryData.put("result", data.getResult());
LogManager.logInfo(TAG, "准备跳转到订单验证页面: " + queryData.toString());
if (listener != null) {
listener.navigateToOrderSelectionPage(queryData);
}
}
/**
* 从info对象中提取卡号
*/
private String extractCardNumber(Object info) {
String cardNo = "";
if (info != null) {
if (info instanceof String) {
// 如果info是字符串直接使用
cardNo = (String) info;
} else if (info instanceof PadApiService.OrderInfo) {
// 如果info是OrderInfo对象获取userId
PadApiService.OrderInfo orderInfo = (PadApiService.OrderInfo) info;
cardNo = orderInfo.getUserId() != null ? orderInfo.getUserId() : "";
} else if (info instanceof JsonObject) {
// 如果info是JsonObject尝试获取card_no字段
JsonObject jsonObject = (JsonObject) info;
if (jsonObject.has("card_no")) {
cardNo = jsonObject.get("card_no").getAsString();
}
}
}
return cardNo;
}
/**
* 获取显示的错误信息
*/
private String getDisplayErrorMessage(int errorCode, String errorMessage) {
// 当服务器有正确的返回时如果code不为0应该显示服务器返回的message
String displayMessage = errorMessage;
// 只有在错误信息为空或null时才使用默认的错误信息
if (errorMessage == null || errorMessage.isEmpty()) {
switch (errorCode) {
case 404:
displayMessage = "请求的资源未找到";
break;
case 500:
displayMessage = "服务器内部错误";
break;
case 401:
displayMessage = "认证失败,请重新登录";
break;
case 403:
displayMessage = "权限不足,无法访问";
break;
default:
displayMessage = "请求失败,错误码: " + errorCode;
break;
}
}
return displayMessage;
}
}

143
网络请求模块重构说明.md

@ -0,0 +1,143 @@
# 网络请求模块重构说明
## 概述
原先在 `OXFaceOnlineActivity.java` 中的网络请求代码过于冗杂,现已重构为两个独立的管理类:
1. **OrderVerificationManager** - 负责执行各种类型的订单验证网络请求
2. **OrderVerificationResultHandler** - 负责处理验证结果,包括成功、失败和异常情况
## 重构后的架构
### 1. OrderVerificationManager(订单验证管理器)
**功能:**
- 封装所有订单验证相关的网络请求逻辑
- 支持4种验证类型:验证码验证、人脸验证、扫码验证、扫码器验证
- 统一的参数校验和错误处理
**主要方法:**
```java
// 执行验证
void performVerification(int verificationType, String verifyCode, String decryptText)
// 验证类型常量
TYPE_VERIFICATION_CODE = 1 // 验证码验证
TYPE_FACE_VERIFICATION = 2 // 人脸验证
TYPE_SCAN_VERIFICATION = 3 // 扫码验证
TYPE_SCANNER_VERIFICATION = 4 // 扫码器验证
```
### 2. OrderVerificationResultHandler(订单验证结果处理器)
**功能:**
- 处理不同验证类型的成功结果
- 统一的错误处理和异常处理
- 页面跳转逻辑封装
**主要方法:**
```java
// 处理验证成功
void handleVerificationSuccess(CheckOrderResult data, int verificationType)
// 处理验证失败
void handleVerificationError(int errorCode, String errorMessage, int verificationType)
// 处理验证异常
void handleVerificationException(Throwable throwable, int verificationType)
```
## 使用方式
### 在 Activity 中的使用
```java
public class OXFaceOnlineActivity extends BaseActivity {
private OrderVerificationManager orderVerificationManager;
private OrderVerificationResultHandler orderVerificationResultHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ... 其他初始化代码 ...
// 初始化网络请求管理器
initNetworkManagers();
}
/**
* 初始化网络请求管理器
*/
private void initNetworkManagers() {
// 初始化验证管理器
orderVerificationManager = new OrderVerificationManager(this,
new OrderVerificationManager.OrderVerificationListener() {
// 实现回调接口
});
// 初始化结果处理器
orderVerificationResultHandler = new OrderVerificationResultHandler(this,
new OrderVerificationResultHandler.OrderVerificationResultListener() {
// 实现回调接口
});
}
/**
* 执行订单验证(简化后的方法)
*/
private void getCheckOrder() {
// 使用新的网络请求管理器执行验证
orderVerificationManager.performVerification(modeType, verifyCode, null);
}
}
```
## 重构优势
### 1. 代码组织更清晰
- **分离关注点**:网络请求逻辑与UI逻辑完全分离
- **模块化设计**:每个类都有明确的职责
- **易于维护**:修改网络请求逻辑不影响UI代码
### 2. 复用性更强
- **独立封装**:网络请求管理器可以在其他Activity中复用
- **接口设计**:通过监听器接口实现松耦合
- **配置灵活**:可以轻松适配不同的UI需求
### 3. 错误处理更统一
- **集中处理**:所有网络请求的错误处理逻辑集中在一个地方
- **类型安全**:使用常量定义验证类型,避免魔法数字
- **异常管理**:统一的异常处理机制
### 4. 测试更容易
- **单一职责**:每个类的功能单一,便于单元测试
- **依赖注入**:通过接口注入依赖,便于Mock测试
- **逻辑清晰**:网络请求逻辑独立,便于测试覆盖
## 文件说明
### 新增文件:
1. `OrderVerificationManager.java` - 订单验证管理器
2. `OrderVerificationResultHandler.java` - 订单验证结果处理器
3. `网络请求模块重构说明.md` - 本文档
### 修改文件:
1. `OXFaceOnlineActivity.java` - 简化网络请求相关代码,使用新的封装类
### 删除的方法:
- `handleVerificationCodeCheck()` - 验证码验证处理
- `handleFaceCheck()` - 人脸验证处理
- `handleScanCheck()` - 扫码验证处理
- `handleScannerCheck()` - 扫码器验证处理
- `handleCheckOrderSuccess()` - 成功结果处理
- `handleCheckOrderError()` - 错误结果处理
这些方法的功能现在都由新的封装类提供,代码更加模块化和可维护。
## 后续优化建议
1. **添加缓存机制**:对验证结果进行缓存,提高性能
2. **添加重试机制**:网络失败时自动重试
3. **添加请求队列**:管理并发请求
4. **添加配置文件**:将网络请求相关配置外部化
Loading…
Cancel
Save