dsh-cloudmusic-player 0.5.0 → 0.7.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +322 -0
- package/README.md +16 -9
- package/client.js +84 -9
- package/cordis.patch.yml +6 -0
- package/index.js +114 -11
- package/lib/backend.mjs +3 -2
- package/lib/channel.mjs +229 -0
- package/lib/elog.mjs +53 -1
- package/lib/helper.ps1 +55 -15
- package/lib/like.mjs +287 -0
- package/lib/player.mjs +199 -3
- package/lib/smtc-browser.mjs +304 -0
- package/lib/smtc-probe.ps1 +190 -0
- package/lib/web-backend.mjs +1444 -0
- package/lib/winrt-audio.mjs +185 -0
- package/lib/winrt-audio.ps1 +163 -0
- package/locale/en.json +6 -1
- package/locale/zh.json +6 -1
- package/package.json +3 -3
- package/test/channel.test.mjs +79 -0
- package/test/elog.test.mjs +155 -2
- package/test/like.test.mjs +632 -0
- package/test/smtc-browser.test.mjs +240 -0
- package/test/web-backend.test.mjs +645 -0
package/lib/helper.ps1
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
# 请求:{"seq":1,"cmd":"state"}
|
|
14
14
|
# 回复:{"type":"reply","seq":1,"ok":true,"cmd":"state","now":{...},"volume":{...}}
|
|
15
15
|
#
|
|
16
|
-
# 命令:ping state refresh play pause toggle next prev seek volume mute procs deep quit
|
|
16
|
+
# 命令:ping state refresh play pause toggle next prev seek volume mute setMode setLike procs deep quit
|
|
17
17
|
#
|
|
18
18
|
# 注意:本文件必须存为 UTF-8 **带 BOM**,否则 PowerShell 5.1 会按 ANSI
|
|
19
19
|
# 解码,中文注释可能被截断成非法字节。
|
|
@@ -25,7 +25,10 @@ param(
|
|
|
25
25
|
# 客户端自己的全局热键。SMTC 拿不到会话时靠它控制播放。
|
|
26
26
|
[string]$HotkeyPlayPause = 'Ctrl+Alt+P',
|
|
27
27
|
[string]$HotkeyNext = 'Ctrl+Alt+Right',
|
|
28
|
-
[string]$HotkeyPrev = 'Ctrl+Alt+Left'
|
|
28
|
+
[string]$HotkeyPrev = 'Ctrl+Alt+Left',
|
|
29
|
+
# 客户端「喜欢/取消喜欢」的全局热键(默认 love = Ctrl+Alt+L)。
|
|
30
|
+
# 与播放热键一样用 keybd_event 发键,不 Activate、不投 webcmd,因此不弹窗。
|
|
31
|
+
[string]$HotkeyLike = 'Ctrl+Alt+L'
|
|
29
32
|
)
|
|
30
33
|
|
|
31
34
|
$ErrorActionPreference = 'Stop'
|
|
@@ -2996,9 +2999,10 @@ function Get-Volume($procName) {
|
|
|
2996
2999
|
}
|
|
2997
3000
|
|
|
2998
3001
|
# 回读「实际写入的那一路」。SYS 车道写完却回 APP 读数,界面就会看着像弹回原值。
|
|
2999
|
-
function Get-VolumeAt([string]$scope) {
|
|
3002
|
+
function Get-VolumeAt([string]$scope, [string]$procName) {
|
|
3003
|
+
$target = if ($procName) { $procName } else { $ProcessName }
|
|
3000
3004
|
if ($scope -eq 'process') {
|
|
3001
|
-
$pv = Get-ProcessVolume $
|
|
3005
|
+
$pv = Get-ProcessVolume $target
|
|
3002
3006
|
if ($pv -ne $null) { return $pv }
|
|
3003
3007
|
return $null
|
|
3004
3008
|
}
|
|
@@ -3006,16 +3010,19 @@ function Get-VolumeAt([string]$scope) {
|
|
|
3006
3010
|
return $null
|
|
3007
3011
|
}
|
|
3008
3012
|
|
|
3009
|
-
function Set-Volume($level, $scope) {
|
|
3013
|
+
function Set-Volume($level, $scope, [string]$procName, [bool]$noFallback = $false) {
|
|
3010
3014
|
if ($level -lt 0) { $level = 0 }
|
|
3011
3015
|
if ($level -gt 1) { $level = 1 }
|
|
3012
|
-
if ($
|
|
3016
|
+
$target = if ($procName) { $procName } else { $ProcessName }
|
|
3017
|
+
if ($scope -eq 'process' -and $target) {
|
|
3013
3018
|
try {
|
|
3014
|
-
foreach ($p in [NcmCore.Api]::Pids($
|
|
3019
|
+
foreach ($p in [NcmCore.Api]::Pids($target)) {
|
|
3015
3020
|
if ([NcmCore.Api]::SetProcVol($p, $level)) { return 'process' }
|
|
3016
3021
|
}
|
|
3017
3022
|
} catch { }
|
|
3018
|
-
#
|
|
3023
|
+
# SMTC 场景明确指定了出声浏览器进程:找不到它的音频会话也绝不退系统音量,
|
|
3024
|
+
# 否则用户拖 APP 音量就会把 SYS 一起拖走。原生通道(未指定 procName)保持旧行为。
|
|
3025
|
+
if ($noFallback) { return '' }
|
|
3019
3026
|
if ([NcmCore.Api]::SetSysVol($level)) { return 'system' }
|
|
3020
3027
|
return ''
|
|
3021
3028
|
}
|
|
@@ -3023,13 +3030,15 @@ function Set-Volume($level, $scope) {
|
|
|
3023
3030
|
return ''
|
|
3024
3031
|
}
|
|
3025
3032
|
|
|
3026
|
-
function Set-Mute($mute, $scope) {
|
|
3027
|
-
if ($
|
|
3033
|
+
function Set-Mute($mute, $scope, [string]$procName, [bool]$noFallback = $false) {
|
|
3034
|
+
$target = if ($procName) { $procName } else { $ProcessName }
|
|
3035
|
+
if ($scope -eq 'process' -and $target) {
|
|
3028
3036
|
try {
|
|
3029
|
-
foreach ($p in [NcmCore.Api]::Pids($
|
|
3037
|
+
foreach ($p in [NcmCore.Api]::Pids($target)) {
|
|
3030
3038
|
if ([NcmCore.Api]::SetProcMute($p, $mute)) { return 'process' }
|
|
3031
3039
|
}
|
|
3032
3040
|
} catch { }
|
|
3041
|
+
if ($noFallback) { return '' }
|
|
3033
3042
|
if ([NcmCore.Api]::SetSysMute($mute)) { return 'system' }
|
|
3034
3043
|
return ''
|
|
3035
3044
|
}
|
|
@@ -3287,16 +3296,45 @@ while ($running) {
|
|
|
3287
3296
|
}
|
|
3288
3297
|
}
|
|
3289
3298
|
|
|
3299
|
+
# setLike:切换当前曲目的「喜欢/取消喜欢」。
|
|
3300
|
+
# 客户端没有 like 的 webcmd,也不给外部直接调用 setLike 的入口;
|
|
3301
|
+
# 但它注册了全局 love 热键(默认 Ctrl+Alt+L),按下后由客户端自己
|
|
3302
|
+
# toggle 红心并写服务端。这里模拟该热键:keybd_event 发键,不 Activate,
|
|
3303
|
+
# 所以客户端窗口不会被唤起或抢焦点(与播放热键同一条路径)。
|
|
3304
|
+
'setLike' {
|
|
3305
|
+
$t = Get-NativeTarget
|
|
3306
|
+
if ($t -eq $null) {
|
|
3307
|
+
$reply.ok = $false
|
|
3308
|
+
$reply.channel = ''
|
|
3309
|
+
$reply.error = 'no-client'
|
|
3310
|
+
$reply.detail = '网易云客户端没在运行'
|
|
3311
|
+
} else {
|
|
3312
|
+
try {
|
|
3313
|
+
[void][NcmWin.Win]::Hotkey($HotkeyLike)
|
|
3314
|
+
Start-Sleep -Milliseconds 240
|
|
3315
|
+
[NcmWin.Win]::Invalidate()
|
|
3316
|
+
$reply.ok = $true
|
|
3317
|
+
$reply.channel = 'native'
|
|
3318
|
+
} catch {
|
|
3319
|
+
$reply.ok = $false
|
|
3320
|
+
$reply.channel = 'native'
|
|
3321
|
+
$reply.error = $_.Exception.Message
|
|
3322
|
+
}
|
|
3323
|
+
}
|
|
3324
|
+
}
|
|
3325
|
+
|
|
3290
3326
|
'volume' {
|
|
3291
3327
|
$scope = [string]$req.scope
|
|
3292
3328
|
if ([string]::IsNullOrEmpty($scope)) { $scope = 'process' }
|
|
3293
|
-
$
|
|
3329
|
+
$volProc = [string]$req.procName
|
|
3330
|
+
$noFallback = [bool]$req.noFallback
|
|
3331
|
+
$applied = Set-Volume ([double]$req.level) $scope $volProc $noFallback
|
|
3294
3332
|
$reply.ok = ($applied -ne '')
|
|
3295
3333
|
$reply.scope = $applied
|
|
3296
3334
|
# 回读实际写入的那一路:写的是系统端点就回系统读数。
|
|
3297
3335
|
# 以前这里恒回 Get-Volume(APP 优先),SYS 车道拖完就显示 APP 的旧值。
|
|
3298
3336
|
$at = $null
|
|
3299
|
-
if ($applied -ne '') { $at = Get-VolumeAt $applied }
|
|
3337
|
+
if ($applied -ne '') { $at = Get-VolumeAt $applied $volProc }
|
|
3300
3338
|
if ($at -eq $null) { $at = Get-Volume $ProcessName }
|
|
3301
3339
|
$reply.volume = $at
|
|
3302
3340
|
$reply.systemVolume = Get-SystemVolume
|
|
@@ -3304,11 +3342,13 @@ while ($running) {
|
|
|
3304
3342
|
'mute' {
|
|
3305
3343
|
$scope = [string]$req.scope
|
|
3306
3344
|
if ([string]::IsNullOrEmpty($scope)) { $scope = 'process' }
|
|
3307
|
-
$
|
|
3345
|
+
$volProc = [string]$req.procName
|
|
3346
|
+
$noFallback = [bool]$req.noFallback
|
|
3347
|
+
$applied = Set-Mute ([bool]$req.muted) $scope $volProc $noFallback
|
|
3308
3348
|
$reply.ok = ($applied -ne '')
|
|
3309
3349
|
$reply.scope = $applied
|
|
3310
3350
|
$at = $null
|
|
3311
|
-
if ($applied -ne '') { $at = Get-VolumeAt $applied }
|
|
3351
|
+
if ($applied -ne '') { $at = Get-VolumeAt $applied $volProc }
|
|
3312
3352
|
if ($at -eq $null) { $at = Get-Volume $ProcessName }
|
|
3313
3353
|
$reply.volume = $at
|
|
3314
3354
|
$reply.systemVolume = Get-SystemVolume
|
package/lib/like.mjs
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/like.mjs —— "我喜欢的音乐"(红心)
|
|
3
|
+
* ============================================================================
|
|
4
|
+
* 为什么不走 webcmd:客户端 3.1.43 的 onWebMessage 命令表没有 like,
|
|
5
|
+
* 而且任何 orpheus:// 消息都会触发 showWindow 抢焦点(helper.ps1:1425-1428)。
|
|
6
|
+
* 这里直接用 Node fetch 打网易云 Web API,凭证从客户端 CEF profile 读
|
|
7
|
+
* (lib/cookies.mjs,纯本地文件读),全程不跟客户端进程通信、不弹窗。
|
|
8
|
+
*
|
|
9
|
+
* 跟日推(lib/daily.mjs)同源:同一份 cookie、同一个 API base 风格。
|
|
10
|
+
* "喜欢"状态按 songId 缓存 60 秒,切歌按新 id 重新查,不串状态。
|
|
11
|
+
* ============================================================================
|
|
12
|
+
*/
|
|
13
|
+
import { loadCookieHeader } from './cookies.mjs'
|
|
14
|
+
|
|
15
|
+
const UA = 'Mozilla/5.0 (Windows NT 10.0; WOW64) Chrome/91.0.4472.164 NeteaseMusicDesktop/3.1.43.205627'
|
|
16
|
+
const HTTP_TIMEOUT_MS = 8000
|
|
17
|
+
const CACHE_TTL_MS = 60 * 1000
|
|
18
|
+
|
|
19
|
+
/** 默认接口基址,和 dailyApiBase / lyricsApiBase 一个风格 */
|
|
20
|
+
export const DEFAULT_API_BASE = 'https://music.163.com/api'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 网易云用 code=301/401 同时表达两件事:「需要登录」和「下架歌曲无法收藏」。
|
|
24
|
+
* 只按 code 判会把下架歌误报成 like-needs-login,前端再硬映射成「请先登录客户端」,
|
|
25
|
+
* 网页版根本不需要客户端登录,文案完全错。这里按服务端文案分流:
|
|
26
|
+
* 含下架/版权类关键词 → like-unavailable-song(新错误码,前端给专门文案);
|
|
27
|
+
* 其余才是真的需要登录 → like-needs-login。非 301/401 一律 like-failed。
|
|
28
|
+
*/
|
|
29
|
+
export function classifyLikeError(code, serverMsg = '') {
|
|
30
|
+
const needLogin = Number(code) === 301 || Number(code) === 401
|
|
31
|
+
if (!needLogin) return 'like-failed'
|
|
32
|
+
const msg = String(serverMsg || '')
|
|
33
|
+
if (/下架|下线|版权|无版权|无法收藏|已删除|不可用|unavailable|copyright/i.test(msg)) {
|
|
34
|
+
return 'like-unavailable-song'
|
|
35
|
+
}
|
|
36
|
+
return 'like-needs-login'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 从 cookie 串里取 __csrf(网易云 form 接口要 csrf_token) */
|
|
40
|
+
export function csrfOf(cookie) {
|
|
41
|
+
const m = /(?:^|;\s*)__csrf=([^;]+)/.exec(cookie || '')
|
|
42
|
+
return m ? decodeURIComponent(m[1].trim()) : ''
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** form-urlencoded POST;测试可通过 deps.fetchJson 整体替换 */
|
|
46
|
+
async function postForm(url, fields, { cookie = '', timeoutMs = HTTP_TIMEOUT_MS } = {}) {
|
|
47
|
+
const ctrl = new AbortController()
|
|
48
|
+
const timer = setTimeout(() => ctrl.abort(), timeoutMs)
|
|
49
|
+
try {
|
|
50
|
+
const headers = {
|
|
51
|
+
'user-agent': UA,
|
|
52
|
+
accept: 'application/json,text/plain,*/*',
|
|
53
|
+
referer: 'https://music.163.com/',
|
|
54
|
+
'content-type': 'application/x-www-form-urlencoded',
|
|
55
|
+
}
|
|
56
|
+
if (cookie) headers.cookie = cookie
|
|
57
|
+
const res = await fetch(url, {
|
|
58
|
+
method: 'POST',
|
|
59
|
+
signal: ctrl.signal,
|
|
60
|
+
headers,
|
|
61
|
+
body: new URLSearchParams(fields).toString(),
|
|
62
|
+
})
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
const err = new Error(`HTTP ${res.status}`)
|
|
65
|
+
err.status = res.status
|
|
66
|
+
throw err
|
|
67
|
+
}
|
|
68
|
+
const text = await res.text()
|
|
69
|
+
if (!text || !text.trim()) throw new Error('empty body')
|
|
70
|
+
return JSON.parse(text)
|
|
71
|
+
} finally {
|
|
72
|
+
clearTimeout(timer)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function toBool(v) {
|
|
77
|
+
if (typeof v === 'boolean') return v
|
|
78
|
+
if (v === 1 || v === '1' || v === 'true') return true
|
|
79
|
+
if (v === 0 || v === '0' || v === 'false') return false
|
|
80
|
+
return null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 兼容几种返回形状:
|
|
85
|
+
* - /api/song/like/check(主用):{ ids: [已喜欢的id...], code: 200 }
|
|
86
|
+
* —— 只返回已喜欢的 id;传入的 songId 不在 ids 里即为未喜欢(false)。
|
|
87
|
+
* - 旧 /api/song/like/get 及第三方实现可能返回:
|
|
88
|
+
* { songs: { "123": true, "456": false } } / { data: { "123": true } }
|
|
89
|
+
* { songs: [{ id: 123, liked: true }] } / { data: [{ id: 123, starred: true }] }
|
|
90
|
+
* 返回 true/false;读不出来返回 null,交给上层报错。
|
|
91
|
+
*/
|
|
92
|
+
export function normalizeLiked(body, songId) {
|
|
93
|
+
const id = String(songId)
|
|
94
|
+
// check 语义:ids 是「已喜欢 id 列表」,命中 true,不命中 false(缺失即未喜欢)。
|
|
95
|
+
if (Array.isArray(body?.ids)) {
|
|
96
|
+
return body.ids.some((v) => String(v) === id)
|
|
97
|
+
}
|
|
98
|
+
const sources = [body?.songs, body?.data, body?.likedMap, body]
|
|
99
|
+
for (const src of sources) {
|
|
100
|
+
if (src == null) continue
|
|
101
|
+
if (typeof src === 'boolean') return src
|
|
102
|
+
if (Array.isArray(src)) {
|
|
103
|
+
for (const item of src) {
|
|
104
|
+
if (String(item?.id ?? item?.songId ?? '') === id) {
|
|
105
|
+
const v = toBool(item?.liked ?? item?.starred ?? item?.fav ?? item?.value)
|
|
106
|
+
if (v != null) return v
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
111
|
+
if (typeof src === 'object') {
|
|
112
|
+
const v = toBool(src[id])
|
|
113
|
+
if (v != null) return v
|
|
114
|
+
// 单曲查询有时直接返回 { liked: true } / { starred: false },不带 id 键
|
|
115
|
+
if (src === body) {
|
|
116
|
+
const top = toBool(src.liked ?? src.starred ?? src.fav ?? src.value)
|
|
117
|
+
if (top != null) return top
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 喜欢服务。依赖注入点都是为了单测不用真解 cookie / 打网络。 */
|
|
125
|
+
export class LikeService {
|
|
126
|
+
constructor(deps = {}) {
|
|
127
|
+
this.getConfig = typeof deps.getConfig === 'function' ? deps.getConfig : () => ({})
|
|
128
|
+
this.log = typeof deps.log === 'function' ? deps.log : () => {}
|
|
129
|
+
this.cookieLoader = deps.cookieLoader || loadCookieHeader
|
|
130
|
+
this.fetchJson = deps.fetchJson || postForm
|
|
131
|
+
this.now = typeof deps.now === 'function' ? deps.now : () => Date.now()
|
|
132
|
+
/** @type {Map<string,{liked:boolean,at:number}>} */
|
|
133
|
+
this.cache = new Map()
|
|
134
|
+
/** @type {Map<string,Promise>} 同 id 的状态查询在途合并 */
|
|
135
|
+
this.lookupInflight = new Map()
|
|
136
|
+
/** @type {Map<string,Promise>} 同 id+目标的写入在途合并 */
|
|
137
|
+
this.setInflight = new Map()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
invalidate() {
|
|
141
|
+
this.cache.clear()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
forget(songId) {
|
|
145
|
+
this.cache.delete(String(songId))
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#opts() {
|
|
149
|
+
const cfg = this.getConfig() || {}
|
|
150
|
+
return {
|
|
151
|
+
apiBase: String(cfg.likeApiBase || cfg.dailyApiBase || DEFAULT_API_BASE).replace(/\/+$/, ''),
|
|
152
|
+
profileDir: String(cfg.dailyProfileDir || ''),
|
|
153
|
+
timeoutMs: Number(cfg.requestTimeoutMs) > 0 ? Number(cfg.requestTimeoutMs) + 4000 : HTTP_TIMEOUT_MS,
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 读 cookie;任何失败都归一化成 { error, message },不抛 */
|
|
158
|
+
async #cookie(opts) {
|
|
159
|
+
let got
|
|
160
|
+
try {
|
|
161
|
+
got = await this.cookieLoader({ profileDir: opts.profileDir, timeoutMs: opts.timeoutMs, log: this.log })
|
|
162
|
+
} catch (err) {
|
|
163
|
+
return { error: 'like-no-cookie', message: '读不到登录凭证,请先在网易云登录后再试(客户端或网页版)', detail: String(err?.message || err) }
|
|
164
|
+
}
|
|
165
|
+
if (got?.ok && got.cookie) return { cookie: got.cookie }
|
|
166
|
+
if (got?.error === 'cookie-unsupported') {
|
|
167
|
+
return { error: 'like-unavailable', message: '这个功能只支持 Windows 上的网易云客户端', detail: got.detail || '' }
|
|
168
|
+
}
|
|
169
|
+
return { error: 'like-needs-login', message: '请先在网易云登录后再试(客户端或网页版)', detail: got?.detail || '' }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 查当前歌曲是否已喜欢。
|
|
174
|
+
* @param {string|number} songId
|
|
175
|
+
* @param {{force?:boolean}} [opts]
|
|
176
|
+
* @returns {Promise<{ok:true,liked:boolean,cached:boolean}|{ok:false,error:string,message:string,detail?:string}>}
|
|
177
|
+
*/
|
|
178
|
+
async isLiked(songId, opts = {}) {
|
|
179
|
+
const id = String(songId || '')
|
|
180
|
+
if (!id || id === '0') return { ok: false, error: 'like-no-song', message: '播放中才能喜欢' }
|
|
181
|
+
const hit = this.cache.get(id)
|
|
182
|
+
if (!opts.force && hit && this.now() - hit.at < CACHE_TTL_MS) {
|
|
183
|
+
return { ok: true, liked: hit.liked, cached: true }
|
|
184
|
+
}
|
|
185
|
+
if (this.lookupInflight.has(id)) return await this.lookupInflight.get(id)
|
|
186
|
+
const task = this.#doIsLiked(id)
|
|
187
|
+
this.lookupInflight.set(id, task)
|
|
188
|
+
try {
|
|
189
|
+
return await task
|
|
190
|
+
} finally {
|
|
191
|
+
this.lookupInflight.delete(id)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async #doIsLiked(id) {
|
|
196
|
+
const opts = this.#opts()
|
|
197
|
+
const c = await this.#cookie(opts)
|
|
198
|
+
if (c.error) return { ok: false, error: c.error, message: c.message, detail: c.detail }
|
|
199
|
+
let body
|
|
200
|
+
try {
|
|
201
|
+
// /song/like/get 会忽略 ids 返回全量列表,无法单曲判定;
|
|
202
|
+
// /song/like/check 才是「传入 trackIds,只返回其中已喜欢的 id」。
|
|
203
|
+
// id 保持字符串原样,避免大 id 经 Number 丢精度。
|
|
204
|
+
body = await this.fetchJson(
|
|
205
|
+
`${opts.apiBase}/song/like/check`,
|
|
206
|
+
{ trackIds: JSON.stringify([id]), csrf_token: csrfOf(c.cookie) },
|
|
207
|
+
{ cookie: c.cookie, timeoutMs: opts.timeoutMs },
|
|
208
|
+
)
|
|
209
|
+
} catch (err) {
|
|
210
|
+
const status = Number(err?.status || 0)
|
|
211
|
+
if (status === 401 || status === 403) {
|
|
212
|
+
return { ok: false, error: 'like-needs-login', message: '请先在网易云登录后再试(客户端或网页版)', detail: String(err?.message || err) }
|
|
213
|
+
}
|
|
214
|
+
return { ok: false, error: 'like-network', message: '读取喜欢状态失败,检查网络后重试', detail: String(err?.message || err) }
|
|
215
|
+
}
|
|
216
|
+
if (body?.code != null && Number(body.code) !== 200) {
|
|
217
|
+
// 服务端会带更准的业务文案(如「下架歌曲无法收藏」「需要登录」),优先透传,
|
|
218
|
+
// 并按文案分流:下架/版权类不再误判成 like-needs-login(前端会硬映射成「请先登录客户端」)。
|
|
219
|
+
const serverMsg = String(body.message || body.msg || '').trim()
|
|
220
|
+
const error = classifyLikeError(body.code, serverMsg)
|
|
221
|
+
return {
|
|
222
|
+
ok: false,
|
|
223
|
+
error,
|
|
224
|
+
message: serverMsg || (error === 'like-needs-login' ? '请先在网易云登录后再试' : '读不到这首歌的喜欢状态'),
|
|
225
|
+
detail: `code=${body.code}${serverMsg ? ' ' + serverMsg : ''}`,
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const liked = normalizeLiked(body, id)
|
|
229
|
+
if (liked == null) return { ok: false, error: 'like-failed', message: '读不到这首歌的喜欢状态', detail: '' }
|
|
230
|
+
this.cache.set(id, { liked, at: this.now() })
|
|
231
|
+
return { ok: true, liked, cached: false }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 设置喜欢状态(true=收藏到"我喜欢的音乐",false=取消)。
|
|
236
|
+
* 成功后写缓存;失败时不动缓存,下一次 state 轮询会纠正 UI。
|
|
237
|
+
*/
|
|
238
|
+
async setLiked(songId, liked) {
|
|
239
|
+
const id = String(songId || '')
|
|
240
|
+
if (!id || id === '0') return { ok: false, error: 'like-no-song', message: '播放中才能喜欢' }
|
|
241
|
+
const want = !!liked
|
|
242
|
+
const key = `${id}:${want}`
|
|
243
|
+
if (this.setInflight.has(key)) return await this.setInflight.get(key)
|
|
244
|
+
|
|
245
|
+
const task = this.#doSetLiked(id, want)
|
|
246
|
+
this.setInflight.set(key, task)
|
|
247
|
+
try {
|
|
248
|
+
return await task
|
|
249
|
+
} finally {
|
|
250
|
+
this.setInflight.delete(key)
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async #doSetLiked(id, want) {
|
|
255
|
+
const opts = this.#opts()
|
|
256
|
+
const c = await this.#cookie(opts)
|
|
257
|
+
if (c.error) return { ok: false, error: c.error, message: c.message, detail: c.detail }
|
|
258
|
+
let body
|
|
259
|
+
try {
|
|
260
|
+
// 明文 api 通道的参数名是 trackId(songid 会返回 code 400 请求参数错误);
|
|
261
|
+
// userid 可省略,like 用字符串 'true'/'false'。
|
|
262
|
+
body = await this.fetchJson(
|
|
263
|
+
`${opts.apiBase}/song/like`,
|
|
264
|
+
{ trackId: id, like: want ? 'true' : 'false', csrf_token: csrfOf(c.cookie) },
|
|
265
|
+
{ cookie: c.cookie, timeoutMs: opts.timeoutMs },
|
|
266
|
+
)
|
|
267
|
+
} catch (err) {
|
|
268
|
+
const status = Number(err?.status || 0)
|
|
269
|
+
if (status === 401 || status === 403) {
|
|
270
|
+
return { ok: false, error: 'like-needs-login', message: '请先在网易云登录后再试(客户端或网页版)', detail: String(err?.message || err) }
|
|
271
|
+
}
|
|
272
|
+
return { ok: false, error: 'like-network', message: '操作失败,检查网络后重试', detail: String(err?.message || err) }
|
|
273
|
+
}
|
|
274
|
+
if (body?.code != null && Number(body.code) !== 200) {
|
|
275
|
+
const serverMsg = String(body.message || body.msg || '').trim()
|
|
276
|
+
const error = classifyLikeError(body.code, serverMsg)
|
|
277
|
+
return {
|
|
278
|
+
ok: false,
|
|
279
|
+
error,
|
|
280
|
+
message: serverMsg || (error === 'like-needs-login' ? '请先在网易云登录后再试' : '操作失败,请重试'),
|
|
281
|
+
detail: `code=${body.code}${serverMsg ? ' ' + serverMsg : ''}`,
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
this.cache.set(id, { liked: want, at: this.now() })
|
|
285
|
+
return { ok: true, liked: want }
|
|
286
|
+
}
|
|
287
|
+
}
|