feat: 商品添加克重字段,订单添加通道费,运费按重量计算

- 商品模型: 添加weight字段(克重)
- 订单模型: 添加channel_fee字段(支付通道费)
- 后台商品管理: 添加克重输入框
- 后台订单详情: 显示通道费
- 订单创建: 根据商品重量计算运费,添加通道费计算
- 购物车: 根据商品重量计算运费,显示通道费

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-10 20:01:45 +08:00
parent ded24ddccc
commit 33f5d242e9
6 changed files with 57 additions and 18 deletions
+16 -7
View File
@@ -243,6 +243,7 @@ func (h *OrderHandler) Create(c *gin.Context) {
var subtotal float64 var subtotal float64
var totalQuantity int var totalQuantity int
var totalWeight float64
var orderItems []models.OrderItem var orderItems []models.OrderItem
supplierMap := make(map[uint]bool) supplierMap := make(map[uint]bool)
@@ -280,6 +281,7 @@ func (h *OrderHandler) Create(c *gin.Context) {
subtotal += cart.Product.Price * float64(cart.Quantity) subtotal += cart.Product.Price * float64(cart.Quantity)
totalQuantity += cart.Quantity totalQuantity += cart.Quantity
totalWeight += cart.Product.Weight * float64(cart.Quantity)
orderItems = append(orderItems, models.OrderItem{ orderItems = append(orderItems, models.OrderItem{
ProductID: cart.ProductID, ProductID: cart.ProductID,
Quantity: cart.Quantity, Quantity: cart.Quantity,
@@ -298,7 +300,7 @@ func (h *OrderHandler) Create(c *gin.Context) {
break break
} }
var shippingFeeFirstWeight, shippingFeePerGram, serviceFeeRate, taxRate float64 var shippingFeeFirstWeight, shippingFeePerGram, serviceFeeRate, taxRate, channelFeeRate float64
var setting models.SystemSetting var setting models.SystemSetting
if err := utils.DB.Where("`key` = ?", "shipping_fee_first_weight").First(&setting).Error; err == nil { if err := utils.DB.Where("`key` = ?", "shipping_fee_first_weight").First(&setting).Error; err == nil {
shippingFeeFirstWeight, _ = strconv.ParseFloat(setting.Value, 64) shippingFeeFirstWeight, _ = strconv.ParseFloat(setting.Value, 64)
@@ -312,17 +314,23 @@ func (h *OrderHandler) Create(c *gin.Context) {
if err := utils.DB.Where("`key` = ?", "tax_rate").First(&setting).Error; err == nil { if err := utils.DB.Where("`key` = ?", "tax_rate").First(&setting).Error; err == nil {
taxRate, _ = strconv.ParseFloat(setting.Value, 64) taxRate, _ = strconv.ParseFloat(setting.Value, 64)
} }
if err := utils.DB.Where("`key` = ?", "payment_channel_fee_rate").First(&setting).Error; err == nil {
channelFeeRate, _ = strconv.ParseFloat(setting.Value, 64)
}
shippingFee := shippingFeeFirstWeight // 根据商品重量计算运费
if subtotal >= 99 || shippingFeeFirstWeight == 0 { shippingFee := 0.0
shippingFee = 0 if subtotal < 99 && shippingFeeFirstWeight > 0 {
} else if totalQuantity > 500 { shippingFee = shippingFeeFirstWeight
shippingFee += shippingFeePerGram * float64(totalQuantity-500) if totalWeight > 500 {
shippingFee += shippingFeePerGram * (totalWeight - 500)
}
} }
serviceFee := subtotal * serviceFeeRate / 100 serviceFee := subtotal * serviceFeeRate / 100
tax := subtotal * taxRate / 100 tax := subtotal * taxRate / 100
totalAmount := subtotal + shippingFee + serviceFee + tax channelFee := subtotal * channelFeeRate / 100
totalAmount := subtotal + shippingFee + serviceFee + tax + channelFee
order := models.Order{ order := models.Order{
UserID: userID, UserID: userID,
@@ -331,6 +339,7 @@ func (h *OrderHandler) Create(c *gin.Context) {
ShippingFee: shippingFee, ShippingFee: shippingFee,
ServiceFee: serviceFee, ServiceFee: serviceFee,
Tax: tax, Tax: tax,
ChannelFee: channelFee,
TotalAmount: totalAmount, TotalAmount: totalAmount,
Status: models.OrderStatusPendingPayment, Status: models.OrderStatusPendingPayment,
ShippingAddressID: &req.ShippingAddressID, ShippingAddressID: &req.ShippingAddressID,
+1
View File
@@ -14,6 +14,7 @@ type Order struct {
ShippingFee float64 `gorm:"type:decimal(10,2);default:0" json:"shipping_fee"` ShippingFee float64 `gorm:"type:decimal(10,2);default:0" json:"shipping_fee"`
ServiceFee float64 `gorm:"type:decimal(10,2);default:0" json:"service_fee"` ServiceFee float64 `gorm:"type:decimal(10,2);default:0" json:"service_fee"`
Tax float64 `gorm:"type:decimal(10,2);default:0" json:"tax"` Tax float64 `gorm:"type:decimal(10,2);default:0" json:"tax"`
ChannelFee float64 `gorm:"type:decimal(10,2);default:0" json:"channel_fee"` // 支付通道费
TotalAmount float64 `gorm:"type:decimal(10,2);not null" json:"total_amount"` TotalAmount float64 `gorm:"type:decimal(10,2);not null" json:"total_amount"`
RefundAmount *float64 `gorm:"type:decimal(10,2)" json:"refund_amount"` RefundAmount *float64 `gorm:"type:decimal(10,2)" json:"refund_amount"`
RefundStatus string `gorm:"size:20" json:"refund_status"` RefundStatus string `gorm:"size:20" json:"refund_status"`
+1
View File
@@ -11,6 +11,7 @@ type Product struct {
Name string `gorm:"size:255;not null" json:"name"` Name string `gorm:"size:255;not null" json:"name"`
Description string `json:"description"` Description string `json:"description"`
Price float64 `gorm:"type:decimal(10,2);not null" json:"price"` Price float64 `gorm:"type:decimal(10,2);not null" json:"price"`
Weight float64 `gorm:"type:decimal(10,2);default:0" json:"weight"` // 商品克重(克)
MinPurchase int `gorm:"default:1" json:"min_purchase"` MinPurchase int `gorm:"default:1" json:"min_purchase"`
MaxPurchase *int `json:"max_purchase"` MaxPurchase *int `json:"max_purchase"`
MinWeight *float64 `json:"min_weight"` MinWeight *float64 `json:"min_weight"`
+4
View File
@@ -104,6 +104,10 @@
<span>税费</span> <span>税费</span>
<span>¥{{ (currentOrder.tax || 0).toFixed(2) }}</span> <span>¥{{ (currentOrder.tax || 0).toFixed(2) }}</span>
</div> </div>
<div class="fee-item" v-if="currentOrder.channel_fee > 0">
<span>通道费</span>
<span>¥{{ (currentOrder.channel_fee || 0).toFixed(2) }}</span>
</div>
<div class="fee-item total"> <div class="fee-item total">
<span>合计</span> <span>合计</span>
<span class="total-amount">¥{{ (currentOrder.total_amount || 0).toFixed(2) }}</span> <span class="total-amount">¥{{ (currentOrder.total_amount || 0).toFixed(2) }}</span>
+15 -8
View File
@@ -80,17 +80,22 @@
<div class="form-section"> <div class="form-section">
<div class="section-title">价格库存</div> <div class="section-title">价格库存</div>
<el-row :gutter="16"> <el-row :gutter="16">
<el-col :span="8"> <el-col :span="6">
<el-form-item label="价格" required> <el-form-item label="价格" required>
<el-input-number v-model="form.price" :precision="2" :min="0" style="width: 100%" /> <el-input-number v-model="form.price" :precision="2" :min="0" style="width: 100%" />
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col :span="8"> <el-col :span="6">
<el-form-item label="库存"> <el-form-item label="库存">
<el-input-number v-model="form.stock" :min="0" style="width: 100%" /> <el-input-number v-model="form.stock" :min="0" style="width: 100%" />
</el-form-item> </el-form-item>
</el-col> </el-col>
<el-col :span="8"> <el-col :span="6">
<el-form-item label="克重(克)">
<el-input-number v-model="form.weight" :precision="1" :min="0" style="width: 100%" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="上架"> <el-form-item label="上架">
<el-switch v-model="form.is_active" /> <el-switch v-model="form.is_active" />
</el-form-item> </el-form-item>
@@ -188,6 +193,7 @@ const form = reactive({
name: '', name: '',
description: '', description: '',
price: 0, price: 0,
weight: 0,
stock: 0, stock: 0,
min_purchase: 1, min_purchase: 1,
max_purchase: undefined as number | undefined, max_purchase: undefined as number | undefined,
@@ -264,11 +270,11 @@ async function fetchBrands() {
function openAdd() { function openAdd() {
editing.value = null editing.value = null
Object.assign(form, { Object.assign(form, {
name: '', description: '', price: 0, stock: 0, name: '', description: '', price: 0, weight: 0, stock: 0,
min_purchase: 1, max_purchase: undefined, min_purchase: 1, max_purchase: undefined,
require_credit: false, credit_cost: 0, credit_reward: 0, require_credit: false, credit_cost: 0, credit_reward: 0,
is_active: true, category_ids: [], brand_id: undefined is_active: true, category_ids: [], brand_id: undefined
}) })
fileList.value = [] fileList.value = []
uploadedUrls.value = [] uploadedUrls.value = []
@@ -281,6 +287,7 @@ function editProd(row: any) {
name: row.name, name: row.name,
description: row.description || '', description: row.description || '',
price: row.price, price: row.price,
weight: row.weight || 0,
stock: row.stock || 0, stock: row.stock || 0,
min_purchase: row.min_purchase || 1, min_purchase: row.min_purchase || 1,
max_purchase: row.max_purchase, max_purchase: row.max_purchase,
+20 -3
View File
@@ -209,7 +209,12 @@
<span>税费 ({{ settings.tax_rate || 0 }}%)</span> <span>税费 ({{ settings.tax_rate || 0 }}%)</span>
<span>¥{{ taxFee.toFixed(2) }}</span> <span>¥{{ taxFee.toFixed(2) }}</span>
</div> </div>
<div class="summary-row" v-if="channelFee > 0">
<span>通道费 ({{ settings.payment_channel_fee_rate || 0 }}%)</span>
<span>¥{{ channelFee.toFixed(2) }}</span>
</div>
<div class="summary-divider"></div> <div class="summary-divider"></div>
<div class="summary-row summary-total"> <div class="summary-row summary-total">
@@ -334,11 +339,18 @@ const selectedQuantity = computed(() => {
.reduce((sum, item) => sum + item.quantity, 0) .reduce((sum, item) => sum + item.quantity, 0)
}) })
//
const selectedWeight = computed(() => {
return items.value
.filter(item => selectedIds.value.includes(item.id))
.reduce((sum, item) => sum + (item.product?.weight || 0) * item.quantity, 0)
})
const shippingFee = computed(() => { const shippingFee = computed(() => {
const firstWeight = parseFloat(settings.value.shipping_fee_first_weight || '0') const firstWeight = parseFloat(settings.value.shipping_fee_first_weight || '0')
const perGram = parseFloat(settings.value.shipping_fee_per_gram || '0') const perGram = parseFloat(settings.value.shipping_fee_per_gram || '0')
if (selectedTotal.value >= 99 || firstWeight === 0) return 0 if (selectedTotal.value >= 99 || firstWeight === 0) return 0
return firstWeight + perGram * Math.max(0, selectedQuantity.value - 500) return firstWeight + perGram * Math.max(0, selectedWeight.value - 500)
}) })
const serviceFee = computed(() => { const serviceFee = computed(() => {
@@ -351,8 +363,13 @@ const taxFee = computed(() => {
return selectedTotal.value * rate / 100 return selectedTotal.value * rate / 100
}) })
const channelFee = computed(() => {
const rate = parseFloat(settings.value.payment_channel_fee_rate || '0')
return selectedTotal.value * rate / 100
})
const grandTotal = computed(() => { const grandTotal = computed(() => {
return selectedTotal.value + shippingFee.value + serviceFee.value + taxFee.value return selectedTotal.value + shippingFee.value + serviceFee.value + taxFee.value + channelFee.value
}) })
const canCheckout = computed(() => { const canCheckout = computed(() => {