feat: 头像上传/修改 + 个人资料连贯设置面板

- 头像:users.avatar 列 + POST /api/auth/avatar(OSS 上传/清除)+ toPublicUser 返回 avatar;
  user store updateAvatar + avatar 计算(自定义优先,ui-avatars 兜底)
- 共享 saveImage 工具(admin 上传与头像复用,消除重复)
- 个人资料布局:单一连贯设置面板(divide-y 分区:基本信息含头像编辑 / 安全设置 / 危险区),
  去掉小卡片堆叠;i18n 头像键
This commit is contained in:
2026-08-06 19:40:41 +08:00
parent f1d2a405f9
commit b9ffcdb874
9 changed files with 306 additions and 137 deletions
+139 -92
View File
@@ -63,6 +63,37 @@ const saveProfile = async () => {
: { ok: false, text: apiError({ data: { code: result.code } }) }
}
/* ---------- 头像 ---------- */
const avatarUpdating = ref(false)
const avatarMsg = ref<{ ok: boolean; text: string } | null>(null)
const onAvatarChange = async (e: Event) => {
const input = e.target as HTMLInputElement
const file = input.files?.[0]
if (!file) return
if (!/^image\//.test(file.type) || file.size > 8 * 1024 * 1024) {
avatarMsg.value = { ok: false, text: t('account.avatar.error') }
input.value = ''
return
}
avatarUpdating.value = true
avatarMsg.value = null
const result = await userStore.updateAvatar(file)
avatarUpdating.value = false
input.value = ''
avatarMsg.value = result.success
? { ok: true, text: t('account.avatar.success') }
: { ok: false, text: t('account.avatar.error') }
}
const removeAvatar = async () => {
avatarUpdating.value = true
avatarMsg.value = null
const result = await userStore.updateAvatar(null)
avatarUpdating.value = false
avatarMsg.value = result.success
? { ok: true, text: t('account.avatar.success') }
: { ok: false, text: t('account.avatar.error') }
}
/* ---------- 修改密码 ---------- */
const pwForm = reactive({ currentPassword: '', newPassword: '', confirmPassword: '' })
const pwSaving = ref(false)
@@ -272,106 +303,122 @@ onBeforeUnmount(() => ctx?.revert())
<!-- 内容 -->
<div class="mt-8 md:mt-10">
<Transition name="panel" mode="out-in">
<!-- 个人资料 -->
<section v-if="activeTab === 'profile'" key="profile" class="space-y-6 max-w-2xl mx-auto">
<!-- 资料编辑 -->
<div class="rounded-2xl border border-gray-100 bg-white shadow-sm overflow-hidden">
<div class="px-6 md:px-8 py-5 border-b border-gray-100">
<!-- 个人资料(单一设置面板,内部分区) -->
<section v-if="activeTab === 'profile'" key="profile" class="max-w-2xl mx-auto">
<div class="rounded-3xl border border-gray-100 bg-white shadow-sm overflow-hidden divide-y divide-gray-100">
<!-- 基本信息 -->
<div class="px-6 md:px-8 py-7">
<h3 class="text-lg font-bold text-black">{{ t('account.profile') }}</h3>
<p class="text-sm text-gray-400 mt-0.5">个人基本信息</p>
</div>
<div class="px-6 md:px-8 py-6 space-y-5">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.username') }}</label>
<input v-model="profileForm.username" type="text"
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.phone') }}</label>
<input v-model="profileForm.phone" type="tel"
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.email') }}</label>
<input :value="userStore.user?.email ?? ''" type="email" disabled
class="w-full rounded-xl border border-gray-200 bg-gray-50 px-3.5 py-2.5 text-sm text-gray-500 cursor-not-allowed" />
</div>
<p v-if="profileMsg" class="text-sm" :class="profileMsg.ok ? 'text-green-600' : 'text-red-600'">{{ profileMsg.text }}</p>
<div class="flex justify-end">
<button
@click="saveProfile"
:disabled="profileSaving"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors disabled:opacity-50"
>
<Icon v-if="profileSaving" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ profileSaving ? t('account.saving') : t('account.save') }}
</button>
</div>
</div>
</div>
<p class="text-sm text-gray-400 mt-0.5">个人基本信息与头像</p>
<!-- 修改密码 -->
<div class="rounded-2xl border border-gray-100 bg-white shadow-sm overflow-hidden">
<div class="px-6 md:px-8 py-5 border-b border-gray-100">
<div class="mt-6 flex items-center gap-6">
<img :src="userStore.avatar" :alt="userStore.user?.username"
class="w-20 h-20 rounded-2xl object-cover ring-1 ring-black/10 flex-shrink-0" />
<div class="space-y-2.5">
<label class="inline-flex cursor-pointer">
<input type="file" accept="image/*" class="hidden" @change="onAvatarChange" />
<span
class="inline-flex items-center gap-1.5 px-4 py-2 rounded-full border border-gray-200 text-sm font-semibold text-gray-700 hover:border-black hover:text-black transition-colors"
>
<Icon v-if="avatarUpdating" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
<Icon v-else name="ph:camera" class="w-4 h-4" />
{{ t('account.avatar.change') }}
</span>
</label>
<button
v-if="userStore.user?.avatar"
@click="removeAvatar"
:disabled="avatarUpdating"
class="block text-sm text-gray-400 hover:text-red-600 transition-colors disabled:opacity-50"
>
{{ t('account.avatar.remove') }}
</button>
</div>
</div>
<p v-if="avatarMsg" class="mt-3 text-sm" :class="avatarMsg.ok ? 'text-green-600' : 'text-red-600'">{{ avatarMsg.text }}</p>
<div class="mt-7 space-y-5">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.username') }}</label>
<input v-model="profileForm.username" type="text"
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.phone') }}</label>
<input v-model="profileForm.phone" type="tel"
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.email') }}</label>
<input :value="userStore.user?.email ?? ''" type="email" disabled
class="w-full rounded-xl border border-gray-200 bg-gray-50 px-3.5 py-2.5 text-sm text-gray-500 cursor-not-allowed" />
</div>
<p v-if="profileMsg" class="text-sm" :class="profileMsg.ok ? 'text-green-600' : 'text-red-600'">{{ profileMsg.text }}</p>
<div class="flex justify-end">
<button @click="saveProfile" :disabled="profileSaving"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full bg-black text-white text-sm font-semibold hover:bg-gray-800 transition-colors disabled:opacity-50">
<Icon v-if="profileSaving" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ profileSaving ? t('account.saving') : t('account.save') }}
</button>
</div>
</div>
</div>
<!-- 安全设置 -->
<div class="px-6 md:px-8 py-7">
<h3 class="text-lg font-bold text-black">{{ t('account.changePassword.title') }}</h3>
<p class="text-sm text-gray-400 mt-0.5">定期更换密码更安全</p>
<form class="mt-6 space-y-5" @submit.prevent="changePassword">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.changePassword.currentPassword') }}</label>
<input v-model="pwForm.currentPassword" type="password" autocomplete="current-password" required
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-5">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.changePassword.newPassword') }}</label>
<input v-model="pwForm.newPassword" type="password" autocomplete="new-password" required
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.changePassword.confirmPassword') }}</label>
<input v-model="pwForm.confirmPassword" type="password" autocomplete="new-password" required
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
</div>
<p v-if="pwError" class="text-sm text-red-600">{{ pwError }}</p>
<p v-if="pwOk" class="text-sm text-green-600">{{ t('account.changePassword.success') }}</p>
<div class="flex justify-end">
<button type="submit" :disabled="pwSaving"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full border border-gray-200 text-gray-700 text-sm font-semibold hover:border-black hover:text-black transition-colors disabled:opacity-50">
<Icon v-if="pwSaving" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ pwSaving ? t('account.changePassword.saving') : t('account.changePassword.submit') }}
</button>
</div>
</form>
</div>
<form class="px-6 md:px-8 py-6 space-y-5" @submit.prevent="changePassword">
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.changePassword.currentPassword') }}</label>
<input v-model="pwForm.currentPassword" type="password" autocomplete="current-password" required
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.changePassword.newPassword') }}</label>
<input v-model="pwForm.newPassword" type="password" autocomplete="new-password" required
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.changePassword.confirmPassword') }}</label>
<input v-model="pwForm.confirmPassword" type="password" autocomplete="new-password" required
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-black focus:ring-2 focus:ring-black/10" />
</div>
<p v-if="pwError" class="text-sm text-red-600">{{ pwError }}</p>
<p v-if="pwOk" class="text-sm text-green-600">{{ t('account.changePassword.success') }}</p>
<div class="flex justify-end">
<button
type="submit"
:disabled="pwSaving"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full border border-gray-200 text-gray-700 text-sm font-semibold hover:border-black hover:text-black transition-colors disabled:opacity-50"
>
<Icon v-if="pwSaving" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ pwSaving ? t('account.changePassword.saving') : t('account.changePassword.submit') }}
</button>
</div>
</form>
</div>
<!-- 删除账号(克制危险区) -->
<div class="rounded-2xl border border-red-200 bg-red-50/40 overflow-hidden">
<div class="px-6 md:px-8 py-5 border-b border-red-100">
<!-- 危险操作 -->
<div class="px-6 md:px-8 py-7 bg-red-50/40">
<h3 class="text-lg font-bold text-red-600">{{ t('account.deleteAccount.title') }}</h3>
<p class="text-sm text-red-400 mt-0.5">危险操作</p>
</div>
<div class="px-6 md:px-8 py-6 space-y-5">
<p class="text-sm text-gray-600 leading-relaxed">{{ t('account.deleteAccount.hint') }}</p>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('auth.password') }}</label>
<input v-model="deletePassword" type="password" autocomplete="current-password"
:placeholder="t('account.deleteAccount.passwordPlaceholder')"
@keydown.enter.prevent="askDeleteAccount"
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-red-400 focus:ring-2 focus:ring-red-500/10" />
</div>
<p v-if="deleteError" class="text-sm text-red-600">{{ deleteError }}</p>
<div class="flex justify-end">
<button
@click="askDeleteAccount"
:disabled="deleting"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full bg-red-600 text-white text-sm font-semibold hover:bg-red-700 transition-colors disabled:opacity-50"
>
<Icon v-if="deleting" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ t('account.deleteAccount.confirmButton') }}
</button>
<div class="mt-6 space-y-5">
<p class="text-sm text-gray-600 leading-relaxed">{{ t('account.deleteAccount.hint') }}</p>
<div>
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('auth.password') }}</label>
<input v-model="deletePassword" type="password" autocomplete="current-password"
:placeholder="t('account.deleteAccount.passwordPlaceholder')"
@keydown.enter.prevent="askDeleteAccount"
class="w-full rounded-xl border border-gray-200 bg-white px-3.5 py-2.5 text-sm outline-none transition-all focus:border-red-400 focus:ring-2 focus:ring-red-500/10" />
</div>
<p v-if="deleteError" class="text-sm text-red-600">{{ deleteError }}</p>
<div class="flex justify-end">
<button @click="askDeleteAccount" :disabled="deleting"
class="inline-flex items-center gap-2 px-6 py-2.5 rounded-full bg-red-600 text-white text-sm font-semibold hover:bg-red-700 transition-colors disabled:opacity-50">
<Icon v-if="deleting" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
{{ t('account.deleteAccount.confirmButton') }}
</button>
</div>
</div>
</div>
</div>