更新网页接口

更新接口
This commit is contained in:
yutou 2020-12-04 14:09:12 +08:00
parent 3edfd2b55f
commit 7ec461ff29
4 changed files with 106 additions and 85 deletions

View File

@ -9,7 +9,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class ToolsApplication { public class ToolsApplication {
public static final String version="1.0.12"; public static final String version="1.0.13";
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("当前版本号:"+version); System.out.println("当前版本号:"+version);

View File

@ -8,6 +8,7 @@ import com.yutou.tools.utils.ConfigTools;
import com.yutou.tools.utils.MusicTools; import com.yutou.tools.utils.MusicTools;
import com.yutou.tools.utils.Tools; import com.yutou.tools.utils.Tools;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
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;
@ -106,6 +107,9 @@ public class MusicController {
@ResponseBody @ResponseBody
public String findFile(@RequestBody JSONObject body) { public String findFile(@RequestBody JSONObject body) {
String path = body.getString("path"); String path = body.getString("path");
if(!path.startsWith(defaultMusicPath)){
path=Tools.base64ToString(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);
@ -129,38 +133,29 @@ public class MusicController {
json.put("code", 0); json.put("code", 0);
return json.toJSONString(); return json.toJSONString();
} }
@RequestMapping(value = "web/image.do",produces = MediaType.IMAGE_JPEG_VALUE)
@RequestMapping("image.do")
@ResponseBody @ResponseBody
public String getImage(@RequestBody JSONObject body) { public byte[] getImage(String fileName){
JSONObject json = new JSONObject(); JSONObject json=new JSONObject();
json.put("fileName",fileName);
return getImage(json);
}
@RequestMapping(value = "image.do",produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public byte[] getImage(@RequestBody JSONObject body) {
String fileName = body.getString("fileName"); String fileName = body.getString("fileName");
json.put("code", 1); if(!fileName.startsWith(defaultMusicPath)){
if (StringUtils.isEmpty(fileName)) { fileName=Tools.base64ToString(fileName);
json.put("msg", "文件为空");
json.put("code", -1);
json.put("data", "");
return json.toJSONString();
} }
File file = new File(fileName); File file=new File(fileName);
if(file.exists()){
try { try {
if (file.exists()) { return musicTools.readImage(file.getAbsolutePath());
json.put("msg", "ok");
json.put("code", 0);
json.put("data", musicTools.readImage(file.getAbsolutePath()));
} else {
json.put("msg", "文件不存在");
json.put("code", -1);
json.put("data", "");
}
} catch (Exception e) { } catch (Exception e) {
json.put("msg", "图片读取失败");
json.put("code", -1);
json.put("data", "");
e.printStackTrace(); e.printStackTrace();
} }
}
return json.toJSONString(); return null;
} }
@RequestMapping("random.do") @RequestMapping("random.do")
@ -182,16 +177,19 @@ public class MusicController {
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(@RequestBody JSONObject body) { public ResponseEntity<FileSystemResource> play(String filePath,String random) {
String filePath = body.getString("filePath"); String _filePath;
boolean random = body.containsKey("random") ? body.getBoolean("random") : StringUtils.isEmpty(filePath); boolean _random;
if (random) { _random= !StringUtils.isEmpty(random) && random.equals("true");
_filePath=Tools.base64ToString(filePath);
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]);
filePath = data.getFile(); _filePath = data.getFile();
} }
File file = new File(filePath); File file = new File(_filePath);
if (file.exists()) { if (file.exists()) {
return Tools.getFile(file); return Tools.getFile(file);
} else { } else {

View File

@ -17,6 +17,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Base64; import java.util.Base64;
@ -374,7 +375,18 @@ public class Tools {
} }
public static String base64ToString(String base){ public static String base64ToString(String base){
base=base.replace(" ","+"); base=base.replace(" ","+");
return new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes())); try {
base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes()));
}catch (Exception e){
try {
base=URLDecoder.decode(base,"UTF-8");
base=base.replace(" ","+");
base=new String(Base64.getDecoder().decode(base.replace("\r\n","").getBytes()));
} catch (Exception e1) {
e1.printStackTrace();
}
}
return base;
} }
/** /**
* 异常输出 * 异常输出

View File

@ -65,10 +65,10 @@
$.get("/nas/music/getlocalhost.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (obj) { $.get("/nas/music/getlocalhost.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (obj) {
try { try {
let json = JSON.parse(obj); let json = JSON.parse(obj);
localhost = json.data+":8000"; localhost = json.data + ":8000";
console.log("音频地址:"+localhost) console.log("音频地址:" + localhost)
if(localhost==='http://null:8000'||localhost===":8000"){ if (localhost === 'http://null:8000' || localhost === ":8000") {
localhost="http://127.0.0.1" localhost = "http://127.0.0.1"
} }
} catch (e) { } catch (e) {
localhost = "" localhost = ""
@ -80,23 +80,29 @@
let listTable = table.render({ let listTable = table.render({
elem: '#playlist' elem: '#playlist'
, url: localhost + '/nas/music/list.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk' //数据接口 , url: localhost + '/nas/music/list.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk' //数据接口
, method: 'post'
, where: {
path: 'root',
type: false
}
, contentType: 'application/json'
, page: true //开启分页 , page: true //开启分页
, cols: [[ , cols: [[
{field: 'title', title: '标题', width: 400,sort:true, templet: "#listTemplet"} {field: 'title', title: '标题', width: 400, sort: true, templet: "#listTemplet"}
, {field: 'artist', title: '艺术家', width: 200} , {field: 'artist', title: '艺术家', width: 200}
, {field: 'album', title: '专辑', width: 200} , {field: 'album', title: '专辑', width: 200}
, {field: 'composer', title: '作曲', width: 200} , {field: 'composer', title: '作曲', width: 200}
, {field: 'track', title: '音轨号', width: 100,sort:true} , {field: 'track', title: '音轨号', width: 100, sort: true}
, {field: "right", width: 150, toolbar: '#music'} , {field: "right", width: 150, toolbar: '#music'}
]] ]]
, done: function (res, curr, count) { , done: function (res, curr, count) {
musicLib = res.data musicLib = res.data
$('#musicSize').html("歌单总数:"+res.size) $('#musicSize').html("歌单总数:" + res.size)
} }
}) })
table.on('rowDouble(music)', function (obj) { table.on('rowDouble(music)', function (obj) {
//obj 同上 //obj 同上
if (obj.data.isdir===1) { if (obj.data.isdir === 1) {
listTable.reload({ listTable.reload({
where: { where: {
path: obj.data.file path: obj.data.file
@ -116,16 +122,16 @@
table.on('tool(music)', function (obj) { table.on('tool(music)', function (obj) {
let data = obj.data; let data = obj.data;
if (obj.event === 'download') { if (obj.event === 'download') {
if (data.isdir===0) { if (data.isdir === 0) {
window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + new Base64().encode(data.file)) window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + new Base64().encode(data.file))
} }
} else if (obj.event === 'play') { } else if (obj.event === 'play') {
$.get(localhost + "/nas/music/list.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&type=all&path=" + new Base64().encode(data.file), function (obj) { $.post(localhost + "/nas/music/list.do", {type: false, path: data.file}, function (obj) {
let json = JSON.parse(obj); let json = JSON.parse(obj);
if(json.code===0){ if (json.code === 0) {
playIndex=0; playIndex = 0;
musicLib=json.data; musicLib = json.data;
isRandom=false; isRandom = false;
playNext() playNext()
} }
@ -142,7 +148,7 @@
player.init({ player.init({
container: '#audioWrap' container: '#audioWrap'
, source: '' , source: ''
, imagePath: '/assets/' , imagePath: '/assets'
, debuggers: false , debuggers: false
, allowSeek: true , allowSeek: true
, endedCallback: function () { , endedCallback: function () {
@ -168,25 +174,25 @@
$('#download').click(function () { $('#download').click(function () {
window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + playNow) window.open(localhost + "/nas/music/play.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&random=false&filePath=" + playNow)
}); });
$('#next').click(function (){ $('#next').click(function () {
playNext() playNext()
}); });
$('#reload').click(function (){ $('#reload').click(function () {
$.get(localhost+"/nas/music/reload.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk",function (obj){ $.post(localhost + "/nas/music/reload.do", function (obj) {
layer.msg("提交刷新请求成功,几分钟后生效") layer.msg("提交刷新请求成功,几分钟后生效")
}) })
}); });
function playNext() { function playNext() {
if(isRandom){ if (isRandom) {
random() random()
return ; return;
} }
if (playIndex === musicLib.length) { if (playIndex === musicLib.length) {
playIndex = 0; playIndex = 0;
} }
if (musicLib[playIndex].isdir===1) { if (musicLib[playIndex].isdir === 1) {
playIndex++ playIndex++
playNext() playNext()
} else { } else {
@ -196,7 +202,7 @@
} }
function random() { function random() {
$.get(localhost + "/nas/music/random.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk", function (data) { $.post(localhost + "/nas/music/random.do", function (data) {
let json = JSON.parse(data) let json = JSON.parse(data)
if (json.code === 0) { if (json.code === 0) {
playNow = json.data playNow = json.data
@ -223,36 +229,41 @@
} }
function update(fileName) { function update(fileName) {
$.get(localhost + '/nas/music/find/file.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&path=' + fileName, function (data) { $.ajax({
type: 'post'
, url: localhost + '/nas/music/find/file.do'
, contentType: "application/json"
, data: JSON.stringify({"path": fileName})
, success: function (data) {
let json = JSON.parse(data); let json = JSON.parse(data);
if(isRandom){ if (isRandom) {
$('#playerNowList').html("播放列表: (全列表随机播放)") $('#playerNowList').html("播放列表: (全列表随机播放)")
}else{ } else {
$('#playerNowList').html("播放列表: (" + playIndex+"/"+musicLib.length+")") $('#playerNowList').html("播放列表: (" + playIndex + "/" + musicLib.length + ")")
} }
$('#title').html("标题:" + json.data.title) $('#title').html("标题:" + json.data.title)
$('#artist').html("艺术家:" + json.data.artist) $('#artist').html("艺术家:" + json.data.artist)
$('#album').html("专辑:" + json.data.album) $('#album').html("专辑:" + json.data.album)
$('#composer').html("作曲:" + json.data.composer) $('#composer').html("作曲:" + json.data.composer)
navigator.mediaSession.metadata=new MediaMetadata({ navigator.mediaSession.metadata = new MediaMetadata({
title:json.data.title, title: json.data.title,
artist:json.data.artist, artist: json.data.artist,
album:json.data.album album: json.data.album,
artwork:[
{
src: localhost+'/nas/music/web/image.do?fileName='+fileName,
type: 'image/png',
sizes: '128x128'
}
]
}); });
navigator.mediaSession.setActionHandler('nexttrack', function () { navigator.mediaSession.setActionHandler('nexttrack', function () {
// 用户点击了“下一首”媒体通知图标 // 用户点击了“下一首”媒体通知图标
playNext(); playNext();
}); });
}); }
$.get(localhost + "/nas/music/image.do?token=PlVodzYhvxRQbOHKakpKs2dvnoc43Cnk&fileName=" + fileName, function (data) {
let json = JSON.parse(data);
$('#img').attr("src", "data:image/png;base64," + json.data),
navigator.mediaSession.metadata.artwork=[{
src:"data:image/png;base64," + json.data,
type: 'image/png',
sizes: '128x128'
}]
}) })
$('#img').attr("src",'/nas/music/web/image.do?fileName='+fileName);
} }
</script> </script>