From 58a9791b54b04a58a33cf58dcb0fd1928cca9b31 Mon Sep 17 00:00:00 2001
From: chen-xin-zhi <3588068430@qq.com>
Date: Thu, 27 Mar 2025 09:36:22 +0800
Subject: [PATCH] this is 3.24 update
---
pom.xml | 7 ++
.../controller/file/FileController.java | 7 ++
.../controller/global/GlobalController.java | 4 +-
.../wx/WeChatLogisticsController.java | 75 ++++++++++++++++---
.../service/file/IHweiYunOBSService.java | 12 +++
.../file/impl/HweiYunOBSServiceImpl.java | 40 +++++++++-
src/main/resources/application.yml | 12 +--
7 files changed, 139 insertions(+), 18 deletions(-)
diff --git a/pom.xml b/pom.xml
index c9ad2e2..26564ce 100644
--- a/pom.xml
+++ b/pom.xml
@@ -197,6 +197,13 @@
+
+ net.coobird
+ thumbnailator
+ 0.4.8
+
+
+
diff --git a/src/main/java/com/cultural/heritage/controller/file/FileController.java b/src/main/java/com/cultural/heritage/controller/file/FileController.java
index 0534113..8a75a29 100644
--- a/src/main/java/com/cultural/heritage/controller/file/FileController.java
+++ b/src/main/java/com/cultural/heritage/controller/file/FileController.java
@@ -161,6 +161,8 @@ public class FileController {
}
+
+
/**
* @param uploadFileRequest 业务名称
* @param multipartFile 文件
@@ -182,4 +184,9 @@ public class FileController {
}
+
+
+
+
+
}
diff --git a/src/main/java/com/cultural/heritage/controller/global/GlobalController.java b/src/main/java/com/cultural/heritage/controller/global/GlobalController.java
index 83c814f..6138ee3 100644
--- a/src/main/java/com/cultural/heritage/controller/global/GlobalController.java
+++ b/src/main/java/com/cultural/heritage/controller/global/GlobalController.java
@@ -360,8 +360,8 @@ public class GlobalController {
* 小程序端预约须知
* @return 当前类别的商品列表
*/
- @GetMapping("/update/notice")
- @Operation(summary = "web端更新预约须知", description = "参数:无,方法名:updateNotice")
+ @GetMapping("/query/notice")
+ @Operation(summary = "web端更新预约须知", description = "参数:无,方法名:queryNotice")
public BaseResponse queryNotice() {
String notice = (String) redisTemplate.opsForValue().get(NOTICE_KEY);
diff --git a/src/main/java/com/cultural/heritage/controller/wx/WeChatLogisticsController.java b/src/main/java/com/cultural/heritage/controller/wx/WeChatLogisticsController.java
index c1f614a..31ad8d1 100644
--- a/src/main/java/com/cultural/heritage/controller/wx/WeChatLogisticsController.java
+++ b/src/main/java/com/cultural/heritage/controller/wx/WeChatLogisticsController.java
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils;
+import com.cultural.heritage.config.WxAccessToken;
import com.cultural.heritage.config.WxWaybillToken;
import com.cultural.heritage.constant.FileConstant;
import com.cultural.heritage.exception.BusinessException;
@@ -19,6 +20,7 @@ import com.cultural.heritage.model.entity.OrderItems;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.good.GoodLogisticsInfoVO;
import com.cultural.heritage.model.vo.good.GoodsInfo;
+import com.cultural.heritage.service.file.IHweiYunOBSService;
import com.cultural.heritage.service.order.OrderItemService;
import com.cultural.heritage.service.order.OrderService;
import com.cultural.heritage.service.user.UserService;
@@ -29,13 +31,16 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
+import net.coobird.thumbnailator.Thumbnails;
+import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -74,7 +79,11 @@ public class WeChatLogisticsController {
private OrderItemService orderItemService;
+ @Resource
+ private IHweiYunOBSService iHweiYunOBSService;
+
+//
// /**
// * (小程序端)获取接口调用凭据
// *
@@ -96,6 +105,29 @@ public class WeChatLogisticsController {
// }
+
+
+
+ /**
+ * (小程序端)获取接口调用凭据
+ */
+ @GetMapping("/get/token")
+ @Operation(summary = "(小程序端)获取接口调用凭据", description = "参数:无, 权限:所有人, 方法名:getAccessToken")
+ public BaseResponse getAccessToken() {
+ String accessToken = (String) redisTemplate.opsForValue().get(ACCESS_TOKEN_KEY);
+ if (accessToken == null) {
+ weChatLogisticsService.getAccessToken();
+ accessToken = (String) redisTemplate.opsForValue().get(ACCESS_TOKEN_KEY);
+ }
+ WxAccessToken wxAccessToken = WxAccessToken.builder()
+ .access_token(accessToken)
+ .expires_in("7200").build();
+ return ResultUtils.success(wxAccessToken);
+ }
+
+
+
+
/**
* (小程序端)根据订单id查询物流信息
*/
@@ -109,7 +141,7 @@ public class WeChatLogisticsController {
String miniOpenId = loginUser.getMiniOpenId();
String accessToken = (String) redisTemplate.opsForValue().get(ACCESS_TOKEN_KEY);
if (accessToken == null) {
- weChatLogisticsService.getAccessToken();
+ weChatLogisticsService.getStableAccessToken();
accessToken = (String) redisTemplate.opsForValue().get(ACCESS_TOKEN_KEY);
}
Long id = commonRequest.getId();
@@ -121,12 +153,37 @@ public class WeChatLogisticsController {
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.eq("orderId", id);
List orderItemsList = orderItemService.list(queryWrapper);
- List goodLogisticsInfoVOS = orderItemsList.stream().map(orderItems -> {
+
+ List goodLogisticsInfoVOS = new ArrayList<>();
+ for (OrderItems orderItems : orderItemsList) {
GoodSnapshot goodSnapshot = orderItems.getGoodSnapshot();
String name = goodSnapshot.getName();
- String goodImg = FileConstant.SERVER_HOST + goodSnapshot.getGoodImg();
- return GoodLogisticsInfoVO.builder().goods_name(name).goods_img_url(goodImg).build();
- }).toList();
+ String goodImg = goodSnapshot.getGoodImg();
+ String[] imgArr = goodImg.split(";");
+ String imgUrl = FileConstant.SERVER_HOST + imgArr[0];
+ String filePath = null;
+ try {
+ URL url = new URL(imgUrl);
+ // 压缩图片到目标质量并返回InputStream
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ Thumbnails.of(url)
+// .scale(0.2f)
+ .size(70, 70) // 设定压缩后的图片大小
+ .outputQuality(1) // 设置压缩质量
+ .toOutputStream(outputStream);
+ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
+ filePath = RandomStringUtils.randomAlphabetic(16) + ".png";
+ iHweiYunOBSService.fileUpload(byteArrayInputStream, filePath);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ String resultUrl = FileConstant.SERVER_HOST + filePath;
+ GoodLogisticsInfoVO goodLogisticsInfoVO = GoodLogisticsInfoVO.builder().goods_name(name).goods_img_url(resultUrl).build();
+ goodLogisticsInfoVOS.add(goodLogisticsInfoVO);
+ }
+
+
GoodsInfo goodsInfo = GoodsInfo.builder().detail_list(goodLogisticsInfoVOS).build();
Map param = new HashMap<>();
param.put("openid", miniOpenId);
diff --git a/src/main/java/com/cultural/heritage/service/file/IHweiYunOBSService.java b/src/main/java/com/cultural/heritage/service/file/IHweiYunOBSService.java
index fbeb399..50682b8 100644
--- a/src/main/java/com/cultural/heritage/service/file/IHweiYunOBSService.java
+++ b/src/main/java/com/cultural/heritage/service/file/IHweiYunOBSService.java
@@ -4,12 +4,24 @@ import com.obs.services.model.PutObjectResult;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
+import java.io.InputStream;
+
/**
* 华为云OBS服务接口
*/
public interface IHweiYunOBSService {
+ /**
+ * @Description 上传文件(通过流来上传)
+ * @param: uploadFile 上传文件
+ * @param: objectKey 具体的文件名(含存储路径)
+ * @return PutObjectResult
+ */
+ PutObjectResult fileUpload(InputStream inputStream, String objectKey);
+
+
+
/**
* @Description 上传文件
* @param: uploadFile 上传文件
diff --git a/src/main/java/com/cultural/heritage/service/file/impl/HweiYunOBSServiceImpl.java b/src/main/java/com/cultural/heritage/service/file/impl/HweiYunOBSServiceImpl.java
index acc13c3..e7a4446 100644
--- a/src/main/java/com/cultural/heritage/service/file/impl/HweiYunOBSServiceImpl.java
+++ b/src/main/java/com/cultural/heritage/service/file/impl/HweiYunOBSServiceImpl.java
@@ -32,7 +32,6 @@ public class HweiYunOBSServiceImpl implements IHweiYunOBSService {
-
/**
* OBS-文件上传
* @param uploadFile 文件对象
@@ -65,6 +64,45 @@ public class HweiYunOBSServiceImpl implements IHweiYunOBSService {
return putObjectResult;
}
+
+
+
+
+ /**
+ * @Description 上传文件(通过流来上传)
+ * @param: uploadFile 上传文件
+ * @param: objectKey 具体的文件名(含存储路径)
+ * @return PutObjectResult
+ */
+ @Override
+ public PutObjectResult fileUpload(InputStream inputStream, String objectKey) {
+ ObsClient obsClient = null;
+ PutObjectResult putObjectResult = null;
+ try {
+ String bucketName = hweiOBSConfig.getBucketName();
+ obsClient = hweiOBSConfig.getInstance();
+ long available = inputStream.available();
+ PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectKey, inputStream);
+
+ ObjectMetadata objectMetadata = new ObjectMetadata();
+ objectMetadata.setContentLength(available);
+ putObjectRequest.setMetadata(objectMetadata);
+ // 设置对象访问权限为公共读
+ putObjectRequest.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
+ putObjectResult = obsClient.putObject(putObjectRequest);
+ // 读取该已上传对象的URL
+ } catch (ObsException | IOException e) {
+ log.info("上传文件失败");
+ } finally {
+ hweiOBSConfig.destroy(obsClient);
+ }
+ return putObjectResult;
+ }
+
+
+
+
+
/*
* @Description : 文件下载
* @author : YBL
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index c8a9dd1..83cbd6c 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -75,12 +75,12 @@ springdoc:
server:
- port: 9092
-# port: 8888
-# ssl:
-# key-store: classpath:carboner.cn.jks
-# key-store-password: 6gsn1hke4m4f7
-# key-store-type: JKS
+# port: 9092
+ port: 8888
+ ssl:
+ key-store: classpath:carboner.cn.jks
+ key-store-password: 6gsn1hke4m4f7
+ key-store-type: JKS
servlet:
context-path: /api