Files
JayuCloud/JC-Salon-WeChat/pages/backfill-order/index.js
T
xiaowang 261723bc6e
发布 Nacos 配置 / publish (push) Successful in 30s
环境分支晋级检查 / environment-promotion-source (pull_request_target) Successful in 1s
功能:优化Salon作品选择与历史补单
2026-10-14 00:47:10 +08:00

89 lines
9.3 KiB
JavaScript

const planningApi = require('../../api/planning')
const employeesApi = require('../../api/employees')
const productsApi = require('../../api/products')
const customersApi = require('../../api/customers')
const dateUtil = require('../../utils/date')
Page({
data: { form: { serviceDate: dateUtil.formatDate(new Date()), manualProductTitle: '', amount: '', customerName: '', phone: '', serviceTime: '', status: 4, remark: '', artistId: '', productId: '', customerUserId: '' }, manualProductDraft: { title: '', price: '' }, manualMoreExpanded: false, productSelectorMode: 'list', artists: [], artistOptions: [], allProductOptions: [], productOptions: [], customerOptions: [], allCustomerOptions: [], productSelectorVisible: false, artistSelectorVisible: false, customerSelectorVisible: false, loadingProducts: false, loadingArtists: false, loadingCustomers: false, productKeyword: '', artistKeyword: '', customerKeyword: '', productError: '', artistError: '', customerError: '', selectedArtistName: '', selectedProductTitle: '', selectedProductPriceText: '', submitting: false },
onLoad(options = {}) {
const serviceDate = options.serviceDate || this.data.form.serviceDate
const artistId = options.artistId || ''
this.setData({ 'form.serviceDate': serviceDate, 'form.artistId': artistId })
this.loadArtists(artistId); this.loadProducts(); this.loadCustomers()
},
async loadArtists(preferredArtistId) {
try {
const result = await employeesApi.list({ page: 1, size: 100 })
const list = result.list || result || []
const normalized = list.map(item => ({ ...item, name: item.name || item.nickname || item.account || `员工${item.id}` }))
const selected = normalized.find(item => String(item.id) === String(preferredArtistId || this.data.form.artistId))
this.setData({ artists: normalized, artistOptions: normalized.map(item => ({ id: item.id, title: item.name, subtitle: item.phone || '' })), selectedArtistName: selected ? selected.name : '', 'form.artistId': selected ? selected.id : '' })
this.refreshProductOptions()
} catch (error) { wx.showToast({ title: error.message || '员工加载失败', icon: 'none' }) }
},
async loadProducts() { this.setData({ loadingProducts: true }); try { const result = await productsApi.list({ page: 1, size: 100 }); const list = result.list || result || []; this.setData({ products: list, productError: '' }); this.refreshProductOptions() } catch (error) { this.setData({ products: [], allProductOptions: [], productOptions: [], productError: error.message || '项目加载失败' }) } finally { this.setData({ loadingProducts: false }) } },
refreshProductOptions() {
const options = (this.data.products || []).map(item => {
const artistIds = String(item.bookableArtists || '').split(',').map(id => id.trim()).filter(Boolean)
const artistNames = artistIds.map(id => this.data.artists.find(artist => String(artist.id) === id)).filter(Boolean).map(artist => artist.name)
const tagList = Array.isArray(item.tagList) ? item.tagList : Array.isArray(item.tagInfos) ? item.tagInfos.map(tag => tag.tagName).filter(Boolean) : String(item.tags || '').split(/[;,,、]/).map(tag => tag.trim()).filter(Boolean)
return {
id: item.id,
title: item.title,
price: item.price,
priceText: item.price == null ? '价格待定' : `¥${item.price}`,
coverImage: Array.isArray(item.imageUrls) ? item.imageUrls[0] || '' : '',
durationText: item.approximateDuration || (item.serviceDurationMinutes ? `${item.serviceDurationMinutes}分钟` : '时长待定'),
artistText: artistIds.length ? artistNames.join('、') || `${artistIds.length}位员工可做` : '全部员工',
tagList: tagList.slice(0, 3)
}
})
const keyword = String(this.data.productKeyword || '').trim().toLowerCase()
this.setData({ allProductOptions: options, productOptions: options.filter(item => !keyword || `${item.title} ${item.artistText} ${item.tagList.join(' ')}`.toLowerCase().includes(keyword)) })
},
async loadCustomers() { this.setData({ loadingCustomers: true }); try { const result = await customersApi.list({ page: 1, size: 30 }); const list = result.list || result || []; const options = list.map(item => ({ id: item.id, title: item.nickname || item.name || item.account, subtitle: item.phone || item.contactPhone || '' })); this.setData({ allCustomerOptions: options, customerOptions: options, customerError: '' }) } catch (error) { this.setData({ customerError: error.message || '客户加载失败' }) } finally { this.setData({ loadingCustomers: false }) } },
openProductSelector() { this.setData({ productSelectorVisible: true, productSelectorMode: 'list' }) },
closeProductSelector() { this.setData({ productSelectorVisible: false, productSelectorMode: 'list' }) },
openManualProduct() { this.setData({ productSelectorMode: 'manual', manualMoreExpanded: false }) },
backToProductList() { this.setData({ productSelectorMode: 'list' }) },
onManualProductInput(e) { this.setData({ [`manualProductDraft.${e.detail.field}`]: e.detail.value }) },
toggleManualProductMore() { this.setData({ manualMoreExpanded: !this.data.manualMoreExpanded }) },
openArtistSelector() { this.setData({ artistSelectorVisible: true }) },
closeArtistSelector() { this.setData({ artistSelectorVisible: false }) },
openCustomerSelector() { this.setData({ customerSelectorVisible: true }) },
closeCustomerSelector() { this.setData({ customerSelectorVisible: false }) },
searchProducts(e) { const keyword = String(e.detail.value || '').trim().toLowerCase(); this.setData({ productKeyword: keyword, productOptions: this.data.allProductOptions.filter(item => !keyword || `${item.title} ${item.artistText} ${item.tagList.join(' ')}`.toLowerCase().includes(keyword)) }) },
searchArtists(e) { const keyword = String(e.detail.value || '').toLowerCase(); this.setData({ artistKeyword: keyword, artistOptions: this.data.artists.map(item => ({ id: item.id, title: item.name, subtitle: item.phone || '' })).filter(item => !keyword || item.title.toLowerCase().includes(keyword)) }) },
searchCustomers(e) { const keyword = String(e.detail.value || '').toLowerCase(); this.setData({ customerKeyword: keyword, customerOptions: this.data.allCustomerOptions.filter(item => !keyword || String(item.title).toLowerCase().includes(keyword) || String(item.subtitle).includes(keyword)) }) },
selectProduct(e) { const item = e.detail.item; this.setData({ 'form.productId': item.id, selectedProductTitle: item.title, selectedProductPriceText: item.priceText || (item.price == null ? '价格待定' : `¥${item.price}`), 'form.manualProductTitle': '', manualProductDraft: { title: '', price: '' }, 'form.amount': item.price == null ? this.data.form.amount : String(item.price), productSelectorVisible: false, productSelectorMode: 'list' }) },
selectArtist(e) { const item = e.detail.item; this.setData({ 'form.artistId': item.id, selectedArtistName: item.title, artistSelectorVisible: false }) },
selectCustomer(e) { const item = e.detail.item; this.setData({ 'form.customerUserId': item.id, 'form.customerName': item.title, 'form.phone': item.subtitle, customerSelectorVisible: false }) },
useWalkIn() { this.setData({ 'form.customerUserId': '', 'form.customerName': '', 'form.phone': '', customerSelectorVisible: false }) },
onCustomerNameInput(e) { this.setData({ 'form.customerName': e.detail.value, 'form.customerUserId': '' }) },
useManualProduct() {
const title = String(this.data.manualProductDraft.title || '').trim()
const priceText = String(this.data.manualProductDraft.price === undefined ? '' : this.data.manualProductDraft.price).trim()
const price = Number(priceText)
if (!title || !priceText || !Number.isFinite(price) || price < 0) {
wx.showToast({ title: '请填写作品名称和价格', icon: 'none' })
return
}
this.setData({ 'form.productId': '', 'form.manualProductTitle': title, selectedProductTitle: '', selectedProductPriceText: `¥${price}`, 'form.amount': String(price), productSelectorVisible: false, productSelectorMode: 'list' })
},
onInput(e) { this.setData({ [`form.${e.currentTarget.dataset.field}`]: e.detail.value }) },
onDateChange(e) { this.setData({ 'form.serviceDate': e.detail.value }) },
onTimeChange(e) { this.setData({ 'form.serviceTime': e.detail.value }) },
onArtistChange(e) { const index = Number(e.detail.value); const item = this.data.artists[index] || {}; this.setData({ artistIndex: index, selectedArtistName: item.name || item.nickname || '', 'form.artistId': item.id }) },
selectStatus(e) { this.setData({ 'form.status': Number(e.currentTarget.dataset.status) }) },
async submit() {
const form = this.data.form
if (!form.serviceDate || (!form.productId && !form.manualProductTitle) || !form.amount || !form.artistId) { wx.showToast({ title: '请填写项目、金额、日期和员工', icon: 'none' }); return }
if (form.phone && !/^1[3-9]\d{9}$/.test(form.phone)) { wx.showToast({ title: '手机号格式不正确', icon: 'none' }); return }
this.setData({ submitting: true })
try { await planningApi.createBackfillOrder(form); wx.showToast({ title: '补单已保存', icon: 'success' }); const app = getApp(); app.globalData.planningNeedRefresh = true; setTimeout(() => wx.navigateBack(), 500) }
catch (error) { wx.showToast({ title: error.message || '保存失败', icon: 'none' }) }
finally { this.setData({ submitting: false }) }
}
})