仰望星辰工作室

starmusic-mobile

starmusic-mobile/ pages/account/account.vue 21.0 KB · 720 行 原始文件
1<template>
2 <view :class="['page', tvCls]" :style="themeStyle">
3 <!-- 自定义背景图(绘制在页面内容之下) -->
4 <view v-if="bgStyle" class="bg-image" :style="bgStyle" />
5 <tv-nav />
6 <scroll-view scroll-y class="page-scroll">
7
8 <!-- ============ 未登录:登录 / 注册 ============ -->
9 <view v-if="!logged" class="section">
10 <view class="card">
11 <view class="tab-bar">
12 <view class="tab d-press-sm" :class="{ active: mode === 'login' }" @click="switchMode('login')">
13 <text>登录</text>
14 </view>
15 <view class="tab d-press-sm" :class="{ active: mode === 'register' }" @click="switchMode('register')">
16 <text>注册</text>
17 </view>
18 </view>
19
20 <view class="form">
21 <view class="field">
22 <text class="field-label">用户名</text>
23 <input
24 v-model="form.username"
25 class="field-input"
26 placeholder="2-16 位中文、字母、数字或下划线"
27 placeholder-style="color:var(--text-muted, #c0c4cc)"
28 :maxlength="16"
29 />
30 </view>
31 <view class="field">
32 <text class="field-label">密码</text>
33 <input
34 v-model="form.password"
35 class="field-input"
36 :password="true"
37 placeholder="至少 6 位"
38 placeholder-style="color:var(--text-muted, #c0c4cc)"
39 :maxlength="64"
40 />
41 </view>
42 <view v-if="mode === 'register'" class="field">
43 <text class="field-label">确认密码</text>
44 <input
45 v-model="form.password2"
46 class="field-input"
47 :password="true"
48 placeholder="再次输入密码"
49 placeholder-style="color:var(--text-muted, #c0c4cc)"
50 :maxlength="64"
51 />
52 </view>
53 <!-- 注册验证码(防批量注册) -->
54 <view v-if="mode === 'register'" class="field">
55 <text class="field-label">验证码</text>
56 <view class="captcha-row">
57 <view class="captcha-box d-press-sm" @click="refreshCaptcha">
58 <text class="captcha-question">{{ captcha.question || '点击获取' }}</text>
59 </view>
60 <input
61 v-model="form.captchaAnswer"
62 class="field-input captcha-input"
63 type="number"
64 placeholder="答案"
65 placeholder-style="color:var(--text-muted, #c0c4cc)"
66 />
67 </view>
68 <text class="field-tip">点击算式可刷新验证码</text>
69 </view>
70
71 <button class="btn-primary d-press" :loading="submitting" @click="handleSubmit">
72 {{ mode === 'login' ? '登 录' : '注 册' }}
73 </button>
74 <text class="form-tip">登录后可将歌单与音源备份到云端,换手机也能一键找回</text>
75 </view>
76 </view>
77 </view>
78
79 <!-- ============ 已登录 ============ -->
80 <template v-else>
81 <!-- 账号信息 -->
82 <view class="section">
83 <view class="section-title">账号</view>
84 <view class="card">
85 <view class="account-head">
86 <view class="avatar">
87 <text>{{ avatarChar }}</text>
88 </view>
89 <view class="account-info">
90 <text class="account-name">{{ account.username }}</text>
91 <text class="account-sub">UID {{ account.user_id || '—' }} · 注册于 {{ formatTime(account.created_at) }}</text>
92 </view>
93 </view>
94 </view>
95 </view>
96
97 <!-- 云同步 -->
98 <view class="section">
99 <view class="section-title">云同步</view>
100 <view class="card">
101 <view class="cell">
102 <text class="cell-label">最近备份</text>
103 <text class="cell-value">{{ account.sync_time ? formatRelativeTime(account.sync_time) : '尚未备份' }}</text>
104 </view>
105 <view class="cell">
106 <text class="cell-label">本地数据</text>
107 <text class="cell-value">{{ localStatsText }}</text>
108 </view>
109 <view class="cell">
110 <view class="cell-main">
111 <text class="cell-label">自动备份</text>
112 <text class="cell-sub">歌单或音源发生变化时自动上传云端</text>
113 </view>
114 <switch :checked="autoBackup" :color="themeColor" @change="handleAutoBackupToggle" />
115 </view>
116 <view class="cell column">
117 <text class="cell-label">备份到云端</text>
118 <text class="cell-desc">上传本地歌单(收藏、试听)与全部自定义音源,覆盖云端旧备份</text>
119 <button class="btn-primary d-press" :loading="working === 'backup'" @click="handleBackup">
120 立即备份
121 </button>
122 </view>
123 <view class="cell column">
124 <text class="cell-label">从云端恢复</text>
125 <text class="cell-desc">合并云端歌单到本地列表,导入缺失的音源(本地数据不会被删除)</text>
126 <button class="btn-plain d-press" :loading="working === 'restore'" @click="handleRestore">
127 恢复数据
128 </button>
129 </view>
130 </view>
131 </view>
132
133 <!-- 账号管理 -->
134 <view class="section">
135 <view class="section-title">账号管理</view>
136 <view class="card">
137 <view class="cell d-press" @click="showPwdForm = !showPwdForm">
138 <text class="cell-label">修改密码</text>
139 <lx-icon name="arrow" :size="32" color="#c0c4cc" />
140 </view>
141 <view v-if="showPwdForm" class="pwd-form">
142 <view class="field">
143 <text class="field-label">原密码</text>
144 <input
145 v-model="pwdForm.old"
146 class="field-input"
147 :password="true"
148 placeholder="请输入原密码"
149 placeholder-style="color:var(--text-muted, #c0c4cc)"
150 :maxlength="64"
151 />
152 </view>
153 <view class="field">
154 <text class="field-label">新密码</text>
155 <input
156 v-model="pwdForm.new1"
157 class="field-input"
158 :password="true"
159 placeholder="至少 6 位"
160 placeholder-style="color:var(--text-muted, #c0c4cc)"
161 :maxlength="64"
162 />
163 </view>
164 <view class="field">
165 <text class="field-label">确认新密码</text>
166 <input
167 v-model="pwdForm.new2"
168 class="field-input"
169 :password="true"
170 placeholder="再次输入新密码"
171 placeholder-style="color:var(--text-muted, #c0c4cc)"
172 :maxlength="64"
173 />
174 </view>
175 <button class="btn-primary d-press" :loading="pwdSubmitting" @click="handleChangePwd">
176 确认修改
177 </button>
178 </view>
179 <view class="cell d-press" @click="handleLogout">
180 <text class="cell-label danger">退出登录</text>
181 </view>
182 </view>
183 </view>
184 </template>
185 </scroll-view>
186 </view>
187</template>
188
189<script>
190import LxIcon from '../../components/lx-icon/lx-icon.vue'
191import event from '../../common/event.js'
192import { deviceClass } from '../../common/tv.js'
193import { getList, LIST_IDS } from '../../common/playlist.js'
194import { getUserApiList } from '../../common/data.js'
195import {
196 getLocalAccount,
197 isLoggedIn,
198 getCaptcha,
199 register,
200 login,
201 logout,
202 fetchProfile,
203 changePassword,
204 backupToCloud,
205 restoreFromCloud,
206 isAutoBackupEnabled,
207 setAutoBackup,
208} from '../../common/account.js'
209
210export default {
211 components: { LxIcon },
212 data() {
213 return {
214 mode: 'login', // login | register
215 form: { username: '', password: '', password2: '', captchaAnswer: '' },
216 captcha: { id: '', question: '' },
217 submitting: false,
218 logged: false,
219 account: {},
220 working: '', // backup | restore
221 /** 自动备份开关状态 */
222 autoBackup: false,
223 showPwdForm: false,
224 pwdForm: { old: '', new1: '', new2: '' },
225 pwdSubmitting: false,
226 /** 本地数据统计(收藏/试听/音源数量) */
227 localStats: { love: 0, default: 0, sources: 0 },
228 // 遥控设备端 UI 类名(手机端为空,零影响)
229 tvCls: '',
230 }
231 },
232 computed: {
233 avatarChar() {
234 return (this.account.username || '?').slice(0, 1).toUpperCase()
235 },
236 /** 本地数据统计文案 */
237 localStatsText() {
238 const { love, default: def, sources } = this.localStats
239 return `收藏 ${love} 首 · 试听 ${def} 首 · 音源 ${sources} 个`
240 },
241 },
242 onLoad() {
243 this.tvCls = deviceClass()
244 this.refreshLocal()
245 // 登录态变化(含 token 失效自动清理)时刷新页面状态
246 this._offAccount = event.on('accountChanged', () => this.refreshLocal())
247 // 自动备份完成后刷新「最近备份」时间
248 this._offAutoBackup = event.on('autoBackupDone', (ok) => {
249 if (ok) this.refreshLocal()
250 })
251 },
252 onShow() {
253 this.refreshLocal()
254 this.refreshLocalStats()
255 // 静默刷新备份时间等信息;token 失效时会在 request 内清理登录态并广播
256 if (isLoggedIn()) {
257 fetchProfile().catch(() => {})
258 }
259 },
260 onUnload() {
261 if (this._offAccount) this._offAccount()
262 if (this._offAutoBackup) this._offAutoBackup()
263 },
264 methods: {
265 refreshLocal() {
266 this.logged = isLoggedIn()
267 this.account = getLocalAccount() || {}
268 this.autoBackup = isAutoBackupEnabled()
269 },
270 /** 刷新本地数据统计(备份前让用户知道会备份什么) */
271 refreshLocalStats() {
272 this.localStats = {
273 love: getList(LIST_IDS.LOVE).length,
274 default: getList(LIST_IDS.DEFAULT).length,
275 sources: getUserApiList().length,
276 }
277 },
278 switchMode(mode) {
279 if (this.mode === mode) return
280 this.mode = mode
281 this.form = { username: '', password: '', password2: '', captchaAnswer: '' }
282 if (mode === 'register' && !this.captcha.id) this.refreshCaptcha()
283 },
284 async refreshCaptcha() {
285 try {
286 const data = await getCaptcha()
287 this.captcha = { id: data.captcha_id, question: data.question }
288 this.form.captchaAnswer = ''
289 } catch (e) {
290 uni.showToast({ title: e.message || '获取验证码失败', icon: 'none' })
291 }
292 },
293 async handleSubmit() {
294 if (this.submitting) return
295 const { username, password, password2, captchaAnswer } = this.form
296 if (!username || !password) {
297 uni.showToast({ title: '请输入用户名和密码', icon: 'none' })
298 return
299 }
300 if (this.mode === 'register') {
301 if (password !== password2) {
302 uni.showToast({ title: '两次输入的密码不一致', icon: 'none' })
303 return
304 }
305 if (!this.captcha.id) {
306 uni.showToast({ title: '请先获取验证码', icon: 'none' })
307 return
308 }
309 if (captchaAnswer === '') {
310 uni.showToast({ title: '请输入验证码答案', icon: 'none' })
311 return
312 }
313 }
314 this.submitting = true
315 try {
316 if (this.mode === 'login') {
317 await login({ username, password })
318 uni.showToast({ title: '登录成功', icon: 'none' })
319 } else {
320 await register({
321 username,
322 password,
323 captchaId: this.captcha.id,
324 captchaAnswer,
325 })
326 uni.showToast({ title: '注册成功,已自动登录', icon: 'none' })
327 }
328 this.form = { username: '', password: '', password2: '', captchaAnswer: '' }
329 this.captcha = { id: '', question: '' }
330 this.refreshLocal()
331 } catch (e) {
332 uni.showToast({ title: e.message || '操作失败', icon: 'none' })
333 // 注册失败(含验证码错误)后刷新验证码,旧的已一次性消费
334 if (this.mode === 'register') this.refreshCaptcha()
335 } finally {
336 this.submitting = false
337 }
338 },
339 async handleBackup() {
340 if (this.working) return
341 this.working = 'backup'
342 uni.showLoading({ title: '备份中...', mask: true })
343 try {
344 const res = await backupToCloud()
345 uni.hideLoading()
346 uni.showToast({ title: `已备份 ${res.playlistCount} 首歌、${res.sourcesCount} 个音源`, icon: 'none' })
347 this.refreshLocal()
348 } catch (e) {
349 uni.hideLoading()
350 uni.showToast({ title: e.message || '备份失败', icon: 'none' })
351 } finally {
352 this.working = ''
353 }
354 },
355 /** 切换自动备份开关(开启后会立即安排一次备份) */
356 handleAutoBackupToggle(e) {
357 const enable = !!(e.detail && e.detail.value)
358 setAutoBackup(enable)
359 this.autoBackup = enable
360 uni.showToast({
361 title: enable ? '已开启自动备份' : '已关闭自动备份',
362 icon: 'none',
363 })
364 },
365 handleRestore() {
366 if (this.working) return
367 uni.showModal({
368 title: '从云端恢复',
369 content: '将把云端备份的歌单合并到本地(收藏/试听列表),并导入缺失的自定义音源。本地已有数据不会被删除,是否继续?',
370 confirmText: '恢复',
371 success: (res) => {
372 if (!res.confirm) return
373 this.doRestore()
374 },
375 })
376 },
377 async doRestore() {
378 this.working = 'restore'
379 uni.showLoading({ title: '恢复中...', mask: true })
380 try {
381 const res = await restoreFromCloud()
382 uni.hideLoading()
383 const parts = []
384 if (res.loveCount) parts.push(`收藏 ${res.loveCount} 首`)
385 if (res.defaultCount) parts.push(`试听 ${res.defaultCount} 首`)
386 if (res.customCount) parts.push(`自建歌单 ${res.customCount} 首`)
387 if (res.sourceCount) parts.push(`音源 ${res.sourceCount} 个`)
388 uni.showToast({
389 title: parts.length ? `恢复成功:${parts.join(',')}` : '云端没有需要恢复的数据',
390 icon: 'none',
391 })
392 // 恢复会修改本地歌单/音源,刷新统计
393 this.refreshLocalStats()
394 } catch (e) {
395 uni.hideLoading()
396 uni.showToast({ title: e.message || '恢复失败', icon: 'none' })
397 } finally {
398 this.working = ''
399 }
400 },
401 async handleChangePwd() {
402 if (this.pwdSubmitting) return
403 const { old: oldPassword, new1, new2 } = this.pwdForm
404 if (!oldPassword || !new1) {
405 uni.showToast({ title: '请填写完整', icon: 'none' })
406 return
407 }
408 if (new1.length < 6) {
409 uni.showToast({ title: '新密码至少 6 位', icon: 'none' })
410 return
411 }
412 if (new1 !== new2) {
413 uni.showToast({ title: '两次输入的新密码不一致', icon: 'none' })
414 return
415 }
416 this.pwdSubmitting = true
417 try {
418 await changePassword({ oldPassword, newPassword: new1 })
419 uni.showToast({ title: '密码已修改', icon: 'none' })
420 this.showPwdForm = false
421 this.pwdForm = { old: '', new1: '', new2: '' }
422 } catch (e) {
423 uni.showToast({ title: e.message || '修改失败', icon: 'none' })
424 } finally {
425 this.pwdSubmitting = false
426 }
427 },
428 handleLogout() {
429 uni.showModal({
430 title: '退出登录',
431 content: '退出后本机将不再自动同步,云端备份仍会保留。',
432 confirmText: '退出',
433 success: (res) => {
434 if (!res.confirm) return
435 logout().then(() => {
436 uni.showToast({ title: '已退出登录', icon: 'none' })
437 this.showPwdForm = false
438 this.pwdForm = { old: '', new1: '', new2: '' }
439 this.refreshLocal()
440 })
441 },
442 })
443 },
444 formatTime(ts) {
445 if (!ts) return ''
446 const d = new Date(ts * 1000)
447 const p = (n) => (n < 10 ? '0' + n : '' + n)
448 return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`
449 },
450 /** 相对时间(用于展示最近备份) */
451 formatRelativeTime(ts) {
452 if (!ts) return ''
453 const diff = Math.floor(Date.now() / 1000) - ts
454 if (diff < 60) return '刚刚'
455 if (diff < 3600) return `${Math.floor(diff / 60)} 分钟前`
456 if (diff < 86400) return `${Math.floor(diff / 3600)} 小时前`
457 if (diff < 172800) return '昨天'
458 if (diff < 2592000) return `${Math.floor(diff / 86400)} 天前`
459 return this.formatTime(ts)
460 },
461 },
462}
463</script>
464
465<style lang="scss" scoped>
466.page {
467 height: 100vh;
468 background: #f5f6f8;
469 width: 100%;
470 max-width: 1100px;
471 margin: 0 auto;
472}
473
474.page-scroll {
475 height: 100%;
476 padding-bottom: 40rpx;
477}
478
479.section {
480 margin: 24rpx;
481}
482
483.section-title {
484 font-size: 26rpx;
485 color: var(--text-muted, #909399);
486 padding: 8rpx 8rpx 16rpx;
487}
488
489.card {
490 background: var(--card-bg, #fff);
491 border-radius: 16rpx;
492 overflow: hidden;
493}
494
495/* ============ 登录/注册 ============ */
496.tab-bar {
497 display: flex;
498 padding: 24rpx 28rpx 0;
499}
500
501.tab {
502 flex: 1;
503 text-align: center;
504 padding: 20rpx 0;
505 font-size: 30rpx;
506 color: var(--text-muted, #909399);
507 border-bottom: 4rpx solid transparent;
508}
509
510.tab.active {
511 color: var(--theme-color, #31c27c);
512 font-weight: 600;
513 border-bottom-color: var(--theme-color, #31c27c);
514}
515
516.form {
517 padding: 16rpx 28rpx 32rpx;
518}
519
520.field {
521 margin-top: 24rpx;
522}
523
524.field-label {
525 display: block;
526 font-size: 26rpx;
527 color: var(--text-muted, #909399);
528 margin-bottom: 12rpx;
529}
530
531.field-input {
532 width: 100%;
533 padding: 20rpx 24rpx;
534 border-radius: 12rpx;
535 background: var(--fill-bg, #f5f6f8);
536 font-size: 30rpx;
537 color: #303133;
538}
539
540.field-tip {
541 display: block;
542 margin-top: 10rpx;
543 font-size: 22rpx;
544 color: var(--text-muted, #c0c4cc);
545}
546
547.captcha-row {
548 display: flex;
549 align-items: center;
550}
551
552.captcha-box {
553 flex-shrink: 0;
554 padding: 20rpx 28rpx;
555 border-radius: 12rpx;
556 background: rgba(var(--theme-color-rgb, 49, 194, 124), 0.08);
557 border: 1rpx dashed rgba(var(--theme-color-rgb, 49, 194, 124), 0.4);
558}
559
560.captcha-question {
561 font-size: 32rpx;
562 color: var(--theme-color, #31c27c);
563 font-weight: 600;
564 letter-spacing: 4rpx;
565}
566
567.captcha-input {
568 margin-left: 16rpx;
569}
570
571.btn-primary {
572 margin-top: 36rpx;
573 width: 100%;
574 height: 88rpx;
575 line-height: 88rpx;
576 border-radius: 44rpx;
577 background: var(--theme-color, #31c27c);
578 color: #fff;
579 font-size: 32rpx;
580 font-weight: 500;
581 border: none;
582}
583
584.btn-primary::after {
585 border: none;
586}
587
588.btn-plain {
589 margin-top: 24rpx;
590 width: 100%;
591 height: 88rpx;
592 line-height: 84rpx;
593 border-radius: 44rpx;
594 background: rgba(var(--theme-color-rgb, 49, 194, 124), 0.08);
595 color: var(--theme-color, #31c27c);
596 font-size: 32rpx;
597 font-weight: 500;
598 border: 2rpx solid rgba(var(--theme-color-rgb, 49, 194, 124), 0.5);
599}
600
601.btn-plain::after {
602 border: none;
603}
604
605.form-tip {
606 display: block;
607 margin-top: 24rpx;
608 text-align: center;
609 font-size: 24rpx;
610 color: var(--text-muted, #c0c4cc);
611}
612
613/* ============ 账号信息 ============ */
614.account-head {
615 display: flex;
616 align-items: center;
617 padding: 32rpx 28rpx;
618}
619
620.avatar {
621 width: 96rpx;
622 height: 96rpx;
623 border-radius: 50%;
624 background: var(--theme-color, #31c27c);
625 display: flex;
626 align-items: center;
627 justify-content: center;
628 flex-shrink: 0;
629}
630
631.avatar text {
632 font-size: 44rpx;
633 color: #fff;
634 font-weight: 600;
635}
636
637.account-info {
638 margin-left: 24rpx;
639 display: flex;
640 flex-direction: column;
641}
642
643.account-name {
644 font-size: 34rpx;
645 font-weight: 600;
646 color: #303133;
647}
648
649.account-sub {
650 margin-top: 8rpx;
651 font-size: 24rpx;
652 color: var(--text-muted, #909399);
653}
654
655/* ============ 单元格 ============ */
656.cell {
657 display: flex;
658 align-items: center;
659 padding: 28rpx;
660 border-bottom: 1rpx solid #f5f6f8;
661}
662
663.cell:last-child {
664 border-bottom: none;
665}
666
667.cell.column {
668 flex-direction: column;
669 align-items: stretch;
670}
671
672.cell-label {
673 font-size: 30rpx;
674 color: #303133;
675 flex-shrink: 0;
676}
677
678.cell-label.danger {
679 color: #ff4d4f;
680}
681
682.cell-value {
683 flex: 1;
684 text-align: right;
685 font-size: 26rpx;
686 color: var(--text-muted, #909399);
687 margin-right: 8rpx;
688}
689
690/* 左侧标签 + 说明的纵向组合(开关类单元格用) */
691.cell-main {
692 flex: 1;
693 min-width: 0;
694 display: flex;
695 flex-direction: column;
696 margin-right: 16rpx;
697}
698
699.cell-sub {
700 margin-top: 8rpx;
701 font-size: 24rpx;
702 color: var(--text-muted, #909399);
703 line-height: 1.4;
704}
705
706.cell-desc {
707 margin-top: 10rpx;
708 font-size: 24rpx;
709 color: var(--text-muted, #909399);
710 line-height: 1.5;
711}
712
713/* 修改密码表单 */
714.pwd-form {
715 padding: 8rpx 28rpx 24rpx;
716 border-bottom: 1rpx solid #f5f6f8;
717 background: var(--card-bg, #fafbfc);
718}
719
720</style>