diff --git a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/enums/OrderStatus.java b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/enums/OrderStatus.java index 717bb59..e9751b1 100644 --- a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/enums/OrderStatus.java +++ b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/enums/OrderStatus.java @@ -5,7 +5,7 @@ import java.util.Optional; public enum OrderStatus { PENDING("Pending", "等待确认"), - PROCESSING("Processing", "已支付,正在处理"), + PROCESSING("Processing", "已支付,等待发货中"), SHIPPED("Shipped", "已发货,等待确认收货"), DELIVERED("Delivered", "已送达"), CANCELLED("Cancelled", "已取消"); diff --git a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/controller/OrderController.java b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/controller/OrderController.java index d455e60..94dbd30 100644 --- a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/controller/OrderController.java +++ b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/controller/OrderController.java @@ -92,8 +92,8 @@ public class OrderController { } try { - OrderVO orderVO = orderService.getOrderById(orderId); - if (orderVO == null) { + Order order = orderService.getOrderById(orderId); + if (order == null) { return Res.error("订单不存在"); } List orderDetailVOList = orderService.getOrderDetailById(orderId); @@ -105,7 +105,7 @@ public class OrderController { List goodVOList = goodsService.listByIds(goodIdList); HashMap resultMap = new HashMap<>(); - resultMap.put("order", orderVO); + resultMap.put("order", OrderVO.convertFrom(order)); resultMap.put("orderItem", orderDetailVOList); resultMap.put("goods", goodVOList); return Res.success(resultMap); @@ -149,4 +149,40 @@ public class OrderController { map.put("orders", orderVOS); return Res.success(map); } + + @PostMapping("/miniprogram/payOrder") + @ResponseBody + public Res payOrder(@RequestBody JSONObject params) { + Long orderId = params.getLong("orderId"); + if (orderId == null) { + return Res.error("参数错误"); + } + + // 查询订单详情 + Order order = orderService.getOrderById(orderId); + if (order == null) { + return Res.error("订单不存在"); + } + + //判断是否可以支付 + OrderStatus orderStatus = OrderStatus.get(order.getOrderStatus()); + if (orderStatus != OrderStatus.PENDING) { + } + switch (orderStatus) { + case PENDING: + // 可以支付 + break; + case PROCESSING: + return Res.error("订单已支付,无需重复支付"); + case CANCELLED: + return Res.error("订单已取消,无需重复支付"); + default: + return Res.error("当前订单状态无法支付"); + } + + // 更新支付信息 + Boolean success = orderService.updateOrderStatus(orderId, OrderStatus.PROCESSING); + + return Res.success(success ? "支付成功" : "支付失败"); + } } diff --git a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/dao/OrderDao.java b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/dao/OrderDao.java index 6780394..352fa73 100644 --- a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/dao/OrderDao.java +++ b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/dao/OrderDao.java @@ -53,8 +53,14 @@ public interface OrderDao { */ int batchInsertOrderDetail(List details); - // 更新订单状态 - int updateOrderStatus(@Param("orderId") Integer orderId, @Param("status") String status); + /** + * 更新订单状态 + * + * @param orderId + * @param status + * @return + */ + int updateOrderStatus(@Param("orderId") Long orderId, @Param("orderStatus") String orderStatus); // 根据订单 ID 删除订单信息及订单详情信息 int deleteOrderById(Long orderId); diff --git a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/service/OrderService.java b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/service/OrderService.java index 274e815..fe694e0 100644 --- a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/service/OrderService.java +++ b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/service/OrderService.java @@ -6,7 +6,6 @@ import com.cxyxiaomo.epp.common.pojo.Order; import com.cxyxiaomo.epp.common.pojo.OrderDetail; import com.cxyxiaomo.epp.common.utils.SnowflakeManager; import com.cxyxiaomo.epp.common.vo.OrderDetailVO; -import com.cxyxiaomo.epp.common.vo.OrderVO; import com.cxyxiaomo.epp.shop.dao.GoodsDao; import com.cxyxiaomo.epp.shop.dao.OrderDao; import org.springframework.stereotype.Service; @@ -89,13 +88,12 @@ public class OrderService { * * @param orderId */ - public OrderVO getOrderById(Long orderId) { + public Order getOrderById(Long orderId) { Order order = orderDao.getOrderById(orderId); if (order == null) { return null; } - OrderVO orderVO = OrderVO.convertFrom(order); - return orderVO; + return order; } /** @@ -122,4 +120,16 @@ public class OrderService { List orders = orderDao.listOrderByUserId(userId, orderStatus); return orders; } + + /** + * 更新订单状态 + * + * @param orderId + * @param orderStatus + * @return + */ + public Boolean updateOrderStatus(Long orderId, OrderStatus orderStatus) { + int affectRows = orderDao.updateOrderStatus(orderId, orderStatus.getValue()); + return affectRows > 0; + } } diff --git a/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderMapper.xml b/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderMapper.xml index 3c923e6..e181f24 100644 --- a/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderMapper.xml +++ b/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderMapper.xml @@ -47,12 +47,13 @@ INSERT INTO `order` (id, user_id, order_date, order_status, order_price) - VALUES (#{id}, #{userId}, #{orderDate,javaType=java.time.LocalDateTime,jdbcType=TIMESTAMP}, #{orderStatus}, #{orderPrice}) + VALUES (#{id}, #{userId}, #{orderDate,javaType=java.time.LocalDateTime,jdbcType=TIMESTAMP}, #{orderStatus}, + #{orderPrice}) - INSERT INTO order_detail (order_id, good_id, good_count, unit_price) + INSERT INTO `order_detail` (order_id, good_id, good_count, unit_price) VALUES @@ -65,18 +66,19 @@ - UPDATE order - SET order_status=#{orderStatus} - WHERE order_id = #{orderId} + UPDATE `order` + SET order_status = #{orderStatus} + WHERE id = #{orderId} LIMIT 1 DELETE - FROM order - WHERE order_id = #{orderId}; - DELETE - FROM goods_order_details - WHERE order_id = #{orderId}; + FROM `order` + WHERE id = #{orderId}; + -- 级联删除,无需手动删除订单详情 +-- DELETE +-- FROM goods_order_details +-- WHERE order_id = #{orderId}; diff --git a/postman-collection/epp.postman_collection.json b/postman-collection/epp.postman_collection.json index 9655ef2..fd825ae 100644 --- a/postman-collection/epp.postman_collection.json +++ b/postman-collection/epp.postman_collection.json @@ -470,6 +470,43 @@ } }, "response": [] + }, + { + "name": "[微服务] 小程序端支付订单", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"userId\": 1,\r\n \"orderList\": [\r\n {\r\n \"goodId\": 2,\r\n \"count\": 3\r\n },\r\n {\r\n \"goodId\": 3,\r\n \"count\": 1\r\n }\r\n ]\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "http://localhost:8003/shop/order/miniprogram/payOrder?orderId=1749496012575215624", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8003", + "path": [ + "shop", + "order", + "miniprogram", + "payOrder" + ], + "query": [ + { + "key": "orderId", + "value": "1749496012575215624" + } + ] + } + }, + "response": [] } ] } diff --git a/weixin-miniprogram/pages/shop/myOrder.js b/weixin-miniprogram/pages/shop/myOrder.js index b73ce8f..7961cb5 100644 --- a/weixin-miniprogram/pages/shop/myOrder.js +++ b/weixin-miniprogram/pages/shop/myOrder.js @@ -1,6 +1,5 @@ // pages/shop/myOrder.js -const goodService = require("../../services/good") const orderService = require("../../services/order") Page({ diff --git a/weixin-miniprogram/pages/shop/orderDetail.js b/weixin-miniprogram/pages/shop/orderDetail.js index d59c0ac..e0a9358 100644 --- a/weixin-miniprogram/pages/shop/orderDetail.js +++ b/weixin-miniprogram/pages/shop/orderDetail.js @@ -112,10 +112,23 @@ Page({ } if (res.confirm) { - wx.showToast({ - title: '支付成功', - icon: 'success', - }) + (async () => { + let payResult = await orderService.payOrder(this.data.orderId) + console.log("payResult", payResult) + wx.showModal({ + title: '支付结果', + content: payResult, + showCancel: false, + complete: (res) => { + wx.navigateBack({ + delta: getCurrentPages().length - 1 + }) + wx.navigateTo({ + url: "/pages/shop/orderDetail?orderId=" + this.data.orderId, + }) + } + }) + })(); } } }) @@ -165,4 +178,4 @@ Page({ }) })(); } -}) \ No newline at end of file +}) diff --git a/weixin-miniprogram/services/order.js b/weixin-miniprogram/services/order.js index bafbaca..b373ab6 100644 --- a/weixin-miniprogram/services/order.js +++ b/weixin-miniprogram/services/order.js @@ -39,3 +39,14 @@ export function listUserOrder(filter) { } }) } + +/** 订单支付 */ +export function payOrder(orderId) { + return send_request({ + url: '/shop/order/miniprogram/payOrder', + method: "POST", + data: { + "orderId": orderId, + } + }) +}