diff --git a/src/main/java/com/cultural/heritage/config/RedisConfig.java b/src/main/java/com/cultural/heritage/config/RedisConfig.java index 40e151e..4f36d39 100644 --- a/src/main/java/com/cultural/heritage/config/RedisConfig.java +++ b/src/main/java/com/cultural/heritage/config/RedisConfig.java @@ -1,6 +1,7 @@ package com.cultural.heritage.config; +import com.cultural.heritage.model.entity.Festival; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; @@ -17,10 +18,25 @@ public class RedisConfig { redisTemplate.setConnectionFactory(factory); // 指定kv的序列化方式 - Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); return redisTemplate; } + + + @Bean + public RedisTemplate festivalRedisTemplate(RedisConnectionFactory factory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(factory); + + // 配置序列化器 + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Festival.class); + redisTemplate.setValueSerializer(serializer); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + + return redisTemplate; + } + } diff --git a/src/main/java/com/cultural/heritage/controller/good/FestivalController.java b/src/main/java/com/cultural/heritage/controller/good/FestivalController.java index 1cc9c9d..ba7f1f5 100644 --- a/src/main/java/com/cultural/heritage/controller/good/FestivalController.java +++ b/src/main/java/com/cultural/heritage/controller/good/FestivalController.java @@ -7,16 +7,14 @@ import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ResultUtils; import com.cultural.heritage.constant.UserConstant; import com.cultural.heritage.exception.BusinessException; +import com.cultural.heritage.model.entity.Festival; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -30,8 +28,8 @@ import java.util.List; public class FestivalController { - @Resource - private RedisTemplate redisTemplate; + @Resource(name = "festivalRedisTemplate") + private RedisTemplate redisTemplate; // 节日集合的键 private static final String FESTIVAL_KEY = "festivalList"; @@ -39,23 +37,49 @@ public class FestivalController { /** * Web端管理员添加节日项 - * @param value 添加的节日项 + * @param festival 节日添加信息 */ - @GetMapping("/add") - @Operation(summary = "Web端管理员添加节日项", description = "参数:节日项名称,权限:管理员(admin, boss),方法名:addFestivalElement") + @PostMapping("/add") + @Operation(summary = "Web端管理员添加节日项", description = "参数:节日添加信息,权限:管理员(admin, boss),方法名:addFestivalElement") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse addFestivalElement(@RequestParam String value) { - if (StringUtils.isBlank(value)) { - throw new BusinessException(ErrorCode.PARAMS_ERROR); + public BaseResponse addFestivalElement(@RequestBody Festival festival) { + if (festival == null || StringUtils.isAnyBlank(festival.getName(), festival.getUrl())) { + throw new BusinessException(ErrorCode.PARAMS_ERROR, "存在参数为空"); } - if (isElementExistInList(value)) { + if (isElementExistInList(festival.getName())) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "该节日项已存在"); } - redisTemplate.opsForList().rightPush(FESTIVAL_KEY, value); + redisTemplate.opsForList().rightPush(FESTIVAL_KEY, festival); return ResultUtils.success(true); } + /** + * Web端管理员根据节日名称获取URL + * @param festivalName 节日名称 + * @return 当前节日的url + */ + @GetMapping("/getUrl") + @Operation(summary = "Web端管理员根据节日名称获取URL", description = "参数:节日名称,权限:管理员(admin, boss),方法名:getFestivalUrl") + @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) + public BaseResponse getFestivalUrl(@RequestParam String festivalName) { + // 获取 Redis 中所有的节日数据 + List festivalList = redisTemplate.opsForList().range(FESTIVAL_KEY, 0, -1); + if (festivalList == null || festivalList.isEmpty()) { + throw new BusinessException(ErrorCode.OPERATION_ERROR, "节日列表为空"); + } + + // 遍历查找匹配的节日 + for (Festival festival : festivalList) { + if (festival.getName().equals(festivalName)) { + return ResultUtils.success(festival.getUrl()); // 返回对应的 URL + } + } + throw new BusinessException(ErrorCode.OPERATION_ERROR, "该节日项不存在"); + } + + + /** * Web端管理员查询节日集合 * @return 节日列表 @@ -63,34 +87,48 @@ public class FestivalController { @GetMapping("/get") @Operation(summary = "Web端管理员查询节日集合", description = "参数:无,权限:管理员(admin, boss),方法名:getFestivalList") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse> getFestivalList() { - List festivalList = redisTemplate.opsForList().range(FESTIVAL_KEY, 0, -1); + public BaseResponse> getFestivalList() { + List festivalList = redisTemplate.opsForList().range(FESTIVAL_KEY, 0, -1); return ResultUtils.success(festivalList); } /** * Web端管理员删除节日项 - * @param value 删除的节日项 - * @return + * @param festivalName 删除的节日项 + * @return 是否删除成功 */ @GetMapping("/delete") @Operation(summary = "Web端管理员删除节日项", description = "参数:无,权限:管理员(admin, boss),方法名:deleteFestivalElement") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) - public BaseResponse deleteFestivalElement(@RequestParam String value) { - if (!isElementExistInList(value)) { + public BaseResponse deleteFestivalElement(@RequestParam String festivalName) { + List festivalList = redisTemplate.opsForList().range(FESTIVAL_KEY, 0, -1); + if (festivalList == null || festivalList.isEmpty()) { throw new BusinessException(ErrorCode.OPERATION_ERROR, "该节日项不存在"); } - redisTemplate.opsForList().remove(FESTIVAL_KEY, 1, value); - return ResultUtils.success(true); + // 遍历列表,找到匹配的对象并删除 + for (Festival festival : festivalList) { + if (festival.getName().equals(festivalName)) { + redisTemplate.opsForList().remove(FESTIVAL_KEY, 1, festival); + return ResultUtils.success(true); + } + } + throw new BusinessException(ErrorCode.OPERATION_ERROR, "该节日项不存在"); } // 检查元素是否存在 - private boolean isElementExistInList(@RequestParam String value) { - List festivalList = redisTemplate.opsForList().range(FESTIVAL_KEY, 0, -1); - return festivalList != null && festivalList.contains(value); + private boolean isElementExistInList(String festivalName) { + List festivalList = redisTemplate.opsForList().range(FESTIVAL_KEY, 0, -1); + if (festivalList == null) return false; + + for (Festival festival : festivalList) { + if (festival.getName().equals(festivalName)) { + return true; + } + } + return false; } diff --git a/src/main/java/com/cultural/heritage/controller/good/GoodController.java b/src/main/java/com/cultural/heritage/controller/good/GoodController.java index f2e612c..c0a3033 100644 --- a/src/main/java/com/cultural/heritage/controller/good/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/good/GoodController.java @@ -39,6 +39,7 @@ import com.cultural.heritage.service.common.CommonService; import com.cultural.heritage.service.good.*; import com.cultural.heritage.service.order.PendingServiceGoodService; import com.cultural.heritage.service.user.UserService; +import com.cultural.heritage.utils.DecoderUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; @@ -458,6 +459,8 @@ public class GoodController { Long id = getByIdRequest.getId(); Good good = goodService.getById(id); ThrowUtils.throwIf(good == null, ErrorCode.OPERATION_ERROR, "商品不存在"); + good.setRichText(DecoderUtils.decodeText(good.getRichText())); + System.out.println(good.getRichText()); GoodPageVO goodPageVO = new GoodPageVO(); BeanUtils.copyProperties(good, goodPageVO); return ResultUtils.success(goodPageVO); diff --git a/src/main/java/com/cultural/heritage/model/entity/Festival.java b/src/main/java/com/cultural/heritage/model/entity/Festival.java new file mode 100644 index 0000000..11110db --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/entity/Festival.java @@ -0,0 +1,26 @@ +package com.cultural.heritage.model.entity; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +public class Festival implements Serializable { + + + /** + * 节日名称 + */ + private String name; + + + /** + * 节日图片 + */ + private String url; + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/test/TestRedis.java b/src/main/java/com/cultural/heritage/test/TestRedis.java index c053d16..54b9973 100644 --- a/src/main/java/com/cultural/heritage/test/TestRedis.java +++ b/src/main/java/com/cultural/heritage/test/TestRedis.java @@ -2,6 +2,8 @@ package com.cultural.heritage.test; import org.springframework.stereotype.Component; +import java.util.Base64; + @Component public class TestRedis { @@ -21,4 +23,18 @@ public class TestRedis { // stringRedisTemplate.opsForHash().get("test", "238"); // } + + public static void main(String[] args) { + String encodedStr = "PGgxIHN0eWxlPSJ0ZXh0LWFsaWduOiBjZW50ZXI7Ij7ov5nmmK/lr4zmlofmnKxiYXNlNjTmtYvor5U8L2gxPg=="; // 前端 btoa("Hello world!") + + // Base64 解码 + byte[] decodedBytes = Base64.getDecoder().decode(encodedStr); + String decodedStr = new String(decodedBytes); + + System.out.println("解码后的字符串: " + decodedStr); + } + + + + } diff --git a/src/main/java/com/cultural/heritage/utils/DecoderUtils.java b/src/main/java/com/cultural/heritage/utils/DecoderUtils.java new file mode 100644 index 0000000..cdfcc23 --- /dev/null +++ b/src/main/java/com/cultural/heritage/utils/DecoderUtils.java @@ -0,0 +1,12 @@ +package com.cultural.heritage.utils; + +import java.util.Base64; + +public class DecoderUtils { + + public static String decodeText (String text) { + // Base64 解码 + byte [] decodedBytes = Base64.getDecoder().decode(text); + return new String(decodedBytes); + } +}