mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 03:12:51 +08:00
feat(music): let a member choose the shared login when their own has no VIP
The order was hardcoded: own account → assigned account → shared. That is the right default (privacy first), but it leaves one common case stuck: a member's own NetEase login is real but has no membership, while the admin's does. They linked their account, so playback keeps using theirs, and every paid track fails — with no way to say "just use the admin's for playback". Two things now: 1. An explicit choice — `user_music_prefs` (userId + platform), exposed as GET/PUT `/api/me/music/:platform/preference` and a radio in the account card. `preferShared: true` outranks EVERYTHING, including `alwaysPersonal` (someone who chose membership over their own recommendations wants the shared login for FM too). It stores a flag, not a cookie — flipping it back restores the personal account untouched. 2. An automatic fallback for people who never touch the switch. Only `getSongUrl` is wrapped: if the personal account returns `url: null` or throws, the same track is retried once on the shared login. Search, playlists and FM stay on the member's own account — that is the point of per-user logins, and widening the fallback would leak their library back to the admin's. Details: - The fallback lives in `resolveProviderForRequest`, so every playback entry point (web click, chat !play, auto-advance, playlists) gets it by construction, and the shared instance is never mutated. - `fallbackToShared: false` is available for callers that must stay strictly on the member's account; the default is on, because "my account has no VIP" is the complaint that produced this feature. - The status payload now carries `preferShared` and `featureEnabled`, so the UI can render the radio without a second round-trip and hide it when the admin has the feature off (PUT answers 409 in that case). 13 new tests: preference overrides own + assigned + alwaysPersonal, feature-off ignored, GET/PUT round-trip, per-platform isolation, stored cookie untouched, non-boolean rejected, 409 when disabled; fallback on null, on throw, keeps the personal result when it works, can be disabled, leaves the shared instance untouched.
This commit is contained in:
1 parent
1ef80be883
commit
d3eb6f2107
6 files changed
+544
-3
No files matched your search
@@ -49,6 +49,54 @@
|
||||
</template>
|
||||
</p>
|
||||
|
||||
<!-- 播放用哪个账号。只有真的持有个人/分配账号、且管理员开了这个功能时
|
||||
才有得选;否则本来就走共享账号。 -->
|
||||
<div
|
||||
v-if="status.featureEnabled && status.linked"
|
||||
class="account-choice"
|
||||
role="radiogroup"
|
||||
aria-label="播放使用哪个账号"
|
||||
>
|
||||
<div class="account-choice-title">播放使用哪个账号</div>
|
||||
|
||||
<div
|
||||
class="account-choice-option"
|
||||
:class="{ active: !status.preferShared }"
|
||||
role="radio"
|
||||
:aria-checked="!status.preferShared"
|
||||
tabindex="0"
|
||||
@click="setPreference(false)"
|
||||
@keydown.enter.prevent="setPreference(false)"
|
||||
@keydown.space.prevent="setPreference(false)"
|
||||
>
|
||||
<span class="account-choice-dot" :class="{ on: !status.preferShared }" />
|
||||
<span class="account-choice-text">
|
||||
<strong>我的账号</strong>
|
||||
<em>
|
||||
{{ status.source === 'granted' ? '管理员分配给我的那个' : '我自己扫码绑定的' }};
|
||||
播放不了时自动改用共享账号
|
||||
</em>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="account-choice-option"
|
||||
:class="{ active: status.preferShared }"
|
||||
role="radio"
|
||||
:aria-checked="status.preferShared"
|
||||
tabindex="0"
|
||||
@click="setPreference(true)"
|
||||
@keydown.enter.prevent="setPreference(true)"
|
||||
@keydown.space.prevent="setPreference(true)"
|
||||
>
|
||||
<span class="account-choice-dot" :class="{ on: status.preferShared }" />
|
||||
<span class="account-choice-text">
|
||||
<strong>共享账号(管理员的)</strong>
|
||||
<em>会员曲目可以完整播放,但播放记录会记到管理员账号名下</em>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="login-methods">
|
||||
<button
|
||||
v-if="status.supportsQrLogin"
|
||||
@@ -119,6 +167,8 @@ const status = reactive({
|
||||
source: 'none' as 'none' | 'personal' | 'granted',
|
||||
canUnlink: false,
|
||||
supportsQrLogin: false,
|
||||
preferShared: false,
|
||||
featureEnabled: false,
|
||||
});
|
||||
const qr = reactive({
|
||||
loading: false,
|
||||
@@ -146,6 +196,8 @@ async function refreshStatus() {
|
||||
status.source = res.data?.source ?? 'none';
|
||||
status.canUnlink = Boolean(res.data?.canUnlink);
|
||||
status.supportsQrLogin = Boolean(res.data?.supportsQrLogin);
|
||||
status.preferShared = Boolean(res.data?.preferShared);
|
||||
status.featureEnabled = Boolean(res.data?.featureEnabled);
|
||||
} catch {
|
||||
// Leave the last known state
|
||||
}
|
||||
@@ -209,6 +261,30 @@ async function pollQrStatus() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch which login playback uses.
|
||||
*
|
||||
* `true` = the bot's shared account (usually the admin's, usually the one with
|
||||
* a membership), `false` = my own / the assigned one. Nothing is unlinked, so
|
||||
* switching back restores the personal account exactly as it was.
|
||||
*/
|
||||
async function setPreference(preferShared: boolean) {
|
||||
if (preferShared === status.preferShared) return;
|
||||
error.value = '';
|
||||
const previous = status.preferShared;
|
||||
status.preferShared = preferShared; // optimistic — the radio must feel instant
|
||||
try {
|
||||
await axios.put(`${base()}/preference`, { preferShared });
|
||||
} catch (err: any) {
|
||||
status.preferShared = previous;
|
||||
const code = err?.response?.status;
|
||||
error.value =
|
||||
code === 409
|
||||
? '管理员未开启「每用户独立账号」,暂时无法切换'
|
||||
: (err?.response?.data?.error ?? '切换失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function unlink() {
|
||||
error.value = '';
|
||||
try {
|
||||
@@ -295,6 +371,88 @@ onUnmounted(stopPolling);
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.account-choice {
|
||||
margin: 12px 0 16px;
|
||||
padding: 12px;
|
||||
border: 1px solid var(--border-color, rgba(128, 128, 128, 0.2));
|
||||
border-radius: var(--radius-sm, 8px);
|
||||
}
|
||||
|
||||
.account-choice-title {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 8px;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.account-choice-option {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 10px;
|
||||
padding: 8px 10px;
|
||||
border-radius: var(--radius-sm, 8px);
|
||||
cursor: pointer;
|
||||
transition: background-color 0.15s ease;
|
||||
|
||||
& + & {
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
background: var(--hover-bg-strong, rgba(128, 128, 128, 0.08));
|
||||
}
|
||||
|
||||
&.active {
|
||||
background: var(--hover-bg-strong, rgba(128, 128, 128, 0.1));
|
||||
}
|
||||
|
||||
&:focus-visible {
|
||||
outline: 2px solid var(--color-primary);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
}
|
||||
|
||||
.account-choice-dot {
|
||||
flex: 0 0 auto;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
margin-top: 2px;
|
||||
border-radius: 50%;
|
||||
border: 2px solid var(--text-secondary, #888);
|
||||
position: relative;
|
||||
|
||||
&.on {
|
||||
border-color: var(--color-primary);
|
||||
|
||||
&::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
inset: 2px;
|
||||
border-radius: 50%;
|
||||
background: var(--color-primary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.account-choice-text {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
font-size: 13px;
|
||||
line-height: 1.45;
|
||||
|
||||
strong {
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
em {
|
||||
font-style: normal;
|
||||
color: var(--text-secondary);
|
||||
font-size: 12px;
|
||||
}
|
||||
}
|
||||
|
||||
.login-methods {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
|
||||
Reference in new issue
Block a user