This commit is contained in:
@@ -22,6 +22,14 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
updateColor(userId, color) {
|
||||
return request({
|
||||
url: `/manager/employees/${userId}/color`,
|
||||
method: 'PUT',
|
||||
data: { color }
|
||||
})
|
||||
},
|
||||
|
||||
createUser(data) {
|
||||
return request({
|
||||
url: '/manager/employees/users/unlinked',
|
||||
|
||||
@@ -1,7 +1,22 @@
|
||||
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: {
|
||||
selected: '#247B63',
|
||||
customColor: '#247B63',
|
||||
id: '',
|
||||
saving: false,
|
||||
selected: DEFAULT_COLOR,
|
||||
customColor: DEFAULT_COLOR,
|
||||
wheelColors: [
|
||||
'#E0627A', '#E7785B', '#E2A64B', '#D8C85C', '#9EBB63', '#63B77B',
|
||||
'#4CB7A6', '#56A7D6', '#5F82D7', '#7C70D4', '#A66BC8', '#CF72A8'
|
||||
@@ -15,6 +30,21 @@ Page({
|
||||
'#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 })
|
||||
@@ -27,14 +57,39 @@ Page({
|
||||
this.setData({ customColor: event.detail.value })
|
||||
},
|
||||
applyCustomColor() {
|
||||
let color = (this.data.customColor || '').trim()
|
||||
if (color && color[0] !== '#') color = `#${color}`
|
||||
if (!/^#[0-9a-fA-F]{6}$/.test(color)) {
|
||||
const color = normalizeColor(this.data.customColor)
|
||||
if (!/^#[0-9A-F]{6}$/.test(color)) {
|
||||
wx.showToast({ title: '请输入6位色值', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ selected: color.toUpperCase(), customColor: color.toUpperCase() })
|
||||
this.setData({ selected: color, customColor: color })
|
||||
},
|
||||
save() { wx.showToast({ title: '已保存到本地' }) },
|
||||
back() { wx.navigateBack() }
|
||||
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()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
const assert = require('assert')
|
||||
const employeesApi = require('../../api/employees')
|
||||
|
||||
const storage = {}
|
||||
const app = { globalData: {} }
|
||||
const toasts = []
|
||||
global.wx = {
|
||||
getAccountInfoSync: () => ({ miniProgram: { envVersion: 'develop' } }),
|
||||
getDeviceInfo: () => ({ platform: 'devtools' }),
|
||||
showToast: options => toasts.push(options),
|
||||
navigateBack: () => {},
|
||||
setStorageSync: (key, value) => { storage[key] = value },
|
||||
getStorageSync: key => storage[key]
|
||||
}
|
||||
global.getApp = () => app
|
||||
global.getCurrentPages = () => []
|
||||
|
||||
let definition
|
||||
global.Page = value => { definition = value }
|
||||
require('./index')
|
||||
|
||||
const page = {
|
||||
...definition,
|
||||
data: JSON.parse(JSON.stringify(definition.data)),
|
||||
setData(values) {
|
||||
Object.entries(values).forEach(([key, value]) => {
|
||||
const parts = key.split('.')
|
||||
let target = this.data
|
||||
for (let index = 0; index < parts.length - 1; index += 1) target = target[parts[index]]
|
||||
target[parts[parts.length - 1]] = value
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async function run() {
|
||||
page.setData({ id: '88' })
|
||||
employeesApi.detail = async () => ({ color: '#8fb89a' })
|
||||
await page.loadColor()
|
||||
assert.strictEqual(page.data.selected, '#8FB89A')
|
||||
assert.strictEqual(page.data.customColor, '#8FB89A')
|
||||
|
||||
page.setData({ selected: '#aabbcc', customColor: '#aabbcc' })
|
||||
let saved
|
||||
employeesApi.updateColor = async (id, color) => {
|
||||
saved = { id, color }
|
||||
return { color: '#AABBCC' }
|
||||
}
|
||||
await page.save()
|
||||
assert.deepStrictEqual(saved, { id: '88', color: '#AABBCC' })
|
||||
assert.strictEqual(page.data.selected, '#AABBCC')
|
||||
assert.strictEqual(page.data.saving, false)
|
||||
assert.strictEqual(app.globalData.planningNeedRefresh, true)
|
||||
assert.strictEqual(storage.planningNeedRefresh, '1')
|
||||
assert.strictEqual(toasts[toasts.length - 1].title, '颜色已保存')
|
||||
|
||||
app.globalData.planningNeedRefresh = false
|
||||
storage.planningNeedRefresh = ''
|
||||
employeesApi.updateColor = async () => {
|
||||
throw new Error('颜色保存失败,请稍后重试')
|
||||
}
|
||||
await page.save()
|
||||
assert.strictEqual(page.data.saving, false)
|
||||
assert.strictEqual(app.globalData.planningNeedRefresh, false)
|
||||
assert.strictEqual(toasts[toasts.length - 1].title, '颜色保存失败,请稍后重试')
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
console.error(error)
|
||||
process.exitCode = 1
|
||||
})
|
||||
@@ -18,7 +18,12 @@ Page({
|
||||
|
||||
onLoad(options) {
|
||||
this.setData({ id: options.id || '' })
|
||||
this.loadDetail()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
if (this.data.id) {
|
||||
this.loadDetail()
|
||||
}
|
||||
},
|
||||
|
||||
async loadDetail() {
|
||||
|
||||
Reference in New Issue
Block a user