feat: 用户资料改回自同步 Casimir(昵称/邮箱)+ 头像改走 Casimir upload-resource
- 站内改昵称/邮箱/头像后,用自己的 access_token 写回 Casimir(update-user,字段每批≤2,401 自动 refresh_token 续期) - 头像不再本地存储:上传直调 Casimir upload-resource (tag=avatar),成功后自动写回 user.Avatar,仅落库返回 URL - me.patch 头像仅存档本地,避免 update-user 重复同步 - 头像上传仅走 Casimir,未配置 Storage provider 时报错提示 - exchangeCodeForIdentity 补 owner/displayName/token,昵称优先 displayName - 新增类型检查依赖 typescript/vue-tsc 与 typecheck 脚本
This commit is contained in:
+52
-7
@@ -3,7 +3,7 @@
|
||||
* 个人中心(重新设计)—— 对齐站点黑白编辑风:极简、大排版、充足留白。
|
||||
* - 资料区:头像 + 大号姓名 + 邮箱 + 角色/加入时间 + 三项统计(细分隔线)
|
||||
* - 导航:下划线页签
|
||||
* - profile:资料编辑(仅用户名;账号信息由 Casdoor 管理)
|
||||
* - profile:资料编辑(昵称/头像/邮箱,改后同步回统一认证中心)
|
||||
* - favorites:作品封面网格;comments:评论列表
|
||||
*/
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
|
||||
@@ -45,21 +45,57 @@ const switchTab = (key: string) => {
|
||||
|
||||
/* ---------- 资料编辑 ---------- */
|
||||
const profileForm = reactive({ username: '' })
|
||||
const profileEmail = ref('')
|
||||
const profileAvatar = ref('')
|
||||
const avatarUploading = ref(false)
|
||||
const profileSaving = ref(false)
|
||||
const profileMsg = ref<{ ok: boolean; text: string } | null>(null)
|
||||
const initProfileForm = () => {
|
||||
profileForm.username = userStore.user?.username ?? ''
|
||||
profileEmail.value = userStore.user?.email ?? ''
|
||||
profileAvatar.value = userStore.user?.avatar ?? ''
|
||||
}
|
||||
const saveProfile = async () => {
|
||||
profileSaving.value = true
|
||||
profileMsg.value = null
|
||||
const result = await userStore.updateProfile({ username: profileForm.username })
|
||||
const result = await userStore.updateProfile({
|
||||
username: profileForm.username,
|
||||
email: profileEmail.value,
|
||||
avatar: profileAvatar.value || undefined,
|
||||
})
|
||||
profileSaving.value = false
|
||||
profileMsg.value = result.success
|
||||
? { ok: true, text: t('account.saved') }
|
||||
: { ok: false, text: apiError({ data: { code: result.code } }) }
|
||||
}
|
||||
|
||||
/* 头像选择并上传,成功后写入表单待保存 */
|
||||
const profileFileInput = ref<HTMLInputElement | null>(null)
|
||||
const triggerAvatarPick = () => profileFileInput.value?.click()
|
||||
const onAvatarPick = async (event: Event) => {
|
||||
const input = event.target as HTMLInputElement
|
||||
const file = input.files?.[0]
|
||||
input.value = ''
|
||||
if (!file) return
|
||||
if (!file.type.startsWith('image/')) {
|
||||
profileMsg.value = { ok: false, text: t('account.avatar.error') }
|
||||
return
|
||||
}
|
||||
avatarUploading.value = true
|
||||
profileMsg.value = null
|
||||
try {
|
||||
const fd = new FormData()
|
||||
fd.append('file', file)
|
||||
const data = await $fetch<{ url: string }>('/api/auth/avatar', { method: 'POST', body: fd })
|
||||
profileAvatar.value = data.url
|
||||
profileMsg.value = { ok: true, text: t('account.avatar.success') }
|
||||
} catch (err) {
|
||||
profileMsg.value = { ok: false, text: apiError({ data: { code: (err as any)?.data?.code ?? 'VALIDATION_ERROR' } }) }
|
||||
} finally {
|
||||
avatarUploading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- 收藏 ---------- */
|
||||
interface FavedWork { slug: string; titleZh: string; titleEn: string; image: string | null; year: string; category: string }
|
||||
const favorites = ref<FavedWork[]>([])
|
||||
@@ -214,21 +250,30 @@ onBeforeUnmount(() => ctx?.revert())
|
||||
<p class="text-sm text-gray-400 mt-0.5">个人基本信息与头像</p>
|
||||
|
||||
<div class="mt-6 flex items-center gap-6">
|
||||
<img :src="userStore.avatar" :alt="userStore.user?.username"
|
||||
<img :src="profileAvatar || userStore.avatar" :alt="userStore.user?.username"
|
||||
class="w-20 h-20 rounded-2xl object-cover ring-1 ring-black/10 flex-shrink-0" />
|
||||
<p class="text-sm text-gray-400">头像由 Casdoor 统一账号管理</p>
|
||||
<div>
|
||||
<button type="button" @click="triggerAvatarPick" :disabled="avatarUploading"
|
||||
class="inline-flex items-center gap-2 px-4 py-2 rounded-full border border-gray-200 text-sm font-semibold hover:border-black hover:text-black transition-colors disabled:opacity-50">
|
||||
<Icon v-if="avatarUploading" name="ph:circle-notch" class="w-4 h-4 animate-spin" />
|
||||
<Icon v-else name="ph:user-circle" class="w-4 h-4" />
|
||||
{{ avatarUploading ? t('account.avatar.uploading') : t('account.avatar.change') }}
|
||||
</button>
|
||||
<input ref="profileFileInput" type="file" accept="image/*" class="hidden" @change="onAvatarPick" />
|
||||
<p class="text-xs text-gray-400 mt-2">{{ t('account.avatarHint') }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<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>
|
||||
<label class="block text-sm font-semibold text-gray-800 mb-1.5">{{ t('account.nickname') }}</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.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" />
|
||||
<input v-model="profileEmail" type="email"
|
||||
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="profileMsg" class="text-sm" :class="profileMsg.ok ? 'text-green-600' : 'text-red-600'">{{ profileMsg.text }}</p>
|
||||
<div class="flex justify-end">
|
||||
|
||||
Reference in New Issue
Block a user