店务小程序支持预约标签设置与展示
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
|
||||
}
|
||||
|
||||
@@ -142,13 +142,18 @@
|
||||
</view>
|
||||
</view>
|
||||
<view class="manual-field manual-field-full">
|
||||
<text class="manual-field-label">标签</text>
|
||||
<picker range="{{tagOptions}}" range-key="name" value="{{tagPickerIndex}}" bindchange="onManualTagChange" disabled="{{tagOptions.length === 0}}">
|
||||
<view class="manual-picker {{form.manualProductTags ? '' : 'placeholder'}}">
|
||||
<text>{{form.manualProductTags || (tagOptions.length ? '请选择标签' : '暂无标签')}}</text>
|
||||
<text class="manual-picker-arrow">›</text>
|
||||
</view>
|
||||
</picker>
|
||||
<view class="manual-tag-head">
|
||||
<text class="manual-field-label">标签</text>
|
||||
<text wx:if="{{tagsLoading}}" class="manual-tag-loading">加载中</text>
|
||||
</view>
|
||||
<view wx:if="{{tagOptions.length}}" class="manual-tag-list">
|
||||
<view wx:for="{{tagOptions}}" wx:key="id" class="manual-tag-chip {{item.selected ? 'active' : ''}}" data-name="{{item.name}}" bindtap="toggleManualTag">{{item.name}}</view>
|
||||
</view>
|
||||
<view wx:else class="manual-tag-empty">暂无标签,可手动新增</view>
|
||||
<view class="manual-tag-input-row">
|
||||
<input class="manual-tag-input" value="{{manualTagInput}}" bindinput="onManualTagInput" placeholder="输入新标签" />
|
||||
<button class="secondary-btn manual-tag-add" loading="{{tagCreating}}" disabled="{{tagCreating || tagsLoading}}" bindtap="createManualTag">添加</button>
|
||||
</view>
|
||||
</view>
|
||||
<view wx:if="{{tagError}}" class="manual-tag-error">{{tagError}}</view>
|
||||
<view class="manual-field manual-field-full manual-remark-field">
|
||||
|
||||
@@ -1114,6 +1114,75 @@
|
||||
font-size: 22rpx;
|
||||
}
|
||||
|
||||
.manual-tag-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12rpx;
|
||||
}
|
||||
|
||||
.manual-tag-loading {
|
||||
color: #8f857a;
|
||||
font-size: 22rpx;
|
||||
}
|
||||
|
||||
.manual-tag-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 10rpx;
|
||||
margin-top: 12rpx;
|
||||
}
|
||||
|
||||
.manual-tag-chip {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 56rpx;
|
||||
padding: 0 18rpx;
|
||||
border: 2rpx solid #e1d6c9;
|
||||
border-radius: 999rpx;
|
||||
background: #fffaf4;
|
||||
color: #6f6257;
|
||||
font-size: 24rpx;
|
||||
font-weight: 800;
|
||||
}
|
||||
|
||||
.manual-tag-chip.active {
|
||||
border-color: #6f8f83;
|
||||
background: #e5f2eb;
|
||||
color: #2f6b55;
|
||||
}
|
||||
|
||||
.manual-tag-empty {
|
||||
margin-top: 12rpx;
|
||||
color: #9a9288;
|
||||
font-size: 22rpx;
|
||||
}
|
||||
|
||||
.manual-tag-input-row {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) 140rpx;
|
||||
gap: 12rpx;
|
||||
margin-top: 12rpx;
|
||||
}
|
||||
|
||||
.manual-tag-input {
|
||||
min-width: 0;
|
||||
height: 70rpx;
|
||||
padding: 0 18rpx;
|
||||
border: 2rpx solid #eadfd2;
|
||||
border-radius: 14rpx;
|
||||
background: #fffaf4;
|
||||
color: #302923;
|
||||
font-size: 26rpx;
|
||||
}
|
||||
|
||||
.manual-tag-add {
|
||||
width: 100%;
|
||||
min-width: 0;
|
||||
max-width: none;
|
||||
}
|
||||
|
||||
.manual-image-row {
|
||||
display: none;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user