diff --git a/app/src/main/java/com/ouxuan/oxface/orderOX/OrderSelectionActivity.java b/app/src/main/java/com/ouxuan/oxface/orderOX/OrderSelectionActivity.java index 6854d3c..41b5d82 100644 --- a/app/src/main/java/com/ouxuan/oxface/orderOX/OrderSelectionActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/orderOX/OrderSelectionActivity.java @@ -278,18 +278,25 @@ public class OrderSelectionActivity extends AppCompatActivity { // 获取订单ID和核销码 String orderId = order.getOrder_no(); - // 将verifyCode声明为final,避免lambda表达式中的问题 - final String verifyCode = getVerificationCode(order); - - if (verifyCode == null || verifyCode.isEmpty()) { - LogManager.logError(TAG, "未找到有效的核销码"); - showToast("未找到有效的核销码,无法核销订单"); - return; - } - // 获取订单类型 int orderType = order.getOrder_type(); + // 获取核销码 - 根据订单类型决定是否需要核销码 + final String verifyCode; + if (orderType != 3) { + // 非年月卡订单需要核销码 + String tempVerifyCode = getVerificationCode(order); + if (tempVerifyCode == null || tempVerifyCode.isEmpty()) { + LogManager.logError(TAG, "未找到有效的核销码"); + showToast("未找到有效的核销码,无法核销订单"); + return; + } + verifyCode = tempVerifyCode; + } else { + // 年月卡订单(orderType == 3)不需要核销码,保持verifyCode为空字符串 + verifyCode = ""; + } + // 获取卡号 - 根据订单类型处理info字段 String cardNo = ""; if (order.getOrder_type() == 3) { @@ -315,7 +322,7 @@ public class OrderSelectionActivity extends AppCompatActivity { com.ouxuan.oxface.network.utils.NetworkUtils.verifyOrder( token, orderId, - verifyCode, + verifyCode, // 根据订单类型决定是否传递核销码 verificationType, // 使用verificationType作为type参数 orderType, // 添加orderType参数 hardwareId, // 添加hardwareId参数 @@ -374,22 +381,6 @@ public class OrderSelectionActivity extends AppCompatActivity { } startActivity(intent); - intent.putExtra("order_type", order.getOrder_type()); - intent.putExtra("card_no", order.getInfo() != null ? order.getCardNoFromInfo() : ""); - intent.putExtra("project", order.getProject()); - intent.putExtra("status", status); - intent.putExtra("message", message); - // 添加设备ID和订单类型参数 - intent.putExtra("hardware_id", hardwareId); - intent.putExtra("order_type_param", orderType); - - // 传递完整的订单数据和核销结果数据 - intent.putExtra("order_data", new Gson().toJson(order)); - if (data != null) { - intent.putExtra("verify_result", new Gson().toJson(data)); - } - - startActivity(intent); // 准备返回结果给调用Activity Intent resultIntent = new Intent(); diff --git a/app/src/main/java/com/ouxuan/oxface/orderOX/OrderVerificationResultActivity.java b/app/src/main/java/com/ouxuan/oxface/orderOX/OrderVerificationResultActivity.java index 08c6fc6..9e3dc0c 100644 --- a/app/src/main/java/com/ouxuan/oxface/orderOX/OrderVerificationResultActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/orderOX/OrderVerificationResultActivity.java @@ -259,7 +259,7 @@ public class OrderVerificationResultActivity extends AppCompatActivity { } // 添加基本信息 - addBasicOrderInfo(); + addBasicOrderInfoWithVerificationCode(); } /** @@ -267,7 +267,12 @@ public class OrderVerificationResultActivity extends AppCompatActivity { */ private void buildPeopleContent() { // 人次核销:显示验证码、订单编号、有效时间、预订信息 - addInfoRow("验证码", formatVerificationCode(verificationCode)); + // 从verifyResult中获取验证码,而不是使用verificationCode变量 + String displayVerificationCode = verificationCode; + if (verifyResult != null && verifyResult.getVCode() != null && !verifyResult.getVCode().isEmpty()) { + displayVerificationCode = verifyResult.getVCode().get(0); + } + addInfoRow("验证码", formatVerificationCode(displayVerificationCode)); addInfoRow("订单编号", orderNo); // 获取有效时间 @@ -289,7 +294,12 @@ public class OrderVerificationResultActivity extends AppCompatActivity { */ private void buildCardContent() { // 年月卡:显示验证码、名称、卡号、核销方式、核销时间 - addInfoRow("验证码", formatVerificationCode(verificationCode)); + // 从verifyResult中获取验证码,而不是使用verificationCode变量 + String displayVerificationCode = verificationCode; + if (verifyResult != null && verifyResult.getVCode() != null && !verifyResult.getVCode().isEmpty()) { + displayVerificationCode = verifyResult.getVCode().get(0); + } + addInfoRow("验证码", formatVerificationCode(displayVerificationCode)); addInfoRow("名称", project != null ? project : "-"); if (orderInfo != null && orderInfo.isJsonObject()) { @@ -325,11 +335,18 @@ public class OrderVerificationResultActivity extends AppCompatActivity { addInfoRow("课程名称", project != null ? project : "-"); addInfoRow("订单编号", orderNo); + // 从verifyResult中获取验证码和核销信息,而不是使用verificationCode变量 + String displayVerificationCode = verificationCode; + if (verifyResult != null && verifyResult.getVCode() != null && !verifyResult.getVCode().isEmpty()) { + displayVerificationCode = verifyResult.getVCode().get(0); + } + if (orderInfo != null && orderInfo.isJsonObject()) { JsonObject courseInfo = orderInfo.getAsJsonObject(); String verifyDesc = getJsonString(courseInfo, "verify_desc", "-"); String verifyTime = getJsonString(courseInfo, "verify_time", "-"); + addInfoRow("签到码", formatVerificationCode(displayVerificationCode)); // 添加签到码显示 addInfoRow("签到方式", verifyDesc); addInfoRow("签到时间", verifyTime); } @@ -339,7 +356,7 @@ public class OrderVerificationResultActivity extends AppCompatActivity { * 构建默认内容 */ private void buildDefaultContent() { - addBasicOrderInfo(); + addBasicOrderInfoWithVerificationCode(); } /** @@ -359,6 +376,29 @@ public class OrderVerificationResultActivity extends AppCompatActivity { } /** + * 添加基本订单信息(包含验证码) + */ + private void addBasicOrderInfoWithVerificationCode() { + addInfoRow("订单编号", orderNo); + + // 从verifyResult中获取验证码,而不是使用verificationCode变量 + String displayVerificationCode = verificationCode; + if (verifyResult != null && verifyResult.getVCode() != null && !verifyResult.getVCode().isEmpty()) { + displayVerificationCode = verifyResult.getVCode().get(0); + } + + if (displayVerificationCode != null && !displayVerificationCode.isEmpty()) { + addInfoRow("验证码", formatVerificationCode(displayVerificationCode)); + } + if (cardNo != null && !cardNo.isEmpty()) { + addInfoRow("卡号", cardNo); + } + if (project != null && !project.isEmpty()) { + addInfoRow("项目", project); + } + } + + /** * 添加信息行 */ private void addInfoRow(String label, String value) { diff --git a/app/src/test/java/com/ouxuan/oxface/JsonParsingVerification.java b/app/src/test/java/com/ouxuan/oxface/JsonParsingVerification.java deleted file mode 100644 index 89c1958..0000000 --- a/app/src/test/java/com/ouxuan/oxface/JsonParsingVerification.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.ouxuan.oxface; - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.ouxuan.oxface.network.api.PadApiService; - -/** - * 验证 JSON 数据解析的简单测试脚本 - */ -public class JsonParsingVerification { - - public static void main(String[] args) { - Gson gson = new Gson(); - - // 测试次卡核销数据(info 为字符串) - String peopleCardJson = "{\n" + - " \"order_no\": \"RC20250910155115457419\",\n" + - " \"start_time\": \"2025-09-10 15:51:16\",\n" + - " \"end_time\": \"4763-08-07 15:51:16\",\n" + - " \"order_type\": 1,\n" + - " \"project\": \"20230727测试\",\n" + - " \"number\": 1,\n" + - " \"v_code\": [\"250910159819\"],\n" + - " \"info\": \"平时\",\n" + - " \"success\": 0,\n" + - " \"pv_usage_duration\": 1,\n" + - " \"many_enter\": false\n" + - "}"; - - // 测试年月卡核销数据(info 为对象) - String monthlyCardJson = "{\n" + - " \"order_no\": \"MC20250908145253448493\",\n" + - " \"start_time\": \"2025-09-08 14:52:59\",\n" + - " \"end_time\": \"2025-10-08 14:52:59\",\n" + - " \"order_type\": 3,\n" + - " \"project\": \"可多次进出\",\n" + - " \"number\": 7,\n" + - " \"v_code\": [\"2509087910\"],\n" + - " \"info\": {\n" + - " \"card_no\": \"2509085314\",\n" + - " \"rest_number\": 5,\n" + - " \"status\": 1,\n" + - " \"verify_desc\": \"人脸验证\",\n" + - " \"verify_time\": \"2025-09-10 11:26:36\",\n" + - " \"verify_type\": 2\n" + - " },\n" + - " \"success\": 2,\n" + - " \"pv_usage_duration\": 0,\n" + - " \"many_enter\": false\n" + - "}"; - - try {\n" + - " // 测试次卡解析\n" + - " System.out.println(\"=== 测试次卡核销数据解析 ===\");\n" + - " PadApiService.VerifyOrderResult peopleCardResult = gson.fromJson(peopleCardJson, PadApiService.VerifyOrderResult.class);\n" + - " \n" + - " System.out.println(\"订单类型: \" + peopleCardResult.getOrderType());\n" + - " System.out.println(\"订单号: \" + peopleCardResult.getOrderNo());\n" + - " \n" + - " JsonElement peopleInfo = peopleCardResult.getInfo();\n" + - " if (peopleInfo != null && peopleInfo.isJsonPrimitive()) {\n" + - " System.out.println(\"Info(字符串): \" + peopleInfo.getAsString());\n" + - " System.out.println(\"辅助方法 getInfoAsString(): \" + peopleCardResult.getInfoAsString());\n" + - " }\n" + - " \n" + - " // 测试年月卡解析\n" + - " System.out.println(\"\\n=== 测试年月卡核销数据解析 ===\");\n" + - " PadApiService.VerifyOrderResult monthlyCardResult = gson.fromJson(monthlyCardJson, PadApiService.VerifyOrderResult.class);\n" + - " \n" + - " System.out.println(\"订单类型: \" + monthlyCardResult.getOrderType());\n" + - " System.out.println(\"订单号: \" + monthlyCardResult.getOrderNo());\n" + - " \n" + - " JsonElement monthlyInfo = monthlyCardResult.getInfo();\n" + - " if (monthlyInfo != null && monthlyInfo.isJsonObject()) {\n" + - " JsonObject cardInfo = monthlyInfo.getAsJsonObject();\n" + - " System.out.println(\"Info(对象)- 卡号: \" + cardInfo.get(\"card_no\").getAsString());\n" + - " System.out.println(\"Info(对象)- 剩余次数: \" + cardInfo.get(\"rest_number\").getAsInt());\n" + - " System.out.println(\"辅助方法 getCardNoFromInfo(): \" + monthlyCardResult.getCardNoFromInfo());\n" + - " }\n" + - " \n" + - " System.out.println(\"\\n✅ 所有测试通过!JSON 解析修复成功。\");\n" + - " \n" + - " } catch (Exception e) {\n" + - " System.err.println(\"❌ JSON 解析失败: \" + e.getMessage());\n" + - " e.printStackTrace();\n" + - " }\n" + - " }\n" + - "} \ No newline at end of file diff --git a/app/src/test/java/com/ouxuan/oxface/OrderVerificationWithoutVCodeTest.java b/app/src/test/java/com/ouxuan/oxface/OrderVerificationWithoutVCodeTest.java new file mode 100644 index 0000000..d47a605 --- /dev/null +++ b/app/src/test/java/com/ouxuan/oxface/OrderVerificationWithoutVCodeTest.java @@ -0,0 +1,113 @@ +package com.ouxuan.oxface; + +import com.google.gson.Gson; +import com.ouxuan.oxface.orderOX.model.OrderVerificationData; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(RobolectricTestRunner.class) +@Config(sdk = 28) +public class OrderVerificationWithoutVCodeTest { + + private Gson gson = new Gson(); + + /** + * 测试年月卡订单(order_type=3)不传递v_code的情况 + */ + @Test + public void testMonthlyCardWithoutVCode() { + // 模拟年月卡订单数据(order_type=3) + String json = "{\n" + + " \"order_no\": \"MC20250910173625971898\",\n" + + " \"start_time\": \"2025-09-10 17:36:30\",\n" + + " \"end_time\": \"2025-09-10 23:59:59\",\n" + + " \"order_type\": 3,\n" + + " \"project\": \"当天有效年月卡\",\n" + + " \"number\": 15,\n" + + " \"v_code\": [\"2509107776\"],\n" + + " \"info\": {\n" + + " \"card_no\": \"2509107498\",\n" + + " \"rest_number\": 15,\n" + + " \"status\": 0,\n" + + " \"user_face_id\": \"\"\n" + + " },\n" + + " \"success\": 0,\n" + + " \"pv_usage_duration\": 0,\n" + + " \"many_enter\": false\n" + + "}"; + + try { + OrderVerificationData.OrderItem orderItem = gson.fromJson(json, OrderVerificationData.OrderItem.class); + + // 验证订单类型 + assertEquals(3, orderItem.getOrder_type()); + + // 验证项目名称 + assertEquals("当天有效年月卡", orderItem.getProject()); + + // 验证info字段 + assertTrue(orderItem.getInfo().isJsonObject()); + assertEquals("2509107498", orderItem.getCardNoFromInfo()); + + // 对于年月卡订单,应该可以不传递v_code进行核销 + // 这里我们验证getVerificationCode方法仍然可以正常工作 + assertEquals("2509107776", orderItem.getVerificationCode()); + + System.out.println("年月卡订单测试通过: " + orderItem.getOrder_no()); + + } catch (Exception e) { + e.printStackTrace(); + System.out.println("年月卡订单测试失败: " + e.getMessage()); + } + } + + /** + * 测试非年月卡订单仍需要v_code的情况 + */ + @Test + public void testNonMonthlyCardWithVCode() { + // 模拟人次票订单数据(order_type=1) + String json = "{\n" + + " \"order_no\": \"RC20250910172452809631\",\n" + + " \"start_time\": \"2025-09-10 17:24:52\",\n" + + " \"end_time\": \"4763-08-07 17:24:52\",\n" + + " \"order_type\": 1,\n" + + " \"project\": \"20230727测试\",\n" + + " \"number\": 5,\n" + + " \"v_code\": [\"250910170565\",\"250910174341\",\"250910177626\"],\n" + + " \"info\": \"平时\",\n" + + " \"success\": 3,\n" + + " \"pv_usage_duration\": 0,\n" + + " \"many_enter\": false\n" + + "}"; + + try { + OrderVerificationData.OrderItem orderItem = gson.fromJson(json, OrderVerificationData.OrderItem.class); + + // 验证订单类型 + assertEquals(1, orderItem.getOrder_type()); + + // 验证项目名称 + assertEquals("20230727测试", orderItem.getProject()); + + // 验证info字段 + assertTrue(orderItem.getInfo().isJsonPrimitive()); + assertEquals("平时", orderItem.getInfoAsString()); + + // 对于非年月卡订单,必须传递v_code进行核销 + assertEquals("250910170565", orderItem.getVerificationCode()); + + System.out.println("非年月卡订单测试通过: " + orderItem.getOrder_no()); + + } catch (Exception e) { + e.printStackTrace(); + System.out.println("非年月卡订单测试失败: " + e.getMessage()); + } + } +} \ No newline at end of file