1.update login token response to string;2.add auth check on music

main
Gary 10 months ago
parent 6c4df54f35
commit 6ff77ebbcc

@ -1,19 +1,31 @@
package com.luoo.music.controller; package com.luoo.music.controller;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
/** /**
* *
*/ */
@Slf4j
@ControllerAdvice @ControllerAdvice
public class BaseExceptionHandler { public class BaseExceptionHandler {
@ExceptionHandler(value = Exception.class) @ExceptionHandler(value = Exception.class)
@ResponseBody @ResponseBody
public Result<Void> error(Exception e){ public Result<Void> error(Exception e) {
e.printStackTrace(); log.error("执行出错", e);
return Result.failed(StatusCode.MUSIC_COMMON_FAILED); return Result.failed(StatusCode.MUSIC_COMMON_FAILED);
} }
@ExceptionHandler(value = BizException.class)
@ResponseBody
public Result<String> error(BizException e) {
log.info("业务错误:{}", e.getMessage());
return Result.failed(StatusCode.MUSIC_COMMON_FAILED, e.getMessage());
}
} }

@ -36,7 +36,7 @@ public class JournalController {
@ApiOperation(value = "1.查询期刊信息") @ApiOperation(value = "1.查询期刊信息")
@GetMapping("/list") @GetMapping("/list")
//@GlobalInterceptor(checkLogin = true) @GlobalInterceptor(checkLogin = true)
public Result<PageResult<JournalRespDTO>> page(@RequestHeader(value = "token", required = false) String token, public Result<PageResult<JournalRespDTO>> page(@RequestHeader(value = "token", required = false) String token,
JournalQueryReq queryReq){ JournalQueryReq queryReq){
Page<Article> pageList = articleService.queryPage(queryReq); Page<Article> pageList = articleService.queryPage(queryReq);

@ -1,10 +1,11 @@
package com.luoo.music.controller; package com.luoo.music.controller;
import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.dto.response.SongRespDTO;
import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Article;
import com.luoo.music.pojo.Song; import com.luoo.music.pojo.Song;
import com.luoo.music.service.SongService; import com.luoo.music.service.SongService;
import annotation.GlobalInterceptor;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import constants.Constants; import constants.Constants;
@ -30,20 +31,36 @@ public class SongController {
/** /**
* *
*
* @return * @return
*/ */
@GetMapping @GetMapping
public Result<List<Song>> findAll(){ public Result<List<Song>> findAll() {
return Result.success(songService.findAll()); return Result.success(songService.findAll());
} }
@ApiOperation(value = "1.根据期刊号查询歌曲信息") @ApiOperation(value = "1.根据期刊号查询歌曲信息")
@GetMapping("/getByJournalNo/{journalNo}") @GetMapping("/getByJournalNo/{journalNo}")
public Result<List<SongRespDTO>> getByJournalNo(@PathVariable String journalNo){ @GlobalInterceptor(checkLogin = true)
List<Song> songs=songService.findByVolid(journalNo); public Result<List<SongRespDTO>> getByJournalNo(@RequestHeader(value = "token", required = false) String token,
List<SongRespDTO> results=songs.stream().map(s->getSongRespDTO(s)).collect(Collectors.toList()); @PathVariable String journalNo) {
List<Song> songs = songService.findByVolid(journalNo);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s)).collect(Collectors.toList());
return Result.success(results); return Result.success(results);
} }
/*
* @ApiOperation(value = "2.随机播放歌曲",notes="雀乐FM")
*
* @GetMapping("/random/{limit}")
*
* @GlobalInterceptor(checkLogin = true) public Result<List<SongRespDTO>>
* getByJournalNo(@RequestHeader(value = "token", required = false) String
* token) { List<Song> songs = songService.random(); List<SongRespDTO> results =
* songs.stream().map(s -> getSongRespDTO(s)).collect(Collectors.toList());
* return Result.success(results); }
*/
private SongRespDTO getSongRespDTO(Song song) { private SongRespDTO getSongRespDTO(Song song) {
SongRespDTO songRespDTO = new SongRespDTO(); SongRespDTO songRespDTO = new SongRespDTO();
songRespDTO.setJournalNo(song.getVolid()); songRespDTO.setJournalNo(song.getVolid());
@ -51,37 +68,39 @@ public class SongController {
songRespDTO.setArtist(song.getArtist()); songRespDTO.setArtist(song.getArtist());
songRespDTO.setId(song.getId()); songRespDTO.setId(song.getId());
songRespDTO.setTitle(song.getName()); songRespDTO.setTitle(song.getName());
songRespDTO.setSrc(Constants.MUSIC_RESOURCE_PREFIX+song.getUrl()); songRespDTO.setSrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl());
songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX+song.getUrl().replace(".mp3",".jpg")); songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg"));
songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX+song.getUrl().replace(".mp3",".lyric")); songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric"));
songRespDTO.setSongNo(song.getSongno()); songRespDTO.setSongNo(song.getSongno());
return songRespDTO; return songRespDTO;
} }
/** /**
* ID * ID
*
* @param id ID * @param id ID
* @return * @return
*/ */
@GetMapping("/{id}") @GetMapping("/{id}")
public Result<Song> findById(@PathVariable String id){ public Result<Song> findById(@PathVariable String id) {
return Result.success(songService.findById(id)); return Result.success(songService.findById(id));
} }
/** /**
* + * +
*
* @param searchMap * @param searchMap
* @param page * @param page
* @param size * @param size
* @return * @return
*/ */
@PostMapping("/search/{page}/{size}") @PostMapping("/search/{page}/{size}")
public Result<PageResult<Song>> findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ public Result<PageResult<Song>> findSearch(@RequestBody Map searchMap, @PathVariable int page,
@PathVariable int size) {
Page<Song> pageList = songService.findSearch(searchMap, page, size); Page<Song> pageList = songService.findSearch(searchMap, page, size);
return Result.success(new PageResult<Song>(pageList.getTotalElements(), pageList.getContent()) ); return Result.success(new PageResult<Song>(pageList.getTotalElements(), pageList.getContent()));
} }
@PostMapping("/init") @PostMapping("/init")
public Result<Void> init(@RequestBody Map map) { public Result<Void> init(@RequestBody Map map) {
List<Map> data = (List) map.get("data"); List<Map> data = (List) map.get("data");
@ -89,32 +108,31 @@ public class SongController {
Song song = new Song(); Song song = new Song();
Set<Article> set = new HashSet<Article>(); Set<Article> set = new HashSet<Article>();
int temp=0; int temp = 0;
int num=0; int num = 0;
for (Map dataMap :data for (Map dataMap : data) {
) {
Article article = new Article(); Article article = new Article();
article.setVolid(dataMap.get("id")+""); article.setVolid(dataMap.get("id") + "");
article.setTitle(dataMap.get("title")+""); article.setTitle(dataMap.get("title") + "");
String avatar = "0000"+dataMap.get("id"); String avatar = "0000" + dataMap.get("id");
String substring = avatar.substring(avatar.length() - 5); String substring = avatar.substring(avatar.length() - 5);
System.out.println(substring); System.out.println(substring);
article.setImage(substring+"/00.jpg"); article.setImage(substring + "/00.jpg");
if (temp== (int) dataMap.get("id")){ if (temp == (int) dataMap.get("id")) {
num = num+1; num = num + 1;
}else { } else {
num = 0; num = 0;
} }
temp = (int) dataMap.get("id"); temp = (int) dataMap.get("id");
String numstr = "0000"+(num+1); String numstr = "0000" + (num + 1);
System.out.println(numstr.substring(numstr.length()-2)); System.out.println(numstr.substring(numstr.length() - 2));
song.setAlbum(dataMap.get("album")+""); song.setAlbum(dataMap.get("album") + "");
song.setArtist(dataMap.get("artist")+""); song.setArtist(dataMap.get("artist") + "");
song.setName(dataMap.get("name")+""); song.setName(dataMap.get("name") + "");
song.setVolid(dataMap.get("id")+""); song.setVolid(dataMap.get("id") + "");
song.setUrl(substring+"/"+numstr.substring(numstr.length()-2)+".mp3"); song.setUrl(substring + "/" + numstr.substring(numstr.length() - 2) + ".mp3");
song.setSongno(num+1); song.setSongno(num + 1);
songService.add(song); songService.add(song);
set.add(article); set.add(article);
@ -129,31 +147,33 @@ public class SongController {
/** /**
* *
*
* @param searchMap * @param searchMap
* @return * @return
*/ */
@PostMapping("/search") @PostMapping("/search")
public Result<List<Song>> findSearch( @RequestBody Map searchMap){ public Result<List<Song>> findSearch(@RequestBody Map searchMap) {
return Result.success(songService.findSearch(searchMap)); return Result.success(songService.findSearch(searchMap));
} }
/** /**
* *
*
* @param song * @param song
*/ */
@PostMapping @PostMapping
public Result<Void> add(@RequestBody Song song){ public Result<Void> add(@RequestBody Song song) {
songService.add(song); songService.add(song);
return Result.success(); return Result.success();
} }
/** /**
* *
*
* @param song * @param song
*/ */
@PutMapping("/{id}") @PutMapping("/{id}")
public Result<Void> update(@RequestBody Song song, @PathVariable String id ){ public Result<Void> update(@RequestBody Song song, @PathVariable String id) {
song.setId(id); song.setId(id);
songService.update(song); songService.update(song);
return Result.success(); return Result.success();
@ -161,10 +181,11 @@ public class SongController {
/** /**
* *
*
* @param id * @param id
*/ */
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Result<Void> delete(@PathVariable String id ){ public Result<Void> delete(@PathVariable String id) {
songService.deleteById(id); songService.deleteById(id);
return Result.success(); return Result.success();
} }

@ -8,12 +8,16 @@ import lombok.Setter;
@Setter @Setter
public class SongRespDTO { public class SongRespDTO {
private String id; private String id;
@ApiModelProperty(value = "歌曲名")
private String title; private String title;
@ApiModelProperty(value = "歌手/乐队名")
private String artist; private String artist;
@ApiModelProperty(value = "专辑") @ApiModelProperty(value = "专辑")
private String album; private String album;
private String src; private String src;
@ApiModelProperty(value = "歌曲专辑封面")
private String pic; private String pic;
@ApiModelProperty(value = "歌词")
private String lrc; private String lrc;
@ApiModelProperty(value = "剘刊号") @ApiModelProperty(value = "剘刊号")
private String journalNo; private String journalNo;

@ -104,12 +104,8 @@ public class UserController {
return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH); return Result.failed(StatusCode.USER_MOBILE_VERIFICATION_CODE_MISMATCH);
} }
User user = userService.loginOrRegister(mobile); User user = userService.loginOrRegister(mobile);
String token = jwtUtil.createJWT(user.getId(),user.getNickname(),Constants.TOKEN_ROLE_APP_USER); String token = jwtUtil.createJWT(user.getId(),user.getNickname(),Constants.TOKEN_ROLE_APP_USER);
Map<String,String> map = new HashMap<>(); return Result.success(token);
map.put("token", token);
return Result.success(map);
}finally { }finally {
redisTemplate.delete(redisMobileCheckCodeKey); redisTemplate.delete(redisMobileCheckCodeKey);
} }
@ -118,12 +114,10 @@ public class UserController {
@ApiOperation(value = "3.游客登录返回token", notes = "token中的subject和roles均为tourist") @ApiOperation(value = "3.游客登录返回token", notes = "token中的subject和roles均为tourist")
@GetMapping("/touristLogin") @GetMapping("/touristLogin")
public Result<Map<String,String>> touristLogin() { public Result<String> touristLogin() {
String nickName="游客-"+NickNameUtil.getRandomNickName(); String nickName="游客-"+NickNameUtil.getRandomNickName();
String token = jwtUtil.createJWT(String.valueOf(idWorker.nextId()),nickName,Constants.TOKEN_ROLE_TOURIST); String token = jwtUtil.createJWT(String.valueOf(idWorker.nextId()),nickName,Constants.TOKEN_ROLE_TOURIST);
Map<String,String> map=new HashMap<>(); return Result.success(token);
map.put("token", token);
return Result.success(map);
} }
/** /**
@ -143,10 +137,8 @@ public class UserController {
@ApiOperation(value = "5.token续期", notes = "有效期7天") @ApiOperation(value = "5.token续期", notes = "有效期7天")
@PostMapping("/autoLogin") @PostMapping("/autoLogin")
@GlobalInterceptor @GlobalInterceptor
public Result<Map<String, String>> autoLogin(@RequestHeader(value = "token", required = false) String token) { public Result<String> autoLogin(@RequestHeader(value = "token", required = false) String token) {
Map<String,String> map=new HashMap<>(); return Result.success(userService.autoLogin(token));
map.put("token", userService.autoLogin(token));
return Result.success(map);
} }
/** /**

Loading…
Cancel
Save