-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathtrack.js
More file actions
181 lines (167 loc) · 4.46 KB
/
track.js
File metadata and controls
181 lines (167 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import store from '@/store';
import request from '@/utils/request';
import { mapTrackPlayableStatus } from '@/utils/common';
import {
cacheTrackDetail,
getTrackDetailFromCache,
cacheLyric,
getLyricFromCache,
} from '@/utils/db';
/**
* 获取音乐 url
* 说明 : 使用歌单详情接口后 , 能得到的音乐的 id, 但不能得到的音乐 url, 调用此接口, 传入的音乐 id( 可多个 , 用逗号隔开 ), 可以获取对应的音乐的 url,
* !!!未登录状态返回试听片段(返回字段包含被截取的正常歌曲的开始时间和结束时间)
* @param {string} id - 音乐的 id,例如 id=405998841,33894312
*/
export function getMP3(id) {
const getBr = () => {
// 当返回的 quality >= 400000时,就会优先返回 hi-res
const quality = store.state.settings?.musicQuality ?? '320000';
return quality === 'flac' ? '350000' : quality;
};
return request({
url: '/song/url',
method: 'get',
params: {
id,
br: getBr(),
},
});
}
/**
* 获取歌曲详情
* 说明 : 调用此接口 , 传入音乐 id(支持多个 id, 用 , 隔开), 可获得歌曲详情(注意:歌曲封面现在需要通过专辑内容接口获取)
* @param {string} ids - 音乐 id, 例如 ids=405998841,33894312
*/
export function getTrackDetail(ids) {
const fetchLatest = () => {
return request({
url: '/song/detail',
method: 'get',
params: {
ids,
},
}).then(data => {
data.songs.map(song => {
const privileges = data.privileges.find(t => t.id === song.id);
cacheTrackDetail(song, privileges);
});
data.songs = mapTrackPlayableStatus(data.songs, data.privileges);
return data;
});
};
fetchLatest();
let idsInArray = [String(ids)];
if (typeof ids === 'string') {
idsInArray = ids.split(',');
}
return getTrackDetailFromCache(idsInArray).then(result => {
if (result) {
result.songs = mapTrackPlayableStatus(result.songs, result.privileges);
}
return result ?? fetchLatest();
});
}
/**
* 获取歌词
* 说明 : 调用此接口 , 传入音乐 id 可获得对应音乐的歌词 ( 不需要登录 )
* @param {number} id - 音乐 id
*/
export function getLyric(id) {
const fetchLatest = () => {
return request({
url: '/lyric',
method: 'get',
params: {
id,
},
}).then(result => {
cacheLyric(id, result);
return result;
});
};
fetchLatest();
return getLyricFromCache(id).then(result => {
return result ?? fetchLatest();
});
}
/**
* 获取云盘歌曲内嵌歌词 * 说明 : 调用此接口 , 传入音乐 id 可获得云盘歌曲的内嵌歌词
* @param {number} songId - 音乐 id
* @param {number} userId - 用户 id
*/
export function getCloudLyric(songId, userId) {
const fetchLatest = () => {
return request({
url: '/api',
method: 'get',
params: {
uri: `/api/cloud/lyric/get`,
data: {
songId,
userId,
lv: '-1',
kv: '-1',
},
crypto: 'eapi',
},
}).then(result => {
cacheLyric(songId, result);
return result;
});
};
fetchLatest();
return getLyricFromCache(songId).then(result => {
return result ?? fetchLatest();
});
}
/**
* 新歌速递
* 说明 : 调用此接口 , 可获取新歌速递
* @param {number} type - 地区类型 id, 对应以下: 全部:0 华语:7 欧美:96 日本:8 韩国:16
*/
export function topSong(type) {
return request({
url: '/top/song',
method: 'get',
params: {
type,
},
});
}
/**
* 喜欢音乐
* 说明 : 调用此接口 , 传入音乐 id, 可喜欢该音乐
* - id - 歌曲 id
* - like - 默认为 true 即喜欢 , 若传 false, 则取消喜欢
* @param {Object} params
* @param {number} params.id
* @param {boolean=} [params.like]
*/
export function likeATrack(params) {
params.timestamp = new Date().getTime();
return request({
url: '/like',
method: 'get',
params,
});
}
/**
* 听歌打卡
* 说明 : 调用此接口 , 传入音乐 id, 来源 id,歌曲时间 time,更新听歌排行数据
* - id - 歌曲 id
* - sourceid - 歌单或专辑 id
* - time - 歌曲播放时间,单位为秒
* @param {Object} params
* @param {number} params.id
* @param {number} params.sourceid
* @param {number=} params.time
*/
export function scrobble(params) {
params.timestamp = new Date().getTime();
return request({
url: '/scrobble',
method: 'get',
params,
});
}