初始化项目

This commit is contained in:
dengtongyu committed 2020-11-20 16:07:37 +08:00
commit 28f327c903
19 files changed
+434

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
ctx.body = 'hi, egg';
}
}
module.exports = HomeController;
+48
View File
@@ -0,0 +1,48 @@
'use strict';
const Controller = require('egg').Controller;
const respFormat = require('../util/respFormat');
class KuGouController extends Controller {
async songSearch() {
const { ctx } = this;
try {
const res = await ctx.service.kugou.getSearchSongList(ctx.query);
if (res.status === 1) {
ctx.body = respFormat({ data: res.data });
} else {
ctx.body = respFormat({ desc: '请求出错,请稍后再试!', errcode: res.err_code });
}
} catch (err) {
ctx.body = respFormat({ desc: '出现未知错误!', errcode: 1000 });
}
}
async newSong() {
const { ctx } = this;
try {
const res = await ctx.service.kugou.getNewSongList(ctx.query);
if (res.data && res.data.length && res.data.length > 0) {
ctx.body = respFormat({ data: res.data });
} else {
ctx.body = respFormat({ desc: '请求出错,请稍后再试!', errcode: res.err_code });
}
} catch (err) {
ctx.body = respFormat({ desc: '出现未知错误!', errcode: 1000 });
}
}
async songMsg() {
const { ctx } = this;
try {
const res = await ctx.service.kugou.getSongMsg(ctx.query);
if (res.status == 1) {
ctx.body = respFormat({ data: res.data });
} else {
ctx.body = respFormat({ desc: '请求出错,请稍后再试!', errcode: res.err_code });
}
} catch (err) {
ctx.body = respFormat({ desc: '出现未知错误!', errcode: 1000 });
}
}
}
module.exports = KuGouController;
+11
View File
@@ -0,0 +1,11 @@
'use strict';
/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
// const { router, controller } = app;
// router.get('/', controller.home.index);
require('./router/music')(app);
};
+14
View File
@@ -0,0 +1,14 @@
'use strict';
// const neteasecloud = '/music/neteasecloud';
const kugou = '/music/kugou';
module.exports = app => {
const { router, controller } = app;
// router.get(neteasecloud, controller.music.neteasecloud);
router.get(kugou + '/songSearch', controller.kugou.songSearch);
router.get(kugou + '/newSong', controller.kugou.newSong);
router.get(kugou + '/songMsg', controller.kugou.songMsg);
};
+59
View File
@@ -0,0 +1,59 @@
'use strict';
const Service = require('egg').Service;
class KuGouService extends Service {
async getSearchSongList(param) {
try {
const header = {
method: 'GET',
dataType: 'json',
data: {
...param,
_: Date.now(),
},
};
const url = 'http://songsearch.kugou.com/song_search_v2';
const res = await this.ctx.curl(url, header);
return res.data;
} catch (err) {
throw new Error(err);
}
}
async getNewSongList(param) {
try {
const header = {
method: 'GET',
dataType: 'json',
data: {
...param,
_: Date.now(),
},
};
const url = 'http://m.kugou.com/';
const res = await this.ctx.curl(url, header);
return res.data;
} catch (err) {
throw new Error(err);
}
}
async getSongMsg(hash) {
try {
const header = {
method: 'GET',
dataType: 'json',
data: {
hash,
r: 'play/getdata',
},
};
const url = 'http://www.kugou.com/yy/index.php';
const res = await this.ctx.curl(url, header);
return res.data;
} catch (err) {
throw new Error(err);
}
}
}
module.exports = KuGouService;
+25
View File
@@ -0,0 +1,25 @@
'use strict';
const getErrorMessge = code => {
let message = '';
switch (code) {
case 0:
message = '请求成功';
break;
default:
message = '请求成功';
break;
}
return message;
};
const respFormat = option => {
const res = {};
res.data = option.hasOwnProperty('data') ? option.data : '';
res.errcode = option.hasOwnProperty('errcode') ? option.errcode : 0;
res.result = option.hasOwnProperty('result') ? option.result : true;
res.desc = option.hasOwnProperty('desc') ? option.desc : getErrorMessge(res.errcode);
return res;
};
module.exports = respFormat;