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 f6d9498..717bb59 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 @@ -1,19 +1,34 @@ package com.cxyxiaomo.epp.common.enums; +import java.util.Arrays; +import java.util.Optional; + public enum OrderStatus { - PENDING("Pending"), - PROCESSING("Processing"), - SHIPPED("Shipped"), - DELIVERED("Delivered"), - CANCELLED("Cancelled"); + PENDING("Pending", "等待确认"), + PROCESSING("Processing", "已支付,正在处理"), + SHIPPED("Shipped", "已发货,等待确认收货"), + DELIVERED("Delivered", "已送达"), + CANCELLED("Cancelled", "已取消"); private final String value; + private final String description; - private OrderStatus(String value) { + private OrderStatus(String value, String description) { this.value = value; + this.description = description; + } + + public static OrderStatus get(String code) { + Optional first = Arrays.stream(OrderStatus.values()).filter(e -> e.getValue().equals(code)).findFirst(); + OrderStatus orderStatus = first.orElse(null); + return orderStatus; } public String getValue() { return value; } + + public String toString() { + return description; + } } diff --git a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/pojo/Order.java b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/pojo/Order.java index 425fe01..9fe7fb6 100644 --- a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/pojo/Order.java +++ b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/pojo/Order.java @@ -1,6 +1,5 @@ package com.cxyxiaomo.epp.common.pojo; -import com.cxyxiaomo.epp.common.enums.OrderStatus; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; @@ -14,5 +13,5 @@ public class Order { private Long id; private Integer userId; private LocalDateTime orderDate; - private OrderStatus orderStatus; + private String orderStatus; } diff --git a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/GoodVO.java b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/GoodVO.java index 25e61af..69b05a9 100644 --- a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/GoodVO.java +++ b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/GoodVO.java @@ -17,7 +17,7 @@ import java.util.stream.Collectors; @Accessors(chain = true) // 链式写法 // 微服务必须要实现Serializable public class GoodVO implements Serializable { - Long id; + String id; String goodsName; Integer categoryId; String brand; @@ -38,6 +38,7 @@ public class GoodVO implements Serializable { } GoodVO goodVO = new GoodVO(); BeanUtils.copyProperties(good, goodVO); + goodVO.setId(String.valueOf(good.getId())); return goodVO; } diff --git a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/OrderDetailVO.java b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/OrderDetailVO.java index 3b6eba7..8dd898e 100644 --- a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/OrderDetailVO.java +++ b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/OrderDetailVO.java @@ -4,6 +4,7 @@ import com.cxyxiaomo.epp.common.pojo.OrderDetail; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; +import org.springframework.beans.BeanUtils; import java.io.Serializable; import java.util.List; @@ -17,8 +18,8 @@ import java.util.stream.Collectors; // 微服务必须要实现Serializable public class OrderDetailVO implements Serializable { private Long id; - private Long orderId; - private Long goodId; + private String orderId; + private String goodId; private Integer goodCount; private Double unitPrice; @@ -27,7 +28,9 @@ public class OrderDetailVO implements Serializable { return null; } OrderDetailVO orderDetailVO = new OrderDetailVO(); - // BeanUtils.copyProperties(orderDetail, orderDetailVO); + BeanUtils.copyProperties(orderDetail, orderDetailVO); + orderDetailVO.setOrderId(String.valueOf(orderDetail.getOrderId())); + orderDetailVO.setGoodId(String.valueOf(orderDetail.getGoodId())); return orderDetailVO; } diff --git a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/OrderVO.java b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/OrderVO.java index b064cb2..4e7f879 100644 --- a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/OrderVO.java +++ b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/OrderVO.java @@ -8,7 +8,6 @@ import lombok.experimental.Accessors; import org.springframework.beans.BeanUtils; import java.io.Serializable; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; import java.util.stream.Collectors; @@ -20,10 +19,10 @@ import java.util.stream.Collectors; @Accessors(chain = true) // 链式写法 // 微服务必须要实现Serializable public class OrderVO implements Serializable { - private Long id; + private String id; private Integer userId; private LocalDateTime orderDate; - private OrderStatus orderStatus; + private String orderStatus; public static OrderVO convertFrom(Order order) { if (order == null) { @@ -31,6 +30,10 @@ public class OrderVO implements Serializable { } OrderVO orderVO = new OrderVO(); BeanUtils.copyProperties(order, orderVO); + + orderVO.setId(String.valueOf(order.getId())); // 转成字符串 避免前端丢失精度 + OrderStatus orderStatus = OrderStatus.get(order.getOrderStatus()); + orderVO.setOrderStatus(orderStatus.toString()); return orderVO; } diff --git a/backend/microservice-provider-shop-8003/pom.xml b/backend/microservice-provider-shop-8003/pom.xml index 3b8f1a4..4fa6652 100644 --- a/backend/microservice-provider-shop-8003/pom.xml +++ b/backend/microservice-provider-shop-8003/pom.xml @@ -109,6 +109,12 @@ org.springframework.boot spring-boot-starter-websocket + + + + + + 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 055bfc3..ee10481 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 @@ -2,14 +2,22 @@ package com.cxyxiaomo.epp.shop.controller; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; +import com.cxyxiaomo.epp.common.pojo.Order; import com.cxyxiaomo.epp.common.pojo.OrderDetail; import com.cxyxiaomo.epp.common.response.Res; +import com.cxyxiaomo.epp.common.vo.GoodVO; +import com.cxyxiaomo.epp.common.vo.OrderDetailVO; +import com.cxyxiaomo.epp.common.vo.OrderVO; +import com.cxyxiaomo.epp.shop.service.GoodsService; import com.cxyxiaomo.epp.shop.service.OrderService; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.util.HashMap; import java.util.LinkedList; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; @RestController @RequestMapping("/shop/order") @@ -18,12 +26,14 @@ public class OrderController { @Resource OrderService orderService; + @Resource + GoodsService goodsService; + /** * 小程序端创建订单 * * @param userId 下单用户 * @param orderList 下单商品 [ { goodId, count }, ... ] - * @param orderList 下单商品 [ { goodId, count }, ... ] * @return */ @PostMapping("/miniprogram/createOrder") @@ -58,7 +68,7 @@ public class OrderController { try { long orderId = orderService.createOrder(userId, orderDetailList); - return Res.success(orderId); + return Res.success(String.valueOf(orderId)); // 转为 String 避免前端丢失精度 } catch (Exception e) { String message = e.getMessage(); if (message.contains("Exception")) { @@ -69,4 +79,63 @@ public class OrderController { } } } + + /** + * 获取指定订单的详情 + * + * @param orderId + * @return + */ + @GetMapping("/miniprogram/orderDetail") + @ResponseBody + public Res orderDetail(@RequestParam Long orderId) { + if (orderId == null) { + return Res.error("订单不存在"); + } + + try { + OrderVO orderVO = orderService.getOrderById(orderId); + if (orderVO == null) { + return Res.error("订单不存在"); + } + List orderDetailVOList = orderService.getOrderDetailById(orderId); + + List goodIdList = orderDetailVOList.stream().map(orderDetailVO -> { + String goodId = orderDetailVO.getGoodId(); + return Long.parseLong(goodId); + }).collect(Collectors.toList()); + + List goodVOList = goodsService.listByIds(goodIdList); + HashMap resultMap = new HashMap<>(); + resultMap.put("order", orderVO); + resultMap.put("orderItem", orderDetailVOList); + resultMap.put("goods", goodVOList); + return Res.success(resultMap); + } catch (Exception e) { + String message = e.getMessage(); + if (message.contains("Exception")) { + // 内部异常不打印出去 + return Res.error("查询订单失败,请重试"); + } else { + return Res.error(message); + } + } + } + + /** + * 获取用户的订单列表 + * + * @param userId + * @return + */ + @GetMapping("/miniprogram/listUserOrder") + @ResponseBody + public Res listUserOrder(@RequestParam Integer userId) { + if (userId == null) { + return Res.error("参数错误"); + } + List orders = orderService.listUserOrder(userId); + List orderVOS = OrderVO.convertFrom(orders); + return Res.success(orderVOS); + } } diff --git a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/dao/GoodsDao.java b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/dao/GoodsDao.java index beeaa46..e0b0ce6 100644 --- a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/dao/GoodsDao.java +++ b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/dao/GoodsDao.java @@ -17,6 +17,8 @@ public interface GoodsDao { Good selectById(Long id); + List listByIds(List id); + Integer insert(Good good); Integer update(Good good); 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 adae7fe..6b343d3 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 @@ -18,7 +18,15 @@ public interface OrderDao { * @param orderId * @return */ - Order getOrderById(Integer orderId); + Order getOrderById(Long orderId); + + /** + * 根据订单 ID 查询订单详情 + * + * @param orderId + * @return + */ + List getOrderDetailById(Long orderId); /** * 根据用户 ID 查询订单列表 @@ -26,7 +34,7 @@ public interface OrderDao { * @param userId * @return */ - List listOrdersByUserId(Integer userId); + List listOrderByUserId(Integer userId); /** * 创建订单 @@ -48,5 +56,5 @@ public interface OrderDao { int updateOrderStatus(@Param("orderId") Integer orderId, @Param("status") String status); // 根据订单 ID 删除订单信息及订单详情信息 - int deleteOrderById(Integer orderId); + int deleteOrderById(Long orderId); } diff --git a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/service/GoodsService.java b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/service/GoodsService.java index d173196..3acd30f 100644 --- a/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/service/GoodsService.java +++ b/backend/microservice-provider-shop-8003/src/main/java/com/cxyxiaomo/epp/shop/service/GoodsService.java @@ -9,6 +9,7 @@ import com.cxyxiaomo.epp.shop.dao.GoodsDao; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.List; @Service @@ -37,4 +38,13 @@ public class GoodsService { List goodCategoryVOList = GoodCategoryVO.convertFrom(list); return goodCategoryVOList; } + + public List listByIds(List goodIdList) { + if (goodIdList == null || goodIdList.size() == 0) { + return new ArrayList<>(); + } + List list = goodsDao.listByIds(goodIdList); + List goodVOList = GoodVO.convertFrom(list); + return goodVOList; + } } 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 818650d..5109a61 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 @@ -5,6 +5,8 @@ import com.cxyxiaomo.epp.common.pojo.Good; 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; @@ -43,7 +45,7 @@ public class OrderService { order.setId(orderId); order.setUserId(userId); order.setOrderDate(LocalDateTime.now()); - order.setOrderStatus(OrderStatus.PENDING); + order.setOrderStatus(OrderStatus.PENDING.getValue()); orderDao.insertOrder(order); // 创建订单详情 @@ -68,4 +70,43 @@ public class OrderService { orderDao.batchInsertOrderDetail(orderItemList); return orderId; } + + /** + * 获取订单 + * + * @param orderId + */ + public OrderVO getOrderById(Long orderId) { + Order order = orderDao.getOrderById(orderId); + if (order == null) { + return null; + } + OrderVO orderVO = OrderVO.convertFrom(order); + return orderVO; + } + + /** + * 获取订单详情 + * + * @param orderId + */ + public List getOrderDetailById(Long orderId) { + List orderDetails = orderDao.getOrderDetailById(orderId); + if (orderDetails == null) { + return null; + } + List orderDetailVOS = OrderDetailVO.convertFrom(orderDetails); + return orderDetailVOS; + } + + /** + * 通过用户 id 获取用户订单列表 + * + * @param userId + * @return + */ + public List listUserOrder(Integer userId) { + List orders = orderDao.listOrderByUserId(userId); + return orders; + } } diff --git a/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/GoodsMapper.xml b/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/GoodsMapper.xml index d720545..2e77e89 100644 --- a/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/GoodsMapper.xml +++ b/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/GoodsMapper.xml @@ -50,6 +50,20 @@ WHERE id = #{id} + + diff --git a/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderDao.xml b/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderMapper.xml similarity index 62% rename from backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderDao.xml rename to backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderMapper.xml index bc3a39e..c726a14 100644 --- a/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderDao.xml +++ b/backend/microservice-provider-shop-8003/src/main/resources/mybatis/mapper/OrderMapper.xml @@ -11,29 +11,40 @@ - - - - - + + + + + + + + - SELECT * - FROM order + FROM `order` WHERE user_id = #{userId} + order by order_date desc - - INSERT INTO `order` (id, user_id, order_date, order_status) VALUES - (#{id}, #{userId}, #{orderDate,javaType=java.time.LocalDateTime,jdbcType=TIMESTAMP}, #{orderStatus}) + + INSERT INTO `order` (id, user_id, order_date, order_status) + VALUES (#{id}, #{userId}, #{orderDate,javaType=java.time.LocalDateTime,jdbcType=TIMESTAMP}, #{orderStatus}) @@ -51,12 +62,18 @@ - UPDATE order SET order_status=#{orderStatus} WHERE order_id=#{orderId} + UPDATE order + SET order_status=#{orderStatus} + WHERE order_id = #{orderId} - DELETE FROM order WHERE order_id = #{orderId}; - DELETE FROM goods_order_details WHERE order_id = #{orderId}; + DELETE + FROM order + WHERE order_id = #{orderId}; + DELETE + FROM goods_order_details + WHERE order_id = #{orderId}; diff --git a/backend/pom.xml b/backend/pom.xml index ddd5dcd..c15df97 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -207,6 +207,13 @@ ${lombok.version} true + + + + org.springframework.boot + spring-boot-devtools + 2.7.5 + diff --git a/database/epp.sql b/database/epp.sql index e48de21..37a2c70 100644 --- a/database/epp.sql +++ b/database/epp.sql @@ -11,7 +11,7 @@ Target Server Version : 80012 File Encoding : 65001 - Date: 19/03/2023 17:36:14 + Date: 20/03/2023 00:21:56 */ SET NAMES utf8mb4; @@ -264,6 +264,16 @@ CREATE TABLE `order` ( -- ---------------------------- -- Records of order -- ---------------------------- +INSERT INTO `order` VALUES (1748800678865801225, 1, '2023-03-19 21:29:33', 'Pending'); +INSERT INTO `order` VALUES (1748880836628975622, 1, '2023-03-19 23:48:04', 'Pending'); +INSERT INTO `order` VALUES (1748881003193176069, 1, '2023-03-19 23:48:44', 'Pending'); +INSERT INTO `order` VALUES (1748881359935508481, 1, '2023-03-19 23:50:09', 'Pending'); +INSERT INTO `order` VALUES (1748881582816628736, 1, '2023-03-19 23:51:02', 'Pending'); +INSERT INTO `order` VALUES (1748881775876247556, 1, '2023-03-19 23:51:48', 'Pending'); +INSERT INTO `order` VALUES (1748883439144275978, 1, '2023-03-19 23:58:25', 'Pending'); +INSERT INTO `order` VALUES (1748883587555528711, 1, '2023-03-19 23:59:00', 'Pending'); +INSERT INTO `order` VALUES (1748884264885293061, 1, '2023-03-20 00:01:41', 'Pending'); +INSERT INTO `order` VALUES (1748884523443163146, 1, '2023-03-20 00:02:43', 'Pending'); -- ---------------------------- -- Table structure for order_detail @@ -285,6 +295,18 @@ CREATE TABLE `order_detail` ( -- ---------------------------- -- Records of order_detail -- ---------------------------- +INSERT INTO `order_detail` VALUES (15, 1748800678865801225, 27, 1, 15.99); +INSERT INTO `order_detail` VALUES (16, 1748800678865801225, 12, 1, 5.99); +INSERT INTO `order_detail` VALUES (17, 1748800678865801225, 13, 1, 5.99); +INSERT INTO `order_detail` VALUES (18, 1748880836628975622, 16, 1, 5.99); +INSERT INTO `order_detail` VALUES (19, 1748881003193176069, 16, 1, 5.99); +INSERT INTO `order_detail` VALUES (20, 1748881359935508481, 16, 1, 5.99); +INSERT INTO `order_detail` VALUES (21, 1748881582816628736, 16, 1, 5.99); +INSERT INTO `order_detail` VALUES (22, 1748881775876247556, 64, 1, 18.00); +INSERT INTO `order_detail` VALUES (23, 1748883439144275978, 16, 1, 5.99); +INSERT INTO `order_detail` VALUES (24, 1748883587555528711, 16, 1, 5.99); +INSERT INTO `order_detail` VALUES (25, 1748884264885293061, 16, 1, 5.99); +INSERT INTO `order_detail` VALUES (26, 1748884523443163146, 57, 1, 2.00); -- ---------------------------- -- Table structure for report diff --git a/postman-collection/epp.postman_collection.json b/postman-collection/epp.postman_collection.json index 13f2ce8..9655ef2 100644 --- a/postman-collection/epp.postman_collection.json +++ b/postman-collection/epp.postman_collection.json @@ -267,7 +267,7 @@ "method": "GET", "header": [], "url": { - "raw": "https://epp.only4.work/shop/good/miniprogram/detail?id=1", + "raw": "https://epp.only4.work/shop/good/miniprogram/detail?id=2", "protocol": "https", "host": [ "epp", @@ -283,7 +283,7 @@ "query": [ { "key": "id", - "value": "1" + "value": "2" } ] } @@ -312,6 +312,164 @@ } }, "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": "https://epp.only4.work/shop/order/miniprogram/createOrder", + "protocol": "https", + "host": [ + "epp", + "only4", + "work" + ], + "path": [ + "shop", + "order", + "miniprogram", + "createOrder" + ] + } + }, + "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/createOrder", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8003", + "path": [ + "shop", + "order", + "miniprogram", + "createOrder" + ] + } + }, + "response": [] + }, + { + "name": "[线上] 小程序订单详情", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "https://epp.only4.work/shop/order/miniprogram/orderDetail?orderId=1", + "protocol": "https", + "host": [ + "epp", + "only4", + "work" + ], + "path": [ + "shop", + "order", + "miniprogram", + "orderDetail" + ], + "query": [ + { + "key": "orderId", + "value": "1" + } + ] + } + }, + "response": [] + }, + { + "name": "[微服务] 小程序订单详情", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:8003/shop/order/miniprogram/orderDetail?orderId=1748800678865801225", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8003", + "path": [ + "shop", + "order", + "miniprogram", + "orderDetail" + ], + "query": [ + { + "key": "orderId", + "value": "1", + "disabled": true + }, + { + "key": "orderId", + "value": "1748800678865801225" + } + ] + } + }, + "response": [] + }, + { + "name": "[微服务] 小程序用户订单列表", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:8003/shop/order/miniprogram/listUserOrder?userId=1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8003", + "path": [ + "shop", + "order", + "miniprogram", + "listUserOrder" + ], + "query": [ + { + "key": "orderId", + "value": "1", + "disabled": true + }, + { + "key": "userId", + "value": "1" + } + ] + } + }, + "response": [] } ] } diff --git a/weixin-miniprogram/app.json b/weixin-miniprogram/app.json index 24b28ce..5bc9270 100644 --- a/weixin-miniprogram/app.json +++ b/weixin-miniprogram/app.json @@ -7,6 +7,7 @@ "pages/residents/reportHistory", "pages/shop/shop", "pages/shop/goodDetail", + "pages/shop/orderConfirm", "pages/scan/entrance", "pages/person/person" ], diff --git a/weixin-miniprogram/pages/index/index.js b/weixin-miniprogram/pages/index/index.js index 64d671d..c2cb890 100644 --- a/weixin-miniprogram/pages/index/index.js +++ b/weixin-miniprogram/pages/index/index.js @@ -55,16 +55,16 @@ Page({ } } - // 开发模式下自动跳转到指定页面,节省开发时间 - console.log("app.globalData", app.globalData) - if (app.globalData.debugMode) { - wx.switchTab({ - // url: '/pages/residents/report' - // url: '/pages/person/person' - url: '/pages/shop/shop' - }) - return - } + // // 开发模式下自动跳转到指定页面,节省开发时间 + // console.log("app.globalData", app.globalData) + // if (app.globalData.debugMode) { + // wx.switchTab({ + // // url: '/pages/residents/report' + // // url: '/pages/person/person' + // url: '/pages/shop/shop' + // }) + // return + // } } }, @@ -170,7 +170,8 @@ Page({ magicButton() { wx.navigateTo({ - url: "/pages/scan/entrance" + // url: "/pages/scan/entrance?a=1" + url: "/pages/shop/orderConfirm?orderId=1748800678865801225" }) } }) diff --git a/weixin-miniprogram/pages/scan/entrance.js b/weixin-miniprogram/pages/scan/entrance.js index 74fc986..13c351f 100644 --- a/weixin-miniprogram/pages/scan/entrance.js +++ b/weixin-miniprogram/pages/scan/entrance.js @@ -5,14 +5,17 @@ Page({ * 页面的初始数据 */ data: { - + options: {}, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { - + console.log(options) + this.setData({ + options: JSON.stringify(options), + }) }, /** diff --git a/weixin-miniprogram/pages/scan/entrance.wxml b/weixin-miniprogram/pages/scan/entrance.wxml index 185a0d5..fc4c6b5 100644 --- a/weixin-miniprogram/pages/scan/entrance.wxml +++ b/weixin-miniprogram/pages/scan/entrance.wxml @@ -1,4 +1,5 @@ 您将要进入社区:未知 + {{ options }} \ No newline at end of file diff --git a/weixin-miniprogram/pages/shop/goodDetail.js b/weixin-miniprogram/pages/shop/goodDetail.js index 5599458..4351ed1 100644 --- a/weixin-miniprogram/pages/shop/goodDetail.js +++ b/weixin-miniprogram/pages/shop/goodDetail.js @@ -1,6 +1,7 @@ // pages/shop/goodDetail.js const goodService = require("../../services/good") +const orderService = require("../../services/order") Page({ @@ -73,6 +74,25 @@ Page({ }, + // 立即下单 + btnBuyTap() { + (async () => { + // 创建订单 + let orderList = [{ + goodId: this.data.goodId, + count: 1, + }] + let orderId = await orderService.createOrder(orderList) + console.log("orderId", orderId) + + // 跳转订单查看页面(携带订单号) + wx.navigateTo({ + // url: "/pages/scan/entrance?a=1" + url: "/pages/shop/orderConfirm?orderId=" + orderId + }) + })(); + }, + loadPageData() { (async () => { let goodDetail = await goodService.getGoodDetail(this.data.goodId) diff --git a/weixin-miniprogram/pages/shop/goodDetail.wxml b/weixin-miniprogram/pages/shop/goodDetail.wxml index 600c8b2..da74433 100644 --- a/weixin-miniprogram/pages/shop/goodDetail.wxml +++ b/weixin-miniprogram/pages/shop/goodDetail.wxml @@ -39,6 +39,6 @@ 加入购物车 - 立即下单 + 立即下单 \ No newline at end of file diff --git a/weixin-miniprogram/pages/shop/orderConfirm.js b/weixin-miniprogram/pages/shop/orderConfirm.js new file mode 100644 index 0000000..d8e470c --- /dev/null +++ b/weixin-miniprogram/pages/shop/orderConfirm.js @@ -0,0 +1,153 @@ +// pages/shop/orderConfirm.js + +const orderService = require("../../services/order") + +Page({ + + /** + * 页面的初始数据 + */ + data: { + orderId: '', + order: {}, + // goods: [], + // orderItem: [], + orderGoodList: [], + + orderTime: '', + }, + + /** + * 生命周期函数--监听页面加载 + */ + onLoad(options) { + console.log(options) + if (!options || !options.orderId) { + wx.showModal({ + title: '订单查询失败', + content: '请刷新页面后重试', + showCancel: false, + complete: (res) => { + wx.navigateBack() + } + }) + } + + this.setData({ + orderId: options.orderId, + }) + this.loadPageData() + }, + + /** + * 生命周期函数--监听页面初次渲染完成 + */ + onReady() { + + }, + + /** + * 生命周期函数--监听页面显示 + */ + onShow() { + + }, + + /** + * 生命周期函数--监听页面隐藏 + */ + onHide() { + + }, + + /** + * 生命周期函数--监听页面卸载 + */ + onUnload() { + + }, + + /** + * 页面相关事件处理函数--监听用户下拉动作 + */ + onPullDownRefresh() { + + }, + + /** + * 页面上拉触底事件的处理函数 + */ + onReachBottom() { + + }, + + /** + * 用户点击右上角分享 + */ + onShareAppMessage() { + + }, + + confirmPayment() { + // 点击确认支付按钮 + wx.showModal({ + title: '拉起支付', + content: '点击确认完成支付,点击取消放弃支付', + complete: (res) => { + if (res.cancel) { + wx.showToast({ + title: '用户取消支付', + icon: 'error', + }) + } + + if (res.confirm) { + wx.showToast({ + title: '支付成功', + icon: 'success', + }) + } + } + }) + }, + + loadPageData() { + (async () => { + console.log("orderId", this.data.orderId) + let orderDetail = await orderService.getOrderDetail(this.data.orderId) + console.log("orderDetail", orderDetail) + + if (!orderDetail) { + wx.showModal({ + title: '订单查询失败', + content: '', + showCancel: false, + complete: (res) => { + wx.navigateBack() + } + }) + return + } + + let orderGoodList = [] + for (let item of orderDetail.orderItem) { + orderGoodList.push({ + wxid: item.goodId, // 用于列表渲染 + good: orderDetail.goods.find(good => good.id == item.goodId), + goodCount: item.goodCount, + unitPrice: item.unitPrice, + }) + } + console.log("orderGoodList", orderGoodList) + + this.setData({ + order: orderDetail.order, + // goods: orderDetail.goods, + // orderItem: orderDetail.orderItem, + orderGoodList: orderGoodList, + + orderTime: orderDetail.order.orderDate.replace("T", " ") + }) + })(); + } +}) \ No newline at end of file diff --git a/weixin-miniprogram/pages/shop/orderConfirm.json b/weixin-miniprogram/pages/shop/orderConfirm.json new file mode 100644 index 0000000..dda4969 --- /dev/null +++ b/weixin-miniprogram/pages/shop/orderConfirm.json @@ -0,0 +1,4 @@ +{ + "usingComponents": {}, + "navigationBarTitleText": "确认订单" +} \ No newline at end of file diff --git a/weixin-miniprogram/pages/shop/orderConfirm.wxml b/weixin-miniprogram/pages/shop/orderConfirm.wxml new file mode 100644 index 0000000..7f8609b --- /dev/null +++ b/weixin-miniprogram/pages/shop/orderConfirm.wxml @@ -0,0 +1,50 @@ + + + 🎉 + + 订单已创建,快去支付吧 + + + + + 订单号:{{order.id}} + 订单创建时间:{{orderTime}} + 订单状态:{{order.orderStatus}} + + +商品列表 + + + + + + + + + {{ item.good.brief }} + + + + + + ¥ + {{ item.unitPrice }} + + + + + + x{{ item.goodCount }} + + + + + + + + + + + 确认支付 + + \ No newline at end of file diff --git a/weixin-miniprogram/pages/shop/orderConfirm.wxss b/weixin-miniprogram/pages/shop/orderConfirm.wxss new file mode 100644 index 0000000..de0474b --- /dev/null +++ b/weixin-miniprogram/pages/shop/orderConfirm.wxss @@ -0,0 +1,144 @@ +/* pages/shop/orderConfirm.wxss */ +.page-title-container { + margin-top: 16px; + text-align: center; +} + +.page-title-emoji { + font-size: 72px; +} + +.page-title { + /* border: 2px dashed black; */ + display: inline-block; + padding: 15px 20px; + font-size: 20px; +} + +.order-info { + background-color: tomato; + padding: 10px 18px; + margin: 10px; + border-radius: 10px; + color: #fff3f1; + font-weight: 500; + line-height: 1.8em; +} + +.good-list-title { + text-align: center; + margin-top: 20px; + margin-bottom: 20px; + font-weight: 500; + font-size: 18px; +} + +/* 商品列表样式 */ +.good-card-container { + /* border: 1px solid grey; */ + border-radius: 12px; + box-shadow: 0 2px 10px 0 rgba(0, 0, 0, .2); + margin: 0 14px; + padding: 15px 20px; + display: grid; + grid-template-columns: 120px 1fr 50px; + grid-template-rows: 4fr 3fr; + place-items: center; + gap: 0 8px; + margin-bottom: 10px; +} + +.good-card-image { + width: 88px; + height: 88px; + background-color: teal; + background-position: center; + background-repeat: no-repeat; + background-size: cover; + border: 1px solid grey; + border-radius: 5px; + grid-row-start: 1; + grid-row-end: 3; +} + +.good-card-info { + width: 100%; +} + +/* 商品标题 */ +.good-title-container { + grid-column-start: 2; + grid-column-end: 4; +} + +.good-title { + font-size: 30rpx; + color: #333; + font-weight: 400; + display: -webkit-box; + height: 72rpx; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; + overflow: hidden; + word-break: break-word; + line-height: 36rpx; + margin: 4rpx 0; +} + +/* 商品价格 */ +.good-price { + white-space: nowrap; + font-weight: 700; + margin: 0; + display: inline; + position: relative; +} + +.good-price-counter { + color: #fa4126; + font-size: 36rpx; +} + +.good-price .good-price-symbol { + font-size: 24rpx; + display: inline; +} + +.good-price .good-price-number { + display: inline; +} + +/* 屏幕底部 确认支付按钮 */ +.bottom-controlbox { + background-color: white; + position: fixed; + z-index: 999; + bottom: 0; + left: 0; + right: 0; + width: 100%; + padding-top: 6px; + padding-bottom: 12px; + border-top: 1px solid grey; + text-align: center; +} + +.bottom-buttons { + height: 100%; + display: inline-block; +} + +.btn { + width: 254rpx; + height: 80rpx; + display: inline-grid; + align-items: center; + justify-content: center; + color: white; +} + +#btn-confirm-payment { + background-color: #fa4126; + color: #fff; + border-radius: 40rpx; +} diff --git a/weixin-miniprogram/pages/shop/shop.wxss b/weixin-miniprogram/pages/shop/shop.wxss index 095acd8..357423f 100644 --- a/weixin-miniprogram/pages/shop/shop.wxss +++ b/weixin-miniprogram/pages/shop/shop.wxss @@ -39,6 +39,9 @@ /* border-bottom: 1px solid rgb(121, 121, 121); */ height: 100%; display: table; + + /* iOS下要设置一下 width */ + width: 100%; } .sidebar-item:last-child { @@ -56,6 +59,7 @@ font-weight: bold; color: #FF764E; border-left: 4px solid #FF764E; + box-sizing: border-box; } .tabbar { diff --git a/weixin-miniprogram/project.config.json b/weixin-miniprogram/project.config.json index 870fdaf..868b32e 100644 --- a/weixin-miniprogram/project.config.json +++ b/weixin-miniprogram/project.config.json @@ -37,7 +37,8 @@ "ignore": [], "disablePlugins": [], "outputPath": "" - } + }, + "ignoreUploadUnusedFiles": false }, "compileType": "miniprogram", "libVersion": "2.19.4", diff --git a/weixin-miniprogram/services/order.js b/weixin-miniprogram/services/order.js new file mode 100644 index 0000000..aa17584 --- /dev/null +++ b/weixin-miniprogram/services/order.js @@ -0,0 +1,28 @@ +/* eslint-disable no-param-reassign */ + +const send_request = require('../utils/sendRequest') + +/** 商品下单 */ +export function createOrder(orderList) { + let userInfo = wx.getStorageSync("userInfo") + // orderList 结构为 [ { goodId, count }, ... ] + return send_request({ + url: '/shop/order/miniprogram/createOrder', + method: "POST", + data: { + "userId": userInfo.id, + "orderList": orderList + } + }) +} + +/** 商品下单 */ +export function getOrderDetail(orderId) { + return send_request({ + url: '/shop/order/miniprogram/orderDetail', + method: "GET", + data: { + "orderId": orderId + } + }) +}