96 lines
3.1 KiB
JavaScript
96 lines
3.1 KiB
JavaScript
const employeesApi = require('../../api/employees')
|
|
const { showErrorToast } = require('../../utils/toast')
|
|
const { markPlanningRefresh } = require('../../utils/planning-refresh')
|
|
|
|
const DEFAULT_COLOR = '#247B63'
|
|
|
|
function normalizeColor(value) {
|
|
const raw = String(value || '').trim()
|
|
if (!raw) return ''
|
|
const color = raw[0] === '#' ? raw : `#${raw}`
|
|
return color.toUpperCase()
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
id: '',
|
|
saving: false,
|
|
selected: DEFAULT_COLOR,
|
|
customColor: DEFAULT_COLOR,
|
|
wheelColors: [
|
|
'#E0627A', '#E7785B', '#E2A64B', '#D8C85C', '#9EBB63', '#63B77B',
|
|
'#4CB7A6', '#56A7D6', '#5F82D7', '#7C70D4', '#A66BC8', '#CF72A8'
|
|
],
|
|
recommendedColors: ['#247B63', '#B8E4D4', '#8FB89A', '#A894C7', '#D7A86E', '#D98DA5', '#79A6A3', '#9BA9C8'],
|
|
paletteColors: [
|
|
'#247B63', '#7BA7C7', '#8AAECA', '#9BA9C8', '#A894C7', '#C19AB7',
|
|
'#D98DA5', '#D9958D', '#D7A86E', '#C8B47E', '#B9A16B', '#9FAD7B',
|
|
'#8FB89A', '#79A6A3', '#6E9E92', '#A6B7A4', '#B8B0A6', '#C7B7A3',
|
|
'#8E8A7D', '#A4A097', '#B6A6A0', '#C9A2A2', '#B58A8A', '#8C7C92',
|
|
'#6D8491', '#829A9A', '#A9B3A2', '#C6B7D1', '#E0C2B6', '#F0D6A8'
|
|
]
|
|
},
|
|
onLoad(options) {
|
|
this.setData({ id: options && options.id ? options.id : '' })
|
|
if (this.data.id) {
|
|
this.loadColor()
|
|
}
|
|
},
|
|
async loadColor() {
|
|
try {
|
|
const detail = await employeesApi.detail(this.data.id)
|
|
const color = normalizeColor(detail && detail.color) || DEFAULT_COLOR
|
|
this.setData({ selected: color, customColor: color })
|
|
} catch (error) {
|
|
showErrorToast(error, '美甲师颜色加载失败')
|
|
}
|
|
},
|
|
select(event) {
|
|
const color = event.currentTarget.dataset.color
|
|
this.setData({ selected: color, customColor: color })
|
|
},
|
|
selectWheelColor(event) {
|
|
const color = event.currentTarget.dataset.color
|
|
this.setData({ selected: color, customColor: color })
|
|
},
|
|
onCustomInput(event) {
|
|
this.setData({ customColor: event.detail.value })
|
|
},
|
|
applyCustomColor() {
|
|
const color = normalizeColor(this.data.customColor)
|
|
if (!/^#[0-9A-F]{6}$/.test(color)) {
|
|
wx.showToast({ title: '请输入6位色值', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ selected: color, customColor: color })
|
|
},
|
|
async save() {
|
|
if (this.data.saving) {
|
|
return
|
|
}
|
|
if (!this.data.id) {
|
|
wx.showToast({ title: '缺少美甲师信息', icon: 'none' })
|
|
return
|
|
}
|
|
const color = normalizeColor(this.data.selected)
|
|
if (!/^#[0-9A-F]{6}$/.test(color)) {
|
|
wx.showToast({ title: '请选择颜色', icon: 'none' })
|
|
return
|
|
}
|
|
this.setData({ saving: true })
|
|
try {
|
|
const saved = await employeesApi.updateColor(this.data.id, color)
|
|
const savedColor = normalizeColor(saved && saved.color) || color
|
|
this.setData({ selected: savedColor, customColor: savedColor, saving: false })
|
|
markPlanningRefresh()
|
|
wx.showToast({ title: '颜色已保存', icon: 'success' })
|
|
} catch (error) {
|
|
this.setData({ saving: false })
|
|
showErrorToast(error, '颜色保存失败')
|
|
}
|
|
},
|
|
back() {
|
|
wx.navigateBack()
|
|
}
|
|
})
|