店务小程序支持预约标签设置与展示

This commit is contained in:
2026-07-23 19:15:55 +08:00
parent dd516d40d3
commit f7c5b34f8b
10 changed files with 590 additions and 28 deletions
+179 -3
View File
@@ -1,4 +1,5 @@
const shopApi = require('../../api/shop')
const tagsApi = require('../../api/tags')
const dateUtil = require('../../utils/date')
const { BRAND_CONFIG } = require('../../utils/config')
const { markPlanningRefresh } = require('../../utils/planning-refresh')
@@ -28,6 +29,13 @@ const EXCEPTION_TYPES = [
]
const DEFAULT_SHOP_INTRO = '专注法式、晕染与手绘设计,适合想要温柔质感的客人。'
const DEFAULT_TAG_STYLE = {
textColor: '#333333',
bgColor: '#FFFFFF',
borderColor: '#E5E5E5',
sortOrder: 100,
frontVisible: 1
}
function copyWorkDays(days) {
if (Array.isArray(days)) {
@@ -77,6 +85,61 @@ function introLength(value) {
return String(value || '').length
}
function normalizeTagOption(tag) {
const name = String((tag && (tag.displayName || tag.tagName || tag.name)) || '').trim()
if (!name) {
return null
}
return {
id: (tag && tag.id) || 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,
sortOrder: (tag && tag.sortOrder) || DEFAULT_TAG_STYLE.sortOrder,
frontVisible: (tag && tag.frontVisible) === 0 ? 0 : DEFAULT_TAG_STYLE.frontVisible
}
}
function normalizeTagOptions(tags) {
const source = Array.isArray(tags) ? tags : []
const names = new Set()
return source
.map(normalizeTagOption)
.filter(Boolean)
.filter(item => {
if (names.has(item.name)) {
return false
}
names.add(item.name)
return true
})
}
function resolveCurrentShopId() {
const app = getApp()
const userInfo = (app && app.globalData && app.globalData.userInfo) || {}
return userInfo.shopId || (app && app.globalData && app.globalData.shopId) || ''
}
function buildTagPayload(tagName, sourceTag) {
const shopId = resolveCurrentShopId()
const payload = {
tagName,
textColor: (sourceTag && sourceTag.textColor) || DEFAULT_TAG_STYLE.textColor,
bgColor: (sourceTag && sourceTag.bgColor) || DEFAULT_TAG_STYLE.bgColor,
borderColor: (sourceTag && sourceTag.borderColor) || DEFAULT_TAG_STYLE.borderColor,
sortOrder: (sourceTag && sourceTag.sortOrder) || DEFAULT_TAG_STYLE.sortOrder,
frontVisible: (sourceTag && sourceTag.frontVisible) === 0 ? 0 : DEFAULT_TAG_STYLE.frontVisible
}
if (shopId) {
payload.shopId = shopId
}
return payload
}
Page({
data: {
presets: PRESETS,
@@ -89,9 +152,16 @@ Page({
exceptions: [],
loading: false,
saving: false,
tagsLoading: false,
tagSaving: false,
coverUploading: false,
coverImagePreviewUrl: '',
introLength: introLength(DEFAULT_SHOP_INTRO),
tagList: [],
tagForm: {
id: '',
name: ''
},
form: {
shopName: BRAND_CONFIG.defaultShopName,
startTime: '10:00',
@@ -116,12 +186,25 @@ Page({
this.loadSettings()
},
async loadTags() {
this.setData({ tagsLoading: true })
try {
const tagsData = await tagsApi.listAll()
this.setData({ tagList: normalizeTagOptions(tagsData.list || tagsData || []) })
} catch (error) {
wx.showToast({ title: error.message || '标签加载失败', icon: 'none' })
} finally {
this.setData({ tagsLoading: false })
}
},
async loadSettings() {
this.setData({ loading: true })
try {
const [settings, exceptions] = await Promise.all([
const [settings, exceptions, tagsData] = await Promise.all([
shopApi.settings(),
shopApi.scheduleExceptions()
shopApi.scheduleExceptions(),
tagsApi.listAll()
])
const hours = String(settings.businessHours || '').split('-')
const preset = buildPreset(settings.schedulePresetType || 'EVERYDAY')
@@ -147,7 +230,8 @@ Page({
startTime: item.startTime ? String(item.startTime).slice(0, 5) : '',
endTime: item.endTime ? String(item.endTime).slice(0, 5) : '',
summary: buildExceptionSummary(item)
}))
})),
tagList: normalizeTagOptions(tagsData.list || tagsData || [])
})
} catch (error) {
wx.showToast({ title: error.message || '店铺设置加载失败', icon: 'none' })
@@ -166,6 +250,98 @@ Page({
this.setData(nextData)
},
onTagInput(event) {
this.setData({ 'tagForm.name': event.detail.value })
},
editTag(event) {
const tagId = String(event.currentTarget.dataset.id || '')
const tag = this.data.tagList.find(item => String(item.id) === tagId)
if (!tag) {
wx.showToast({ title: '标签不存在', icon: 'none' })
return
}
this.setData({
tagForm: {
id: tag.id,
name: tag.tagName || tag.name
}
})
},
clearTagForm() {
this.setData({
tagForm: {
id: '',
name: ''
}
})
},
async saveTag() {
if (this.data.tagSaving) {
return
}
const tagName = String(this.data.tagForm.name || '').trim()
if (!tagName) {
wx.showToast({ title: '请输入标签名称', icon: 'none' })
return
}
const tagId = this.data.tagForm.id
const editingTag = tagId
? this.data.tagList.find(item => String(item.id) === String(tagId))
: null
this.setData({ tagSaving: true })
try {
if (tagId) {
await tagsApi.update(tagId, buildTagPayload(tagName, editingTag))
wx.showToast({ title: '标签已更新', icon: 'success' })
} else {
await tagsApi.create(buildTagPayload(tagName, null))
wx.showToast({ title: '标签已新增', icon: 'success' })
}
this.clearTagForm()
await this.loadTags()
} catch (error) {
wx.showToast({ title: error.message || '标签保存失败', icon: 'none' })
} finally {
this.setData({ tagSaving: false })
}
},
deleteTag(event) {
const tagId = String(event.currentTarget.dataset.id || '')
const tag = this.data.tagList.find(item => String(item.id) === tagId)
if (!tag) {
wx.showToast({ title: '标签不存在', icon: 'none' })
return
}
wx.showModal({
title: '删除标签',
content: `确认删除标签“${tag.name}”?`,
confirmText: '删除',
confirmColor: '#d98d8d',
success: async result => {
if (!result.confirm) {
return
}
this.setData({ tagSaving: true })
try {
await tagsApi.delete(tag.id)
if (String(this.data.tagForm.id) === String(tag.id)) {
this.clearTagForm()
}
await this.loadTags()
wx.showToast({ title: '标签已删除', icon: 'success' })
} catch (error) {
wx.showToast({ title: error.message || '标签删除失败', icon: 'none' })
} finally {
this.setData({ tagSaving: false })
}
}
})
},
async chooseCoverImage() {
if (this.data.coverUploading || this.data.saving) {
return
@@ -63,6 +63,31 @@
</view>
</view>
<view class="section">
<view class="section-title">标签设置</view>
<view class="card tag-setting-card">
<view class="tag-form-row">
<input class="tag-input" value="{{tagForm.name}}" bindinput="onTagInput" placeholder="输入标签名称" />
<button class="secondary-btn tag-save-btn" loading="{{tagSaving}}" disabled="{{tagSaving || tagsLoading}}" bindtap="saveTag">{{tagForm.id ? '更新' : '新增'}}</button>
</view>
<view wx:if="{{tagForm.id}}" class="tag-edit-tip">
<text>正在编辑“{{tagForm.name}}”</text>
<text class="tag-clear" bindtap="clearTagForm">取消</text>
</view>
<view wx:if="{{tagsLoading}}" class="tag-empty">标签加载中</view>
<view wx:elif="{{tagList.length}}" class="tag-list">
<view wx:for="{{tagList}}" wx:key="id" class="tag-item">
<view class="tag-preview" style="background: {{item.bgColor}}; color: {{item.textColor}}; border-color: {{item.borderColor}};">{{item.name}}</view>
<view class="tag-actions">
<text data-id="{{item.id}}" bindtap="editTag">编辑</text>
<text class="danger" data-id="{{item.id}}" bindtap="deleteTag">删除</text>
</view>
</view>
</view>
<view wx:else class="tag-empty">暂无标签,可用于新建预约时快速选择</view>
</view>
</view>
<view class="section">
<view class="section-title">店铺排班</view>
<view class="card rule-card">
@@ -278,6 +278,94 @@
font-weight: 800;
}
.tag-setting-card {
display: flex;
flex-direction: column;
gap: 18rpx;
padding: 20rpx 24rpx;
}
.tag-form-row {
display: grid;
grid-template-columns: minmax(0, 1fr) 150rpx;
gap: 12rpx;
}
.tag-input {
min-width: 0;
height: 70rpx;
padding: 0 18rpx;
border: 1rpx solid #eadfd2;
border-radius: 14rpx;
background: #fff8f2;
color: #3d3128;
font-size: 26rpx;
}
.tag-save-btn {
width: 100%;
min-width: 0;
}
.tag-edit-tip {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12rpx;
color: #7b7167;
font-size: 22rpx;
}
.tag-clear {
color: #2f6b55;
}
.tag-list {
display: grid;
gap: 12rpx;
}
.tag-item {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12rpx;
}
.tag-preview {
display: inline-flex;
align-items: center;
min-width: 0;
max-width: 100%;
min-height: 54rpx;
padding: 0 16rpx;
overflow: hidden;
border: 1rpx solid;
border-radius: 999rpx;
font-size: 24rpx;
font-weight: 800;
text-overflow: ellipsis;
white-space: nowrap;
}
.tag-actions {
display: flex;
flex: 0 0 auto;
gap: 16rpx;
color: #2f6b55;
font-size: 22rpx;
font-weight: 800;
}
.tag-actions .danger {
color: #c36a6a;
}
.tag-empty {
color: #8c8177;
font-size: 24rpx;
}
.rule-card {
display: flex;
flex-direction: column;