From 4088f26758179f692a2bdb2a46ace3bbc5a5c66d Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Wed, 6 Nov 2024 10:12:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E5=95=86=E5=93=81?= =?UTF-8?q?=E7=B1=BB=E5=88=AB=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/order/OrderController.java | 28 ++++++ .../controller/wx/WeChatController.java | 95 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/main/java/com/cultural/heritage/controller/wx/WeChatController.java diff --git a/src/main/java/com/cultural/heritage/controller/order/OrderController.java b/src/main/java/com/cultural/heritage/controller/order/OrderController.java index fc271e2..714e3fc 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -10,6 +10,7 @@ import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.constant.UserConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.order.OrderAddRequest; import com.cultural.heritage.model.dto.order.OrderItemAddRequest; import com.cultural.heritage.model.dto.order.OrderQueryRequest; @@ -176,6 +177,33 @@ public class OrderController { // } // + /** + * 小程序端用户查询订单 + * @param commonRequest 用户id请求体 + * @return 用户订单列表 + */ + @PostMapping("/list") + @Operation(summary = "小程序端用户查询订单", description = "参数:用户id请求体,权限:所有人,方法名:listUserOrder") + public BaseResponse<List<OrderVO>> listUserOrder(@RequestBody CommonRequest commonRequest) { + if (commonRequest == null || commonRequest.getId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + Long id = commonRequest.getId(); + QueryWrapper<Order> queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("userId", id); + List<Order> orders = orderService.list(queryWrapper); + List<OrderVO> orderVOS = orders.stream().map(order -> { + Long orderId = order.getId(); + QueryWrapper<OrderItems> orderItemsQueryWrapper = new QueryWrapper<>(); + orderItemsQueryWrapper.eq("orderId", orderId); + List<OrderItems> orderItemsList = orderItemService.list(orderItemsQueryWrapper); + OrderVO orderVO = new OrderVO(); + BeanUtils.copyProperties(order, orderVO); + orderVO.setOrderItemList(orderItemsList); + return orderVO; + }).toList(); + return ResultUtils.success(orderVOS); + } /** diff --git a/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java b/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java new file mode 100644 index 0000000..5075585 --- /dev/null +++ b/src/main/java/com/cultural/heritage/controller/wx/WeChatController.java @@ -0,0 +1,95 @@ +package com.cultural.heritage.controller.wx; + + +import com.alibaba.fastjson.JSON; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + + +@RestController +@RequestMapping("/wx") +@Slf4j +@Tag(name = "小程序获取公众号文章") +public class WeChatController { + + private String getToken() throws MalformedURLException, IOException, ProtocolException { + // access_token接口https请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET + String path = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; + String appid = "wx3f968a09e31d6bed"; + String secret = "847bdda7c2b01e88d59948b9ba50ef8d"; + URL url = new URL(path+"&appid=" + appid + "&secret=" + secret); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.connect(); + + InputStream in = connection.getInputStream(); + byte[] b = new byte[100]; + int len = -1; + StringBuffer sb = new StringBuffer(); + while((len = in.read(b)) != -1) { + sb.append(new String(b,0,len)); + } + + System.out.println(sb.toString()); + in.close(); + return sb.toString(); + } + + + private String getContentList(String token) throws IOException { + String path = " https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" + token; + URL url = new URL(path); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + connection.setRequestProperty("content-type", "application/json;charset=utf-8"); + connection.connect(); + // post发送的参数 + Map<String, Object> map = new HashMap<>(); + map.put("type", "news"); // news表示图文类型的素材,具体看API文档 + map.put("offset", 0); + map.put("count", 1); + // 将map转换成json字符串 + String paramBody = JSON.toJSONString(map); // 这里用了Alibaba的fastjson + + OutputStream out = connection.getOutputStream(); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); + bw.write(paramBody); // 向流中写入参数字符串 + bw.flush(); + + InputStream in = connection.getInputStream(); + byte[] b = new byte[100]; + int len = -1; + StringBuffer sb = new StringBuffer(); + while((len = in.read(b)) != -1) { + sb.append(new String(b,0,len)); + } + + in.close(); + return sb.toString(); + } + + + @Test + public void test() throws IOException { + + String result1 = getToken(); + Map<String,Object> token = (Map<String, Object>) JSON.parseObject(result1); + String result2 = getContentList(token.get("access_token").toString()); + System.out.println(result2); + } + + +} +