修复员工颜色设置
This commit is contained in:
@@ -1,47 +1,172 @@
|
||||
<template>
|
||||
<MobileShell title="颜色设置">
|
||||
<section class="color-grid">
|
||||
<button v-for="color in presets" :key="color" class="color-swatch" :class="{ active: selected === color }" :style="{ backgroundColor: color }" type="button" @click="selectColor(color)"></button>
|
||||
<LoadingState v-if="loading" text="正在加载颜色" />
|
||||
<template v-else-if="employee">
|
||||
<section class="card color-preview">
|
||||
<span class="preview-dot" :style="{ backgroundColor: selected }"></span>
|
||||
<div>
|
||||
<h2>{{ employeeName }}</h2>
|
||||
<p>{{ selected }}</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="color-grid section">
|
||||
<button v-for="color in presets" :key="color" class="color-swatch" :class="{ active: selected === color }" :style="{ backgroundColor: color }" type="button" @click="selectColor(color)"></button>
|
||||
</section>
|
||||
|
||||
<section class="card form-card section">
|
||||
<div class="field color-picker-field">
|
||||
<label>色盘选择</label>
|
||||
<input class="native-color" type="color" :value="selected" @input="onPickerInput" />
|
||||
</div>
|
||||
<div class="field"><label>自定义色值</label><input v-model.trim="customColor" placeholder="#6F8F83" @blur="applyCustom" /></div>
|
||||
</section>
|
||||
|
||||
<button class="primary-btn section" type="button" :disabled="saving" @click="save">{{ saving ? '保存中...' : '保存颜色' }}</button>
|
||||
</template>
|
||||
<section v-else class="card empty-card">
|
||||
<strong>员工不存在</strong>
|
||||
<p>请返回员工列表重新选择</p>
|
||||
</section>
|
||||
<section class="card form-card section">
|
||||
<div class="field"><label>自定义色值</label><input v-model.trim="customColor" placeholder="#6F8F83" /></div>
|
||||
</section>
|
||||
<button class="primary-btn section" type="button" @click="applyCustom">应用自定义颜色</button>
|
||||
<button class="secondary-btn section" type="button" @click="save">保存</button>
|
||||
</MobileShell>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import LoadingState from '@/components/LoadingState.vue'
|
||||
import MobileShell from '@/components/MobileShell.vue'
|
||||
import { managerEmployeesApi } from '@/api/managerEmployees'
|
||||
import { useToastStore } from '@/stores/toast'
|
||||
import type { Artist } from '@/types/manager'
|
||||
|
||||
const route = useRoute()
|
||||
const toast = useToastStore()
|
||||
const presets = ['#6F8F83', '#C58C6D', '#8B6F8F', '#6F7F8F', '#B86B6B', '#B8A36B', '#7A9D96', '#9D7A7A']
|
||||
const defaultColor = presets[0] as string
|
||||
const selected = ref(defaultColor)
|
||||
const customColor = ref(defaultColor)
|
||||
const loading = ref(false)
|
||||
const saving = ref(false)
|
||||
const employee = ref<Artist | null>(null)
|
||||
const employeeId = computed(() => String(route.params.id))
|
||||
const employeeName = computed(() => employee.value?.name || employee.value?.nickname || '未命名员工')
|
||||
|
||||
const selectColor = (color: string): void => {
|
||||
const normalizeColor = (value: string | undefined): string | null => {
|
||||
if (!value) {
|
||||
return null
|
||||
}
|
||||
const color = value.trim().toUpperCase()
|
||||
return /^#[0-9A-F]{6}$/.test(color) ? color : null
|
||||
}
|
||||
|
||||
const syncColor = (color: string): void => {
|
||||
selected.value = color
|
||||
customColor.value = color
|
||||
}
|
||||
|
||||
const applyCustom = (): void => {
|
||||
if (!/^#[0-9a-fA-F]{6}$/.test(customColor.value)) {
|
||||
toast.error('请输入6位色值')
|
||||
return
|
||||
}
|
||||
selected.value = customColor.value.toUpperCase()
|
||||
customColor.value = selected.value
|
||||
const errorMessage = (error: { message?: string } | null, fallback: string): string => {
|
||||
return error?.message ? error.message : fallback
|
||||
}
|
||||
|
||||
const save = (): void => {
|
||||
toast.success('已保存到本地')
|
||||
const selectColor = (color: string): void => {
|
||||
const normalizedColor = normalizeColor(color)
|
||||
if (normalizedColor) {
|
||||
syncColor(normalizedColor)
|
||||
}
|
||||
}
|
||||
|
||||
const onPickerInput = (event: Event): void => {
|
||||
const input = event.target as HTMLInputElement
|
||||
const normalizedColor = normalizeColor(input.value)
|
||||
if (normalizedColor) {
|
||||
syncColor(normalizedColor)
|
||||
}
|
||||
}
|
||||
|
||||
const applyCustom = (): boolean => {
|
||||
const normalizedColor = normalizeColor(customColor.value)
|
||||
if (!normalizedColor) {
|
||||
toast.error('请输入#开头的6位色值')
|
||||
return false
|
||||
}
|
||||
syncColor(normalizedColor)
|
||||
return true
|
||||
}
|
||||
|
||||
const loadEmployee = async (): Promise<void> => {
|
||||
loading.value = true
|
||||
try {
|
||||
const detail = await managerEmployeesApi.detail(employeeId.value)
|
||||
if (!detail) {
|
||||
employee.value = null
|
||||
toast.error('员工不存在')
|
||||
return
|
||||
}
|
||||
employee.value = detail
|
||||
syncColor(normalizeColor(detail.color) || defaultColor)
|
||||
} catch (error) {
|
||||
toast.error(errorMessage(error as { message?: string } | null, '颜色加载失败,请稍后重试'))
|
||||
employee.value = null
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const save = async (): Promise<void> => {
|
||||
if (!employee.value || !applyCustom()) {
|
||||
return
|
||||
}
|
||||
saving.value = true
|
||||
try {
|
||||
const color = selected.value
|
||||
const saved = await managerEmployeesApi.save({
|
||||
...employee.value,
|
||||
userId: employee.value.userId || employeeId.value,
|
||||
color
|
||||
})
|
||||
employee.value = { ...employee.value, ...saved, color: normalizeColor(saved.color) || color }
|
||||
syncColor(color)
|
||||
toast.success('颜色已保存')
|
||||
} catch (error) {
|
||||
toast.error(errorMessage(error as { message?: string } | null, '颜色保存失败,请稍后重试'))
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void loadEmployee()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.color-preview {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 14px;
|
||||
padding: 18px;
|
||||
}
|
||||
|
||||
.preview-dot {
|
||||
width: 58px;
|
||||
height: 58px;
|
||||
border: 4px solid var(--card-bg);
|
||||
border-radius: 20px;
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
|
||||
.color-preview h2,
|
||||
.color-preview p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.color-preview p {
|
||||
margin-top: 4px;
|
||||
color: var(--muted);
|
||||
font-weight: 800;
|
||||
}
|
||||
|
||||
.color-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
@@ -59,4 +184,42 @@ const save = (): void => {
|
||||
border-color: var(--card-bg);
|
||||
outline: 3px solid var(--accent);
|
||||
}
|
||||
|
||||
.color-picker-field {
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.field .native-color {
|
||||
flex: 0 0 54px;
|
||||
width: 54px;
|
||||
height: 36px;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 12px;
|
||||
background: var(--card-bg);
|
||||
}
|
||||
|
||||
.native-color::-webkit-color-swatch-wrapper {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.native-color::-webkit-color-swatch {
|
||||
border: 0;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.primary-btn:disabled {
|
||||
opacity: 0.65;
|
||||
}
|
||||
|
||||
.empty-card {
|
||||
padding: 24px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.empty-card p {
|
||||
margin: 8px 0 0;
|
||||
color: var(--muted);
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user