starmusic-mobile
1/**
2 * 网易音乐 (wy) 源 SDK(迁移自 lx-music-mobile/src/utils/musicSdk/wy)
3 *
4 * 注意:网易新版接口(eapi/weapi,即 /eapi/batch、/weapi/cloudsearch/get/web 等)
5 * 在当前环境下未登录会被服务端风控(返回空 body 或 code 50000005),原项目所用的
6 * eapi 加密请求实测同样不可用。因此这里改用网易公开的明文老接口
7 * (/api/search/get/web、/api/song/detail),免登录、无需加密,实测可用。
8 */
9import { httpFetch } from './request.js'
10import { decodeName, formatPlayTime } from '../utils.js'
11
12const wy = {
13 name: '网易音乐',
14 limit: 30,
15 total: 0,
16 page: 0,
17 allPage: 1,
18
19 /** 搜索请求(明文老接口,免登录) */
20 musicSearch(str, page, limit) {
21 const query = [
22 `s=${encodeURIComponent(str)}`,
23 'type=1',
24 `limit=${limit}`,
25 `offset=${limit * (page - 1)}`,
26 ].join('&')
27 return httpFetch(`https://music.163.com/api/search/get/web?${query}`, {
28 headers: {
29 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
30 Referer: 'https://music.163.com/search/',
31 },
32 }).promise.then(({ body }) => body)
33 },
34
35 /** 歌手拼接 */
36 getSinger(singers) {
37 return singers.map((singer) => singer.name).join('、')
38 },
39
40 /** 处理搜索结果(老接口结构:artists / album / duration / fee,无音质信息) */
41 handleResult(rawList) {
42 if (!rawList) return []
43 return rawList.map((item) => {
44 return {
45 singer: decodeName(this.getSinger(item.artists || [])),
46 name: decodeName(item.name || ''),
47 albumName: item.album && item.album.name ? decodeName(item.album.name) : '',
48 albumId: item.album && item.album.id ? item.album.id : '',
49 source: 'wy',
50 interval: formatPlayTime((item.duration || 0) / 1000),
51 songmid: item.id,
52 img: '',
53 lrc: null,
54 types: [],
55 _types: {},
56 typeUrl: {},
57 }
58 })
59 },
60
61 /** 搜索 */
62 search(str, page = 1, limit) {
63 if (limit == null) limit = this.limit
64 return this.musicSearch(str, page, limit).then((result) => {
65 if (!result) throw new Error('网易搜索接口返回为空')
66 if (result.code !== 200 || !result.result) throw new Error('网易搜索失败')
67 const list = this.handleResult(result.result.songs || [])
68
69 this.total = result.result.songCount || 0
70 this.page = page
71 this.allPage = Math.ceil(this.total / this.limit)
72
73 return {
74 list,
75 allPage: this.allPage,
76 limit: this.limit,
77 total: this.total,
78 source: 'wy',
79 }
80 })
81 },
82
83 /** 获取歌曲信息(明文老接口,用于封面) */
84 getMusicInfo(songmid) {
85 const ids = encodeURIComponent(`[${songmid}]`)
86 return httpFetch(`https://music.163.com/api/song/detail/?id=${songmid}&ids=${ids}`, {
87 headers: {
88 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
89 Referer: `https://music.163.com/song?id=${songmid}`,
90 },
91 }).promise.then(({ body }) => {
92 if (body.code !== 200 || !body.songs || !body.songs.length) return Promise.reject(new Error('获取歌曲信息失败'))
93 return body.songs[0]
94 })
95 },
96
97 /** 获取封面 */
98 getPic(songInfo) {
99 if (songInfo.img) return Promise.resolve(songInfo.img)
100 return this.getMusicInfo(songInfo.songmid).then((info) => (info.album && info.album.picUrl) || '')
101 },
102
103 /** 获取歌词(明文老接口,免登录) */
104 getLyric(songInfo) {
105 const id = songInfo.songmid
106 return httpFetch(`https://music.163.com/api/song/lyric?id=${id}&lv=-1&kv=-1&tv=-1`, {
107 headers: {
108 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
109 Referer: `https://music.163.com/song?id=${id}`,
110 },
111 }).promise.then(({ body }) => {
112 if (!body || body.code !== 200 || !body.lrc || !body.lrc.lyric) return Promise.reject(new Error('获取歌词失败'))
113 return {
114 lyric: body.lrc.lyric,
115 tlyric: (body.tlyric && body.tlyric.lyric) || '',
116 }
117 })
118 },
119}
120
121export default wy