店务小程序支持预约标签设置与展示
This commit is contained in:
@@ -21,6 +21,14 @@ const ARTIST_PRESET_LABELS = {
|
||||
LATE_SHIFT: '晚班',
|
||||
CUSTOM: '自定义'
|
||||
}
|
||||
const DEFAULT_TAG_STYLE = {
|
||||
textColor: '#333333',
|
||||
bgColor: '#FFFFFF',
|
||||
borderColor: '#E5E5E5',
|
||||
sortOrder: 100,
|
||||
frontVisible: 1
|
||||
}
|
||||
const TAG_SPLIT_PATTERN = /[;,,、]/
|
||||
|
||||
function buildClientRequestId(prefix) {
|
||||
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
|
||||
@@ -555,6 +563,7 @@ function normalizeProduct(product, artists) {
|
||||
return {
|
||||
...product,
|
||||
coverImage: imageUrls[0] || '',
|
||||
allTags: parseProductTags(product),
|
||||
tagList: parseProductTags(product).slice(0, 4),
|
||||
artistText: artistNames.length > 0 ? artistNames.join('、') : '全部美甲师',
|
||||
priceText: productPriceText,
|
||||
@@ -569,7 +578,12 @@ function normalizeTagOption(tag) {
|
||||
}
|
||||
return {
|
||||
id: (tag && tag.id) || name,
|
||||
name
|
||||
name,
|
||||
tagName: (tag && tag.tagName) || name,
|
||||
displayName: (tag && tag.displayName) || name,
|
||||
textColor: (tag && tag.textColor) || DEFAULT_TAG_STYLE.textColor,
|
||||
bgColor: (tag && tag.bgColor) || DEFAULT_TAG_STYLE.bgColor,
|
||||
borderColor: (tag && tag.borderColor) || DEFAULT_TAG_STYLE.borderColor
|
||||
}
|
||||
}
|
||||
|
||||
@@ -588,8 +602,52 @@ function normalizeTagOptions(tags) {
|
||||
})
|
||||
}
|
||||
|
||||
function splitTagValues(value) {
|
||||
return String(value || '')
|
||||
.split(TAG_SPLIT_PATTERN)
|
||||
.map(item => item.trim())
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
function encodeTagValues(tags) {
|
||||
const names = new Set()
|
||||
return (Array.isArray(tags) ? tags : [])
|
||||
.map(item => String(item || '').trim())
|
||||
.filter(Boolean)
|
||||
.filter(item => {
|
||||
if (names.has(item)) {
|
||||
return false
|
||||
}
|
||||
names.add(item)
|
||||
return true
|
||||
})
|
||||
.join(';')
|
||||
}
|
||||
|
||||
function buildSelectableTagOptions(tagOptions, value) {
|
||||
const selected = new Set(splitTagValues(value))
|
||||
return (Array.isArray(tagOptions) ? tagOptions : [])
|
||||
.map(normalizeTagOption)
|
||||
.filter(Boolean)
|
||||
.map(item => ({
|
||||
...item,
|
||||
selected: selected.has(item.name)
|
||||
}))
|
||||
}
|
||||
|
||||
function findTagOptionByName(tagOptions, value) {
|
||||
const tagName = String(value || '').trim()
|
||||
if (!tagName) {
|
||||
return null
|
||||
}
|
||||
return (Array.isArray(tagOptions) ? tagOptions : [])
|
||||
.map(normalizeTagOption)
|
||||
.filter(Boolean)
|
||||
.find(item => item.name === tagName) || null
|
||||
}
|
||||
|
||||
function resolveTagPickerIndex(tagOptions, value) {
|
||||
const firstTag = String(value || '').split(/[;,,、]/)[0].trim()
|
||||
const firstTag = splitTagValues(value)[0] || ''
|
||||
if (!firstTag) {
|
||||
return 0
|
||||
}
|
||||
@@ -598,20 +656,41 @@ function resolveTagPickerIndex(tagOptions, value) {
|
||||
}
|
||||
|
||||
function resolveTagValue(tagOptions, value) {
|
||||
const tokens = String(value || '')
|
||||
.split(/[;,,、]/)
|
||||
.map(item => item.trim())
|
||||
.filter(Boolean)
|
||||
if (tokens.length === 0) {
|
||||
return ''
|
||||
return encodeTagValues(splitTagValues(value))
|
||||
}
|
||||
|
||||
function toggleTagValue(value, tagName) {
|
||||
const current = splitTagValues(value)
|
||||
if (current.includes(tagName)) {
|
||||
return encodeTagValues(current.filter(item => item !== tagName))
|
||||
}
|
||||
if (!Array.isArray(tagOptions) || tagOptions.length === 0) {
|
||||
return ''
|
||||
return encodeTagValues(current.concat(tagName))
|
||||
}
|
||||
|
||||
function appendTagValue(value, tagName) {
|
||||
return encodeTagValues(splitTagValues(value).concat(tagName))
|
||||
}
|
||||
|
||||
function resolveCurrentShopId() {
|
||||
const app = getApp()
|
||||
const userInfo = (app && app.globalData && app.globalData.userInfo) || {}
|
||||
return userInfo.shopId || (app && app.globalData && app.globalData.shopId) || ''
|
||||
}
|
||||
|
||||
function buildTagCreatePayload(tagName) {
|
||||
const shopId = resolveCurrentShopId()
|
||||
const payload = {
|
||||
tagName,
|
||||
textColor: DEFAULT_TAG_STYLE.textColor,
|
||||
bgColor: DEFAULT_TAG_STYLE.bgColor,
|
||||
borderColor: DEFAULT_TAG_STYLE.borderColor,
|
||||
sortOrder: DEFAULT_TAG_STYLE.sortOrder,
|
||||
frontVisible: DEFAULT_TAG_STYLE.frontVisible
|
||||
}
|
||||
const matched = tokens
|
||||
.map(token => tagOptions.find(item => item.name === token))
|
||||
.find(Boolean)
|
||||
return matched ? matched.name : ''
|
||||
if (shopId) {
|
||||
payload.shopId = shopId
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
function splitManualImageValues(value) {
|
||||
@@ -758,6 +837,8 @@ Page({
|
||||
tagsLoading: false,
|
||||
tagPickerIndex: 0,
|
||||
tagError: '',
|
||||
tagCreating: false,
|
||||
manualTagInput: '',
|
||||
conflictTip: '',
|
||||
conflictLevel: 'free',
|
||||
canCreateAppointment: true,
|
||||
@@ -871,7 +952,7 @@ Page({
|
||||
const tagOptions = normalizeTagOptions(tagsData.list || tagsData || [])
|
||||
const selectedTag = resolveTagValue(tagOptions, this.data.form.manualProductTags)
|
||||
this.setData({
|
||||
tagOptions,
|
||||
tagOptions: buildSelectableTagOptions(tagOptions, selectedTag),
|
||||
tagsLoaded: true,
|
||||
tagPickerIndex: resolveTagPickerIndex(tagOptions, selectedTag),
|
||||
'form.manualProductTags': selectedTag,
|
||||
@@ -1105,6 +1186,8 @@ Page({
|
||||
manualProductPanelVisible: true,
|
||||
productModalVisible: false,
|
||||
manualProductImageUrls: imageUrls,
|
||||
manualTagInput: '',
|
||||
tagOptions: buildSelectableTagOptions(this.data.tagOptions, this.data.form.manualProductTags),
|
||||
tagPickerIndex: resolveTagPickerIndex(this.data.tagOptions, this.data.form.manualProductTags)
|
||||
}
|
||||
if (!productTitle) {
|
||||
@@ -1137,7 +1220,7 @@ Page({
|
||||
const selectedProduct = this.data.filteredProducts.find(item => String(item.id) === String(event.currentTarget.dataset.id)) || {}
|
||||
const price = formatProductPrice(selectedProduct.price)
|
||||
const priceUndetermined = price === '' || price === '0'
|
||||
const productTag = resolveTagValue(this.data.tagOptions, parseProductTags(selectedProduct).join(','))
|
||||
const productTag = resolveTagValue(this.data.tagOptions, (selectedProduct.allTags || parseProductTags(selectedProduct)).join(','))
|
||||
const imageUrls = Array.isArray(selectedProduct.imageUrls) ? selectedProduct.imageUrls : []
|
||||
this.setData({
|
||||
selectedProduct,
|
||||
@@ -1152,6 +1235,7 @@ Page({
|
||||
'form.manualProductImageUrl': imageUrls.join(','),
|
||||
manualProductImageUrls: imageUrls,
|
||||
productModalVisible: false,
|
||||
tagOptions: buildSelectableTagOptions(this.data.tagOptions, productTag),
|
||||
tagPickerIndex: resolveTagPickerIndex(this.data.tagOptions, productTag),
|
||||
'form.durationMinutes': String(parseInt(selectedProduct.approximateDuration, 10) || this.data.form.durationMinutes || '')
|
||||
})
|
||||
@@ -1159,14 +1243,80 @@ Page({
|
||||
this.checkAvailability()
|
||||
},
|
||||
|
||||
toggleManualTag(event) {
|
||||
const tagName = String(event.currentTarget.dataset.name || '').trim()
|
||||
if (!tagName) {
|
||||
return
|
||||
}
|
||||
const nextValue = toggleTagValue(this.data.form.manualProductTags, tagName)
|
||||
this.setData({
|
||||
'form.manualProductTags': nextValue,
|
||||
tagOptions: buildSelectableTagOptions(this.data.tagOptions, nextValue),
|
||||
tagPickerIndex: resolveTagPickerIndex(this.data.tagOptions, nextValue)
|
||||
})
|
||||
},
|
||||
|
||||
onManualTagInput(event) {
|
||||
this.setData({ manualTagInput: event.detail.value })
|
||||
},
|
||||
|
||||
async createManualTag() {
|
||||
if (this.data.tagCreating || this.data.tagsLoading) {
|
||||
return
|
||||
}
|
||||
const tagName = String(this.data.manualTagInput || '').trim()
|
||||
if (!tagName) {
|
||||
wx.showToast({ title: '请输入标签名称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
const existingTag = findTagOptionByName(this.data.tagOptions, tagName)
|
||||
if (existingTag) {
|
||||
const nextValue = appendTagValue(this.data.form.manualProductTags, existingTag.name)
|
||||
this.setData({
|
||||
manualTagInput: '',
|
||||
'form.manualProductTags': nextValue,
|
||||
tagOptions: buildSelectableTagOptions(this.data.tagOptions, nextValue),
|
||||
tagPickerIndex: resolveTagPickerIndex(this.data.tagOptions, nextValue),
|
||||
tagError: ''
|
||||
})
|
||||
return
|
||||
}
|
||||
this.setData({ tagCreating: true })
|
||||
try {
|
||||
const createdId = await tagsApi.create(buildTagCreatePayload(tagName))
|
||||
const nextOptions = normalizeTagOptions(this.data.tagOptions.concat({
|
||||
id: createdId || tagName,
|
||||
tagName,
|
||||
displayName: tagName
|
||||
}))
|
||||
const nextValue = appendTagValue(this.data.form.manualProductTags, tagName)
|
||||
this.setData({
|
||||
manualTagInput: '',
|
||||
tagOptions: buildSelectableTagOptions(nextOptions, nextValue),
|
||||
tagPickerIndex: resolveTagPickerIndex(nextOptions, nextValue),
|
||||
'form.manualProductTags': nextValue,
|
||||
tagsLoaded: true,
|
||||
tagError: ''
|
||||
})
|
||||
wx.showToast({ title: '标签已新增', icon: 'success' })
|
||||
} catch (error) {
|
||||
this.setData({ tagError: error.message || '标签新增失败' })
|
||||
wx.showToast({ title: error.message || '标签新增失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ tagCreating: false })
|
||||
}
|
||||
},
|
||||
|
||||
onManualTagChange(event) {
|
||||
const tag = this.data.tagOptions[Number(event.detail.value)]
|
||||
if (!tag) {
|
||||
return
|
||||
}
|
||||
const nextValue = toggleTagValue(this.data.form.manualProductTags, tag.name)
|
||||
this.setData({
|
||||
tagPickerIndex: Number(event.detail.value),
|
||||
'form.manualProductTags': tag.name
|
||||
'form.manualProductTags': nextValue,
|
||||
tagOptions: buildSelectableTagOptions(this.data.tagOptions, nextValue)
|
||||
})
|
||||
},
|
||||
|
||||
@@ -1483,7 +1633,7 @@ Page({
|
||||
payload.manualProductTitle = String(form.manualProductTitle || '').trim()
|
||||
payload.manualProductPrice = appointmentPrice
|
||||
payload.manualProductDescription = String(form.manualProductDescription || '').trim() || undefined
|
||||
payload.manualProductTags = String(form.manualProductTags || '').trim() || undefined
|
||||
payload.manualProductTags = resolveTagValue(this.data.tagOptions, form.manualProductTags) || undefined
|
||||
payload.manualProductPrecautions = String(form.manualProductPrecautions || '').trim() || undefined
|
||||
payload.manualProductImageFileIds = String(form.manualProductImageFileIds || '').trim() || undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user