diff --git a/app/main.js b/app/main.js index 8dbb596..f53a3ca 100644 --- a/app/main.js +++ b/app/main.js @@ -2,7 +2,7 @@ const http = require("http"); const url = require("url"); const { ServerModel } = require('./model/ServerModel'); const { SearchModel } = require('./model/SearchModel'); -const { storeMusic, storeLrc, storePic } = require('./model/StoreModel'); +const { storeMusic, storeLrc, storePic, checkMusicExists } = require('./model/StoreModel'); const { SuccessResult, ErrorResult } = require('./model/ResultModel'); const { QQSource } = require('./source/QQSource'); @@ -34,6 +34,7 @@ searcher.registerSourceModel(new mfm.MyFreemp31tingSource()); // *********************************************************************** // 添加API接口 const _server = new ServerModel(); + _server.get("/api/source/all", (req, res, query) => { // 罗列都有哪些源 return new SuccessResult(searcher.getSourceList()); @@ -45,6 +46,11 @@ _server.get("/api/search", async (req, res, query) => { return new SuccessResult(result); }); +_server.get("/api/store/check", async (req, res, query) => { + let is_exists = checkMusicExists(query.music_path); + return new SuccessResult(is_exists); +}); + _server.get("/api/store", async (req, res, query) => { //存储文件 let music_path = query.music_path; @@ -80,6 +86,7 @@ const server = http.createServer(async (req, res) => { let reqUrl = req.url; let reqApi = reqUrl.split('?')[0]; console.log(method, reqUrl); + let result; try { if(method == "GET") { @@ -94,6 +101,7 @@ const server = http.createServer(async (req, res) => { console.error(err); result = new ErrorResult(err); } + res.setHeader('Access-Control-Allow-Origin', '*'); //res.setHeader('Access-Control-Allow-Methods', '*'); res.setHeader('Access-Control-Allow-Headers', '*'); diff --git a/app/model/StoreModel.js b/app/model/StoreModel.js index 42a9f47..2612ad2 100644 --- a/app/model/StoreModel.js +++ b/app/model/StoreModel.js @@ -41,8 +41,13 @@ async function storePic(path, url) { return await storeResource(path, url); } +function checkMusicExists(path) { + return fs.existsSync(path); +} + module.exports = { storeMusic, storeLrc, - storePic + storePic, + checkMusicExists } \ No newline at end of file diff --git a/html/index.html b/html/index.html index 91f0613..cf67e54 100644 --- a/html/index.html +++ b/html/index.html @@ -35,7 +35,7 @@
- + 搜索
@@ -79,15 +79,15 @@

{{ renderedStorePath }}

- 确认存储 + 确认存储

{{ store_message }}

- -
{{ current_item.lrc }}
+ +
@@ -97,7 +97,7 @@
- +
@@ -122,8 +122,8 @@ el: "#app", data() { const ajax = axios.create({ - baseURL: 'http://www.amuliang.top:5750', - //baseURL: 'http://localhost:5000', + //baseURL: 'http://www.amuliang.top:5750', + baseURL: 'http://localhost:5000', timeout: 10000, headers: { 'Content-Type': 'application/x-www-form-urlencoded', @@ -197,10 +197,11 @@ this.$set(this, 'result', result); this.is_searching = false; if(result.length > 0) { - this.$set(this, 'current_item', result[0]); + this.handleSelectItem(result[0]); } }, handleSelectItem(item) { + this.store_message = ''; this.$set(this, 'current_item', item); }, search(source_name, keywords, result) { @@ -223,10 +224,32 @@ resolve([]); }); }); - + }, + async handleStoreClick() { + this.is_storing = true; + let is_exists = await this.handleCheckMusicExists(); + if(is_exists) { + this.$Modal.confirm({ + title: '同名文件处理方式', + content: '歌曲已经存在,是否覆盖?', + onOk: () => { + this.handleStore(); + }, + onCancel: () => { + this.is_storing = false; + } + }); + }else { + this.handleStore(); + } + }, + async handleCheckMusicExists() { + let response = await this.get('/api/store/check', { + music_path: this.renderedStorePath + }); + return response.data.data; }, handleStore() { - this.is_storing = true; this.store_message = ''; if(getExtension(this.renderedStorePath) == '') { this.$Message.error("请指定有效歌曲后缀名");