fix:商品套餐添加固定价格
Build and Deploy Vue3 / build (push) Successful in 1m19s
Build and Deploy Vue3 / deploy (push) Successful in 1m26s

This commit is contained in:
2026-02-05 15:15:13 +08:00
parent 4d45cf535e
commit fdc9db9a9c
3 changed files with 206 additions and 1220 deletions
+141 -15
View File
@@ -520,13 +520,15 @@
:title="planFormType === 'add' ? '新增套餐' : '编辑套餐'"
width="700px"
append-to-body
class="plan-form-dialog"
>
<el-form
ref="planFormRef"
:model="planForm"
:rules="planFormRules"
label-width="100px"
>
<div class="plan-form-content">
<el-form
ref="planFormRef"
:model="planForm"
:rules="planFormRules"
label-width="100px"
>
<el-form-item label="套餐名称" prop="name">
<el-input v-model="planForm.name" placeholder="请输入套餐名称" />
</el-form-item>
@@ -665,6 +667,22 @@
</el-form-item>
<el-form-item label="库存数量" prop="inventory">
<el-input-number v-model="planForm.inventory" :min="0" style="width: 100%" placeholder="库存数量" />
<div class="form-tip">0 表示没有库存</div>
</el-form-item>
<el-form-item label="启用固定价格" prop="enable_fixed_price">
<el-switch
v-model="planForm.enable_fixed_price"
:active-value="true"
:inactive-value="false"
active-text="启用"
inactive-text="禁用"
:loading="fixedPriceLoading"
@change="handleFixedPriceChange"
/>
<div class="form-tip">启用后套餐价格将使用固定价格不再根据参数计算</div>
</el-form-item>
<el-form-item label="固定价格(元)" prop="fixed_price" v-if="planForm.enable_fixed_price === true">
<el-input-number v-model="planForm.fixed_price" :min="0" :precision="2" :step="0.01" style="width: 100%" placeholder="请输入固定价格(元)" />
</el-form-item>
<el-form-item label="排序索引" prop="index">
<el-input-number v-model="planForm.index" :min="0" style="width: 100%" />
@@ -675,7 +693,8 @@
<el-radio :value="true">禁用</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
</el-form>
</div>
<template #footer>
<div class="dialog-footer">
<el-button @click="planFormDialogVisible = false">取消</el-button>
@@ -793,7 +812,9 @@ import { getProductList, createProduct, updateProduct, deleteProduct, getProduct
updateProductPlan,
deleteProductPlan,
disableProductPlan,
enableProductPlan
enableProductPlan,
disablePlanFixedPrice,
enablePlanFixedPrice
} from '@/api/admin/product'
// 查询参数
@@ -1603,6 +1624,7 @@ const currentPlanProductName = ref('')
const planFormDialogVisible = ref(false)
const planFormType = ref('add')
const planFormRef = ref(null)
const fixedPriceLoading = ref(false) // 固定价格开关加载状态
const planForm = reactive({
plan_id: undefined,
name: '',
@@ -1611,6 +1633,8 @@ const planForm = reactive({
extra_arg_ids: '',
extra_arg_ids_array: [],
inventory: 0,
fixed_price: 0,
enable_fixed_price: false,
index: 0,
disable: false
})
@@ -1876,10 +1900,15 @@ const initSelectedArgsFromJson = (argsJson, extraArgIds = []) => {
// 处理额外参数(排除已在args中的参数)
if (extraArgIds && extraArgIds.length > 0) {
selectedExtraArgIds.value = extraArgIds.filter(id => !argsParamIds.includes(id))
console.log('initSelectedArgsFromJson - 额外参数已设置:', selectedExtraArgIds.value)
} else {
console.log('initSelectedArgsFromJson - 无额外参数或为空, extraArgIds:', extraArgIds)
}
// 根据选择状态重新生成 JSON(确保初始状态就有 JSON 展示)
updateArgsJson()
// 同步更新 planForm.extra_arg_ids
updateExtraArgIds()
}
// 打开套餐管理
@@ -1967,6 +1996,8 @@ const handleAddPlan = async () => {
extra_arg_ids: '',
extra_arg_ids_array: [],
inventory: 0,
fixed_price: 0,
enable_fixed_price: false,
index: 0,
disable: false
})
@@ -1990,16 +2021,28 @@ const handleEditPlan = async (row) => {
if (res.data.code === 200) {
const data = res.data.data
// 处理 extra_arg_ids
// 处理 extra_arg_ids(兼容驼峰和下划线命名,以及 extraArgs 对象数组)
let extraArgIdsArray = []
if (data.extraArgIds) {
if (Array.isArray(data.extraArgIds)) {
extraArgIdsArray = data.extraArgIds
} else if (typeof data.extraArgIds === 'string') {
extraArgIdsArray = data.extraArgIds.split(',').filter(Boolean).map(Number)
const extraArgIdsRaw = data.extraArgIds || data.extra_arg_ids
const extraArgsRaw = data.extraArgs || data.extra_args
if (extraArgIdsRaw) {
// 优先使用 extra_arg_ids 字段
if (Array.isArray(extraArgIdsRaw)) {
extraArgIdsArray = extraArgIdsRaw.map(Number)
} else if (typeof extraArgIdsRaw === 'string' && extraArgIdsRaw.trim()) {
extraArgIdsArray = extraArgIdsRaw.split(',').filter(Boolean).map(Number)
}
} else if (extraArgsRaw && Array.isArray(extraArgsRaw)) {
// 如果有 extraArgs 对象数组,从中提取参数ID
extraArgIdsArray = extraArgsRaw.map(arg => arg.id || arg.arg_id || arg.argId).filter(Boolean).map(Number)
}
console.log('编辑套餐 - 原始数据:', data)
console.log('编辑套餐 - extraArgIdsRaw:', extraArgIdsRaw)
console.log('编辑套餐 - extraArgsRaw:', extraArgsRaw)
console.log('编辑套餐 - extraArgIdsArray:', extraArgIdsArray)
Object.assign(planForm, {
plan_id: data.id,
name: data.name || '',
@@ -2008,6 +2051,8 @@ const handleEditPlan = async (row) => {
extra_arg_ids: extraArgIdsArray.join(','),
extra_arg_ids_array: extraArgIdsArray,
inventory: data.inventory || 0,
fixed_price: ((data.fixedPrice || data.fixed_price || 0) / 100).toFixed(2) * 1, // 分转元
enable_fixed_price: !!(data.enableFixedPrice || data.enable_fixed_price), // 转为布尔值
index: data.index || 0,
disable: data.disable || false
})
@@ -2054,21 +2099,71 @@ const handleTogglePlanStatus = async (row) => {
}
}
// 处理固定价格开关变化(编辑模式下调用API)
const handleFixedPriceChange = async (value) => {
// 只有编辑模式下才调用API
if (planFormType.value !== 'edit' || !planForm.plan_id) {
return
}
fixedPriceLoading.value = true
try {
const data = {
good_id: String(currentPlanProductId.value),
plan_id: String(planForm.plan_id)
}
let res
if (value === true) {
res = await enablePlanFixedPrice(data)
} else {
res = await disablePlanFixedPrice(data)
}
if (res.data.code === 200) {
ElMessage.success(value ? '已启用固定价格' : '已禁用固定价格')
} else {
// 恢复原值
planForm.enable_fixed_price = !value
ElMessage.error(res.data.message || '操作失败')
}
} catch (error) {
// 恢复原值
planForm.enable_fixed_price = !value
console.error('切换固定价格状态失败:', error)
ElMessage.error('操作失败')
} finally {
fixedPriceLoading.value = false
}
}
// 提交套餐表单
const submitPlanForm = () => {
planFormRef.value?.validate(async (valid) => {
if (valid) {
try {
// 确保 extra_arg_ids 是最新的
const extraArgIdsStr = selectedExtraArgIds.value.join(',')
const submitData = {
good_id: String(currentPlanProductId.value),
name: planForm.name,
note: planForm.note || '',
args: planForm.args || '',
extra_arg_ids: planForm.extra_arg_ids || '',
extra_arg_ids: extraArgIdsStr || planForm.extra_arg_ids || '',
inventory: Number(planForm.inventory) || 0,
fixed_price: Math.round(Number(planForm.fixed_price) * 100) || 0, // 元转分
index: Number(planForm.index) || 0
}
// 只有创建时才传递 enable_fixed_price
if (planFormType.value === 'add') {
submitData.enable_fixed_price = planForm.enable_fixed_price === true
}
console.log('提交套餐数据:', submitData)
console.log('selectedExtraArgIds:', selectedExtraArgIds.value)
let res
if (planFormType.value === 'add') {
res = await createProductPlan(submitData)
@@ -2356,6 +2451,37 @@ const submitPlanForm = () => {
overflow: hidden;
}
/* 套餐表单弹窗样式 */
.plan-form-content {
max-height: 60vh;
overflow-y: auto;
padding-right: 8px;
margin-right: -8px;
}
/* 隐形滚动条样式 */
.plan-form-content::-webkit-scrollbar {
width: 6px;
}
.plan-form-content::-webkit-scrollbar-track {
background: transparent;
}
.plan-form-content::-webkit-scrollbar-thumb {
background-color: transparent;
border-radius: 3px;
transition: background-color 0.3s;
}
.plan-form-content:hover::-webkit-scrollbar-thumb {
background-color: rgba(144, 147, 153, 0.3);
}
.plan-form-content::-webkit-scrollbar-thumb:hover {
background-color: rgba(144, 147, 153, 0.5);
}
/* 参数配置选择器样式 */
.args-config-container {
width: 100%;