app.js 1.6 KB
Newer Older
1 2 3 4 5 6
import request from 'request'
import { origin, globalOption } from './config'
import { deepCopy } from './util'

let api = {
  search: (name = null, callback = null, limit = 3, offset = 0) => {
zhuangtongfa's avatar
update  
zhuangtongfa 已提交
7 8 9 10 11 12 13
    let option = deepCopy(globalOption)
    let url = origin + '/api/search/suggest/web'
    let form = {
      s: name,
      limit,
      type: 1,
      offset
14
    }
zhuangtongfa's avatar
update  
zhuangtongfa 已提交
15 16 17 18 19 20 21 22 23 24
    let method = 'POST'
    Object.assign(option, { url, form, method })
    request(option, (error, response, body) => {
      if (!error && response.statusCode == 200) {
        let info = JSON.parse(body);
        callback && callback(JSON.stringify(info, '', 2))
      } else {
        console.log(error)
      }
    })
25 26 27 28 29
  },
  song: (id, callback = null) => {
    let option = deepCopy(globalOption)
    let url = origin + '/api/song/detail?ids=%5B' + id + '%5d'
    let method = 'GET'
zhuangtongfa's avatar
update  
zhuangtongfa 已提交
30
    Object.assign(option, { url, method })
31 32 33 34 35 36 37 38 39 40 41 42 43
    request(option, (error, response, body) => {
      if (!error && response.statusCode == 200) {
        let info = JSON.parse(body);
        callback && callback(JSON.stringify(info, '', 2))
      } else {
        console.log(error)
      }
    })
  },
  lrc: (id, callback = null, lv = -1) => {
    let option = deepCopy(globalOption)
    let url = origin + '/api/song/lyric?lv=' + lv + '&id=' + id
    let method = 'GET'
zhuangtongfa's avatar
update  
zhuangtongfa 已提交
44
    Object.assign(option, { url, method })
45 46 47 48 49 50 51 52 53 54 55
    request(option, (error, response, body) => {
      if (!error && response.statusCode == 200) {
        let info = JSON.parse(body);
        callback && callback(JSON.stringify(info, '', 2))
      } else {
        console.log(error)
      }
    })
  }
}
export {api}