# Conflicts: # backend/app/models/user.py # docs/demand.md # docs/funclist.md # frontend/src/features/auth/stores/auth.store.ts # dev existing problems
272 lines
11 KiB
Vue
272 lines
11 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* features/user/views/UserProfilePage.vue
|
||
* 用途:工作台个人中心,展示与编辑当前用户资料、语言/主题偏好,并提供修改密码入口。
|
||
* 依赖:workspace.context(WORKSPACE_KEY)、lucide 图标、vue-i18n、isPlatformAdminUser。
|
||
* 逻辑:路由 /workspace/profile 懒加载本页;资料状态由 useWorkspaceProfile 经工作台注入。
|
||
* 角色展示优先用权限组名称;旧会话缺 role_name 时再按 is_platform_admin 回退。
|
||
*/
|
||
import { computed, inject } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import {
|
||
User, Mail, Building, CreditCard, Globe, Save, Palette,
|
||
} from 'lucide-vue-next';
|
||
import { getUserAvatarLabel } from '@/shared/utils/user-avatar';
|
||
import { WORKSPACE_KEY } from '@/features/workspace/composables/workspace.context';
|
||
import { isPlatformAdminUser } from '@/shared/utils/auth';
|
||
|
||
const { t } = useI18n();
|
||
const userProfile = inject(WORKSPACE_KEY)!;
|
||
|
||
/** 只读字段值:与 input-field 同高,正文统一 14px。 */
|
||
const fieldValueClass = 'flex min-h-11 items-center rounded-xl border border-default bg-surface-soft px-4 text-sm font-medium text-secondary';
|
||
/** 表单标签:与分区标题区分层级,全页统一 14px。 */
|
||
const fieldLabelClass = 'mb-1.5 flex items-center gap-1.5 text-sm font-medium text-muted';
|
||
|
||
/**
|
||
* 身份主标题:优先真实姓名,缺失时回退用户名。
|
||
*/
|
||
const displayName = computed(() => {
|
||
return userProfile.profileUser.real_name || userProfile.profileUser.username || '-';
|
||
});
|
||
|
||
/**
|
||
* 个人中心角色标签。
|
||
* 核心逻辑:有权限组名称就展示名称;否则按 is_platform_admin 回退内置文案。
|
||
*/
|
||
const profileRoleLabel = computed(() => {
|
||
const user = userProfile.profileUser || {};
|
||
if (user.role_name) return user.role_name;
|
||
return isPlatformAdminUser(user) ? t('profile.roleAdmin') : t('profile.roleUser');
|
||
});
|
||
|
||
/**
|
||
* 身份副标题:用户名(与主标题不同时)、部门、角色用间隔点拼接,空项省略。
|
||
*/
|
||
const identityMeta = computed(() => {
|
||
const username = userProfile.profileUser.username;
|
||
const showUsername = Boolean(username && username !== displayName.value);
|
||
return [showUsername ? username : '', userProfile.profileUser.department, profileRoleLabel.value]
|
||
.filter(Boolean)
|
||
.join(' · ');
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<div v-if="userProfile.isProfileLoading" class="flex justify-center py-20">
|
||
<div class="animate-spin rounded-full h-8 w-8 border-b-2 border-brand-600"></div>
|
||
</div>
|
||
|
||
<div v-else class="space-y-6">
|
||
<div class="rounded-3xl border border-default bg-surface px-6 py-6 shadow-sm md:px-8">
|
||
<div class="flex flex-col gap-6 lg:flex-row lg:items-start lg:justify-between">
|
||
<div class="flex flex-1 flex-col gap-6 sm:flex-row sm:items-center sm:justify-start">
|
||
<div class="flex h-24 w-24 shrink-0 items-center justify-center rounded-full bg-brand-100 text-4xl font-bold text-brand-600" :class="userProfile.isMobile ? 'h-14 w-14 text-2xl' : ''">
|
||
{{ getUserAvatarLabel(userProfile.profileUser) }}
|
||
</div>
|
||
<div class="min-w-0 space-y-0.5">
|
||
<div class="truncate text-lg font-semibold text-primary">{{ displayName }}</div>
|
||
<div v-if="identityMeta" class="truncate text-sm text-muted">{{ identityMeta }}</div>
|
||
<div v-if="userProfile.profileUser.email" class="break-all text-sm text-muted">
|
||
{{ userProfile.profileUser.email }}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div v-if="!userProfile.isMobile" class="shrink-0">
|
||
<button type="button" class="btn-primary btn-sm" @click="userProfile.handleChangePasswordClick">
|
||
{{ $t('profile.changePassword') }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
<div v-if="userProfile.isMobile" class="mt-4 flex items-center gap-3">
|
||
<button
|
||
type="button"
|
||
class="btn-primary btn-sm flex-1"
|
||
@click="userProfile.handleChangePasswordClick"
|
||
>
|
||
{{ $t('profile.changePassword') }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
class="btn-secondary btn-sm flex-1 text-red-600 hover:bg-red-50"
|
||
@click="userProfile.handleLogout"
|
||
>
|
||
{{ $t('workspace.nav.logout') }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="space-y-6">
|
||
<div class="rounded-2xl border border-default bg-surface p-5 shadow-sm md:p-6">
|
||
<div class="mb-5 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||
<div class="flex items-center gap-2" :class="userProfile.isMobile ? 'w-full justify-between' : ''">
|
||
<h3 class="section-title flex items-center gap-2">
|
||
<User class="h-5 w-5 text-brand-500" />
|
||
{{ $t('profile.basicInfo') }}
|
||
</h3>
|
||
<button
|
||
v-if="userProfile.isMobile && !userProfile.isProfileEditing"
|
||
type="button"
|
||
class="btn-secondary btn-sm"
|
||
@click="userProfile.startProfileEditing"
|
||
>
|
||
{{ $t('profile.edit') }}
|
||
</button>
|
||
</div>
|
||
<p v-if="!userProfile.isMobile" class="section-desc">
|
||
{{ userProfile.isProfileEditing ? $t('profile.directEditHint') : $t('profile.switchEditHint') }}
|
||
</p>
|
||
<div v-if="!userProfile.isMobile" class="flex items-center gap-2">
|
||
<button
|
||
v-if="!userProfile.isProfileEditing"
|
||
type="button"
|
||
class="btn-secondary btn-sm"
|
||
@click="userProfile.startProfileEditing"
|
||
>
|
||
{{ $t('profile.edit') }}
|
||
</button>
|
||
<template v-else>
|
||
<button type="button" class="btn-secondary btn-sm" @click="userProfile.cancelProfileEditing">
|
||
{{ $t('profile.cancel') }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
class="btn-primary btn-sm disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="userProfile.isProfileSaving || !userProfile.hasProfileUnsavedChanges"
|
||
@click="userProfile.saveProfile"
|
||
>
|
||
<Save class="h-4 w-4" />
|
||
<span>{{ userProfile.isProfileSaving ? $t('profile.saving') : $t('workspace.save') }}</span>
|
||
</button>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||
<div>
|
||
<label :class="fieldLabelClass">
|
||
<User class="h-4 w-4" /> {{ $t('profile.username') }}
|
||
</label>
|
||
<div :class="fieldValueClass">
|
||
{{ userProfile.profileUser.username || '-' }}
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<label :class="fieldLabelClass">
|
||
<CreditCard class="h-4 w-4" /> {{ $t('profile.employeeId') }}
|
||
</label>
|
||
<input
|
||
v-if="userProfile.isProfileEditing"
|
||
v-model="userProfile.profileUser.employee_id"
|
||
type="text"
|
||
class="input-field text-sm"
|
||
>
|
||
<div v-else :class="fieldValueClass">
|
||
{{ userProfile.profileUser.employee_id || '-' }}
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<label :class="fieldLabelClass">
|
||
<User class="h-4 w-4" /> {{ $t('profile.realName') }}
|
||
</label>
|
||
<input
|
||
v-if="userProfile.isProfileEditing"
|
||
v-model="userProfile.profileUser.real_name"
|
||
type="text"
|
||
class="input-field text-sm"
|
||
>
|
||
<div v-else :class="fieldValueClass">
|
||
{{ userProfile.profileUser.real_name || '-' }}
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<label :class="fieldLabelClass">
|
||
<Building class="h-4 w-4" /> {{ $t('profile.department') }}
|
||
</label>
|
||
<input
|
||
v-if="userProfile.isProfileEditing"
|
||
v-model="userProfile.profileUser.department"
|
||
type="text"
|
||
class="input-field text-sm"
|
||
>
|
||
<div v-else :class="fieldValueClass">
|
||
{{ userProfile.profileUser.department || '-' }}
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<label :class="fieldLabelClass">
|
||
<Mail class="h-4 w-4" /> {{ $t('profile.email') }}
|
||
</label>
|
||
<input
|
||
v-if="userProfile.isProfileEditing"
|
||
v-model="userProfile.profileUser.email"
|
||
type="email"
|
||
class="input-field text-sm"
|
||
>
|
||
<div v-else :class="fieldValueClass">
|
||
{{ userProfile.profileUser.email || '-' }}
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<label :class="fieldLabelClass">
|
||
<Globe class="h-4 w-4" /> {{ $t('profile.phoneNumber') }}
|
||
</label>
|
||
<input
|
||
v-if="userProfile.isProfileEditing"
|
||
v-model="userProfile.profileUser.phone_number"
|
||
type="text"
|
||
class="input-field text-sm"
|
||
>
|
||
<div v-else :class="fieldValueClass">
|
||
{{ userProfile.profileUser.phone_number || '-' }}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="rounded-2xl border border-default bg-surface p-5 shadow-sm md:p-6">
|
||
<h3 class="section-title mb-5 flex items-center gap-2">
|
||
<Palette class="h-5 w-5 text-brand-500" />
|
||
{{ $t('profile.preferences') }}
|
||
</h3>
|
||
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||
<div>
|
||
<label class="mb-1.5 block text-sm font-medium text-muted">{{ $t('workspace.languageLabel') }}</label>
|
||
<select
|
||
v-model="userProfile.profileUser.language"
|
||
class="select-field text-sm"
|
||
:disabled="!userProfile.isProfileEditing"
|
||
>
|
||
<option value="en">English</option>
|
||
<option value="zh-CN">{{ $t('workspace.lang.zhCN') }}</option>
|
||
<option value="zh-TW">{{ $t('workspace.lang.zhTW') }}</option>
|
||
<option value="th">Thai</option>
|
||
</select>
|
||
</div>
|
||
<div>
|
||
<label class="mb-1.5 block text-sm font-medium text-muted">{{ $t('profile.theme') }}</label>
|
||
<div
|
||
class="segment-control segment-control--cols-3 segment-control--md"
|
||
role="radiogroup"
|
||
:aria-label="$t('profile.theme')"
|
||
>
|
||
<button
|
||
v-for="item in userProfile.themeOptions"
|
||
:key="item.value"
|
||
type="button"
|
||
role="radio"
|
||
class="segment-item"
|
||
:aria-checked="userProfile.profileUser.theme_preference === item.value"
|
||
:disabled="!userProfile.isProfileEditing"
|
||
:class="userProfile.profileUser.theme_preference === item.value ? 'segment-item--active' : ''"
|
||
@click="userProfile.profileUser.theme_preference = item.value"
|
||
>
|
||
{{ item.label() }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|