Files
JayuCloud/JY-Salon-WeChat/pages/color-setting/index.js
T

129 lines
3.3 KiB
JavaScript

const employeesApi = require('../../api/employees')
const { markPlanningRefresh } = require('../../utils/planning-refresh')
function normalizeColor(value) {
let color = String(value || '').trim()
if (!color) {
return ''
}
if (color[0] !== '#') {
color = `#${color}`
}
if (!/^#[0-9a-fA-F]{6}$/.test(color)) {
return ''
}
return color.toUpperCase()
}
Page({
data: {
id: '',
employee: {},
selected: '#7BA7C7',
customColor: '#7BA7C7',
saving: false,
loadError: '',
wheelColors: [
'#E0627A', '#E7785B', '#E2A64B', '#D8C85C', '#9EBB63', '#63B77B',
'#4CB7A6', '#56A7D6', '#5F82D7', '#7C70D4', '#A66BC8', '#CF72A8'
],
recommendedColors: ['#7BA7C7', '#8FB89A', '#A894C7', '#D7A86E', '#D98DA5', '#79A6A3', '#B9A16B', '#9BA9C8'],
paletteColors: [
'#6F8F83', '#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.id || '' })
this.loadDetail()
},
async loadDetail() {
const userId = this.data.id
if (!userId) {
this.setData({ loadError: '员工ID不能为空' })
return
}
try {
const data = await employeesApi.detail(userId)
const color = normalizeColor(data.color) || this.data.selected
this.setData({
loadError: '',
employee: data,
selected: color,
customColor: color
})
} catch (error) {
this.setData({
employee: {},
loadError: error.message || '美甲师详情加载失败'
})
}
},
select(event) {
const color = normalizeColor(event.currentTarget.dataset.color)
if (!color) {
return
}
this.setData({ selected: color, customColor: color })
},
selectWheelColor(event) {
const color = normalizeColor(event.currentTarget.dataset.color)
if (!color) {
return
}
this.setData({ selected: color, customColor: color })
},
onCustomInput(event) {
this.setData({ customColor: event.detail.value })
},
applyCustomColor() {
const color = normalizeColor(this.data.customColor)
if (!color) {
wx.showToast({ title: '请输入6位色值', icon: 'none' })
return
}
this.setData({ selected: color, customColor: color })
},
async save() {
if (!this.data.id) {
wx.showToast({ title: '员工ID不能为空', icon: 'none' })
return
}
const color = normalizeColor(this.data.selected)
if (!color) {
wx.showToast({ title: '请选择正确颜色', icon: 'none' })
return
}
this.setData({ saving: true })
try {
await employeesApi.save({
userId: this.data.id,
color
})
markPlanningRefresh()
wx.showToast({ title: '已保存', icon: 'success' })
setTimeout(() => {
wx.navigateBack()
}, 300)
} catch (error) {
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
} finally {
this.setData({ saving: false })
}
},
back() {
wx.navigateBack()
}
})