From 355eada582b2acd028cf4197d1a3423c90975dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Thu, 5 Feb 2026 16:10:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor(shop):=20=E9=87=8D=E6=9E=84=E5=88=86?= =?UTF-8?q?=E9=94=80=E5=95=86=E8=AE=BE=E7=BD=AE=E5=AD=98=E5=82=A8=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E5=B9=B6=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将单个设置对象改为按类型分组的映射结构,支持基础设置、分销商条件、结算等功能模块 - 新增 getBooleanFlag、getNumberFlag、pickEnumValue 等工具函数统一数据转换逻辑 - 实现 ensurePath 和 setWordValue 函数用于深层对象路径操作和文案配置 - 优化图片上传相关函数 getUploadUrl 和 toUploadFileList 的 URL 获取逻辑 - 将整体保存逻辑拆分为按设置类型分别保存的 saveSettingItem 方法 - 更新分销商类型显示,将"配送员"修改为"总分销商"以符合业务需求 - 调整精度控制从4位小数改为3位小数以匹配实际业务场景 --- src/views/shop/shopDealerSetting/index.vue | 278 ++++++++++++++---- .../components/shopDealerUserEdit.vue | 4 +- src/views/shop/shopDealerUser/index.vue | 2 +- 3 files changed, 218 insertions(+), 66 deletions(-) diff --git a/src/views/shop/shopDealerSetting/index.vue b/src/views/shop/shopDealerSetting/index.vue index 6a264b6..d32de64 100644 --- a/src/views/shop/shopDealerSetting/index.vue +++ b/src/views/shop/shopDealerSetting/index.vue @@ -245,10 +245,16 @@ const activeTab = ref('basic'); // 保存状态 const saving = ref(false); - const currentSetting = ref(null); - const hasSetting = ref(false); - const SETTING_KEY = 'dealer_setting'; - const SETTING_DESCRIPTION = '分销商设置'; + const settingMap = ref>({}); + const settingValuesMap = ref>({}); + const settingMeta = { + basic: '基础设置', + condition: '分销商条件', + settlement: '结算', + license: '申请协议', + words: '自定义文字', + background: '页面背景图' + }; // 基础设置 const basicSettings = reactive({ @@ -289,19 +295,61 @@ backgroundImages: [] }); - const applySettingGroup = (target: Record, source?: any) => { - if (source && typeof source === 'object') { - Object.assign(target, source); - } + const getBooleanFlag = (value: any) => value === 1 || value === true; + const getNumberFlag = (value: any) => (value ? 1 : 0); + const pickEnumValue = (value: any, options: number[], fallback: number) => + options.includes(value) ? value : fallback; + + const ensurePath = (root: Record, path: string[]) => { + let current = root; + path.forEach((key) => { + if (!current[key] || typeof current[key] !== 'object') { + current[key] = {}; + } + current = current[key]; + }); + return current; }; - const applySettingValues = (values: any) => { - applySettingGroup(basicSettings, values?.basic); - applySettingGroup(commissionSettings, values?.commission); - applySettingGroup(withdrawSettings, values?.withdraw); - applySettingGroup(agreementSettings, values?.agreement); - applySettingGroup(notificationSettings, values?.notification); - applySettingGroup(pageSettings, values?.page); + const setWordValue = ( + root: Record, + path: string[], + value: string + ) => { + const node = ensurePath(root, path); + if (typeof node.default !== 'string') { + node.default = value; + } + node.value = value; + }; + + const getUploadUrl = (fileList: any[]) => { + if (!Array.isArray(fileList) || fileList.length === 0) { + return ''; + } + const file = fileList[0]; + return ( + file?.url || + file?.thumbUrl || + file?.response?.url || + file?.response?.data?.url || + file?.response?.data?.path || + '' + ); + }; + + const toUploadFileList = (url?: string) => { + if (!url) { + return []; + } + return [ + { + uid: 'background-0', + name: '背景图', + status: 'done', + url + } + ]; }; /* 图片预览 */ @@ -313,73 +361,177 @@ const loadSettings = async () => { try { const list = await listShopDealerSetting(); - const setting = - list.find((item) => item.key === SETTING_KEY) ?? list[0]; - if (!setting) { - hasSetting.value = false; - currentSetting.value = null; - return; + const nextMap: Record = {}; + const nextValues: Record = {}; + list.forEach((item) => { + if (!item.key) { + return; + } + nextMap[item.key] = item; + if (item.values) { + try { + nextValues[item.key] = JSON.parse(item.values); + } catch (error) { + console.warn('解析设置失败:', item.key, error); + } + } + }); + + settingMap.value = nextMap; + settingValuesMap.value = nextValues; + + const basicValue = nextValues.basic || {}; + basicSettings.enableDistribution = getBooleanFlag(basicValue.is_open); + basicSettings.distributionLevel = basicValue.level ?? 3; + basicSettings.dealerSelfBuy = getBooleanFlag(basicValue.self_buy); + + const conditionValue = nextValues.condition || {}; + commissionSettings.applyType = pickEnumValue( + conditionValue.become, + [10, 20], + commissionSettings.applyType + ); + + const settlementValue = nextValues.settlement || {}; + commissionSettings.settlementType = pickEnumValue( + settlementValue.settle_days ?? settlementValue.settlement_type, + [10, 20], + commissionSettings.settlementType + ); + commissionSettings.minWithdrawAmount = + settlementValue.min_money ?? commissionSettings.minWithdrawAmount; + + withdrawSettings.withdrawMethods = Array.isArray(settlementValue.pay_type) + ? settlementValue.pay_type + : withdrawSettings.withdrawMethods; + withdrawSettings.withdrawFeeRate = + typeof settlementValue.fee_rate === 'number' + ? settlementValue.fee_rate + : withdrawSettings.withdrawFeeRate; + withdrawSettings.withdrawAudit = + settlementValue.audit === undefined + ? withdrawSettings.withdrawAudit + : getBooleanFlag(settlementValue.audit); + + const licenseValue = nextValues.license || {}; + if (licenseValue.license) { + agreementSettings.dealerAgreement = licenseValue.license; } - currentSetting.value = setting; - hasSetting.value = true; - if (setting.values) { - try { - const parsed = JSON.parse(setting.values); - applySettingValues(parsed); - } catch (error) { - console.warn('解析设置失败:', error); - message.warning('设置数据解析失败,已使用默认值'); - } + const wordsValue = nextValues.words || {}; + if (wordsValue.index?.title?.value) { + pageSettings.centerTitle = wordsValue.index.title.value; } + if (wordsValue.apply?.words?.wait_audit?.value) { + notificationSettings.applySuccessText = + wordsValue.apply.words.wait_audit.value; + } + if (wordsValue.apply?.words?.fail?.value) { + notificationSettings.applyFailText = wordsValue.apply.words.fail.value; + } + if (wordsValue.withdraw_apply?.words?.success?.value) { + notificationSettings.withdrawSuccessText = + wordsValue.withdraw_apply.words.success.value; + } + + const backgroundValue = nextValues.background || {}; + const backgroundUrl = + backgroundValue.index || + backgroundValue.apply || + backgroundValue.withdraw_apply; + pageSettings.backgroundImages = toUploadFileList(backgroundUrl); } catch (error) { console.error('加载设置失败:', error); - hasSetting.value = false; - currentSetting.value = null; message.error('加载设置失败'); } }; + const saveSettingItem = async ( + key: keyof typeof settingMeta, + values: Record + ) => { + const existSetting = settingMap.value[key]; + const payload: ShopDealerSetting = { + ...(existSetting || {}), + key, + describe: existSetting?.describe ?? settingMeta[key], + values: JSON.stringify(values), + updateTime: Date.now() + }; + + const saveOrUpdate = existSetting + ? updateShopDealerSetting + : addShopDealerSetting; + await saveOrUpdate(payload); + settingMap.value[key] = payload; + settingValuesMap.value[key] = values; + }; + /* 保存设置 */ const saveSettings = async () => { saving.value = true; try { - // 收集所有设置数据 - const allSettings = { - basic: { ...basicSettings }, - commission: { ...commissionSettings }, - withdraw: { ...withdrawSettings }, - agreement: { ...agreementSettings }, - notification: { ...notificationSettings }, - page: { - ...pageSettings, - backgroundImages: [...pageSettings.backgroundImages] - } + const basicValues = { + is_open: getNumberFlag(basicSettings.enableDistribution), + level: basicSettings.distributionLevel, + self_buy: getNumberFlag(basicSettings.dealerSelfBuy) }; - console.log('保存设置:', allSettings); - - const payload: ShopDealerSetting = { - ...currentSetting.value, - key: currentSetting.value?.key ?? SETTING_KEY, - describe: currentSetting.value?.describe ?? SETTING_DESCRIPTION, - values: JSON.stringify(allSettings), - updateTime: Date.now() + const conditionValues = { + ...(settingValuesMap.value.condition || {}), + become: commissionSettings.applyType }; - const saveOrUpdate = hasSetting.value - ? updateShopDealerSetting - : addShopDealerSetting; - await saveOrUpdate(payload); + const settlementValues = { + ...(settingValuesMap.value.settlement || {}), + pay_type: [...withdrawSettings.withdrawMethods], + min_money: commissionSettings.minWithdrawAmount, + settle_days: commissionSettings.settlementType, + fee_rate: withdrawSettings.withdrawFeeRate, + audit: getNumberFlag(withdrawSettings.withdrawAudit) + }; - if (!hasSetting.value) { - await loadSettings(); - } else { - currentSetting.value = payload; - } + const licenseValues = { + license: agreementSettings.dealerAgreement + }; - // 模拟保存 - await new Promise((resolve) => setTimeout(resolve, 1000)); + const wordsValues = { + ...(settingValuesMap.value.words || {}) + }; + setWordValue(wordsValues, ['index', 'title'], pageSettings.centerTitle); + setWordValue( + wordsValues, + ['apply', 'words', 'wait_audit'], + notificationSettings.applySuccessText + ); + setWordValue( + wordsValues, + ['apply', 'words', 'fail'], + notificationSettings.applyFailText + ); + setWordValue( + wordsValues, + ['withdraw_apply', 'words', 'success'], + notificationSettings.withdrawSuccessText + ); + + const backgroundUrl = getUploadUrl(pageSettings.backgroundImages); + const backgroundValues = { + ...(settingValuesMap.value.background || {}), + index: backgroundUrl || settingValuesMap.value.background?.index || '', + apply: backgroundUrl || settingValuesMap.value.background?.apply || '', + withdraw_apply: + backgroundUrl || settingValuesMap.value.background?.withdraw_apply || '' + }; + + await saveSettingItem('basic', basicValues); + await saveSettingItem('condition', conditionValues); + await saveSettingItem('settlement', settlementValues); + await saveSettingItem('license', licenseValues); + await saveSettingItem('words', wordsValues); + await saveSettingItem('background', backgroundValues); + + await loadSettings(); message.success('设置保存成功'); } catch (error) { diff --git a/src/views/shop/shopDealerUser/components/shopDealerUserEdit.vue b/src/views/shop/shopDealerUser/components/shopDealerUserEdit.vue index 1ba841d..4eaecaf 100644 --- a/src/views/shop/shopDealerUser/components/shopDealerUserEdit.vue +++ b/src/views/shop/shopDealerUser/components/shopDealerUserEdit.vue @@ -40,7 +40,7 @@ 分销商 门店 - 配送员 + 总分销商 @@ -136,7 +136,7 @@ class="ele-fluid" :min="0" :max="1" - :precision="4" + :precision="3" stringMode :disabled="true" placeholder="例如 0.007" diff --git a/src/views/shop/shopDealerUser/index.vue b/src/views/shop/shopDealerUser/index.vue index 67ee842..d7d858a 100644 --- a/src/views/shop/shopDealerUser/index.vue +++ b/src/views/shop/shopDealerUser/index.vue @@ -26,7 +26,7 @@