feat(P1): 前端双 Token 适配 + 401 自动刷新 + UserAvatar src 支持 + Account API 扩展

This commit is contained in:
Sisyphus
2026-04-25 18:54:22 +08:00
parent 9b20df4bf3
commit 2322383036
30 changed files with 4284 additions and 4236 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,72 +1,72 @@
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import { ApiError } from '../api/client'
import * as accountApi from '../api/account'
import { useToastStore } from '../stores/toast'
const router = useRouter()
const toast = useToastStore()
const busy = ref(false)
const form = reactive({ username: '', oldPassword: '', newPassword: '' })
async function submit() {
if (busy.value) return
const username = form.username.trim()
const oldPassword = form.oldPassword.trim()
const newPassword = form.newPassword.trim()
if (!username || !oldPassword || !newPassword) {
toast.error('请把信息填完整')
return
}
busy.value = true
try {
await accountApi.changePassword(username, oldPassword, newPassword)
toast.success('密码已修改,请重新登录')
await router.push('/account')
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
busy.value = false
}
}
</script>
<template>
<AppShell>
<div class="grid two">
<div class="card">
<p class="title">修改密码</p>
<p class="subtle">不需要登录(对应后端 `/account/changePassword`)。</p>
<div class="grid" style="margin-top: 12px">
<div>
<label>username</label>
<input v-model.trim="form.username" autocomplete="username" />
</div>
<div>
<label>old_password</label>
<input v-model.trim="form.oldPassword" type="password" autocomplete="current-password" />
</div>
<div>
<label>new_password</label>
<input v-model.trim="form.newPassword" type="password" autocomplete="new-password" />
</div>
<div class="row" style="justify-content: flex-end">
<button class="primary" type="button" :disabled="busy" @click="submit">提交</button>
</div>
</div>
</div>
<div class="card">
<p class="title">提示</p>
<p class="muted">改密成功后后端会让旧 token 失效;请在「账号」页重新登录。</p>
</div>
</div>
</AppShell>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import { ApiError } from '../api/client'
import * as accountApi from '../api/account'
import { useToastStore } from '../stores/toast'
const router = useRouter()
const toast = useToastStore()
const busy = ref(false)
const form = reactive({ username: '', oldPassword: '', newPassword: '' })
async function submit() {
if (busy.value) return
const username = form.username.trim()
const oldPassword = form.oldPassword.trim()
const newPassword = form.newPassword.trim()
if (!username || !oldPassword || !newPassword) {
toast.error('请把信息填完整')
return
}
busy.value = true
try {
await accountApi.changePassword(username, oldPassword, newPassword)
toast.success('密码已修改,请重新登录')
await router.push('/account')
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
busy.value = false
}
}
</script>
<template>
<AppShell>
<div class="grid two">
<div class="card">
<p class="title">修改密码</p>
<p class="subtle">不需要登录(对应后端 `/account/changePassword`)。</p>
<div class="grid" style="margin-top: 12px">
<div>
<label>username</label>
<input v-model.trim="form.username" autocomplete="username" />
</div>
<div>
<label>old_password</label>
<input v-model.trim="form.oldPassword" type="password" autocomplete="current-password" />
</div>
<div>
<label>new_password</label>
<input v-model.trim="form.newPassword" type="password" autocomplete="new-password" />
</div>
<div class="row" style="justify-content: flex-end">
<button class="primary" type="button" :disabled="busy" @click="submit">提交</button>
</div>
</div>
</div>
<div class="card">
<p class="title">提示</p>
<p class="muted">改密成功后后端会让旧 token 失效;请在「账号」页重新登录。</p>
</div>
</div>
</AppShell>
</template>

View File

@@ -1,262 +1,262 @@
<script setup lang="ts">
import { computed, onMounted, reactive, watch } from 'vue'
import AppShell from '../components/AppShell.vue'
import JsonBox from '../components/JsonBox.vue'
import FeedVideoCard from '../components/FeedVideoCard.vue'
import { ApiError } from '../api/client'
import * as feedApi from '../api/feed'
import * as likeApi from '../api/like'
import type { FeedVideoItem } from '../api/types'
import { useAuthStore } from '../stores/auth'
const auth = useAuthStore()
type ListState = {
loading: boolean
error: string
items: FeedVideoItem[]
has_more: boolean
}
const latest = reactive<ListState & { limit: number; next_time: number }>({
loading: false,
error: '',
items: [],
has_more: false,
limit: 10,
next_time: 0,
})
const likesCount = reactive<ListState & { limit: number; next_likes_count_before?: number; next_id_before?: number }>({
loading: false,
error: '',
items: [],
has_more: false,
limit: 10,
next_likes_count_before: undefined,
next_id_before: undefined,
})
const following = reactive<ListState & { limit: number; next_time: number }>({
loading: false,
error: '',
items: [],
has_more: false,
limit: 10,
next_time: 0,
})
const action = reactive<{ loading: boolean; error: string; payload: unknown; name: string }>({
loading: false,
error: '',
payload: null,
name: '',
})
const canLike = computed(() => auth.isLoggedIn)
async function runAction(name: string, fn: () => Promise<unknown>) {
action.name = name
action.loading = true
action.error = ''
action.payload = null
try {
action.payload = await fn()
} catch (e) {
action.error = e instanceof ApiError ? e.message : String(e)
action.payload = e instanceof ApiError ? e.payload : null
} finally {
action.loading = false
}
}
async function loadLatest(reset: boolean) {
latest.loading = true
latest.error = ''
try {
const latest_time = reset ? 0 : latest.next_time
const res = await feedApi.listLatest({ limit: latest.limit, latest_time })
latest.has_more = res.has_more
latest.next_time = res.next_time
latest.items = reset ? res.video_list : latest.items.concat(res.video_list)
} catch (e) {
latest.error = e instanceof ApiError ? e.message : String(e)
} finally {
latest.loading = false
}
}
async function loadLikesCount(reset: boolean) {
likesCount.loading = true
likesCount.error = ''
try {
const res = await feedApi.listLikesCount({
limit: likesCount.limit,
likes_count_before: reset ? undefined : likesCount.next_likes_count_before,
id_before: reset ? undefined : likesCount.next_id_before,
})
likesCount.has_more = res.has_more
likesCount.next_likes_count_before = res.next_likes_count_before
likesCount.next_id_before = res.next_id_before
likesCount.items = reset ? res.video_list : likesCount.items.concat(res.video_list)
} catch (e) {
likesCount.error = e instanceof ApiError ? e.message : String(e)
} finally {
likesCount.loading = false
}
}
async function loadFollowing(reset: boolean) {
following.loading = true
following.error = ''
try {
const latest_time = reset ? 0 : following.next_time
const res = await feedApi.listByFollowing({ limit: following.limit, latest_time })
following.has_more = res.has_more
following.next_time = res.next_time
following.items = reset ? res.video_list : following.items.concat(res.video_list)
} catch (e) {
following.error = e instanceof ApiError ? e.message : String(e)
} finally {
following.loading = false
}
}
async function toggleLike(item: FeedVideoItem) {
if (!auth.isLoggedIn) return
await runAction(item.is_liked ? '取消点赞' : '点赞', async () => {
if (item.is_liked) await likeApi.unlike(item.id)
else await likeApi.like(item.id)
item.is_liked = !item.is_liked
item.likes_count = Math.max(0, item.likes_count + (item.is_liked ? 1 : -1))
return { ok: true, is_liked: item.is_liked, likes_count: item.likes_count }
})
}
onMounted(async () => {
await loadLatest(true)
await loadLikesCount(true)
if (auth.isLoggedIn) {
await loadFollowing(true)
}
})
watch(
() => auth.isLoggedIn,
async (v) => {
if (v && following.items.length === 0) {
await loadFollowing(true)
}
},
)
</script>
<template>
<AppShell>
<div class="grid two">
<div class="card">
<p class="title">Feed</p>
<p class="subtle">`/feed/listLatest` 与 `/feed/listLikesCount` 支持匿名(可选 JWT);`/feed/listByFollowing` 需要 JWT。</p>
<div class="card" style="margin-top: 12px">
<div class="row" style="justify-content: space-between">
<div>
<p class="title">最新流(listLatest)</p>
<div class="subtle">limit:{{ latest.limit }} · next_time:{{ latest.next_time }} · has_more:{{ latest.has_more }}</div>
</div>
<div class="row">
<label class="subtle" style="margin: 0">limit</label>
<input v-model.number="latest.limit" type="number" min="1" max="50" style="width: 90px" />
<button class="primary" type="button" :disabled="latest.loading" @click="loadLatest(true)">刷新</button>
<button type="button" :disabled="latest.loading || !latest.has_more" @click="loadLatest(false)">加载更多</button>
</div>
</div>
<div v-if="latest.error" class="pill bad" style="margin-top: 10px">错误:{{ latest.error }}</div>
<div class="grid" style="gap: 10px; margin-top: 12px">
<FeedVideoCard
v-for="item in latest.items"
:key="`latest-${item.id}`"
:item="item"
:can-like="canLike"
:busy="action.loading"
@toggle-like="toggleLike"
/>
</div>
</div>
<div class="card" style="margin-top: 12px">
<div class="row" style="justify-content: space-between">
<div>
<p class="title">点赞数流(listLikesCount)</p>
<div class="subtle">
limit:{{ likesCount.limit }} · next=(likes={{ likesCount.next_likes_count_before }}, id={{ likesCount.next_id_before }})
· has_more:{{ likesCount.has_more }}
</div>
</div>
<div class="row">
<label class="subtle" style="margin: 0">limit</label>
<input v-model.number="likesCount.limit" type="number" min="1" max="50" style="width: 90px" />
<button class="primary" type="button" :disabled="likesCount.loading" @click="loadLikesCount(true)">刷新</button>
<button type="button" :disabled="likesCount.loading || !likesCount.has_more" @click="loadLikesCount(false)">
加载更多
</button>
</div>
</div>
<div v-if="likesCount.error" class="pill bad" style="margin-top: 10px">错误:{{ likesCount.error }}</div>
<div class="grid" style="gap: 10px; margin-top: 12px">
<FeedVideoCard
v-for="item in likesCount.items"
:key="`likes-${item.id}`"
:item="item"
:can-like="canLike"
:busy="action.loading"
@toggle-like="toggleLike"
/>
</div>
</div>
<div class="card" style="margin-top: 12px">
<div class="row" style="justify-content: space-between">
<div>
<p class="title">关注流(listByFollowing,JWT)</p>
<div class="subtle">
limit:{{ following.limit }} · next_time:{{ following.next_time }} · has_more:{{ following.has_more }}
</div>
</div>
<div class="row">
<label class="subtle" style="margin: 0">limit</label>
<input v-model.number="following.limit" type="number" min="1" max="50" style="width: 90px" />
<button class="primary" type="button" :disabled="following.loading" @click="loadFollowing(true)">刷新</button>
<button type="button" :disabled="following.loading || !following.has_more" @click="loadFollowing(false)">加载更多</button>
</div>
</div>
<div v-if="!auth.isLoggedIn" class="pill bad" style="margin-top: 10px">未登录:无法访问关注流</div>
<div v-if="following.error" class="pill bad" style="margin-top: 10px">错误:{{ following.error }}</div>
<div class="grid" style="gap: 10px; margin-top: 12px">
<FeedVideoCard
v-for="item in following.items"
:key="`following-${item.id}`"
:item="item"
:can-like="canLike"
:busy="action.loading"
@toggle-like="toggleLike"
/>
</div>
</div>
</div>
<div class="card">
<p class="title">动作输出(点赞等)</p>
<div class="row" style="margin-bottom: 10px">
<span class="pill">动作:{{ action.name || '-' }}</span>
<span v-if="action.loading" class="pill">请求中…</span>
<span v-if="action.error" class="pill bad">错误:{{ action.error }}</span>
</div>
<JsonBox :value="action.payload" />
</div>
</div>
</AppShell>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, watch } from 'vue'
import AppShell from '../components/AppShell.vue'
import JsonBox from '../components/JsonBox.vue'
import FeedVideoCard from '../components/FeedVideoCard.vue'
import { ApiError } from '../api/client'
import * as feedApi from '../api/feed'
import * as likeApi from '../api/like'
import type { FeedVideoItem } from '../api/types'
import { useAuthStore } from '../stores/auth'
const auth = useAuthStore()
type ListState = {
loading: boolean
error: string
items: FeedVideoItem[]
has_more: boolean
}
const latest = reactive<ListState & { limit: number; next_time: number }>({
loading: false,
error: '',
items: [],
has_more: false,
limit: 10,
next_time: 0,
})
const likesCount = reactive<ListState & { limit: number; next_likes_count_before?: number; next_id_before?: number }>({
loading: false,
error: '',
items: [],
has_more: false,
limit: 10,
next_likes_count_before: undefined,
next_id_before: undefined,
})
const following = reactive<ListState & { limit: number; next_time: number }>({
loading: false,
error: '',
items: [],
has_more: false,
limit: 10,
next_time: 0,
})
const action = reactive<{ loading: boolean; error: string; payload: unknown; name: string }>({
loading: false,
error: '',
payload: null,
name: '',
})
const canLike = computed(() => auth.isLoggedIn)
async function runAction(name: string, fn: () => Promise<unknown>) {
action.name = name
action.loading = true
action.error = ''
action.payload = null
try {
action.payload = await fn()
} catch (e) {
action.error = e instanceof ApiError ? e.message : String(e)
action.payload = e instanceof ApiError ? e.payload : null
} finally {
action.loading = false
}
}
async function loadLatest(reset: boolean) {
latest.loading = true
latest.error = ''
try {
const latest_time = reset ? 0 : latest.next_time
const res = await feedApi.listLatest({ limit: latest.limit, latest_time })
latest.has_more = res.has_more
latest.next_time = res.next_time
latest.items = reset ? res.video_list : latest.items.concat(res.video_list)
} catch (e) {
latest.error = e instanceof ApiError ? e.message : String(e)
} finally {
latest.loading = false
}
}
async function loadLikesCount(reset: boolean) {
likesCount.loading = true
likesCount.error = ''
try {
const res = await feedApi.listLikesCount({
limit: likesCount.limit,
likes_count_before: reset ? undefined : likesCount.next_likes_count_before,
id_before: reset ? undefined : likesCount.next_id_before,
})
likesCount.has_more = res.has_more
likesCount.next_likes_count_before = res.next_likes_count_before
likesCount.next_id_before = res.next_id_before
likesCount.items = reset ? res.video_list : likesCount.items.concat(res.video_list)
} catch (e) {
likesCount.error = e instanceof ApiError ? e.message : String(e)
} finally {
likesCount.loading = false
}
}
async function loadFollowing(reset: boolean) {
following.loading = true
following.error = ''
try {
const latest_time = reset ? 0 : following.next_time
const res = await feedApi.listByFollowing({ limit: following.limit, latest_time })
following.has_more = res.has_more
following.next_time = res.next_time
following.items = reset ? res.video_list : following.items.concat(res.video_list)
} catch (e) {
following.error = e instanceof ApiError ? e.message : String(e)
} finally {
following.loading = false
}
}
async function toggleLike(item: FeedVideoItem) {
if (!auth.isLoggedIn) return
await runAction(item.is_liked ? '取消点赞' : '点赞', async () => {
if (item.is_liked) await likeApi.unlike(item.id)
else await likeApi.like(item.id)
item.is_liked = !item.is_liked
item.likes_count = Math.max(0, item.likes_count + (item.is_liked ? 1 : -1))
return { ok: true, is_liked: item.is_liked, likes_count: item.likes_count }
})
}
onMounted(async () => {
await loadLatest(true)
await loadLikesCount(true)
if (auth.isLoggedIn) {
await loadFollowing(true)
}
})
watch(
() => auth.isLoggedIn,
async (v) => {
if (v && following.items.length === 0) {
await loadFollowing(true)
}
},
)
</script>
<template>
<AppShell>
<div class="grid two">
<div class="card">
<p class="title">Feed</p>
<p class="subtle">`/feed/listLatest` 与 `/feed/listLikesCount` 支持匿名(可选 JWT);`/feed/listByFollowing` 需要 JWT。</p>
<div class="card" style="margin-top: 12px">
<div class="row" style="justify-content: space-between">
<div>
<p class="title">最新流(listLatest)</p>
<div class="subtle">limit:{{ latest.limit }} · next_time:{{ latest.next_time }} · has_more:{{ latest.has_more }}</div>
</div>
<div class="row">
<label class="subtle" style="margin: 0">limit</label>
<input v-model.number="latest.limit" type="number" min="1" max="50" style="width: 90px" />
<button class="primary" type="button" :disabled="latest.loading" @click="loadLatest(true)">刷新</button>
<button type="button" :disabled="latest.loading || !latest.has_more" @click="loadLatest(false)">加载更多</button>
</div>
</div>
<div v-if="latest.error" class="pill bad" style="margin-top: 10px">错误:{{ latest.error }}</div>
<div class="grid" style="gap: 10px; margin-top: 12px">
<FeedVideoCard
v-for="item in latest.items"
:key="`latest-${item.id}`"
:item="item"
:can-like="canLike"
:busy="action.loading"
@toggle-like="toggleLike"
/>
</div>
</div>
<div class="card" style="margin-top: 12px">
<div class="row" style="justify-content: space-between">
<div>
<p class="title">点赞数流(listLikesCount)</p>
<div class="subtle">
limit:{{ likesCount.limit }} · next=(likes={{ likesCount.next_likes_count_before }}, id={{ likesCount.next_id_before }})
· has_more:{{ likesCount.has_more }}
</div>
</div>
<div class="row">
<label class="subtle" style="margin: 0">limit</label>
<input v-model.number="likesCount.limit" type="number" min="1" max="50" style="width: 90px" />
<button class="primary" type="button" :disabled="likesCount.loading" @click="loadLikesCount(true)">刷新</button>
<button type="button" :disabled="likesCount.loading || !likesCount.has_more" @click="loadLikesCount(false)">
加载更多
</button>
</div>
</div>
<div v-if="likesCount.error" class="pill bad" style="margin-top: 10px">错误:{{ likesCount.error }}</div>
<div class="grid" style="gap: 10px; margin-top: 12px">
<FeedVideoCard
v-for="item in likesCount.items"
:key="`likes-${item.id}`"
:item="item"
:can-like="canLike"
:busy="action.loading"
@toggle-like="toggleLike"
/>
</div>
</div>
<div class="card" style="margin-top: 12px">
<div class="row" style="justify-content: space-between">
<div>
<p class="title">关注流(listByFollowing,JWT)</p>
<div class="subtle">
limit:{{ following.limit }} · next_time:{{ following.next_time }} · has_more:{{ following.has_more }}
</div>
</div>
<div class="row">
<label class="subtle" style="margin: 0">limit</label>
<input v-model.number="following.limit" type="number" min="1" max="50" style="width: 90px" />
<button class="primary" type="button" :disabled="following.loading" @click="loadFollowing(true)">刷新</button>
<button type="button" :disabled="following.loading || !following.has_more" @click="loadFollowing(false)">加载更多</button>
</div>
</div>
<div v-if="!auth.isLoggedIn" class="pill bad" style="margin-top: 10px">未登录:无法访问关注流</div>
<div v-if="following.error" class="pill bad" style="margin-top: 10px">错误:{{ following.error }}</div>
<div class="grid" style="gap: 10px; margin-top: 12px">
<FeedVideoCard
v-for="item in following.items"
:key="`following-${item.id}`"
:item="item"
:can-like="canLike"
:busy="action.loading"
@toggle-like="toggleLike"
/>
</div>
</div>
</div>
<div class="card">
<p class="title">动作输出(点赞等)</p>
<div class="row" style="margin-bottom: 10px">
<span class="pill">动作:{{ action.name || '-' }}</span>
<span v-if="action.loading" class="pill">请求中…</span>
<span v-if="action.error" class="pill bad">错误:{{ action.error }}</span>
</div>
<JsonBox :value="action.payload" />
</div>
</div>
</AppShell>
</template>

View File

@@ -1,157 +1,157 @@
<script setup lang="ts">
import { computed, onMounted, reactive } from 'vue'
import { ApiError } from '../api/client'
import * as feedApi from '../api/feed'
import * as likeApi from '../api/like'
import type { FeedVideoItem } from '../api/types'
import AppShell from '../components/AppShell.vue'
import FeedVideoCard from '../components/FeedVideoCard.vue'
import { useAuthStore } from '../stores/auth'
import { useToastStore } from '../stores/toast'
const auth = useAuthStore()
const toast = useToastStore()
const canLike = computed(() => auth.isLoggedIn)
const state = reactive({
loading: false,
error: '',
items: [] as FeedVideoItem[],
hasMore: false,
limit: 10,
asOf: 0,
nextOffset: 0,
})
const likeBusy = reactive<Record<string, boolean>>({})
async function loadHot(reset: boolean) {
if (state.loading) return
state.loading = true
state.error = ''
try {
const res = await feedApi.listByPopularity({
limit: state.limit,
as_of: reset ? 0 : state.asOf,
offset: reset ? 0 : state.nextOffset,
})
state.hasMore = res.has_more
state.asOf = res.as_of
state.nextOffset = res.next_offset
state.items = reset ? res.video_list : state.items.concat(res.video_list)
} catch (e) {
state.error = e instanceof ApiError ? e.message : String(e)
} finally {
state.loading = false
}
}
async function toggleLike(item: FeedVideoItem) {
if (!auth.isLoggedIn) {
toast.error('请先登录')
return
}
const key = String(item.id)
if (likeBusy[key]) return
likeBusy[key] = true
try {
if (item.is_liked) await likeApi.unlike(item.id)
else await likeApi.like(item.id)
item.is_liked = !item.is_liked
item.likes_count = Math.max(0, item.likes_count + (item.is_liked ? 1 : -1))
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
likeBusy[key] = false
}
}
onMounted(async () => {
await loadHot(true)
})
</script>
<template>
<AppShell>
<div class="card">
<div class="row" style="justify-content: space-between; align-items: baseline">
<div>
<p class="title" style="margin: 0">热榜</p>
<p class="subtle" style="margin: 6px 0 0">按热度排序(/feed/listByPopularity)</p>
</div>
<div class="row">
<label class="subtle" style="margin: 0">limit</label>
<input v-model.number="state.limit" type="number" min="1" max="50" style="width: 90px" :disabled="state.loading" />
<button class="primary" type="button" :disabled="state.loading" @click="loadHot(true)">刷新</button>
<button type="button" :disabled="state.loading || !state.hasMore" @click="loadHot(false)">加载更多</button>
</div>
</div>
<div v-if="state.error" class="pill bad" style="margin-top: 12px">错误:{{ state.error }}</div>
<div v-else-if="state.loading && state.items.length === 0" class="subtle" style="margin-top: 12px">加载中…</div>
<div v-else-if="state.items.length === 0" class="subtle" style="margin-top: 12px">暂无内容</div>
<div v-if="state.items.length" class="rank-list" style="margin-top: 14px">
<div v-for="(item, idx) in state.items" :key="`hot-${item.id}`" class="rank-row">
<div class="rank-num" :class="idx < 3 ? 'top' : ''">{{ idx + 1 }}</div>
<FeedVideoCard
:item="item"
:can-like="canLike"
:busy="!!likeBusy[String(item.id)]"
@toggle-like="toggleLike"
/>
</div>
</div>
</div>
</AppShell>
</template>
<style scoped>
.rank-list {
display: grid;
gap: 12px;
}
.rank-row {
display: grid;
grid-template-columns: 44px minmax(0, 1fr);
gap: 12px;
align-items: start;
}
.rank-num {
height: 44px;
width: 44px;
border-radius: 16px;
display: grid;
place-items: center;
font-weight: 900;
letter-spacing: 0.2px;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.06);
color: rgba(255, 255, 255, 0.86);
user-select: none;
}
.rank-num.top {
border-color: rgba(254, 44, 85, 0.55);
background: rgba(254, 44, 85, 0.18);
color: rgba(255, 255, 255, 0.96);
}
@media (max-width: 900px) {
.rank-row {
grid-template-columns: 38px minmax(0, 1fr);
gap: 10px;
}
.rank-num {
height: 38px;
width: 38px;
border-radius: 14px;
}
}
</style>
<script setup lang="ts">
import { computed, onMounted, reactive } from 'vue'
import { ApiError } from '../api/client'
import * as feedApi from '../api/feed'
import * as likeApi from '../api/like'
import type { FeedVideoItem } from '../api/types'
import AppShell from '../components/AppShell.vue'
import FeedVideoCard from '../components/FeedVideoCard.vue'
import { useAuthStore } from '../stores/auth'
import { useToastStore } from '../stores/toast'
const auth = useAuthStore()
const toast = useToastStore()
const canLike = computed(() => auth.isLoggedIn)
const state = reactive({
loading: false,
error: '',
items: [] as FeedVideoItem[],
hasMore: false,
limit: 10,
asOf: 0,
nextOffset: 0,
})
const likeBusy = reactive<Record<string, boolean>>({})
async function loadHot(reset: boolean) {
if (state.loading) return
state.loading = true
state.error = ''
try {
const res = await feedApi.listByPopularity({
limit: state.limit,
as_of: reset ? 0 : state.asOf,
offset: reset ? 0 : state.nextOffset,
})
state.hasMore = res.has_more
state.asOf = res.as_of
state.nextOffset = res.next_offset
state.items = reset ? res.video_list : state.items.concat(res.video_list)
} catch (e) {
state.error = e instanceof ApiError ? e.message : String(e)
} finally {
state.loading = false
}
}
async function toggleLike(item: FeedVideoItem) {
if (!auth.isLoggedIn) {
toast.error('请先登录')
return
}
const key = String(item.id)
if (likeBusy[key]) return
likeBusy[key] = true
try {
if (item.is_liked) await likeApi.unlike(item.id)
else await likeApi.like(item.id)
item.is_liked = !item.is_liked
item.likes_count = Math.max(0, item.likes_count + (item.is_liked ? 1 : -1))
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
likeBusy[key] = false
}
}
onMounted(async () => {
await loadHot(true)
})
</script>
<template>
<AppShell>
<div class="card">
<div class="row" style="justify-content: space-between; align-items: baseline">
<div>
<p class="title" style="margin: 0">热榜</p>
<p class="subtle" style="margin: 6px 0 0">按热度排序(/feed/listByPopularity)</p>
</div>
<div class="row">
<label class="subtle" style="margin: 0">limit</label>
<input v-model.number="state.limit" type="number" min="1" max="50" style="width: 90px" :disabled="state.loading" />
<button class="primary" type="button" :disabled="state.loading" @click="loadHot(true)">刷新</button>
<button type="button" :disabled="state.loading || !state.hasMore" @click="loadHot(false)">加载更多</button>
</div>
</div>
<div v-if="state.error" class="pill bad" style="margin-top: 12px">错误:{{ state.error }}</div>
<div v-else-if="state.loading && state.items.length === 0" class="subtle" style="margin-top: 12px">加载中…</div>
<div v-else-if="state.items.length === 0" class="subtle" style="margin-top: 12px">暂无内容</div>
<div v-if="state.items.length" class="rank-list" style="margin-top: 14px">
<div v-for="(item, idx) in state.items" :key="`hot-${item.id}`" class="rank-row">
<div class="rank-num" :class="idx < 3 ? 'top' : ''">{{ idx + 1 }}</div>
<FeedVideoCard
:item="item"
:can-like="canLike"
:busy="!!likeBusy[String(item.id)]"
@toggle-like="toggleLike"
/>
</div>
</div>
</div>
</AppShell>
</template>
<style scoped>
.rank-list {
display: grid;
gap: 12px;
}
.rank-row {
display: grid;
grid-template-columns: 44px minmax(0, 1fr);
gap: 12px;
align-items: start;
}
.rank-num {
height: 44px;
width: 44px;
border-radius: 16px;
display: grid;
place-items: center;
font-weight: 900;
letter-spacing: 0.2px;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.06);
color: rgba(255, 255, 255, 0.86);
user-select: none;
}
.rank-num.top {
border-color: rgba(254, 44, 85, 0.55);
background: rgba(254, 44, 85, 0.18);
color: rgba(255, 255, 255, 0.96);
}
@media (max-width: 900px) {
.rank-row {
grid-template-columns: 38px minmax(0, 1fr);
gap: 10px;
}
.rank-num {
height: 38px;
width: 38px;
border-radius: 14px;
}
}
</style>

View File

@@ -1,67 +1,67 @@
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import { ApiError } from '../api/client'
import * as accountApi from '../api/account'
import { useToastStore } from '../stores/toast'
const router = useRouter()
const toast = useToastStore()
const busy = ref(false)
const form = reactive({ username: '', password: '' })
async function submit() {
if (busy.value) return
const username = form.username.trim()
const password = form.password.trim()
if (!username || !password) {
toast.error('请输入 username 和 password')
return
}
busy.value = true
try {
await accountApi.register(username, password)
toast.success('注册成功,请登录')
await router.push('/account')
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
busy.value = false
}
}
</script>
<template>
<AppShell>
<div class="grid two">
<div class="card">
<p class="title">注册</p>
<p class="subtle">创建新账号(对应后端 `/account/register`)。</p>
<div class="grid" style="margin-top: 12px">
<div>
<label>username</label>
<input v-model.trim="form.username" autocomplete="username" />
</div>
<div>
<label>password</label>
<input v-model.trim="form.password" type="password" autocomplete="new-password" />
</div>
<div class="row" style="justify-content: flex-end">
<button class="primary" type="button" :disabled="busy" @click="submit">注册</button>
</div>
</div>
</div>
<div class="card">
<p class="title">提示</p>
<p class="muted">注册成功后会跳回「账号」页进行登录。</p>
</div>
</div>
</AppShell>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import { ApiError } from '../api/client'
import * as accountApi from '../api/account'
import { useToastStore } from '../stores/toast'
const router = useRouter()
const toast = useToastStore()
const busy = ref(false)
const form = reactive({ username: '', password: '' })
async function submit() {
if (busy.value) return
const username = form.username.trim()
const password = form.password.trim()
if (!username || !password) {
toast.error('请输入 username 和 password')
return
}
busy.value = true
try {
await accountApi.register(username, password)
toast.success('注册成功,请登录')
await router.push('/account')
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
busy.value = false
}
}
</script>
<template>
<AppShell>
<div class="grid two">
<div class="card">
<p class="title">注册</p>
<p class="subtle">创建新账号(对应后端 `/account/register`)。</p>
<div class="grid" style="margin-top: 12px">
<div>
<label>username</label>
<input v-model.trim="form.username" autocomplete="username" />
</div>
<div>
<label>password</label>
<input v-model.trim="form.password" type="password" autocomplete="new-password" />
</div>
<div class="row" style="justify-content: flex-end">
<button class="primary" type="button" :disabled="busy" @click="submit">注册</button>
</div>
</div>
</div>
<div class="card">
<p class="title">提示</p>
<p class="muted">注册成功后会跳回「账号」页进行登录。</p>
</div>
</div>
</AppShell>
</template>

View File

@@ -1,168 +1,168 @@
<script setup lang="ts">
import { computed, nextTick, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import UserAvatar from '../components/UserAvatar.vue'
import { ApiError } from '../api/client'
import * as accountApi from '../api/account'
import { useAuthStore } from '../stores/auth'
import { useToastStore } from '../stores/toast'
const router = useRouter()
const auth = useAuthStore()
const toast = useToastStore()
const busy = ref(false)
const me = computed(() => ({
id: auth.claims?.account_id ?? 0,
username: auth.claims?.username ?? '',
}))
const rename = reactive({
open: false,
newUsername: '',
})
async function openRename() {
if (!auth.isLoggedIn) return
rename.open = true
rename.newUsername = me.value.username
await nextTick()
}
async function submitRename() {
if (!auth.isLoggedIn) return
if (busy.value) return
const newUsername = rename.newUsername.trim()
if (!newUsername) {
toast.error('请输入新用户名')
return
}
busy.value = true
try {
const res = await accountApi.rename(newUsername)
auth.setToken(res.token)
rename.open = false
toast.success('改名成功(已刷新 token)')
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
busy.value = false
}
}
async function goLogin() {
await router.push('/account')
}
async function goChangePassword() {
await router.push('/account/change-password')
}
async function onLogout() {
if (!auth.isLoggedIn) return
if (busy.value) return
if (!window.confirm('确认退出登录?')) return
busy.value = true
try {
await accountApi.logout()
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(`登出失败:${msg}`)
} finally {
auth.clearToken()
rename.open = false
toast.info('已退出登录')
busy.value = false
await router.push('/')
}
}
</script>
<template>
<AppShell>
<div v-if="!auth.isLoggedIn" class="grid two">
<div class="card">
<p class="title">设置</p>
<p class="subtle">需要先登录后才能进行改名/退出等操作。</p>
<div class="row" style="margin-top: 12px; justify-content: flex-end">
<button class="primary" type="button" @click="goLogin">去登录</button>
</div>
</div>
<div class="card">
<p class="title">提示</p>
<p class="muted">登录入口在「账号」页。</p>
</div>
</div>
<div v-else class="grid two">
<div class="card">
<div class="row" style="justify-content: space-between; align-items: flex-start">
<div class="row" style="gap: 12px; align-items: center">
<UserAvatar :username="me.username" :id="me.id" :size="56" />
<div>
<div class="title" style="margin: 0">@{{ me.username }}</div>
<div class="subtle mono">#{{ me.id }}</div>
</div>
</div>
</div>
<div class="card" style="margin-top: 14px">
<div class="row" style="justify-content: space-between; align-items: center">
<p class="title" style="margin: 0">账号设置</p>
<button class="ghost" type="button" :disabled="busy" @click="openRename">改名</button>
</div>
<div v-if="rename.open" class="grid" style="margin-top: 12px">
<div>
<label>new_username</label>
<input v-model.trim="rename.newUsername" @keydown.enter="submitRename" />
</div>
<div class="row" style="justify-content: flex-end">
<button type="button" :disabled="busy" @click="rename.open = false">取消</button>
<button class="primary" type="button" :disabled="busy" @click="submitRename">提交</button>
</div>
</div>
</div>
<div class="card" style="margin-top: 14px">
<p class="title">账号安全</p>
<div class="row">
<button class="ghost" type="button" :disabled="busy" @click="goChangePassword">修改密码</button>
<button class="danger" type="button" :disabled="busy" @click="onLogout">退出登录</button>
</div>
</div>
</div>
<div class="card">
<p class="title">说明</p>
<div class="grid" style="margin-top: 10px">
<div class="pill ok">改名后会返回新 token,旧 token 立即失效</div>
<div class="pill ok">退出登录会清空本地 token</div>
<div class="pill">修改密码无需登录,但成功后会让旧 token 失效</div>
</div>
</div>
</div>
</AppShell>
</template>
<style scoped>
.ghost {
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(0, 0, 0, 0.18);
color: rgba(255, 255, 255, 0.86);
border-radius: 12px;
padding: 10px 12px;
cursor: pointer;
}
.ghost:hover {
background: rgba(255, 255, 255, 0.1);
}
</style>
<script setup lang="ts">
import { computed, nextTick, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import UserAvatar from '../components/UserAvatar.vue'
import { ApiError } from '../api/client'
import * as accountApi from '../api/account'
import { useAuthStore } from '../stores/auth'
import { useToastStore } from '../stores/toast'
const router = useRouter()
const auth = useAuthStore()
const toast = useToastStore()
const busy = ref(false)
const me = computed(() => ({
id: auth.claims?.account_id ?? 0,
username: auth.claims?.username ?? '',
}))
const rename = reactive({
open: false,
newUsername: '',
})
async function openRename() {
if (!auth.isLoggedIn) return
rename.open = true
rename.newUsername = me.value.username
await nextTick()
}
async function submitRename() {
if (!auth.isLoggedIn) return
if (busy.value) return
const newUsername = rename.newUsername.trim()
if (!newUsername) {
toast.error('请输入新用户名')
return
}
busy.value = true
try {
const res = await accountApi.rename(newUsername)
auth.setToken(res.token)
rename.open = false
toast.success('改名成功(已刷新 token)')
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
busy.value = false
}
}
async function goLogin() {
await router.push('/account')
}
async function goChangePassword() {
await router.push('/account/change-password')
}
async function onLogout() {
if (!auth.isLoggedIn) return
if (busy.value) return
if (!window.confirm('确认退出登录?')) return
busy.value = true
try {
await accountApi.logout()
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(`登出失败:${msg}`)
} finally {
auth.clearTokens()
rename.open = false
toast.info('已退出登录')
busy.value = false
await router.push('/')
}
}
</script>
<template>
<AppShell>
<div v-if="!auth.isLoggedIn" class="grid two">
<div class="card">
<p class="title">设置</p>
<p class="subtle">需要先登录后才能进行改名/退出等操作。</p>
<div class="row" style="margin-top: 12px; justify-content: flex-end">
<button class="primary" type="button" @click="goLogin">去登录</button>
</div>
</div>
<div class="card">
<p class="title">提示</p>
<p class="muted">登录入口在「账号」页。</p>
</div>
</div>
<div v-else class="grid two">
<div class="card">
<div class="row" style="justify-content: space-between; align-items: flex-start">
<div class="row" style="gap: 12px; align-items: center">
<UserAvatar :username="me.username" :id="me.id" :size="56" />
<div>
<div class="title" style="margin: 0">@{{ me.username }}</div>
<div class="subtle mono">#{{ me.id }}</div>
</div>
</div>
</div>
<div class="card" style="margin-top: 14px">
<div class="row" style="justify-content: space-between; align-items: center">
<p class="title" style="margin: 0">账号设置</p>
<button class="ghost" type="button" :disabled="busy" @click="openRename">改名</button>
</div>
<div v-if="rename.open" class="grid" style="margin-top: 12px">
<div>
<label>new_username</label>
<input v-model.trim="rename.newUsername" @keydown.enter="submitRename" />
</div>
<div class="row" style="justify-content: flex-end">
<button type="button" :disabled="busy" @click="rename.open = false">取消</button>
<button class="primary" type="button" :disabled="busy" @click="submitRename">提交</button>
</div>
</div>
</div>
<div class="card" style="margin-top: 14px">
<p class="title">账号安全</p>
<div class="row">
<button class="ghost" type="button" :disabled="busy" @click="goChangePassword">修改密码</button>
<button class="danger" type="button" :disabled="busy" @click="onLogout">退出登录</button>
</div>
</div>
</div>
<div class="card">
<p class="title">说明</p>
<div class="grid" style="margin-top: 10px">
<div class="pill ok">改名后会返回新 token,旧 token 立即失效</div>
<div class="pill ok">退出登录会清空本地 token</div>
<div class="pill">修改密码无需登录,但成功后会让旧 token 失效</div>
</div>
</div>
</div>
</AppShell>
</template>
<style scoped>
.ghost {
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(0, 0, 0, 0.18);
color: rgba(255, 255, 255, 0.86);
border-radius: 12px;
padding: 10px 12px;
cursor: pointer;
}
.ghost:hover {
background: rgba(255, 255, 255, 0.1);
}
</style>

View File

@@ -1,455 +1,455 @@
<script setup lang="ts">
import { computed, onMounted, reactive, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import UserAvatar from '../components/UserAvatar.vue'
import { ApiError } from '../api/client'
import * as accountApi from '../api/account'
import * as socialApi from '../api/social'
import type { Account, Video } from '../api/types'
import * as videoApi from '../api/video'
import { useAuthStore } from '../stores/auth'
import { useSocialStore } from '../stores/social'
import { useToastStore } from '../stores/toast'
const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const social = useSocialStore()
const toast = useToastStore()
const userId = computed(() => Number(route.params.id))
const myId = computed(() => auth.claims?.account_id ?? 0)
const isMe = computed(() => myId.value > 0 && myId.value === userId.value)
const state = reactive({
loading: false,
error: '',
user: null as Account | null,
videos: [] as Video[],
followers: [] as Account[],
vloggers: [] as Account[],
socialLoading: false,
socialError: '',
})
const isFollowing = computed(() => (auth.isLoggedIn ? social.isFollowing(userId.value) : false))
async function loadProfile() {
if (!Number.isFinite(userId.value) || userId.value <= 0) {
state.error = '无效的用户 id'
return
}
state.loading = true
state.error = ''
try {
const [u, vids] = await Promise.all([accountApi.findById(userId.value), videoApi.listByAuthorId(userId.value)])
state.user = u
state.videos = vids
} catch (e) {
state.error = e instanceof ApiError ? e.message : String(e)
state.user = null
state.videos = []
} finally {
state.loading = false
}
await loadSocialCounts()
}
async function loadSocialCounts() {
state.socialError = ''
state.followers = []
state.vloggers = []
if (!auth.isLoggedIn) return
if (!Number.isFinite(userId.value) || userId.value <= 0) return
state.socialLoading = true
try {
const [followersRes, vloggersRes] = await Promise.all([
socialApi.getAllFollowers(userId.value),
socialApi.getAllVloggers(userId.value),
])
state.followers = followersRes.followers
state.vloggers = vloggersRes.vloggers
} catch (e) {
state.socialError = e instanceof ApiError ? e.message : String(e)
} finally {
state.socialLoading = false
}
}
async function toggleFollow() {
if (isMe.value) return
if (!auth.isLoggedIn) {
toast.error('请先登录')
await router.push('/account')
return
}
try {
if (isFollowing.value) {
await social.unfollow(userId.value)
toast.info('已取关')
} else {
await social.follow(userId.value)
toast.success('已关注')
}
await loadSocialCounts()
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
}
}
type ListTab = 'followers' | 'following'
const drawer = reactive({
open: false,
tab: 'followers' as ListTab,
})
function openFollowers() {
drawer.tab = 'followers'
drawer.open = true
}
function openFollowing() {
drawer.tab = 'following'
drawer.open = true
}
function closeDrawer() {
drawer.open = false
}
const listTitle = computed(() => (drawer.tab === 'followers' ? '粉丝' : '关注'))
const listItems = computed(() => (drawer.tab === 'followers' ? state.followers : state.vloggers))
async function goUser(id: number) {
drawer.open = false
await router.push(`/u/${id}`)
}
async function goVideo(videoId: number) {
await router.push(`/video/${videoId}`)
}
watch(
() => route.params.id,
async () => {
drawer.open = false
await loadProfile()
},
)
watch(
() => auth.isLoggedIn,
async () => {
await loadSocialCounts()
},
)
onMounted(loadProfile)
</script>
<template>
<AppShell>
<div class="card">
<div class="row" style="justify-content: space-between; align-items: flex-start">
<div class="row" style="gap: 12px; align-items: center">
<UserAvatar :username="state.user?.username ?? 'User'" :id="state.user?.id ?? userId" :size="64" />
<div>
<div class="title" style="margin: 0">@{{ state.user?.username ?? '-' }}</div>
<div class="subtle mono">#{{ state.user?.id ?? userId }}</div>
</div>
</div>
<div class="row">
<button v-if="isMe" class="ghost" type="button" @click="router.push('/account')">我的账号</button>
<button v-else class="primary" type="button" :disabled="!state.user || state.loading" @click="toggleFollow">
{{ isFollowing ? '已关注' : '关注' }}
</button>
</div>
</div>
<div v-if="state.loading" class="hint" style="margin-top: 12px">加载中…</div>
<div v-else-if="state.error" class="hint bad" style="margin-top: 12px">{{ state.error }}</div>
<div v-else class="row" style="margin-top: 14px">
<button class="metric" type="button" :disabled="!auth.isLoggedIn || state.socialLoading" @click="openFollowers">
<div class="metric-num">{{ auth.isLoggedIn ? (state.socialLoading ? '…' : state.followers.length) : '—' }}</div>
<div class="metric-label">粉丝</div>
</button>
<button class="metric" type="button" :disabled="!auth.isLoggedIn || state.socialLoading" @click="openFollowing">
<div class="metric-num">{{ auth.isLoggedIn ? (state.socialLoading ? '…' : state.vloggers.length) : '—' }}</div>
<div class="metric-label">关注</div>
</button>
<div class="metric static">
<div class="metric-num">{{ state.videos.length }}</div>
<div class="metric-label">作品</div>
</div>
<div v-if="!auth.isLoggedIn" class="subtle" style="margin-left: 8px">登录后可查看粉丝/关注列表</div>
<div v-else-if="state.socialError" class="subtle" style="margin-left: 8px">社交信息加载失败:{{ state.socialError }}</div>
</div>
</div>
<div class="card" style="margin-top: 14px">
<div class="row" style="justify-content: space-between">
<p class="title" style="margin: 0">作品</p>
<div class="subtle">点击封面进入播放页</div>
</div>
<div v-if="state.videos.length === 0" class="hint" style="margin-top: 12px">暂无作品</div>
<div v-else class="video-grid" style="margin-top: 12px">
<button v-for="v in state.videos" :key="v.id" class="video-card" type="button" @click="goVideo(v.id)">
<img class="video-cover" :src="v.cover_url" :alt="v.title" loading="lazy" />
<div class="video-meta">
<div class="video-title">{{ v.title }}</div>
<div class="video-sub subtle">❤️ {{ v.likes_count }} · {{ new Date(v.create_time).toLocaleDateString() }}</div>
</div>
</button>
</div>
</div>
<div v-if="drawer.open" class="drawer-backdrop" @click.self="closeDrawer">
<div class="drawer">
<div class="drawer-head">
<div class="drawer-title">{{ listTitle }}</div>
<button class="drawer-x" type="button" @click="closeDrawer">×</button>
</div>
<div class="drawer-body">
<div v-if="state.socialLoading" class="drawer-hint">加载中…</div>
<div v-else-if="state.socialError" class="drawer-hint bad">{{ state.socialError }}</div>
<div v-else-if="listItems.length === 0" class="drawer-hint">暂无</div>
<button v-for="u in listItems" :key="u.id" class="user-row" type="button" @click="goUser(u.id)">
<UserAvatar :username="u.username" :id="u.id" :size="40" />
<div class="user-meta">
<div class="user-name">@{{ u.username }}</div>
<div class="user-id mono">#{{ u.id }}</div>
</div>
</button>
</div>
</div>
</div>
</AppShell>
</template>
<style scoped>
.ghost {
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(0, 0, 0, 0.18);
color: rgba(255, 255, 255, 0.86);
border-radius: 12px;
padding: 10px 12px;
cursor: pointer;
}
.ghost:hover {
background: rgba(255, 255, 255, 0.1);
}
.metric {
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.06);
border-radius: 16px;
padding: 12px 14px;
min-width: 120px;
cursor: pointer;
display: grid;
gap: 4px;
text-align: left;
}
.metric.static {
cursor: default;
}
.metric:hover {
background: rgba(255, 255, 255, 0.1);
}
.metric:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.metric-num {
font-size: 18px;
font-weight: 900;
letter-spacing: 0.2px;
}
.metric-label {
font-size: 12px;
color: rgba(255, 255, 255, 0.65);
}
.hint {
color: rgba(255, 255, 255, 0.78);
}
.hint.bad {
color: rgba(254, 44, 85, 0.92);
}
.video-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 12px;
}
@media (max-width: 1100px) {
.video-grid {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
@media (max-width: 800px) {
.video-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
.video-card {
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.05);
border-radius: 16px;
overflow: hidden;
cursor: pointer;
padding: 0;
text-align: left;
}
.video-card:hover {
background: rgba(255, 255, 255, 0.08);
}
.video-cover {
width: 100%;
aspect-ratio: 9/12;
object-fit: cover;
display: block;
background: rgba(0, 0, 0, 0.35);
}
.video-meta {
padding: 10px 10px;
}
.video-title {
font-weight: 800;
font-size: 13px;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.video-sub {
margin-top: 6px;
font-size: 12px;
}
.drawer-backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.55);
backdrop-filter: blur(10px);
z-index: 120;
display: grid;
justify-items: center;
align-items: center;
padding: 16px;
}
.drawer {
width: min(520px, calc(100vw - 18px));
max-height: min(78vh, 720px);
background: rgba(0, 0, 0, 0.65);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 18px;
overflow: hidden;
display: grid;
grid-template-rows: auto 1fr;
}
.drawer-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 14px 14px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.drawer-title {
font-weight: 900;
}
.drawer-x {
width: 34px;
height: 34px;
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(255, 255, 255, 0.06);
color: rgba(255, 255, 255, 0.9);
cursor: pointer;
font-size: 20px;
line-height: 1;
}
.drawer-body {
overflow: auto;
padding: 12px 14px;
display: grid;
gap: 10px;
}
.drawer-hint {
color: rgba(255, 255, 255, 0.78);
padding: 12px 0;
}
.drawer-hint.bad {
color: rgba(254, 44, 85, 0.92);
}
.user-row {
text-align: left;
display: grid;
grid-template-columns: auto 1fr;
gap: 12px;
align-items: center;
padding: 10px 10px;
border-radius: 14px;
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(255, 255, 255, 0.05);
cursor: pointer;
}
.user-row:hover {
background: rgba(255, 255, 255, 0.08);
}
.user-meta {
min-width: 0;
}
.user-name {
font-weight: 800;
}
.user-id {
font-size: 12px;
color: rgba(255, 255, 255, 0.6);
}
.mono {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
}
</style>
<script setup lang="ts">
import { computed, onMounted, reactive, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import UserAvatar from '../components/UserAvatar.vue'
import { ApiError } from '../api/client'
import * as accountApi from '../api/account'
import * as socialApi from '../api/social'
import type { Account, Video } from '../api/types'
import * as videoApi from '../api/video'
import { useAuthStore } from '../stores/auth'
import { useSocialStore } from '../stores/social'
import { useToastStore } from '../stores/toast'
const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const social = useSocialStore()
const toast = useToastStore()
const userId = computed(() => Number(route.params.id))
const myId = computed(() => auth.claims?.account_id ?? 0)
const isMe = computed(() => myId.value > 0 && myId.value === userId.value)
const state = reactive({
loading: false,
error: '',
user: null as Account | null,
videos: [] as Video[],
followers: [] as Account[],
vloggers: [] as Account[],
socialLoading: false,
socialError: '',
})
const isFollowing = computed(() => (auth.isLoggedIn ? social.isFollowing(userId.value) : false))
async function loadProfile() {
if (!Number.isFinite(userId.value) || userId.value <= 0) {
state.error = '无效的用户 id'
return
}
state.loading = true
state.error = ''
try {
const [u, vids] = await Promise.all([accountApi.findById(userId.value), videoApi.listByAuthorId(userId.value)])
state.user = u
state.videos = vids
} catch (e) {
state.error = e instanceof ApiError ? e.message : String(e)
state.user = null
state.videos = []
} finally {
state.loading = false
}
await loadSocialCounts()
}
async function loadSocialCounts() {
state.socialError = ''
state.followers = []
state.vloggers = []
if (!auth.isLoggedIn) return
if (!Number.isFinite(userId.value) || userId.value <= 0) return
state.socialLoading = true
try {
const [followersRes, vloggersRes] = await Promise.all([
socialApi.getAllFollowers(userId.value),
socialApi.getAllVloggers(userId.value),
])
state.followers = followersRes.followers
state.vloggers = vloggersRes.vloggers
} catch (e) {
state.socialError = e instanceof ApiError ? e.message : String(e)
} finally {
state.socialLoading = false
}
}
async function toggleFollow() {
if (isMe.value) return
if (!auth.isLoggedIn) {
toast.error('请先登录')
await router.push('/account')
return
}
try {
if (isFollowing.value) {
await social.unfollow(userId.value)
toast.info('已取关')
} else {
await social.follow(userId.value)
toast.success('已关注')
}
await loadSocialCounts()
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
}
}
type ListTab = 'followers' | 'following'
const drawer = reactive({
open: false,
tab: 'followers' as ListTab,
})
function openFollowers() {
drawer.tab = 'followers'
drawer.open = true
}
function openFollowing() {
drawer.tab = 'following'
drawer.open = true
}
function closeDrawer() {
drawer.open = false
}
const listTitle = computed(() => (drawer.tab === 'followers' ? '粉丝' : '关注'))
const listItems = computed(() => (drawer.tab === 'followers' ? state.followers : state.vloggers))
async function goUser(id: number) {
drawer.open = false
await router.push(`/u/${id}`)
}
async function goVideo(videoId: number) {
await router.push(`/video/${videoId}`)
}
watch(
() => route.params.id,
async () => {
drawer.open = false
await loadProfile()
},
)
watch(
() => auth.isLoggedIn,
async () => {
await loadSocialCounts()
},
)
onMounted(loadProfile)
</script>
<template>
<AppShell>
<div class="card">
<div class="row" style="justify-content: space-between; align-items: flex-start">
<div class="row" style="gap: 12px; align-items: center">
<UserAvatar :username="state.user?.username ?? 'User'" :id="state.user?.id ?? userId" :size="64" />
<div>
<div class="title" style="margin: 0">@{{ state.user?.username ?? '-' }}</div>
<div class="subtle mono">#{{ state.user?.id ?? userId }}</div>
</div>
</div>
<div class="row">
<button v-if="isMe" class="ghost" type="button" @click="router.push('/account')">我的账号</button>
<button v-else class="primary" type="button" :disabled="!state.user || state.loading" @click="toggleFollow">
{{ isFollowing ? '已关注' : '关注' }}
</button>
</div>
</div>
<div v-if="state.loading" class="hint" style="margin-top: 12px">加载中…</div>
<div v-else-if="state.error" class="hint bad" style="margin-top: 12px">{{ state.error }}</div>
<div v-else class="row" style="margin-top: 14px">
<button class="metric" type="button" :disabled="!auth.isLoggedIn || state.socialLoading" @click="openFollowers">
<div class="metric-num">{{ auth.isLoggedIn ? (state.socialLoading ? '…' : state.followers.length) : '—' }}</div>
<div class="metric-label">粉丝</div>
</button>
<button class="metric" type="button" :disabled="!auth.isLoggedIn || state.socialLoading" @click="openFollowing">
<div class="metric-num">{{ auth.isLoggedIn ? (state.socialLoading ? '…' : state.vloggers.length) : '—' }}</div>
<div class="metric-label">关注</div>
</button>
<div class="metric static">
<div class="metric-num">{{ state.videos.length }}</div>
<div class="metric-label">作品</div>
</div>
<div v-if="!auth.isLoggedIn" class="subtle" style="margin-left: 8px">登录后可查看粉丝/关注列表</div>
<div v-else-if="state.socialError" class="subtle" style="margin-left: 8px">社交信息加载失败:{{ state.socialError }}</div>
</div>
</div>
<div class="card" style="margin-top: 14px">
<div class="row" style="justify-content: space-between">
<p class="title" style="margin: 0">作品</p>
<div class="subtle">点击封面进入播放页</div>
</div>
<div v-if="state.videos.length === 0" class="hint" style="margin-top: 12px">暂无作品</div>
<div v-else class="video-grid" style="margin-top: 12px">
<button v-for="v in state.videos" :key="v.id" class="video-card" type="button" @click="goVideo(v.id)">
<img class="video-cover" :src="v.cover_url" :alt="v.title" loading="lazy" />
<div class="video-meta">
<div class="video-title">{{ v.title }}</div>
<div class="video-sub subtle">❤️ {{ v.likes_count }} · {{ new Date(v.create_time).toLocaleDateString() }}</div>
</div>
</button>
</div>
</div>
<div v-if="drawer.open" class="drawer-backdrop" @click.self="closeDrawer">
<div class="drawer">
<div class="drawer-head">
<div class="drawer-title">{{ listTitle }}</div>
<button class="drawer-x" type="button" @click="closeDrawer">×</button>
</div>
<div class="drawer-body">
<div v-if="state.socialLoading" class="drawer-hint">加载中…</div>
<div v-else-if="state.socialError" class="drawer-hint bad">{{ state.socialError }}</div>
<div v-else-if="listItems.length === 0" class="drawer-hint">暂无</div>
<button v-for="u in listItems" :key="u.id" class="user-row" type="button" @click="goUser(u.id)">
<UserAvatar :username="u.username" :id="u.id" :size="40" />
<div class="user-meta">
<div class="user-name">@{{ u.username }}</div>
<div class="user-id mono">#{{ u.id }}</div>
</div>
</button>
</div>
</div>
</div>
</AppShell>
</template>
<style scoped>
.ghost {
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(0, 0, 0, 0.18);
color: rgba(255, 255, 255, 0.86);
border-radius: 12px;
padding: 10px 12px;
cursor: pointer;
}
.ghost:hover {
background: rgba(255, 255, 255, 0.1);
}
.metric {
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.06);
border-radius: 16px;
padding: 12px 14px;
min-width: 120px;
cursor: pointer;
display: grid;
gap: 4px;
text-align: left;
}
.metric.static {
cursor: default;
}
.metric:hover {
background: rgba(255, 255, 255, 0.1);
}
.metric:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.metric-num {
font-size: 18px;
font-weight: 900;
letter-spacing: 0.2px;
}
.metric-label {
font-size: 12px;
color: rgba(255, 255, 255, 0.65);
}
.hint {
color: rgba(255, 255, 255, 0.78);
}
.hint.bad {
color: rgba(254, 44, 85, 0.92);
}
.video-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 12px;
}
@media (max-width: 1100px) {
.video-grid {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
@media (max-width: 800px) {
.video-grid {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
.video-card {
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.05);
border-radius: 16px;
overflow: hidden;
cursor: pointer;
padding: 0;
text-align: left;
}
.video-card:hover {
background: rgba(255, 255, 255, 0.08);
}
.video-cover {
width: 100%;
aspect-ratio: 9/12;
object-fit: cover;
display: block;
background: rgba(0, 0, 0, 0.35);
}
.video-meta {
padding: 10px 10px;
}
.video-title {
font-weight: 800;
font-size: 13px;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.video-sub {
margin-top: 6px;
font-size: 12px;
}
.drawer-backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.55);
backdrop-filter: blur(10px);
z-index: 120;
display: grid;
justify-items: center;
align-items: center;
padding: 16px;
}
.drawer {
width: min(520px, calc(100vw - 18px));
max-height: min(78vh, 720px);
background: rgba(0, 0, 0, 0.65);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 18px;
overflow: hidden;
display: grid;
grid-template-rows: auto 1fr;
}
.drawer-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 14px 14px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.drawer-title {
font-weight: 900;
}
.drawer-x {
width: 34px;
height: 34px;
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(255, 255, 255, 0.06);
color: rgba(255, 255, 255, 0.9);
cursor: pointer;
font-size: 20px;
line-height: 1;
}
.drawer-body {
overflow: auto;
padding: 12px 14px;
display: grid;
gap: 10px;
}
.drawer-hint {
color: rgba(255, 255, 255, 0.78);
padding: 12px 0;
}
.drawer-hint.bad {
color: rgba(254, 44, 85, 0.92);
}
.user-row {
text-align: left;
display: grid;
grid-template-columns: auto 1fr;
gap: 12px;
align-items: center;
padding: 10px 10px;
border-radius: 14px;
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(255, 255, 255, 0.05);
cursor: pointer;
}
.user-row:hover {
background: rgba(255, 255, 255, 0.08);
}
.user-meta {
min-width: 0;
}
.user-name {
font-weight: 800;
}
.user-id {
font-size: 12px;
color: rgba(255, 255, 255, 0.6);
}
.mono {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
}
</style>

File diff suppressed because it is too large Load Diff

View File

@@ -1,346 +1,346 @@
<script setup lang="ts">
import { onUnmounted, reactive, ref, watch } from 'vue'
import { RouterLink, useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import { ApiError } from '../api/client'
import * as videoApi from '../api/video'
import type { Video } from '../api/types'
import { useAuthStore } from '../stores/auth'
import { useToastStore } from '../stores/toast'
const router = useRouter()
const auth = useAuthStore()
const toast = useToastStore()
const busy = ref(false)
const stage = ref('')
const published = ref<Video | null>(null)
const videoInput = ref<HTMLInputElement | null>(null)
const coverInput = ref<HTMLInputElement | null>(null)
const publishForm = reactive({
title: '',
description: '',
video: null as File | null,
cover: null as File | null,
})
const preview = reactive({
videoUrl: '',
coverUrl: '',
})
function setPreviewVideo(file: File | null) {
if (preview.videoUrl) URL.revokeObjectURL(preview.videoUrl)
preview.videoUrl = file ? URL.createObjectURL(file) : ''
}
function setPreviewCover(file: File | null) {
if (preview.coverUrl) URL.revokeObjectURL(preview.coverUrl)
preview.coverUrl = file ? URL.createObjectURL(file) : ''
}
watch(
() => publishForm.video,
(f) => setPreviewVideo(f),
)
watch(
() => publishForm.cover,
(f) => setPreviewCover(f),
)
onUnmounted(() => {
setPreviewVideo(null)
setPreviewCover(null)
})
function pickVideo(e: Event) {
const input = e.target as HTMLInputElement
publishForm.video = input.files?.[0] ?? null
}
function pickCover(e: Event) {
const input = e.target as HTMLInputElement
publishForm.cover = input.files?.[0] ?? null
}
function openVideoPicker() {
videoInput.value?.click()
}
function openCoverPicker() {
coverInput.value?.click()
}
function clearVideo() {
publishForm.video = null
if (videoInput.value) videoInput.value.value = ''
}
function clearCover() {
publishForm.cover = null
if (coverInput.value) coverInput.value.value = ''
}
async function onPublish() {
if (busy.value) return
if (!auth.isLoggedIn) {
toast.error('请先登录')
await router.push('/account')
return
}
const title = publishForm.title.trim()
const description = publishForm.description.trim()
if (!title) {
toast.error('请输入 title')
return
}
if (!publishForm.video) {
toast.error('请选择视频文件(.mp4)')
return
}
if (!publishForm.cover) {
toast.error('请选择封面图片(jpg/png/webp)')
return
}
busy.value = true
stage.value = ''
published.value = null
try {
stage.value = '上传封面'
const coverRes = await videoApi.uploadCover(publishForm.cover!)
stage.value = '上传视频'
const videoRes = await videoApi.uploadVideo(publishForm.video!)
const coverUrl = coverRes.url || coverRes.cover_url || ''
const playUrl = videoRes.url || videoRes.play_url || ''
if (!coverUrl || !playUrl) {
toast.error('上传成功但缺少 url')
return
}
stage.value = '发布视频'
const res = await videoApi.publishVideo({ title, description, play_url: playUrl, cover_url: coverUrl })
published.value = res
toast.success('已发布')
publishForm.title = ''
publishForm.description = ''
clearVideo()
clearCover()
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
busy.value = false
stage.value = ''
}
}
</script>
<template>
<AppShell>
<div class="publish-wrap">
<div class="card publish-card">
<div class="row" style="justify-content: space-between; align-items: baseline">
<p class="title" style="margin: 0">发布视频</p>
<div v-if="busy" class="pill">进行中:{{ stage || '…' }}</div>
</div>
<p class="subtle" style="margin-top: 10px">选择视频文件与封面图片,上传到本机后自动生成 URL,再写入 `/video/publish`。</p>
<div class="grid form-grid" style="margin-top: 16px">
<div>
<label>title</label>
<input v-model.trim="publishForm.title" class="big-input" :disabled="busy" />
</div>
<div>
<label>description</label>
<textarea v-model.trim="publishForm.description" class="big-input" :disabled="busy" />
</div>
<div class="grid two">
<div>
<label>video (.mp4)</label>
<input ref="videoInput" class="file-native" type="file" accept="video/mp4" :disabled="busy" @change="pickVideo" />
<div class="file-box">
<button type="button" :disabled="busy" @click="openVideoPicker">选择视频</button>
<div class="file-name" :class="publishForm.video ? '' : 'muted'">
{{ publishForm.video ? publishForm.video.name : '未选择文件' }}
</div>
<button v-if="publishForm.video" type="button" :disabled="busy" @click="clearVideo">清除</button>
</div>
<div v-if="publishForm.video" class="subtle" style="margin-top: 6px">
已选择:{{ publishForm.video.name }}({{ Math.ceil(publishForm.video.size / 1024 / 1024) }} MB)
</div>
</div>
<div>
<label>cover (jpg/png/webp)</label>
<input
ref="coverInput"
class="file-native"
type="file"
accept="image/jpeg,image/png,image/webp"
:disabled="busy"
@change="pickCover"
/>
<div class="file-box">
<button type="button" :disabled="busy" @click="openCoverPicker">选择封面</button>
<div class="file-name" :class="publishForm.cover ? '' : 'muted'">
{{ publishForm.cover ? publishForm.cover.name : '未选择文件' }}
</div>
<button v-if="publishForm.cover" type="button" :disabled="busy" @click="clearCover">清除</button>
</div>
<div v-if="publishForm.cover" class="subtle" style="margin-top: 6px">已选择:{{ publishForm.cover.name }}</div>
</div>
</div>
<div v-if="preview.coverUrl || preview.videoUrl" class="grid two">
<div v-if="preview.coverUrl" class="preview-card">
<div class="subtle">封面预览</div>
<img class="cover" :src="preview.coverUrl" alt="cover preview" />
</div>
<div v-if="preview.videoUrl" class="preview-card">
<div class="subtle">视频预览</div>
<video class="video" :src="preview.videoUrl" controls playsinline preload="metadata" />
</div>
</div>
<div class="row" style="justify-content: flex-end; margin-top: 8px">
<button class="primary big-btn" type="button" :disabled="busy" @click="onPublish">发布</button>
</div>
</div>
<div v-if="published" class="card" style="margin-top: 14px">
<p class="title">已发布</p>
<div class="row" style="justify-content: space-between">
<div>
<div class="title" style="margin: 0">{{ published.title }}</div>
<div class="subtle mono">#{{ published.id }}</div>
</div>
<div class="row">
<RouterLink class="pill" :to="`/video/${published.id}`">去播放</RouterLink>
<a class="pill mono" :href="published.play_url" target="_blank" rel="noreferrer">play_url</a>
<a class="pill mono" :href="published.cover_url" target="_blank" rel="noreferrer">cover_url</a>
</div>
</div>
</div>
</div>
</div>
</AppShell>
</template>
<style scoped>
.publish-wrap {
display: grid;
justify-items: center;
}
.publish-card {
width: min(980px, 100%);
padding: 22px;
}
.form-grid {
gap: 16px;
}
.form-grid .grid.two {
gap: 20px;
}
.form-grid .grid.two > * {
min-width: 0;
}
.form-grid input[type='file'] {
max-width: 100%;
}
.file-native {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
.file-box {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.06);
border-radius: 14px;
min-height: 46px;
}
.file-box button {
padding: 8px 10px;
border-radius: 12px;
}
.file-name {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 13px;
color: rgba(255, 255, 255, 0.88);
}
.muted {
color: rgba(255, 255, 255, 0.55);
}
.big-input {
box-sizing: border-box;
width: 100%;
max-width: 100%;
padding: 12px 14px;
font-size: 14px;
border-radius: 14px;
}
.big-btn {
padding: 12px 18px;
font-size: 14px;
border-radius: 14px;
}
.preview-card {
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.05);
border-radius: 16px;
padding: 12px;
display: grid;
gap: 10px;
}
.cover {
width: 100%;
aspect-ratio: 9/12;
object-fit: cover;
border-radius: 14px;
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.35);
}
.video {
width: 100%;
border-radius: 14px;
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.35);
}
</style>
<script setup lang="ts">
import { onUnmounted, reactive, ref, watch } from 'vue'
import { RouterLink, useRouter } from 'vue-router'
import AppShell from '../components/AppShell.vue'
import { ApiError } from '../api/client'
import * as videoApi from '../api/video'
import type { Video } from '../api/types'
import { useAuthStore } from '../stores/auth'
import { useToastStore } from '../stores/toast'
const router = useRouter()
const auth = useAuthStore()
const toast = useToastStore()
const busy = ref(false)
const stage = ref('')
const published = ref<Video | null>(null)
const videoInput = ref<HTMLInputElement | null>(null)
const coverInput = ref<HTMLInputElement | null>(null)
const publishForm = reactive({
title: '',
description: '',
video: null as File | null,
cover: null as File | null,
})
const preview = reactive({
videoUrl: '',
coverUrl: '',
})
function setPreviewVideo(file: File | null) {
if (preview.videoUrl) URL.revokeObjectURL(preview.videoUrl)
preview.videoUrl = file ? URL.createObjectURL(file) : ''
}
function setPreviewCover(file: File | null) {
if (preview.coverUrl) URL.revokeObjectURL(preview.coverUrl)
preview.coverUrl = file ? URL.createObjectURL(file) : ''
}
watch(
() => publishForm.video,
(f) => setPreviewVideo(f),
)
watch(
() => publishForm.cover,
(f) => setPreviewCover(f),
)
onUnmounted(() => {
setPreviewVideo(null)
setPreviewCover(null)
})
function pickVideo(e: Event) {
const input = e.target as HTMLInputElement
publishForm.video = input.files?.[0] ?? null
}
function pickCover(e: Event) {
const input = e.target as HTMLInputElement
publishForm.cover = input.files?.[0] ?? null
}
function openVideoPicker() {
videoInput.value?.click()
}
function openCoverPicker() {
coverInput.value?.click()
}
function clearVideo() {
publishForm.video = null
if (videoInput.value) videoInput.value.value = ''
}
function clearCover() {
publishForm.cover = null
if (coverInput.value) coverInput.value.value = ''
}
async function onPublish() {
if (busy.value) return
if (!auth.isLoggedIn) {
toast.error('请先登录')
await router.push('/account')
return
}
const title = publishForm.title.trim()
const description = publishForm.description.trim()
if (!title) {
toast.error('请输入 title')
return
}
if (!publishForm.video) {
toast.error('请选择视频文件(.mp4)')
return
}
if (!publishForm.cover) {
toast.error('请选择封面图片(jpg/png/webp)')
return
}
busy.value = true
stage.value = ''
published.value = null
try {
stage.value = '上传封面'
const coverRes = await videoApi.uploadCover(publishForm.cover!)
stage.value = '上传视频'
const videoRes = await videoApi.uploadVideo(publishForm.video!)
const coverUrl = coverRes.url || coverRes.cover_url || ''
const playUrl = videoRes.url || videoRes.play_url || ''
if (!coverUrl || !playUrl) {
toast.error('上传成功但缺少 url')
return
}
stage.value = '发布视频'
const res = await videoApi.publishVideo({ title, description, play_url: playUrl, cover_url: coverUrl })
published.value = res
toast.success('已发布')
publishForm.title = ''
publishForm.description = ''
clearVideo()
clearCover()
} catch (e) {
const msg = e instanceof ApiError ? e.message : String(e)
toast.error(msg)
} finally {
busy.value = false
stage.value = ''
}
}
</script>
<template>
<AppShell>
<div class="publish-wrap">
<div class="card publish-card">
<div class="row" style="justify-content: space-between; align-items: baseline">
<p class="title" style="margin: 0">发布视频</p>
<div v-if="busy" class="pill">进行中:{{ stage || '…' }}</div>
</div>
<p class="subtle" style="margin-top: 10px">选择视频文件与封面图片,上传到本机后自动生成 URL,再写入 `/video/publish`。</p>
<div class="grid form-grid" style="margin-top: 16px">
<div>
<label>title</label>
<input v-model.trim="publishForm.title" class="big-input" :disabled="busy" />
</div>
<div>
<label>description</label>
<textarea v-model.trim="publishForm.description" class="big-input" :disabled="busy" />
</div>
<div class="grid two">
<div>
<label>video (.mp4)</label>
<input ref="videoInput" class="file-native" type="file" accept="video/mp4" :disabled="busy" @change="pickVideo" />
<div class="file-box">
<button type="button" :disabled="busy" @click="openVideoPicker">选择视频</button>
<div class="file-name" :class="publishForm.video ? '' : 'muted'">
{{ publishForm.video ? publishForm.video.name : '未选择文件' }}
</div>
<button v-if="publishForm.video" type="button" :disabled="busy" @click="clearVideo">清除</button>
</div>
<div v-if="publishForm.video" class="subtle" style="margin-top: 6px">
已选择:{{ publishForm.video.name }}({{ Math.ceil(publishForm.video.size / 1024 / 1024) }} MB)
</div>
</div>
<div>
<label>cover (jpg/png/webp)</label>
<input
ref="coverInput"
class="file-native"
type="file"
accept="image/jpeg,image/png,image/webp"
:disabled="busy"
@change="pickCover"
/>
<div class="file-box">
<button type="button" :disabled="busy" @click="openCoverPicker">选择封面</button>
<div class="file-name" :class="publishForm.cover ? '' : 'muted'">
{{ publishForm.cover ? publishForm.cover.name : '未选择文件' }}
</div>
<button v-if="publishForm.cover" type="button" :disabled="busy" @click="clearCover">清除</button>
</div>
<div v-if="publishForm.cover" class="subtle" style="margin-top: 6px">已选择:{{ publishForm.cover.name }}</div>
</div>
</div>
<div v-if="preview.coverUrl || preview.videoUrl" class="grid two">
<div v-if="preview.coverUrl" class="preview-card">
<div class="subtle">封面预览</div>
<img class="cover" :src="preview.coverUrl" alt="cover preview" />
</div>
<div v-if="preview.videoUrl" class="preview-card">
<div class="subtle">视频预览</div>
<video class="video" :src="preview.videoUrl" controls playsinline preload="metadata" />
</div>
</div>
<div class="row" style="justify-content: flex-end; margin-top: 8px">
<button class="primary big-btn" type="button" :disabled="busy" @click="onPublish">发布</button>
</div>
</div>
<div v-if="published" class="card" style="margin-top: 14px">
<p class="title">已发布</p>
<div class="row" style="justify-content: space-between">
<div>
<div class="title" style="margin: 0">{{ published.title }}</div>
<div class="subtle mono">#{{ published.id }}</div>
</div>
<div class="row">
<RouterLink class="pill" :to="`/video/${published.id}`">去播放</RouterLink>
<a class="pill mono" :href="published.play_url" target="_blank" rel="noreferrer">play_url</a>
<a class="pill mono" :href="published.cover_url" target="_blank" rel="noreferrer">cover_url</a>
</div>
</div>
</div>
</div>
</div>
</AppShell>
</template>
<style scoped>
.publish-wrap {
display: grid;
justify-items: center;
}
.publish-card {
width: min(980px, 100%);
padding: 22px;
}
.form-grid {
gap: 16px;
}
.form-grid .grid.two {
gap: 20px;
}
.form-grid .grid.two > * {
min-width: 0;
}
.form-grid input[type='file'] {
max-width: 100%;
}
.file-native {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
.file-box {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.06);
border-radius: 14px;
min-height: 46px;
}
.file-box button {
padding: 8px 10px;
border-radius: 12px;
}
.file-name {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 13px;
color: rgba(255, 255, 255, 0.88);
}
.muted {
color: rgba(255, 255, 255, 0.55);
}
.big-input {
box-sizing: border-box;
width: 100%;
max-width: 100%;
padding: 12px 14px;
font-size: 14px;
border-radius: 14px;
}
.big-btn {
padding: 12px 18px;
font-size: 14px;
border-radius: 14px;
}
.preview-card {
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.05);
border-radius: 16px;
padding: 12px;
display: grid;
gap: 10px;
}
.cover {
width: 100%;
aspect-ratio: 9/12;
object-fit: cover;
border-radius: 14px;
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.35);
}
.video {
width: 100%;
border-radius: 14px;
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.35);
}
</style>