From 3edfd2b55f45499d3f0fd1b7dad24c092476f2b9 Mon Sep 17 00:00:00 2001 From: yutou Date: Thu, 3 Dec 2020 18:32:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9F=B3=E4=B9=90=E6=8E=A5=E5=8F=A3=E6=94=B9?= =?UTF-8?q?=E6=88=90=E4=BA=86POST=E8=AF=B7=E6=B1=82=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BA=86=E6=94=B6=E8=97=8F=E5=A4=B9=E5=8A=9F=E8=83=BD=20?= =?UTF-8?q?=E7=BD=91=E9=A1=B5=E7=89=88=E6=92=AD=E6=94=BE=E5=99=A8=E8=BF=98?= =?UTF-8?q?=E6=B2=A1=E6=94=B9=E6=88=90POST?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yutou/tools/home/nas/MusicController.java | 142 ++++++++------- .../home/nas/MusicFavoritesController.java | 161 ++++++++++++------ .../com/yutou/tools/utils/ConfigTools.java | 2 +- .../com/yutou/tools/utils/MusicTools.java | 2 +- src/main/resources/mapper/MusicDataDao.xml | 10 +- 5 files changed, 195 insertions(+), 122 deletions(-) diff --git a/src/main/java/com/yutou/tools/home/nas/MusicController.java b/src/main/java/com/yutou/tools/home/nas/MusicController.java index 594c610..5508184 100644 --- a/src/main/java/com/yutou/tools/home/nas/MusicController.java +++ b/src/main/java/com/yutou/tools/home/nas/MusicController.java @@ -11,6 +11,7 @@ import org.springframework.core.io.FileSystemResource; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -25,7 +26,7 @@ import java.util.List; @RequestMapping("/nas/music/") public class MusicController { - public static String defaultMusicPath="/media/yutou/4t/public/音乐"; + public static String defaultMusicPath = "/media/yutou/4t/public/音乐"; @Resource MusicTools musicTools; @@ -37,95 +38,103 @@ public class MusicController { json.put("code", 0); json.put("scan", musicTools.isScan()); json.put("size", musicTools.getLength()); - json.put("data", JSONArray.toJSON(musicTools.getMusicList())); + json.put("data", JSONArray.toJSON(musicTools.getMusicList())); return json.toJSONString(); } + @RequestMapping("list.do") @ResponseBody - public String getMusicListOfPath(String path,String type){ - if(!StringUtils.isEmpty(path)&&!new File(path).exists()){ - path=Tools.base64ToString(path); - } - System.out.println("接收到地址:"+path); - if(StringUtils.isEmpty(path) - ||path.equals("root") - ||!path.contains(defaultMusicPath) - ){ - path=defaultMusicPath; + public String getMusicListOfPath(@RequestBody JSONObject body) { + String path = body.getString("path"); + boolean type = body.containsKey("type") ? body.getBoolean("type") : false; + System.out.println("接收到地址:" + path); + if (StringUtils.isEmpty(path) + || path.equals("root") + || !path.contains(defaultMusicPath) + ) { + path = defaultMusicPath; } //path=path.replace(defaultMusicPath+File.separator,""); - boolean isDir= !StringUtils.isEmpty(type) && (type.equals("true")); - JSONObject json=new JSONObject(); + JSONObject json = new JSONObject(); json.put("code", 0); json.put("scan", musicTools.isScan()); json.put("size", musicTools.getLength()); - json.put("data", JSONArray.toJSON(musicTools.getPath(path,isDir))); + json.put("data", JSONArray.toJSON(musicTools.getPath(path, type))); return json.toJSONString(); } + @ResponseBody @RequestMapping("getAlbum.do") - public String getAlbum(String album){ - JSONObject json=new JSONObject(); - json.put("code",0); - if(StringUtils.isEmpty(album)){ - json.put("data",JSONArray.toJSON(musicTools.getAllAlbum())); - }else{ - json.put("data",JSONArray.toJSON(musicTools.selectAlbum(album))); + public String getAlbum(@RequestBody JSONObject body) { + String album = body.getString("album"); + JSONObject json = new JSONObject(); + json.put("code", 0); + if (StringUtils.isEmpty(album)) { + json.put("data", JSONArray.toJSON(musicTools.getAllAlbum())); + } else { + json.put("data", JSONArray.toJSON(musicTools.selectAlbum(album))); } return json.toJSONString(); } + @ResponseBody @RequestMapping("getArtist.do") - public String getArtist(String artist){ - JSONObject json=new JSONObject(); - json.put("code",0); - if(StringUtils.isEmpty(artist)){ - json.put("data",JSONArray.toJSON(musicTools.getAllArtist())); - }else{ - json.put("data",JSONArray.toJSON(musicTools.selectArtist(artist))); + public String getArtist(@RequestBody JSONObject body) { + String artist = body.getString("artist"); + JSONObject json = new JSONObject(); + json.put("code", 0); + if (StringUtils.isEmpty(artist)) { + json.put("data", JSONArray.toJSON(musicTools.getAllArtist())); + } else { + json.put("data", JSONArray.toJSON(musicTools.selectArtist(artist))); } return json.toJSONString(); } + @RequestMapping("reload.do") @ResponseBody - public String reload(){ - JSONObject json=new JSONObject(); + public String reload() { + JSONObject json = new JSONObject(); musicTools.scanMusic(); - json.put("msg","ok"); - json.put("code",0); + json.put("msg", "ok"); + json.put("code", 0); return json.toJSONString(); } + @RequestMapping("find/file.do") @ResponseBody - public String findFile(String path){ - JSONObject json=new JSONObject(); - if(StringUtils.isEmpty(path)){ - json.put("code",-1); - json.put("msg","地址为空"); + public String findFile(@RequestBody JSONObject body) { + String path = body.getString("path"); + JSONObject json = new JSONObject(); + if (StringUtils.isEmpty(path)) { + json.put("code", -1); + json.put("msg", "地址为空"); return json.toJSONString(); } - json.put("code",0); - json.put("data",musicTools.getMusicData(path)); + json.put("code", 0); + json.put("data", musicTools.getMusicData(path)); return json.toJSONString(); } + @RequestMapping("getlocalhost.do") @ResponseBody - public String getLocalHost(){ - JSONObject json=new JSONObject(); - if(ConfigTools.load(ConfigTools.CONFIG, "model").equals("dev")){ - json.put("data",""); - }else{ - json.put("data", "http://"+UpdateIp.nas_ip); + public String getLocalHost() { + JSONObject json = new JSONObject(); + if (ConfigTools.load(ConfigTools.CONFIG, "model").equals("dev")) { + json.put("data", ""); + } else { + json.put("data", "http://" + UpdateIp.nas_ip); } - json.put("code",0); + json.put("code", 0); return json.toJSONString(); } @RequestMapping("image.do") @ResponseBody - public String getImage(String fileName) { + public String getImage(@RequestBody JSONObject body) { JSONObject json = new JSONObject(); + String fileName = body.getString("fileName"); json.put("code", 1); if (StringUtils.isEmpty(fileName)) { json.put("msg", "文件为空"); @@ -133,7 +142,7 @@ public class MusicController { json.put("data", ""); return json.toJSONString(); } - File file = new File(Tools.base64ToString(fileName)); + File file = new File(fileName); try { if (file.exists()) { json.put("msg", "ok"); @@ -144,7 +153,7 @@ public class MusicController { json.put("code", -1); json.put("data", ""); } - }catch (Exception e){ + } catch (Exception e) { json.put("msg", "图片读取失败"); json.put("code", -1); json.put("data", ""); @@ -153,33 +162,34 @@ public class MusicController { return json.toJSONString(); } + @RequestMapping("random.do") @ResponseBody - public String random(){ - List list=musicTools.getMusicList(); - MusicData data=list.get(Tools.randomCommon(0,list.size()-1,1)[0]); - JSONObject json=new JSONObject(); - json.put("code",0); + public String random() { + List list = musicTools.getMusicList(); + MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]); + JSONObject json = new JSONObject(); + json.put("code", 0); try { - json.put("data", URLEncoder.encode(getBase64(data.getFile()),"UTF-8")); + json.put("data", URLEncoder.encode(getBase64(data.getFile()), "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return json.toJSONString(); } - private String getBase64(String str){ + + private String getBase64(String str) { return new String(Base64.getEncoder().encode(str.getBytes())); } - + @RequestMapping("play.do") - public ResponseEntity play(String filePath,boolean random) { - if(!StringUtils.isEmpty(filePath)){ - filePath=Tools.base64ToString(filePath); - } - if(random){ - List list=musicTools.getMusicList(); - MusicData data=list.get(Tools.randomCommon(0,list.size()-1,1)[0]); - filePath=data.getFile(); + public ResponseEntity play(@RequestBody JSONObject body) { + String filePath = body.getString("filePath"); + boolean random = body.containsKey("random") ? body.getBoolean("random") : StringUtils.isEmpty(filePath); + if (random) { + List list = musicTools.getMusicList(); + MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]); + filePath = data.getFile(); } File file = new File(filePath); if (file.exists()) { diff --git a/src/main/java/com/yutou/tools/home/nas/MusicFavoritesController.java b/src/main/java/com/yutou/tools/home/nas/MusicFavoritesController.java index 235a603..c91377b 100644 --- a/src/main/java/com/yutou/tools/home/nas/MusicFavoritesController.java +++ b/src/main/java/com/yutou/tools/home/nas/MusicFavoritesController.java @@ -2,101 +2,164 @@ package com.yutou.tools.home.nas; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.yutou.tools.mybatis.dao.MusicDataDao; import com.yutou.tools.mybatis.dao.MusicFavoritesDao; import com.yutou.tools.mybatis.dao.MusicFavoritesDirDao; -import com.yutou.tools.mybatis.model.MusicFavorites; -import com.yutou.tools.mybatis.model.MusicFavoritesDir; -import com.yutou.tools.mybatis.model.MusicFavoritesDirExample; -import com.yutou.tools.mybatis.model.MusicFavoritesExample; +import com.yutou.tools.mybatis.model.*; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.Date; +import java.util.LinkedHashMap; +import java.util.List; /** * 收藏夹相关 */ @Controller @RequestMapping("/nas/music/favorite/") -public class MusicFavoritesController{ +public class MusicFavoritesController { @Resource MusicFavoritesDao favoritesDao; @Resource MusicFavoritesDirDao favoritesDirDao; + @Resource + MusicDataDao musicDataDao; @RequestMapping("dir/add.do") @ResponseBody - public String addFavoriteDir(String favorite){ - JSONObject json=new JSONObject(); - MusicFavoritesDirExample example=new MusicFavoritesDirExample(); + public String addFavoriteDir(@RequestBody JSONObject body) { + String favorite = body.getString("favorite"); + JSONObject json = new JSONObject(); + MusicFavoritesDirExample example = new MusicFavoritesDirExample(); example.createCriteria().andTitleEqualTo(favorite); - if(favoritesDirDao.selectByExample(example).isEmpty()){ - json.put("code",-1); - json.put("msg","已有该收藏夹"); - }else{ - MusicFavoritesDir favoritesDir=new MusicFavoritesDir(); + if (!favoritesDirDao.selectByExample(example).isEmpty()) { + json.put("code", -1); + json.put("msg", "已有该收藏夹"); + } else { + MusicFavoritesDir favoritesDir = new MusicFavoritesDir(); favoritesDir.setTitle(favorite); - int ret=favoritesDirDao.insert(favoritesDir); - json.put("code",ret==0?-1:0); - json.put("msg",ret==0?"添加失败":"添加成功"); + int ret = favoritesDirDao.insert(favoritesDir); + json.put("code", ret == 0 ? -1 : 0); + json.put("msg", ret == 0 ? "添加失败" : "添加成功"); } return json.toJSONString(); } + @RequestMapping("dir/list.do") @ResponseBody - public String getAllFavoriteDir(){ - JSONObject json=new JSONObject(); - json.put("code",0); - json.put("data", JSONArray.toJSON(favoritesDirDao.selectByExample(new MusicFavoritesDirExample()))); + public String getAllFavoriteDir() { + JSONObject json = new JSONObject(); + json.put("code", 0); + List list = favoritesDirDao.selectByExample(new MusicFavoritesDirExample()); + JSONArray array = new JSONArray(); + for (MusicFavoritesDir favoritesDir : list) { + JSONObject data = new JSONObject(); + data.put("id", favoritesDir.getId()); + data.put("title", favoritesDir.getTitle()); + MusicFavoritesExample example = new MusicFavoritesExample(); + example.createCriteria().andFavoriteidEqualTo(favoritesDir.getId()); + data.put("count", favoritesDao.countByExample(example)); + array.add(data); + } + json.put("data", array); return json.toJSONString(); } + @RequestMapping("dir/rename.do") @ResponseBody - public String renameFavoriteDir(String id,String title){ - JSONObject json=new JSONObject(); - MusicFavoritesDir favoritesDir=favoritesDirDao.selectByPrimaryKey(Integer.parseInt(id)); - if(favoritesDir==null){ - json.put("code",-1); - json.put("msg","没有该收藏夹"); - }else{ + public String renameFavoriteDir(@RequestBody JSONObject body) { + int id = body.getInteger("id"); + String title = body.getString("title"); + JSONObject json = new JSONObject(); + MusicFavoritesDir favoritesDir = favoritesDirDao.selectByPrimaryKey(id); + if (favoritesDir == null) { + json.put("code", -1); + json.put("msg", "没有该收藏夹"); + } else { favoritesDir.setTitle(title); favoritesDirDao.updateByPrimaryKey(favoritesDir); - json.put("code",0); - json.put("msg","更新成功"); + json.put("code", 0); + json.put("msg", "更新成功"); } return json.toJSONString(); } + @RequestMapping("add.do") @ResponseBody - public String addFavorite(int fid,String md5s){ - JSONObject json=new JSONObject(); - JSONArray array=JSONArray.parseArray(md5s); - if(array==null){ - json.put("code",-1); - json.put("msg","参数异常"); + public String addFavorite(@RequestBody JSONObject body) { + JSONObject json = new JSONObject(); + JSONArray array = body.getJSONArray("md5s"); + int fid = body.getInteger("fid"); + if (array == null || fid == -1) { + json.put("code", -1); + json.put("msg", "参数异常: " + body); return json.toJSONString(); } - for (Object o : array) { - String md5= (String) o; - MusicFavorites favorites=new MusicFavorites(); - favorites.setMusisMd5(md5); - favorites.setFavoriteid(fid); - favorites.setSubTime(new Date()); - favoritesDao.insert(favorites); + for (Object md5 : array) { + MusicFavoritesExample example = new MusicFavoritesExample(); + example.createCriteria().andMusisMd5EqualTo((String) md5); + if (favoritesDao.countByExample(example) == 0) { + MusicFavorites favorites = new MusicFavorites(); + favorites.setMusisMd5((String) md5); + favorites.setFavoriteid(fid); + favorites.setSubTime(new Date()); + favoritesDao.insert(favorites); + } } - json.put("code",0); - json.put("msg","添加成功"); + json.put("code", 0); + json.put("msg", "添加成功"); return json.toJSONString(); } + @RequestMapping("remove.do") @ResponseBody - public String removeFavorite(int fid){ - JSONObject json=new JSONObject(); - int ret=favoritesDao.deleteByPrimaryKey(fid); - json.put("code",ret==0?-1:0); - json.put("msg",ret==0?"移除失败":"已从收藏夹中移除"); + public String removeFavorite(@RequestBody JSONObject body) { + JSONArray md5s = body.getJSONArray("data"); + JSONObject json = new JSONObject(); + for (Object md5 : md5s) { + LinkedHashMap js= (LinkedHashMap) md5; + MusicFavoritesExample example = new MusicFavoritesExample(); + example.createCriteria().andMusisMd5EqualTo((String) js.get("md5")) + .andFavoriteidEqualTo((Integer) js.get("fid")); + favoritesDao.deleteByExample(example); + } + json.put("code", 0); + json.put("msg", "已从收藏夹中移除"); + return json.toJSONString(); + } + + @RequestMapping("get.do") + @ResponseBody + public String getFavorite(@RequestBody JSONObject body) { + int fid = body.getInteger("fid"); + JSONObject json = new JSONObject(); + try { + MusicFavoritesExample example = new MusicFavoritesExample(); + example.createCriteria().andFavoriteidEqualTo(fid); + List list = favoritesDao.selectByExample(example); + List dataList = new ArrayList<>(); + for (MusicFavorites favorites : list) { + MusicDataExample dataExample = new MusicDataExample(); + dataExample.createCriteria().andMd5EqualTo(favorites.getMusisMd5()); + try { + dataList.add(musicDataDao.selectByExample(dataExample).get(0)); + } catch (Exception ignored) { + } + } + list.clear(); + json.put("code", 0); + json.put("data", JSONArray.toJSON(dataList)); + } catch (Exception e) { + e.printStackTrace(); + json.put("code", -1); + json.put("msg", e.getLocalizedMessage()); + json.put("data", new ArrayList<>()); + } return json.toJSONString(); } } diff --git a/src/main/java/com/yutou/tools/utils/ConfigTools.java b/src/main/java/com/yutou/tools/utils/ConfigTools.java index e8589b1..2838c80 100644 --- a/src/main/java/com/yutou/tools/utils/ConfigTools.java +++ b/src/main/java/com/yutou/tools/utils/ConfigTools.java @@ -29,7 +29,7 @@ public class ConfigTools { } public static Object load(String type,String key){ File file=new File(type); - System.out.println(type+"配置文件地址:"+file.getAbsolutePath()); + //System.out.println(type+"配置文件地址:"+file.getAbsolutePath()); String src=readFile(file); if(src!=null){ JSONObject json=JSONObject.parseObject(src); diff --git a/src/main/java/com/yutou/tools/utils/MusicTools.java b/src/main/java/com/yutou/tools/utils/MusicTools.java index 428d1a5..a12ac67 100644 --- a/src/main/java/com/yutou/tools/utils/MusicTools.java +++ b/src/main/java/com/yutou/tools/utils/MusicTools.java @@ -201,7 +201,7 @@ public class MusicTools implements MusicToolsService { @Override public MusicData getMusicData(String path) { MusicDataExample example = new MusicDataExample(); - example.createCriteria().andFileEqualTo(Tools.base64ToString(path)); + example.createCriteria().andFileEqualTo(path); List list = musicDataDao.selectByExample(example); if (list != null && list.size() > 0) { return list.get(0); diff --git a/src/main/resources/mapper/MusicDataDao.xml b/src/main/resources/mapper/MusicDataDao.xml index b6438fa..79de6e2 100644 --- a/src/main/resources/mapper/MusicDataDao.xml +++ b/src/main/resources/mapper/MusicDataDao.xml @@ -276,23 +276,23 @@ update music_data