From 6c4df54f35200cb473ea97abb4e8202f751b4ac7 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 17 Jan 2024 02:31:03 +0800 Subject: [PATCH] 1.move common annotation/constants/enums to common module, 2.enable journal --- .../java}/annotation/GlobalInterceptor.java | 17 +- .../src/main/java/annotation/VerifyParam.java | 21 ++ .../src/main/java}/constants/Constants.java | 4 +- .../java}/enums/RequestFrequencyTypeEnum.java | 2 +- .../src/main/java}/enums/VerifyRegexEnum.java | 2 +- .../src/main/java/util/VerifyUtils.java | 21 ++ .../java/com/luoo/music/MusicApplication.java | 5 + .../luoo/music/aspect/OperationAspect.java | 221 ++++++++++++++++++ .../music/controller/JournalController.java | 55 +++++ .../luoo/music/controller/SongController.java | 31 ++- .../music/dto/request/JournalQueryReq.java | 28 +++ .../music/dto/response/JournalRespDTO.java | 18 ++ .../luoo/music/dto/response/SongRespDTO.java | 22 ++ .../main/java/com/luoo/music/pojo/SongVO.java | 88 ------- .../luoo/music/service/ArticleService.java | 26 +++ .../com/luoo/music/service/SongService.java | 24 +- .../java/com/luoo/music/util/RedisUtils.java | 101 ++++++++ .../com/luoo/user/annotation/VerifyParam.java | 17 -- .../com/luoo/user/aspect/OperationAspect.java | 14 +- .../luoo/user/controller/AdminController.java | 4 +- .../luoo/user/controller/MyController.java | 7 +- .../luoo/user/controller/UserController.java | 13 +- .../java/com/luoo/user/enumerate/Gender.java | 14 -- .../com/luoo/user/service/UserService.java | 9 +- .../java/com/luoo/user/util/VerifyUtils.java | 31 --- 25 files changed, 579 insertions(+), 216 deletions(-) rename {luoo_user/src/main/java/com/luoo/user => luoo_common/src/main/java}/annotation/GlobalInterceptor.java (59%) create mode 100644 luoo_common/src/main/java/annotation/VerifyParam.java rename {luoo_user/src/main/java/com/luoo/user => luoo_common/src/main/java}/constants/Constants.java (83%) rename {luoo_user/src/main/java/com/luoo/user => luoo_common/src/main/java}/enums/RequestFrequencyTypeEnum.java (95%) rename {luoo_user/src/main/java/com/luoo/user => luoo_common/src/main/java}/enums/VerifyRegexEnum.java (94%) create mode 100644 luoo_common/src/main/java/util/VerifyUtils.java create mode 100644 luoo_music/src/main/java/com/luoo/music/aspect/OperationAspect.java create mode 100644 luoo_music/src/main/java/com/luoo/music/controller/JournalController.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/response/SongRespDTO.java delete mode 100644 luoo_music/src/main/java/com/luoo/music/pojo/SongVO.java create mode 100644 luoo_music/src/main/java/com/luoo/music/util/RedisUtils.java delete mode 100644 luoo_user/src/main/java/com/luoo/user/annotation/VerifyParam.java delete mode 100644 luoo_user/src/main/java/com/luoo/user/enumerate/Gender.java delete mode 100644 luoo_user/src/main/java/com/luoo/user/util/VerifyUtils.java diff --git a/luoo_user/src/main/java/com/luoo/user/annotation/GlobalInterceptor.java b/luoo_common/src/main/java/annotation/GlobalInterceptor.java similarity index 59% rename from luoo_user/src/main/java/com/luoo/user/annotation/GlobalInterceptor.java rename to luoo_common/src/main/java/annotation/GlobalInterceptor.java index f798b86..7177629 100644 --- a/luoo_user/src/main/java/com/luoo/user/annotation/GlobalInterceptor.java +++ b/luoo_common/src/main/java/annotation/GlobalInterceptor.java @@ -1,21 +1,24 @@ -package com.luoo.user.annotation; +package annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import com.luoo.user.enums.RequestFrequencyTypeEnum; - +import enums.RequestFrequencyTypeEnum; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface GlobalInterceptor { boolean checkLogin() default false; + boolean checkAppUserLogin() default false; + boolean checkAdminLogin() default false; - boolean checkParam() default true; - - int requestFrequencyThreshold() default 0; - RequestFrequencyTypeEnum frequencyType() default RequestFrequencyTypeEnum.NO_LIMIT; + + boolean checkParam() default true; + + int requestFrequencyThreshold() default 0; + + RequestFrequencyTypeEnum frequencyType() default RequestFrequencyTypeEnum.NO_LIMIT; } diff --git a/luoo_common/src/main/java/annotation/VerifyParam.java b/luoo_common/src/main/java/annotation/VerifyParam.java new file mode 100644 index 0000000..881ab89 --- /dev/null +++ b/luoo_common/src/main/java/annotation/VerifyParam.java @@ -0,0 +1,21 @@ +package annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import enums.VerifyRegexEnum; + +@Target({ ElementType.PARAMETER, ElementType.FIELD }) +@Retention(RetentionPolicy.RUNTIME) +public @interface VerifyParam { + VerifyRegexEnum regex() default VerifyRegexEnum.NO; + + int min() default -1; + + int max() default -1; + + boolean required() default false; + +} diff --git a/luoo_user/src/main/java/com/luoo/user/constants/Constants.java b/luoo_common/src/main/java/constants/Constants.java similarity index 83% rename from luoo_user/src/main/java/com/luoo/user/constants/Constants.java rename to luoo_common/src/main/java/constants/Constants.java index 1abd754..04dd5ac 100644 --- a/luoo_user/src/main/java/com/luoo/user/constants/Constants.java +++ b/luoo_common/src/main/java/constants/Constants.java @@ -1,4 +1,4 @@ -package com.luoo.user.constants; +package constants; public class Constants { public static final String IMAGE_CHECK_CODE_KEY="image_check_code_key"; @@ -14,4 +14,6 @@ public class Constants { public static final String FOLDER_AVATAR = "avatar/"; + + public static final String MUSIC_RESOURCE_PREFIX="http://wx.gisocn.com:18599/music/"; } diff --git a/luoo_user/src/main/java/com/luoo/user/enums/RequestFrequencyTypeEnum.java b/luoo_common/src/main/java/enums/RequestFrequencyTypeEnum.java similarity index 95% rename from luoo_user/src/main/java/com/luoo/user/enums/RequestFrequencyTypeEnum.java rename to luoo_common/src/main/java/enums/RequestFrequencyTypeEnum.java index ce5984c..8ebd069 100644 --- a/luoo_user/src/main/java/com/luoo/user/enums/RequestFrequencyTypeEnum.java +++ b/luoo_common/src/main/java/enums/RequestFrequencyTypeEnum.java @@ -1,4 +1,4 @@ -package com.luoo.user.enums; +package enums; public enum RequestFrequencyTypeEnum { DAY(60 * 60 * 24, "一天"), HOUR(60 * 60, "一小时"), MINUTE(60, "一分钟"), SECONDS(1, "一秒"), NO_LIMIT(0, "不限制"); diff --git a/luoo_user/src/main/java/com/luoo/user/enums/VerifyRegexEnum.java b/luoo_common/src/main/java/enums/VerifyRegexEnum.java similarity index 94% rename from luoo_user/src/main/java/com/luoo/user/enums/VerifyRegexEnum.java rename to luoo_common/src/main/java/enums/VerifyRegexEnum.java index 030b871..f36acda 100644 --- a/luoo_user/src/main/java/com/luoo/user/enums/VerifyRegexEnum.java +++ b/luoo_common/src/main/java/enums/VerifyRegexEnum.java @@ -1,4 +1,4 @@ -package com.luoo.user.enums; +package enums; public enum VerifyRegexEnum { NO("", "不校验"), diff --git a/luoo_common/src/main/java/util/VerifyUtils.java b/luoo_common/src/main/java/util/VerifyUtils.java new file mode 100644 index 0000000..78c4d06 --- /dev/null +++ b/luoo_common/src/main/java/util/VerifyUtils.java @@ -0,0 +1,21 @@ +package util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import enums.VerifyRegexEnum; +public class VerifyUtils { + + public static boolean verify(String regex, String value) { + if (StringTools.isEmpty(value)) { + return false; + } + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(value); + return matcher.matches(); + } + + public static boolean verify(VerifyRegexEnum regexEnum, String value) { + return verify(regexEnum.getRegex(), value); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/MusicApplication.java b/luoo_music/src/main/java/com/luoo/music/MusicApplication.java index 2a5bcfd..4f74b9f 100644 --- a/luoo_music/src/main/java/com/luoo/music/MusicApplication.java +++ b/luoo_music/src/main/java/com/luoo/music/MusicApplication.java @@ -15,6 +15,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; import util.IdWorker; +import util.JwtUtil; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @@ -44,4 +45,8 @@ public class MusicApplication { return new IdWorker(1, 1); } + @Bean + public JwtUtil jwtUtil(){ + return new JwtUtil(); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/aspect/OperationAspect.java b/luoo_music/src/main/java/com/luoo/music/aspect/OperationAspect.java new file mode 100644 index 0000000..db17959 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/aspect/OperationAspect.java @@ -0,0 +1,221 @@ +package com.luoo.music.aspect; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.lang3.ArrayUtils; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.reflect.MethodSignature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import com.luoo.music.util.RedisUtils; + +import constants.Constants; + +import annotation.GlobalInterceptor; +import annotation.VerifyParam; +import api.StatusCode; +import dto.UserLoginDto; +import enums.RequestFrequencyTypeEnum; +import exception.BizException; +import util.JwtUtil; +import util.StringTools; +import util.VerifyUtils; + +@Aspect +@Component("operationAspect") +public class OperationAspect { + static Logger logger = LoggerFactory.getLogger(OperationAspect.class); + private static final String[] BASE_TYPE_ARRAY = new String[] { "java.lang.String", "java.lang.Integer", + "java.lang.Long" }; + + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private RedisUtils redisUtils; + + @Before("@annotation(annotation.GlobalInterceptor)") + public void interceptorDo(JoinPoint point) { + Object[] arguments = point.getArgs(); + Object target = point.getTarget(); + Method method = ((MethodSignature) point.getSignature()).getMethod(); + GlobalInterceptor interceptor = method.getAnnotation(GlobalInterceptor.class); + if (null == interceptor) { + return; + } + /** + * 登录校验 + */ + if (interceptor.checkLogin()) { + checkLogin(); + } + + /** + * 管理用户登录校验 + */ + if (interceptor.checkAdminLogin()) { + checkAdminLogin(); + } + + /** + * app用户登录校验 + */ + if (interceptor.checkAppUserLogin()) { + checkAppUserLogin(); + } + + /** + * 校验参数 + */ + if (interceptor.checkParam()) { + validateParams(method, arguments); + } + + /** + * 频次校验 + */ + if (interceptor.frequencyType() != RequestFrequencyTypeEnum.NO_LIMIT && interceptor.requestFrequencyThreshold() != 0) { + String fullMethodName = target.getClass().getName() + "." + method.getName(); + checkRequestFrequency(fullMethodName, interceptor.frequencyType(), interceptor.requestFrequencyThreshold()); + } + } + private void checkRequestFrequency(String fullMethodName, RequestFrequencyTypeEnum frequencyTyp, Integer threshold) { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + String ip = getIpAddr(request); + ip = ip.replace(":", ""); + String redisKey = fullMethodName + ip; + Object count=redisUtils.get(redisKey); + Integer cuCount= null==count?0:Integer.valueOf((String)count); + if (cuCount != null && cuCount > threshold - 1) { + throw new BizException(StatusCode.REQUEST_TOO_FREQUENCY); + } + redisUtils.increment(redisKey, 1, frequencyTyp.getSeconds()); + } + private void checkAppUserLogin() { + UserLoginDto userLoginDto = getUserLoginDtoFromToken(); + if (userLoginDto == null) { + throw new BizException(StatusCode.UNAUTHORIZED); + } + if (!Constants.TOKEN_ROLE_APP_USER.equals(userLoginDto.getRoles())) { + throw new BizException(StatusCode.FORBIDDEN); + } + } + + private void checkAdminLogin() { + UserLoginDto userLoginDto = getUserLoginDtoFromToken(); + if (userLoginDto == null) { + throw new BizException(StatusCode.UNAUTHORIZED); + } + if (!Constants.TOKEN_ROLE_ADMIN_USER.equals(userLoginDto.getRoles())) { + throw new BizException(StatusCode.FORBIDDEN); + } + } + + private void checkLogin() { + UserLoginDto userLoginDto = getUserLoginDtoFromToken(); + if (userLoginDto == null) { + throw new BizException(StatusCode.UNAUTHORIZED); + } + } + + private UserLoginDto getUserLoginDtoFromToken() { + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) + .getRequest(); + String token = request.getHeader("token"); + return jwtUtil.getUserLoginDto(token); + } + + private void validateParams(Method method, Object[] arguments) { + Parameter[] parameters = method.getParameters(); + for (int i = 0; i < parameters.length; i++) { + Parameter parameter = parameters[i]; + Object value = arguments[i]; + VerifyParam verifyParam = parameter.getAnnotation(VerifyParam.class); + if (null == verifyParam) { + continue; + } + String paramTypeName = parameter.getParameterizedType().getTypeName(); + if (ArrayUtils.contains(BASE_TYPE_ARRAY, paramTypeName)) { + checkValue(value, verifyParam); + } else { + checkObjValue(parameter, value); + } + } + } + + private void checkObjValue(Parameter parameter, Object value) { + try { + String typeName = parameter.getParameterizedType().getTypeName(); + Class clazz = Class.forName(typeName); + Field[] fields = clazz.getDeclaredFields(); + for (Field field : fields) { + VerifyParam fieldVerifyParam = field.getAnnotation(VerifyParam.class); + if (null == fieldVerifyParam) { + continue; + } + field.setAccessible(true); + Object resultValue = field.get(value); + checkValue(resultValue, fieldVerifyParam); + } + + } catch (Exception e) { + logger.error(StatusCode.VALIDATE_FAILED.getMessage(), e.getMessage()); + throw new BizException(StatusCode.VALIDATE_FAILED); + } + } + + private void checkValue(Object value, VerifyParam verifyParam) { + boolean isEmpty = null == value || StringTools.isEmpty(value.toString()); + int length = null == value ? 0 : value.toString().length(); + if (isEmpty && verifyParam.required()) { + throw new BizException(StatusCode.VALIDATE_FAILED); + } + if (!isEmpty && (-1 != verifyParam.max() && verifyParam.max() < length + || -1 != verifyParam.min() && verifyParam.min() > length)) { + throw new BizException(StatusCode.VALIDATE_FAILED); + } + if (!isEmpty && !StringTools.isEmpty(verifyParam.regex().getRegex()) + && !VerifyUtils.verify(verifyParam.regex(), String.valueOf(value))) { + throw new BizException(StatusCode.VALIDATE_FAILED); + } + } + private String getIpAddr(HttpServletRequest request) { + String ip = request.getHeader("x-forwarded-for"); + if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { + // 多次反向代理后会有多个ip值,第一个ip才是真实ip + if (ip.indexOf(",") != -1) { + ip = ip.split(",")[0]; + } + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_CLIENT_IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_X_FORWARDED_FOR"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("X-Real-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + return ip; + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java new file mode 100644 index 0000000..1410ada --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -0,0 +1,55 @@ +package com.luoo.music.controller; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.luoo.music.client.UserClient; +import com.luoo.music.dto.request.JournalQueryReq; +import com.luoo.music.dto.response.JournalRespDTO; + +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.web.bind.annotation.*; +import com.luoo.music.pojo.Article; +import com.luoo.music.service.ArticleService; + +import annotation.GlobalInterceptor; +import api.PageResult; +import api.Result; +import constants.Constants; +import io.swagger.annotations.ApiOperation; + +/** + * 控制器层 + * @author Administrator + * + */ +@RestController +@CrossOrigin +@RequestMapping("/journal") +public class JournalController { + + @Autowired + private ArticleService articleService; + + @ApiOperation(value = "1.查询期刊信息") + @GetMapping("/list") + //@GlobalInterceptor(checkLogin = true) + public Result> page(@RequestHeader(value = "token", required = false) String token, + JournalQueryReq queryReq){ + Page
pageList = articleService.queryPage(queryReq); + List list=pageList.stream().map(a->getArticleRespDTO(a)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()), list) ); + } + private JournalRespDTO getArticleRespDTO(Article article) { + JournalRespDTO journalRespDTO=new JournalRespDTO(); + journalRespDTO.setId(article.getId()); + journalRespDTO.setJournalNo(article.getVolid()); + journalRespDTO.setTitle(article.getTitle()); + journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX+article.getImage()); + return journalRespDTO; + } + +} diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index 1d272ba..d995fb0 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -1,12 +1,15 @@ package com.luoo.music.controller; +import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Song; -import com.luoo.music.pojo.SongVO; import com.luoo.music.service.SongService; import api.PageResult; import api.Result; +import constants.Constants; +import io.swagger.annotations.ApiOperation; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; @@ -15,6 +18,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; @RestController @CrossOrigin @@ -32,12 +36,27 @@ public class SongController { public Result> findAll(){ return Result.success(songService.findAll()); } - - @GetMapping("/findByVolid/{volid}") - public Result> findByVolid(@PathVariable String volid){ - return Result.success(songService.findByVolid(volid)); + @ApiOperation(value = "1.根据期刊号查询歌曲信息") + @GetMapping("/getByJournalNo/{journalNo}") + public Result> getByJournalNo(@PathVariable String journalNo){ + List songs=songService.findByVolid(journalNo); + List results=songs.stream().map(s->getSongRespDTO(s)).collect(Collectors.toList()); + return Result.success(results); + } + + private SongRespDTO getSongRespDTO(Song song) { + SongRespDTO songRespDTO = new SongRespDTO(); + songRespDTO.setJournalNo(song.getVolid()); + songRespDTO.setAlbum(song.getAlbum()); + songRespDTO.setArtist(song.getArtist()); + songRespDTO.setId(song.getId()); + songRespDTO.setTitle(song.getName()); + songRespDTO.setSrc(Constants.MUSIC_RESOURCE_PREFIX+song.getUrl()); + songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX+song.getUrl().replace(".mp3",".jpg")); + songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX+song.getUrl().replace(".mp3",".lyric")); + songRespDTO.setSongNo(song.getSongno()); + return songRespDTO; } - /** * 根据ID查询 * @param id ID diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java new file mode 100644 index 0000000..9fc12c4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java @@ -0,0 +1,28 @@ +package com.luoo.music.dto.request; + +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * 标签列表查询参数 + */ +@Data +@ApiModel +public class JournalQueryReq implements Serializable { + private static final long serialVersionUID = -1198060864891902188L; + @ApiModelProperty(value = "风格") + private String style; + @ApiModelProperty(value = "语言") + private String language; + @ApiModelProperty(value = "分页: 页码", example = "1") + private int pageNum = 1; + @ApiModelProperty(value = "分页: 每页数量", example = "10") + private int pageSize = 10; +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java new file mode 100644 index 0000000..0eecdd9 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java @@ -0,0 +1,18 @@ +package com.luoo.music.dto.response; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +public class JournalRespDTO { + @ApiModelProperty(value = "ID") + private String id; + @ApiModelProperty(value = "剘刊号") + private String journalNo; + @ApiModelProperty(value = "期刊名") + private String title; + @ApiModelProperty(value = "文案") + private String content; + @ApiModelProperty(value = "期刊封面") + private String image; +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/SongRespDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/SongRespDTO.java new file mode 100644 index 0000000..a1fea21 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/SongRespDTO.java @@ -0,0 +1,22 @@ +package com.luoo.music.dto.response; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class SongRespDTO { + private String id; + private String title; + private String artist; + @ApiModelProperty(value = "专辑") + private String album; + private String src; + private String pic; + private String lrc; + @ApiModelProperty(value = "剘刊号") + private String journalNo; + @ApiModelProperty(value = "歌曲号") + private Integer songNo; +} diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/SongVO.java b/luoo_music/src/main/java/com/luoo/music/pojo/SongVO.java deleted file mode 100644 index 412a285..0000000 --- a/luoo_music/src/main/java/com/luoo/music/pojo/SongVO.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.luoo.music.pojo; - -public class SongVO { - private String id; - - private String title; - - private String artist; - private String album; - private String src; - private String pic; - private String lrc; - private String volid; - - private Integer songno; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getArtist() { - return artist; - } - - public void setArtist(String artist) { - this.artist = artist; - } - - public String getAlbum() { - return album; - } - - public void setAlbum(String album) { - this.album = album; - } - - public String getSrc() { - return src; - } - - public void setSrc(String src) { - this.src = src; - } - - public String getPic() { - return pic; - } - - public void setPic(String pic) { - this.pic = pic; - } - - public String getLrc() { - return lrc; - } - - public void setLrc(String lrc) { - this.lrc = lrc; - } - - public String getVolid() { - return volid; - } - - public void setVolid(String volid) { - this.volid = volid; - } - - public Integer getSongno() { - return songno; - } - - public void setSongno(Integer songno) { - this.songno = songno; - } -} diff --git a/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java b/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java index 2128564..d08b4a9 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java @@ -9,6 +9,7 @@ import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.persistence.criteria.Selection; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -20,6 +21,7 @@ import org.springframework.stereotype.Service; import util.IdWorker; import com.luoo.music.dao.ArticleDao; +import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.pojo.Article; /** @@ -209,4 +211,28 @@ public class ArticleService { public Article findByVolid(String volid) { return articleDao.findArticleByVolid(volid); } + + + public Page
queryPage(JournalQueryReq queryReq) { + Specification
specification = createArticleSpecification(queryReq); + PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize()); + return articleDao.findAll(specification, pageRequest); + } + + /** + * + * as article do not support style and langauge, only sort the result + */ + private Specification
createArticleSpecification(JournalQueryReq queryReq) { + return new Specification
() { + private static final long serialVersionUID = 1L; + @Override + public Predicate toPredicate(Root
root, CriteriaQuery query, CriteriaBuilder cb) { + List predicateList = new ArrayList(); + query.orderBy(cb.desc(root.get("volid").as(Integer.class))); + return cb.and( predicateList.toArray(new Predicate[predicateList.size()])); + } + }; + + } } diff --git a/luoo_music/src/main/java/com/luoo/music/service/SongService.java b/luoo_music/src/main/java/com/luoo/music/service/SongService.java index a387cb9..ea0615f 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/SongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/SongService.java @@ -1,8 +1,9 @@ package com.luoo.music.service; import com.luoo.music.dao.SongDao; +import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.Song; -import com.luoo.music.pojo.SongVO; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; @@ -141,24 +142,7 @@ public class SongService { }; } - - public List findByVolid(String volid) { - - List songList = songDao.findSongsByVolid(volid); - List songVOList = new ArrayList<>(); - for (Song song: songList) { - SongVO songVO = new SongVO(); - songVO.setVolid(song.getVolid()); - songVO.setAlbum(song.getAlbum()); - songVO.setArtist(song.getArtist()); - songVO.setId(song.getId()); - songVO.setTitle(song.getName()); - songVO.setSrc("http://wx.gisocn.com:18599/music/"+song.getUrl()); - songVO.setPic("http://wx.gisocn.com:18599/music/"+song.getUrl().replace(".mp3",".jpg")); - songVO.setLrc("http://wx.gisocn.com:18599/music/"+song.getUrl().replace(".mp3",".lyric")); - songVOList.add(songVO); - } - return songVOList; - + public List findByVolid(String volid){ + return songDao.findSongsByVolid(volid); } } diff --git a/luoo_music/src/main/java/com/luoo/music/util/RedisUtils.java b/luoo_music/src/main/java/com/luoo/music/util/RedisUtils.java new file mode 100644 index 0000000..de293cd --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/util/RedisUtils.java @@ -0,0 +1,101 @@ +package com.luoo.music.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import java.util.Collection; +import java.util.concurrent.TimeUnit; + +@Component("redisUtils") +public class RedisUtils { + + @Autowired + private RedisTemplate redisTemplate; + + private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class); + + /** + * 删除缓存 + * + * @param key 可以传一个值 或多个 + */ + public void delete(String... key) { + if (key != null && key.length > 0) { + if (key.length == 1) { + redisTemplate.delete(key[0]); + } else { + redisTemplate.delete((Collection) CollectionUtils.arrayToList(key)); + } + } + } + + public V get(String key) { + return key == null ? null : redisTemplate.opsForValue().get(key); + } + + /** + * 普通缓存放入 + * + * @param key 键 + * @param value 值 + * @return true成功 false失败 + */ + public boolean set(String key, V value) { + try { + redisTemplate.opsForValue().set(key, value); + return true; + } catch (Exception e) { + logger.error("设置redisKey:{},value:{}失败", key, value); + return false; + } + } + + /** + * 普通缓存放入并设置时间 + * + * @param key 键 + * @param value 值 + * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 + * @return true成功 false 失败 + */ + public boolean setex(String key, V value, long time) { + try { + if (time > 0) { + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); + } else { + set(key, value); + } + return true; + } catch (Exception e) { + logger.error("设置redisKey:{},value:{}失败", key, value); + return false; + } + } + + public long increment(String key, long delta, long time) { + if (delta < 0) { + throw new RuntimeException("递增因子必须大于0"); + } + long result = redisTemplate.opsForValue().increment(key, delta); + if (result == 1) { + expire(key, time); + } + return result; + } + + public boolean expire(String key, long time) { + try { + if (time > 0) { + redisTemplate.expire(key, time, TimeUnit.SECONDS); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } +} diff --git a/luoo_user/src/main/java/com/luoo/user/annotation/VerifyParam.java b/luoo_user/src/main/java/com/luoo/user/annotation/VerifyParam.java deleted file mode 100644 index cad3012..0000000 --- a/luoo_user/src/main/java/com/luoo/user/annotation/VerifyParam.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.luoo.user.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import com.luoo.user.enums.VerifyRegexEnum; -@Target({ElementType.PARAMETER,ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface VerifyParam { - VerifyRegexEnum regex() default VerifyRegexEnum.NO; - int min() default -1; - int max() default -1; - boolean required() default false; - -} diff --git a/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java b/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java index ee8dde7..64f8cda 100644 --- a/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java +++ b/luoo_user/src/main/java/com/luoo/user/aspect/OperationAspect.java @@ -4,7 +4,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Parameter; -import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.ArrayUtils; @@ -15,23 +14,22 @@ import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -import com.luoo.user.annotation.GlobalInterceptor; -import com.luoo.user.annotation.VerifyParam; -import com.luoo.user.constants.Constants; -import com.luoo.user.enums.RequestFrequencyTypeEnum; import com.luoo.user.util.RedisUtils; -import com.luoo.user.util.VerifyUtils; +import annotation.GlobalInterceptor; +import annotation.VerifyParam; import api.StatusCode; +import constants.Constants; import dto.UserLoginDto; +import enums.RequestFrequencyTypeEnum; import exception.BizException; import util.JwtUtil; import util.StringTools; +import util.VerifyUtils; @Aspect @Component("operationAspect") @@ -46,7 +44,7 @@ public class OperationAspect { @Autowired private RedisUtils redisUtils; - @Before("@annotation(com.luoo.user.annotation.GlobalInterceptor)") + @Before("@annotation(annotation.GlobalInterceptor)") public void interceptorDo(JoinPoint point) { Object[] arguments = point.getArgs(); Object target = point.getTarget(); diff --git a/luoo_user/src/main/java/com/luoo/user/controller/AdminController.java b/luoo_user/src/main/java/com/luoo/user/controller/AdminController.java index 6a5907a..fa14839 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/AdminController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/AdminController.java @@ -6,11 +6,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; -import com.luoo.user.annotation.GlobalInterceptor; -import com.luoo.user.annotation.VerifyParam; import com.luoo.user.pojo.Admin; import com.luoo.user.service.AdminService; +import annotation.GlobalInterceptor; +import annotation.VerifyParam; import api.PageResult; import api.Result; import api.StatusCode; diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index d57a8cb..8251f3c 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -18,14 +18,13 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import com.luoo.user.annotation.GlobalInterceptor; -import com.luoo.user.annotation.VerifyParam; -import com.luoo.user.constants.Constants; +import constants.Constants; import com.luoo.user.dto.response.UserRespDTO; -import com.luoo.user.enums.VerifyRegexEnum; import com.luoo.user.pojo.User; import com.luoo.user.service.UserService; +import annotation.GlobalInterceptor; +import annotation.VerifyParam; import api.Result; import api.StatusCode; import dto.UserLoginDto; diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java index 2cc61d7..020ac8f 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java @@ -8,27 +8,24 @@ import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServletResponse; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; -import com.luoo.user.annotation.GlobalInterceptor; -import com.luoo.user.annotation.VerifyParam; -import com.luoo.user.constants.Constants; +import constants.Constants; import com.luoo.user.dto.response.CreateImageCode; -import com.luoo.user.dto.response.UserRespDTO; -import com.luoo.user.enums.RequestFrequencyTypeEnum; -import com.luoo.user.enums.VerifyRegexEnum; import com.luoo.user.pojo.User; import com.luoo.user.service.UserService; import com.luoo.user.util.NickNameUtil; +import annotation.GlobalInterceptor; +import annotation.VerifyParam; import api.PageResult; import api.Result; import api.StatusCode; -import io.jsonwebtoken.Claims; +import enums.RequestFrequencyTypeEnum; +import enums.VerifyRegexEnum; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import util.IdWorker; diff --git a/luoo_user/src/main/java/com/luoo/user/enumerate/Gender.java b/luoo_user/src/main/java/com/luoo/user/enumerate/Gender.java deleted file mode 100644 index f4a3525..0000000 --- a/luoo_user/src/main/java/com/luoo/user/enumerate/Gender.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.luoo.user.enumerate; - -public enum Gender { - Male("0"), Female("1"); - private String code; - - private Gender(String code) { - this.code = code; - } - - public String getCode() { - return code; - } -} diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserService.java b/luoo_user/src/main/java/com/luoo/user/service/UserService.java index ee2acfe..bed4844 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserService.java @@ -11,7 +11,6 @@ import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -23,18 +22,12 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; -import com.apifan.common.random.RandomSource; -import com.luoo.user.constants.Constants; +import constants.Constants; import com.luoo.user.dao.UserDao; -import com.luoo.user.dto.response.UserRespDTO; -import com.luoo.user.enumerate.Gender; import com.luoo.user.pojo.User; import com.luoo.user.util.NickNameUtil; -import api.Result; -import api.StatusCode; import dto.UserLoginDto; -import io.jsonwebtoken.Claims; import util.JwtUtil; /** diff --git a/luoo_user/src/main/java/com/luoo/user/util/VerifyUtils.java b/luoo_user/src/main/java/com/luoo/user/util/VerifyUtils.java deleted file mode 100644 index 3491674..0000000 --- a/luoo_user/src/main/java/com/luoo/user/util/VerifyUtils.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.luoo.user.util; - - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import com.luoo.user.enums.VerifyRegexEnum; - -import util.StringTools; - -/** - * @ClassName VerifyUtils - * @Description TODO - * @Author Administrator - * @Date 2023/8/30 21:59 - */ -public class VerifyUtils { - - public static boolean verify(String regex,String value){ - if(StringTools.isEmpty(value)){ - return false; - } - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(value); - return matcher.matches(); - } - - public static boolean verify(VerifyRegexEnum regexEnum,String value){ - return verify(regexEnum.getRegex(),value); - } -}