From 6ff77ebbcc0fb1111a94d0da1b4141075676e5b7 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 17 Jan 2024 09:31:50 +0800 Subject: [PATCH] 1.update login token response to string;2.add auth check on music --- .../controller/BaseExceptionHandler.java | 26 +- .../music/controller/JournalController.java | 2 +- .../luoo/music/controller/SongController.java | 307 ++++++++++-------- .../luoo/music/dto/response/SongRespDTO.java | 6 +- .../luoo/user/controller/UserController.java | 18 +- 5 files changed, 194 insertions(+), 165 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java index 5bfcce7..965a3c1 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java @@ -1,19 +1,31 @@ package com.luoo.music.controller; + import api.Result; import api.StatusCode; +import exception.BizException; +import lombok.extern.slf4j.Slf4j; + import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; + /** * 统一异常处理类 */ +@Slf4j @ControllerAdvice public class BaseExceptionHandler { - - @ExceptionHandler(value = Exception.class) - @ResponseBody - public Result error(Exception e){ - e.printStackTrace(); - return Result.failed(StatusCode.MUSIC_COMMON_FAILED); - } + @ExceptionHandler(value = Exception.class) + @ResponseBody + public Result error(Exception e) { + log.error("执行出错", e); + return Result.failed(StatusCode.MUSIC_COMMON_FAILED); + } + + @ExceptionHandler(value = BizException.class) + @ResponseBody + public Result error(BizException e) { + log.info("业务错误:{}", e.getMessage()); + return Result.failed(StatusCode.MUSIC_COMMON_FAILED, e.getMessage()); + } } 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 index 1410ada..234ea34 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -36,7 +36,7 @@ public class JournalController { @ApiOperation(value = "1.查询期刊信息") @GetMapping("/list") - //@GlobalInterceptor(checkLogin = true) + @GlobalInterceptor(checkLogin = true) public Result> page(@RequestHeader(value = "token", required = false) String token, JournalQueryReq queryReq){ Page
pageList = articleService.queryPage(queryReq); 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 d995fb0..3c6e728 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,10 +1,11 @@ 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.service.SongService; + +import annotation.GlobalInterceptor; import api.PageResult; import api.Result; import constants.Constants; @@ -25,147 +26,167 @@ import java.util.stream.Collectors; @RequestMapping("/song") public class SongController { - @Autowired - private SongService songService; - - /** - * 查询全部数据 - * @return - */ - @GetMapping - public Result> findAll(){ - return Result.success(songService.findAll()); - } - @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 - * @return - */ - @GetMapping("/{id}") - public Result findById(@PathVariable String id){ - return Result.success(songService.findById(id)); - } - - - /** - * 分页+多条件查询 - * @param searchMap 查询条件封装 - * @param page 页码 - * @param size 页大小 - * @return 分页结果 - */ - @PostMapping("/search/{page}/{size}") - public Result> findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ - Page pageList = songService.findSearch(searchMap, page, size); - return Result.success(new PageResult(pageList.getTotalElements(), pageList.getContent()) ); - } - - - @PostMapping("/init") - public Result init(@RequestBody Map map) { - List data = (List) map.get("data"); - System.out.println(data.size()); - Song song = new Song(); - Set
set = new HashSet
(); - - int temp=0; - int num=0; - for (Map dataMap :data - ) { - Article article = new Article(); - article.setVolid(dataMap.get("id")+""); - article.setTitle(dataMap.get("title")+""); - String avatar = "0000"+dataMap.get("id"); - String substring = avatar.substring(avatar.length() - 5); - System.out.println(substring); - article.setImage(substring+"/00.jpg"); - if (temp== (int) dataMap.get("id")){ - num = num+1; - }else { - num = 0; - } - temp = (int) dataMap.get("id"); - String numstr = "0000"+(num+1); - System.out.println(numstr.substring(numstr.length()-2)); - - song.setAlbum(dataMap.get("album")+""); - song.setArtist(dataMap.get("artist")+""); - song.setName(dataMap.get("name")+""); - song.setVolid(dataMap.get("id")+""); - song.setUrl(substring+"/"+numstr.substring(numstr.length()-2)+".mp3"); - song.setSongno(num+1); - - songService.add(song); - set.add(article); - } - System.out.println(set.size()); - - for (Article pojo : set) { + @Autowired + private SongService songService; + + /** + * 查询全部数据 + * + * @return + */ + @GetMapping + public Result> findAll() { + return Result.success(songService.findAll()); + } + + @ApiOperation(value = "1.根据期刊号查询歌曲信息") + @GetMapping("/getByJournalNo/{journalNo}") + @GlobalInterceptor(checkLogin = true) + public Result> getByJournalNo(@RequestHeader(value = "token", required = false) String token, + @PathVariable String journalNo) { + List songs = songService.findByVolid(journalNo); + List results = songs.stream().map(s -> getSongRespDTO(s)).collect(Collectors.toList()); + return Result.success(results); + } + + /* + * @ApiOperation(value = "2.随机播放歌曲",notes="雀乐FM") + * + * @GetMapping("/random/{limit}") + * + * @GlobalInterceptor(checkLogin = true) public Result> + * getByJournalNo(@RequestHeader(value = "token", required = false) String + * token) { List songs = songService.random(); 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 + * @return + */ + @GetMapping("/{id}") + public Result findById(@PathVariable String id) { + return Result.success(songService.findById(id)); + } + + /** + * 分页+多条件查询 + * + * @param searchMap 查询条件封装 + * @param page 页码 + * @param size 页大小 + * @return 分页结果 + */ + @PostMapping("/search/{page}/{size}") + public Result> findSearch(@RequestBody Map searchMap, @PathVariable int page, + @PathVariable int size) { + Page pageList = songService.findSearch(searchMap, page, size); + return Result.success(new PageResult(pageList.getTotalElements(), pageList.getContent())); + } + + @PostMapping("/init") + public Result init(@RequestBody Map map) { + List data = (List) map.get("data"); + System.out.println(data.size()); + Song song = new Song(); + Set
set = new HashSet
(); + + int temp = 0; + int num = 0; + for (Map dataMap : data) { + Article article = new Article(); + article.setVolid(dataMap.get("id") + ""); + article.setTitle(dataMap.get("title") + ""); + String avatar = "0000" + dataMap.get("id"); + String substring = avatar.substring(avatar.length() - 5); + System.out.println(substring); + article.setImage(substring + "/00.jpg"); + if (temp == (int) dataMap.get("id")) { + num = num + 1; + } else { + num = 0; + } + temp = (int) dataMap.get("id"); + String numstr = "0000" + (num + 1); + System.out.println(numstr.substring(numstr.length() - 2)); + + song.setAlbum(dataMap.get("album") + ""); + song.setArtist(dataMap.get("artist") + ""); + song.setName(dataMap.get("name") + ""); + song.setVolid(dataMap.get("id") + ""); + song.setUrl(substring + "/" + numstr.substring(numstr.length() - 2) + ".mp3"); + song.setSongno(num + 1); + + songService.add(song); + set.add(article); + } + System.out.println(set.size()); + + for (Article pojo : set) { // articleService.add(pojo); - } - return Result.success(); - } - - /** - * 根据条件查询 - * @param searchMap - * @return - */ - @PostMapping("/search") - public Result> findSearch( @RequestBody Map searchMap){ - return Result.success(songService.findSearch(searchMap)); - } - - - /** - * 增加 - * @param song - */ - @PostMapping - public Result add(@RequestBody Song song){ - songService.add(song); - return Result.success(); - } - - /** - * 修改 - * @param song - */ - @PutMapping("/{id}") - public Result update(@RequestBody Song song, @PathVariable String id ){ - song.setId(id); - songService.update(song); - return Result.success(); - } - - /** - * 删除 - * @param id - */ - @DeleteMapping("/{id}") - public Result delete(@PathVariable String id ){ - songService.deleteById(id); - return Result.success(); - } + } + return Result.success(); + } + + /** + * 根据条件查询 + * + * @param searchMap + * @return + */ + @PostMapping("/search") + public Result> findSearch(@RequestBody Map searchMap) { + return Result.success(songService.findSearch(searchMap)); + } + + /** + * 增加 + * + * @param song + */ + @PostMapping + public Result add(@RequestBody Song song) { + songService.add(song); + return Result.success(); + } + + /** + * 修改 + * + * @param song + */ + @PutMapping("/{id}") + public Result update(@RequestBody Song song, @PathVariable String id) { + song.setId(id); + songService.update(song); + return Result.success(); + } + + /** + * 删除 + * + * @param id + */ + @DeleteMapping("/{id}") + public Result delete(@PathVariable String id) { + songService.deleteById(id); + return Result.success(); + } } 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 index a1fea21..2ee2a7d 100644 --- 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 @@ -8,12 +8,16 @@ import lombok.Setter; @Setter public class SongRespDTO { private String id; + @ApiModelProperty(value = "歌曲名") private String title; + @ApiModelProperty(value = "歌手/乐队名") private String artist; - @ApiModelProperty(value = "专辑") + @ApiModelProperty(value = "专辑名") private String album; private String src; + @ApiModelProperty(value = "歌曲专辑封面") private String pic; + @ApiModelProperty(value = "歌词") private String lrc; @ApiModelProperty(value = "剘刊号") private String journalNo; 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 020ac8f..f49b9cc 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 @@ -104,12 +104,8 @@ public class UserController { return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH); } User user = userService.loginOrRegister(mobile); - - String token = jwtUtil.createJWT(user.getId(),user.getNickname(),Constants.TOKEN_ROLE_APP_USER); - Map map = new HashMap<>(); - map.put("token", token); - return Result.success(map); + return Result.success(token); }finally { redisTemplate.delete(redisMobileCheckCodeKey); } @@ -118,12 +114,10 @@ public class UserController { @ApiOperation(value = "3.游客登录返回token", notes = "token中的subject和roles均为tourist") @GetMapping("/touristLogin") - public Result> touristLogin() { + public Result touristLogin() { String nickName="游客-"+NickNameUtil.getRandomNickName(); String token = jwtUtil.createJWT(String.valueOf(idWorker.nextId()),nickName,Constants.TOKEN_ROLE_TOURIST); - Map map=new HashMap<>(); - map.put("token", token); - return Result.success(map); + return Result.success(token); } /** @@ -143,10 +137,8 @@ public class UserController { @ApiOperation(value = "5.token续期", notes = "有效期7天") @PostMapping("/autoLogin") @GlobalInterceptor - public Result> autoLogin(@RequestHeader(value = "token", required = false) String token) { - Map map=new HashMap<>(); - map.put("token", userService.autoLogin(token)); - return Result.success(map); + public Result autoLogin(@RequestHeader(value = "token", required = false) String token) { + return Result.success(userService.autoLogin(token)); } /**