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 @@