339 lines
13 KiB
Vue
339 lines
13 KiB
Vue
<script setup lang="ts">
|
|
import { ref, onMounted } from 'vue';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { useRouter } from 'vue-router';
|
|
import {
|
|
User,
|
|
Mail,
|
|
Building,
|
|
CreditCard,
|
|
Shield,
|
|
Globe,
|
|
LogOut,
|
|
Save,
|
|
ChevronLeft,
|
|
X
|
|
} from 'lucide-vue-next';
|
|
|
|
const { t, locale } = useI18n();
|
|
const router = useRouter();
|
|
|
|
// State
|
|
const isLoading = ref(true);
|
|
const user = ref({
|
|
id: 0,
|
|
username: '',
|
|
real_name: '',
|
|
email: '',
|
|
phone_number: '',
|
|
department: '',
|
|
role: '',
|
|
employee_id: '',
|
|
language: 'zh-CN'
|
|
});
|
|
|
|
const isEditing = ref(false);
|
|
const passwordForm = ref({
|
|
current: '',
|
|
new: '',
|
|
confirm: ''
|
|
});
|
|
|
|
// Methods
|
|
const getAuthHeaders = (): Record<string, string> => {
|
|
let stored = localStorage.getItem('hxops_user');
|
|
if (!stored) stored = sessionStorage.getItem('hxops_user');
|
|
|
|
if (!stored) return {};
|
|
|
|
try {
|
|
const data = JSON.parse(stored);
|
|
const u = data.user || data;
|
|
return {
|
|
'X-Username': u.username,
|
|
'X-Role': u.role,
|
|
'X-User-Id': String(u.id)
|
|
};
|
|
} catch (e) {
|
|
return {};
|
|
}
|
|
};
|
|
|
|
const fetchProfile = async () => {
|
|
isLoading.value = true;
|
|
try {
|
|
const res = await fetch('/api/user/profile', { headers: getAuthHeaders() });
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
user.value = { ...user.value, ...data };
|
|
if (data.language) locale.value = data.language;
|
|
} else {
|
|
console.error('Failed to load profile');
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
};
|
|
|
|
const showPasswordModal = ref(false);
|
|
|
|
const handleSaveProfile = async () => {
|
|
try {
|
|
const res = await fetch('/api/user/profile', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify({
|
|
real_name: user.value.real_name,
|
|
phone_number: user.value.phone_number,
|
|
language: user.value.language,
|
|
email: user.value.email,
|
|
employee_id: user.value.employee_id,
|
|
department: user.value.department
|
|
})
|
|
});
|
|
|
|
if (res.ok) {
|
|
alert(t('profile.saveSuccess') || 'Profile saved successfully');
|
|
isEditing.value = false;
|
|
// Update locale immediately if changed
|
|
locale.value = user.value.language;
|
|
} else {
|
|
const err = await res.json();
|
|
alert(err.error || 'Failed to save profile');
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
alert('Error saving profile');
|
|
}
|
|
};
|
|
|
|
const handleChangePassword = async () => {
|
|
if (passwordForm.value.new !== passwordForm.value.confirm) {
|
|
alert(t('profile.passwordMismatch') || 'Passwords do not match');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await fetch('/api/user/change-password', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify({
|
|
current_password: passwordForm.value.current,
|
|
new_password: passwordForm.value.new
|
|
})
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (res.ok) {
|
|
alert(t('profile.passwordChanged') || 'Password changed successfully');
|
|
passwordForm.value = { current: '', new: '', confirm: '' };
|
|
showPasswordModal.value = false;
|
|
} else {
|
|
alert(data.error || 'Failed to change password');
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
alert('Error changing password');
|
|
}
|
|
};
|
|
|
|
const handleLogout = () => {
|
|
localStorage.removeItem('hxops_user');
|
|
router.push('/');
|
|
};
|
|
|
|
onMounted(() => {
|
|
fetchProfile();
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<div class="min-h-screen bg-slate-50 pb-20">
|
|
<!-- Header -->
|
|
<header class="bg-white border-b border-slate-200 sticky top-0 z-30">
|
|
<div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
|
|
<div class="flex items-center gap-4">
|
|
<button @click="router.push('/workspace')" class="p-2 hover:bg-slate-100 rounded-full text-slate-500 transition-colors">
|
|
<ChevronLeft class="w-5 h-5" />
|
|
</button>
|
|
<h1 class="text-xl font-bold text-slate-900">{{ $t('profile.title') || 'My Profile' }}</h1>
|
|
</div>
|
|
<button @click="handleLogout" class="text-sm font-medium text-red-600 hover:text-red-700 flex items-center gap-2 px-3 py-1.5 hover:bg-red-50 rounded-lg transition-colors">
|
|
<LogOut class="w-4 h-4" />
|
|
<span class="hidden sm:inline">{{ $t('workspace.logout') }}</span>
|
|
</button>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
|
|
<div v-if="isLoading" 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="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
|
<!-- Left Column: User Card -->
|
|
<div class="lg:col-span-1 space-y-6">
|
|
<div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6 text-center">
|
|
<div class="w-24 h-24 bg-brand-100 rounded-full mx-auto flex items-center justify-center text-brand-600 mb-4 text-3xl font-bold">
|
|
{{ user.real_name ? user.real_name.charAt(0).toUpperCase() : user.username.charAt(0).toUpperCase() }}
|
|
</div>
|
|
<h2 class="text-xl font-bold text-slate-900">{{ user.real_name || user.username }}</h2>
|
|
<p class="text-slate-500 text-sm mt-1">{{ user.email }}</p>
|
|
<div class="mt-4 inline-flex items-center px-3 py-1 rounded-full bg-slate-100 text-slate-600 text-xs font-medium">
|
|
{{ user.role === 'admin' ? 'Administrator' : 'User' }}
|
|
</div>
|
|
</div>
|
|
|
|
<div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
|
|
<h3 class="font-semibold text-slate-900 mb-4 flex items-center gap-2">
|
|
<Shield class="w-4 h-4 text-brand-500" />
|
|
{{ $t('profile.security') || 'Security' }}
|
|
</h3>
|
|
<button @click="showPasswordModal = true" class="btn-primary w-full py-2 text-sm flex items-center justify-center gap-2">
|
|
<Shield class="w-4 h-4" />
|
|
{{ $t('profile.changePassword') || 'Change Password' }}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Right Column: Details & Settings -->
|
|
<div class="lg:col-span-2 space-y-6">
|
|
<div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6 md:p-8">
|
|
<div class="flex justify-between items-center mb-6">
|
|
<h3 class="text-lg font-bold text-slate-900 flex items-center gap-2">
|
|
<User class="w-5 h-5 text-brand-500" />
|
|
{{ $t('profile.personalInfo') || 'Personal Information' }}
|
|
</h3>
|
|
<button @click="isEditing = !isEditing" class="text-sm font-medium text-brand-600 hover:text-brand-700">
|
|
{{ isEditing ? ($t('profile.cancel') || 'Cancel') : ($t('profile.edit') || 'Edit') }}
|
|
</button>
|
|
</div>
|
|
|
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase flex items-center gap-1.5 mb-1.5">
|
|
<User class="w-3.5 h-3.5" /> {{ $t('login.username') }}
|
|
</label>
|
|
<div class="p-2.5 bg-slate-50 rounded-lg text-slate-700 border border-slate-200 text-sm font-medium cursor-not-allowed">
|
|
{{ user.username }}
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase flex items-center gap-1.5 mb-1.5">
|
|
<CreditCard class="w-3.5 h-3.5" /> {{ $t('register.fields.employeeId') || 'Employee ID' }}
|
|
</label>
|
|
<input v-if="isEditing" v-model="user.employee_id" type="text" class="input-field" />
|
|
<div v-else class="p-2.5 bg-slate-50 rounded-lg text-slate-700 border border-slate-200 text-sm font-medium">
|
|
{{ user.employee_id || '-' }}
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase flex items-center gap-1.5 mb-1.5">
|
|
<User class="w-3.5 h-3.5" /> {{ $t('register.fields.realName') || 'Real Name' }}
|
|
</label>
|
|
<input v-if="isEditing" v-model="user.real_name" type="text" class="input-field" />
|
|
<div v-else class="p-2.5 bg-white rounded-lg text-slate-900 border border-transparent text-sm font-medium">
|
|
{{ user.real_name || '-' }}
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase flex items-center gap-1.5 mb-1.5">
|
|
<Building class="w-3.5 h-3.5" /> {{ $t('admin.user.dept') || 'Department' }}
|
|
</label>
|
|
<input v-if="isEditing" v-model="user.department" type="text" class="input-field" />
|
|
<div v-else class="p-2.5 bg-slate-50 rounded-lg text-slate-700 border border-slate-200 text-sm font-medium">
|
|
{{ user.department || '-' }}
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase flex items-center gap-1.5 mb-1.5">
|
|
<Mail class="w-3.5 h-3.5" /> {{ $t('register.email') }}
|
|
</label>
|
|
<input v-if="isEditing" v-model="user.email" type="email" class="input-field" />
|
|
<div v-else class="p-2.5 bg-slate-50 rounded-lg text-slate-700 border border-slate-200 text-sm font-medium">
|
|
{{ user.email }}
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase flex items-center gap-1.5 mb-1.5">
|
|
<Globe class="w-3.5 h-3.5" /> {{ $t('register.fields.phone') || 'Phone' }}
|
|
</label>
|
|
<input v-if="isEditing" v-model="user.phone_number" type="text" class="input-field" />
|
|
<div v-else class="p-2.5 bg-white rounded-lg text-slate-900 border border-transparent text-sm font-medium">
|
|
{{ user.phone_number || '-' }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div v-if="isEditing" class="mt-8 flex justify-end pt-6 border-t border-slate-100">
|
|
<button @click="handleSaveProfile" class="btn-primary flex items-center gap-2">
|
|
<Save class="w-4 h-4" />
|
|
{{ $t('workspace.save') }}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6 md:p-8">
|
|
<h3 class="text-lg font-bold text-slate-900 mb-6 flex items-center gap-2">
|
|
<Globe class="w-5 h-5 text-brand-500" />
|
|
{{ $t('profile.preferences') || 'Preferences' }}
|
|
</h3>
|
|
|
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase mb-1.5 block">Language</label>
|
|
<select v-model="user.language" :disabled="!isEditing" class="input-field appearance-none bg-no-repeat bg-right pr-8" :class="{ 'bg-slate-50': !isEditing }">
|
|
<option value="en">English</option>
|
|
<option value="zh-CN">简体中文</option>
|
|
<option value="zh-TW">繁體中文</option>
|
|
<option value="th">ไทย</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
<div v-if="showPasswordModal" class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm p-4">
|
|
<div class="bg-white rounded-2xl shadow-xl w-full max-w-md p-6 animate-fade-in-up">
|
|
<div class="flex justify-between items-center mb-6">
|
|
<h3 class="text-lg font-bold text-slate-900">{{ $t('profile.changePassword') || 'Change Password' }}</h3>
|
|
<button @click="showPasswordModal = false" class="p-1 hover:bg-slate-100 rounded-full transition-colors">
|
|
<X class="w-5 h-5 text-slate-500" />
|
|
</button>
|
|
</div>
|
|
|
|
<form @submit.prevent="handleChangePassword" class="space-y-4">
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase">{{ $t('profile.currentPassword') || 'Current Password' }}</label>
|
|
<input v-model="passwordForm.current" type="password" class="input-field mt-1" required />
|
|
</div>
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase">{{ $t('profile.newPassword') || 'New Password' }}</label>
|
|
<input v-model="passwordForm.new" type="password" class="input-field mt-1" required />
|
|
</div>
|
|
<div>
|
|
<label class="text-xs font-medium text-slate-500 uppercase">{{ $t('profile.confirmPassword') || 'Confirm Password' }}</label>
|
|
<input v-model="passwordForm.confirm" type="password" class="input-field mt-1" required />
|
|
</div>
|
|
<div class="flex gap-3 pt-2">
|
|
<button type="button" @click="showPasswordModal = false" class="flex-1 px-4 py-2 border border-slate-200 rounded-lg text-slate-600 hover:bg-slate-50 font-medium transition-colors">
|
|
{{ $t('profile.cancel') || 'Cancel' }}
|
|
</button>
|
|
<button type="submit" class="flex-1 btn-primary py-2 text-sm">
|
|
{{ $t('profile.changePassword') || 'Update' }}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</template>
|