音乐接口改成了POST请求

更新了收藏夹功能
网页版播放器还没改成POST
This commit is contained in:
yutou 2020-12-03 18:32:32 +08:00
parent ca012000a3
commit 3edfd2b55f
5 changed files with 195 additions and 122 deletions

View File

@ -11,6 +11,7 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils; 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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@ -40,12 +41,12 @@ public class MusicController {
json.put("data", JSONArray.toJSON(musicTools.getMusicList())); json.put("data", JSONArray.toJSON(musicTools.getMusicList()));
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("list.do") @RequestMapping("list.do")
@ResponseBody @ResponseBody
public String getMusicListOfPath(String path,String type){ public String getMusicListOfPath(@RequestBody JSONObject body) {
if(!StringUtils.isEmpty(path)&&!new File(path).exists()){ String path = body.getString("path");
path=Tools.base64ToString(path); boolean type = body.containsKey("type") ? body.getBoolean("type") : false;
}
System.out.println("接收到地址:" + path); System.out.println("接收到地址:" + path);
if (StringUtils.isEmpty(path) if (StringUtils.isEmpty(path)
|| path.equals("root") || path.equals("root")
@ -55,17 +56,18 @@ public class MusicController {
} }
//path=path.replace(defaultMusicPath+File.separator,""); //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("code", 0);
json.put("scan", musicTools.isScan()); json.put("scan", musicTools.isScan());
json.put("size", musicTools.getLength()); 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(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping("getAlbum.do") @RequestMapping("getAlbum.do")
public String getAlbum(String album){ public String getAlbum(@RequestBody JSONObject body) {
String album = body.getString("album");
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("code", 0); json.put("code", 0);
if (StringUtils.isEmpty(album)) { if (StringUtils.isEmpty(album)) {
@ -75,9 +77,11 @@ public class MusicController {
} }
return json.toJSONString(); return json.toJSONString();
} }
@ResponseBody @ResponseBody
@RequestMapping("getArtist.do") @RequestMapping("getArtist.do")
public String getArtist(String artist){ public String getArtist(@RequestBody JSONObject body) {
String artist = body.getString("artist");
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("code", 0); json.put("code", 0);
if (StringUtils.isEmpty(artist)) { if (StringUtils.isEmpty(artist)) {
@ -87,6 +91,7 @@ public class MusicController {
} }
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("reload.do") @RequestMapping("reload.do")
@ResponseBody @ResponseBody
public String reload() { public String reload() {
@ -96,9 +101,11 @@ public class MusicController {
json.put("code", 0); json.put("code", 0);
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("find/file.do") @RequestMapping("find/file.do")
@ResponseBody @ResponseBody
public String findFile(String path){ public String findFile(@RequestBody JSONObject body) {
String path = body.getString("path");
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
if (StringUtils.isEmpty(path)) { if (StringUtils.isEmpty(path)) {
json.put("code", -1); json.put("code", -1);
@ -109,6 +116,7 @@ public class MusicController {
json.put("data", musicTools.getMusicData(path)); json.put("data", musicTools.getMusicData(path));
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("getlocalhost.do") @RequestMapping("getlocalhost.do")
@ResponseBody @ResponseBody
public String getLocalHost() { public String getLocalHost() {
@ -124,8 +132,9 @@ public class MusicController {
@RequestMapping("image.do") @RequestMapping("image.do")
@ResponseBody @ResponseBody
public String getImage(String fileName) { public String getImage(@RequestBody JSONObject body) {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
String fileName = body.getString("fileName");
json.put("code", 1); json.put("code", 1);
if (StringUtils.isEmpty(fileName)) { if (StringUtils.isEmpty(fileName)) {
json.put("msg", "文件为空"); json.put("msg", "文件为空");
@ -133,7 +142,7 @@ public class MusicController {
json.put("data", ""); json.put("data", "");
return json.toJSONString(); return json.toJSONString();
} }
File file = new File(Tools.base64ToString(fileName)); File file = new File(fileName);
try { try {
if (file.exists()) { if (file.exists()) {
json.put("msg", "ok"); json.put("msg", "ok");
@ -153,6 +162,7 @@ public class MusicController {
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("random.do") @RequestMapping("random.do")
@ResponseBody @ResponseBody
public String random() { public String random() {
@ -167,15 +177,15 @@ public class MusicController {
} }
return json.toJSONString(); return json.toJSONString();
} }
private String getBase64(String str) { private String getBase64(String str) {
return new String(Base64.getEncoder().encode(str.getBytes())); return new String(Base64.getEncoder().encode(str.getBytes()));
} }
@RequestMapping("play.do") @RequestMapping("play.do")
public ResponseEntity<FileSystemResource> play(String filePath,boolean random) { public ResponseEntity<FileSystemResource> play(@RequestBody JSONObject body) {
if(!StringUtils.isEmpty(filePath)){ String filePath = body.getString("filePath");
filePath=Tools.base64ToString(filePath); boolean random = body.containsKey("random") ? body.getBoolean("random") : StringUtils.isEmpty(filePath);
}
if (random) { if (random) {
List<MusicData> list = musicTools.getMusicList(); List<MusicData> list = musicTools.getMusicList();
MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]); MusicData data = list.get(Tools.randomCommon(0, list.size() - 1, 1)[0]);

View File

@ -2,18 +2,20 @@ package com.yutou.tools.home.nas;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; 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.MusicFavoritesDao;
import com.yutou.tools.mybatis.dao.MusicFavoritesDirDao; import com.yutou.tools.mybatis.dao.MusicFavoritesDirDao;
import com.yutou.tools.mybatis.model.MusicFavorites; import com.yutou.tools.mybatis.model.*;
import com.yutou.tools.mybatis.model.MusicFavoritesDir;
import com.yutou.tools.mybatis.model.MusicFavoritesDirExample;
import com.yutou.tools.mybatis.model.MusicFavoritesExample;
import org.springframework.stereotype.Controller; 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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
/** /**
* 收藏夹相关 * 收藏夹相关
@ -25,14 +27,17 @@ public class MusicFavoritesController{
MusicFavoritesDao favoritesDao; MusicFavoritesDao favoritesDao;
@Resource @Resource
MusicFavoritesDirDao favoritesDirDao; MusicFavoritesDirDao favoritesDirDao;
@Resource
MusicDataDao musicDataDao;
@RequestMapping("dir/add.do") @RequestMapping("dir/add.do")
@ResponseBody @ResponseBody
public String addFavoriteDir(String favorite){ public String addFavoriteDir(@RequestBody JSONObject body) {
String favorite = body.getString("favorite");
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
MusicFavoritesDirExample example = new MusicFavoritesDirExample(); MusicFavoritesDirExample example = new MusicFavoritesDirExample();
example.createCriteria().andTitleEqualTo(favorite); example.createCriteria().andTitleEqualTo(favorite);
if(favoritesDirDao.selectByExample(example).isEmpty()){ if (!favoritesDirDao.selectByExample(example).isEmpty()) {
json.put("code", -1); json.put("code", -1);
json.put("msg", "已有该收藏夹"); json.put("msg", "已有该收藏夹");
} else { } else {
@ -44,19 +49,34 @@ public class MusicFavoritesController{
} }
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("dir/list.do") @RequestMapping("dir/list.do")
@ResponseBody @ResponseBody
public String getAllFavoriteDir() { public String getAllFavoriteDir() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("code", 0); json.put("code", 0);
json.put("data", JSONArray.toJSON(favoritesDirDao.selectByExample(new MusicFavoritesDirExample()))); List<MusicFavoritesDir> 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(); return json.toJSONString();
} }
@RequestMapping("dir/rename.do") @RequestMapping("dir/rename.do")
@ResponseBody @ResponseBody
public String renameFavoriteDir(String id,String title){ public String renameFavoriteDir(@RequestBody JSONObject body) {
int id = body.getInteger("id");
String title = body.getString("title");
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
MusicFavoritesDir favoritesDir=favoritesDirDao.selectByPrimaryKey(Integer.parseInt(id)); MusicFavoritesDir favoritesDir = favoritesDirDao.selectByPrimaryKey(id);
if (favoritesDir == null) { if (favoritesDir == null) {
json.put("code", -1); json.put("code", -1);
json.put("msg", "没有该收藏夹"); json.put("msg", "没有该收藏夹");
@ -68,35 +88,78 @@ public class MusicFavoritesController{
} }
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("add.do") @RequestMapping("add.do")
@ResponseBody @ResponseBody
public String addFavorite(int fid,String md5s){ public String addFavorite(@RequestBody JSONObject body) {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
JSONArray array=JSONArray.parseArray(md5s); JSONArray array = body.getJSONArray("md5s");
if(array==null){ int fid = body.getInteger("fid");
if (array == null || fid == -1) {
json.put("code", -1); json.put("code", -1);
json.put("msg","参数异常"); json.put("msg", "参数异常: " + body);
return json.toJSONString(); return json.toJSONString();
} }
for (Object o : array) { for (Object md5 : array) {
String md5= (String) o; MusicFavoritesExample example = new MusicFavoritesExample();
example.createCriteria().andMusisMd5EqualTo((String) md5);
if (favoritesDao.countByExample(example) == 0) {
MusicFavorites favorites = new MusicFavorites(); MusicFavorites favorites = new MusicFavorites();
favorites.setMusisMd5(md5); favorites.setMusisMd5((String) md5);
favorites.setFavoriteid(fid); favorites.setFavoriteid(fid);
favorites.setSubTime(new Date()); favorites.setSubTime(new Date());
favoritesDao.insert(favorites); favoritesDao.insert(favorites);
} }
}
json.put("code", 0); json.put("code", 0);
json.put("msg", "添加成功"); json.put("msg", "添加成功");
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping("remove.do") @RequestMapping("remove.do")
@ResponseBody @ResponseBody
public String removeFavorite(int fid){ public String removeFavorite(@RequestBody JSONObject body) {
JSONArray md5s = body.getJSONArray("data");
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
int ret=favoritesDao.deleteByPrimaryKey(fid); for (Object md5 : md5s) {
json.put("code",ret==0?-1:0); LinkedHashMap<String,Object> js= (LinkedHashMap<String, Object>) md5;
json.put("msg",ret==0?"移除失败":"已从收藏夹中移除"); 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<MusicFavorites> list = favoritesDao.selectByExample(example);
List<MusicData> 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(); return json.toJSONString();
} }
} }

View File

@ -29,7 +29,7 @@ public class ConfigTools {
} }
public static Object load(String type,String key){ public static Object load(String type,String key){
File file=new File(type); File file=new File(type);
System.out.println(type+"配置文件地址:"+file.getAbsolutePath()); //System.out.println(type+"配置文件地址:"+file.getAbsolutePath());
String src=readFile(file); String src=readFile(file);
if(src!=null){ if(src!=null){
JSONObject json=JSONObject.parseObject(src); JSONObject json=JSONObject.parseObject(src);

View File

@ -201,7 +201,7 @@ public class MusicTools implements MusicToolsService {
@Override @Override
public MusicData getMusicData(String path) { public MusicData getMusicData(String path) {
MusicDataExample example = new MusicDataExample(); MusicDataExample example = new MusicDataExample();
example.createCriteria().andFileEqualTo(Tools.base64ToString(path)); example.createCriteria().andFileEqualTo(path);
List<MusicData> list = musicDataDao.selectByExample(example); List<MusicData> list = musicDataDao.selectByExample(example);
if (list != null && list.size() > 0) { if (list != null && list.size() > 0) {
return list.get(0); return list.get(0);

View File

@ -276,23 +276,23 @@
</if> </if>
</select> </select>
<select id="selectByRegexp" resultType="com.yutou.tools.mybatis.model.MusicData"> <select id="selectByRegexp" resultType="com.yutou.tools.mybatis.model.MusicData">
select * from web_tools.music_data where `file` REGEXP #{regexp,jdbcType=VARCHAR} select * from music_data where `file` REGEXP #{regexp,jdbcType=VARCHAR}
</select> </select>
<select id="selectAllAlbum" resultType="java.lang.String"> <select id="selectAllAlbum" resultType="java.lang.String">
SELECT album FROM web_tools.music_data group by `album`; SELECT album FROM music_data group by `album`;
</select> </select>
<select id="selectAllArtist" resultType="java.lang.String"> <select id="selectAllArtist" resultType="java.lang.String">
SELECT artist FROM web_tools.music_data group by `artist`; SELECT artist FROM music_data group by `artist`;
</select> </select>
<select id="selectAlbum" resultType="com.yutou.tools.mybatis.model.MusicData"> <select id="selectAlbum" resultType="com.yutou.tools.mybatis.model.MusicData">
select select
<include refid="Base_Column_List" /> <include refid="Base_Column_List" />
from web_tools.music_data where `album`= #{album,jdbcType=VARCHAR} from music_data where `album`= #{album,jdbcType=VARCHAR}
</select> </select>
<select id="selectArtist" resultType="com.yutou.tools.mybatis.model.MusicData"> <select id="selectArtist" resultType="com.yutou.tools.mybatis.model.MusicData">
select select
<include refid="Base_Column_List" /> <include refid="Base_Column_List" />
from web_tools.music_data where `artist`= #{artist,jdbcType=VARCHAR} from music_data where `artist`= #{artist,jdbcType=VARCHAR}
</select> </select>
<update id="updateByExampleSelective" parameterType="map"> <update id="updateByExampleSelective" parameterType="map">
update music_data update music_data