Permalink
Browse files

feature: 更新music接口

  • Loading branch information...
littlewin-wang committed Apr 6, 2018
1 parent 976f1a5 commit 3c40b4cef0531f5d938dfe7a156d659fe70bb320
Showing with 115 additions and 1 deletion.
  1. +106 −0 controllers/music.js
  2. +2 −1 package.json
  3. +7 −0 routes/index.js
@@ -0,0 +1,106 @@
/**
* @file 音乐控制
* @author littlewin(littlewin.wang@gmail.com)
*/

const NeteseMusic = require('simple-netease-cloud-music')
const neteseMusic = new NeteseMusic()

class Music {
// 获取歌单列表
static async getPlaylist (ctx) {
const id = ctx.params.id

if (!id || Object.is(Number(id), NaN)) {
ctx.throw(400, '歌单列表ID无效')
return
}

let playlist = await neteseMusic._playlist(id)

ctx.status = 200
ctx.body = {
success: true,
message: "歌单列表获取成功",
data: playlist
}
}

// 获取歌曲详情
static async getMucic (ctx) {
const id = ctx.params.id

if (!id || Object.is(Number(id), NaN)) {
ctx.throw(400, '歌曲ID无效')
return
}

let music = await neteseMusic.song(id)

ctx.status = 200
ctx.body = {
success: true,
message: "歌曲详情获取成功",
data: music
}
}

// 获取歌曲地址
static async getUrl (ctx) {
const id = ctx.params.id

if (!id || Object.is(Number(id), NaN)) {
ctx.throw(400, '歌曲ID无效')
return
}

let url = await neteseMusic.url(id, 128)

ctx.status = 200
ctx.body = {
success: true,
message: "歌曲url获取成功",
data: url
}
}

// 获取歌词
static async getLrc (ctx) {
const id = ctx.params.id

if (!id || Object.is(Number(id), NaN)) {
ctx.throw(400, '歌曲ID无效')
return
}

let lrc = await neteseMusic.lyric(id)

ctx.status = 200
ctx.body = {
success: true,
message: "歌词获取成功",
data: lrc
}
}

// 获取歌曲图片封面
static async getPic (ctx) {
const id = ctx.params.id

if (!id || Object.is(Number(id), NaN)) {
ctx.throw(400, '歌曲ID无效')
return
}

let pic = await neteseMusic.picture(id, 700)

ctx.status = 200
ctx.body = {
success: true,
message: "歌曲封面成功",
data: pic
}
}
}

module.exports = Music
@@ -1,6 +1,6 @@
{
"name": "littlewin.server",
"version": "0.1.0",
"version": "0.1.1",
"private": true,
"scripts": {
"dev": "NODE_ENV=development ./node_modules/.bin/nodemon bin/run ",
@@ -33,6 +33,7 @@
"request": "^2.81.0",
"request-promise": "^4.2.1",
"runkoa": "^1.3.1",
"simple-netease-cloud-music": "^0.3.4",
"sitemap": "^1.13.0"
},
"devDependencies": {
@@ -21,6 +21,7 @@ const Event = require('controllers/event')
const Message = require('controllers/message')
const Spotify = require('controllers/spotify')
const GA = require('controllers/ga')
const Music = require('controllers/music')
const middleware = require('middlewares')

router
@@ -95,4 +96,10 @@ router

.post('/ga', GA.post)

.get('/music/list/:id', Music.getPlaylist)
.get('/music/:id', Music.getMucic)
.get('/music/url/:id', Music.getUrl)
.get('/music/lrc/:id', Music.getLrc)
.get('/music/pic/:id', Music.getPic)

module.exports = router

0 comments on commit 3c40b4c

Please sign in to comment.