feat: 订单创建页面商品数量支持+号-号,且不超过库存
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -53,12 +53,17 @@
|
||||
<view class="item-edit">
|
||||
<view class="quantity-edit">
|
||||
<text class="qty-label">数量</text>
|
||||
<view class="qty-wrapper">
|
||||
<text class="qty-btn" @click="qtyMinus(index)">-</text>
|
||||
<input
|
||||
class="qty-input"
|
||||
type="number"
|
||||
v-model="item.quantity"
|
||||
@change="calcAmount"
|
||||
@change="onQuantityChange(index)"
|
||||
/>
|
||||
<text class="qty-btn" @click="qtyPlus(index)">+</text>
|
||||
</view>
|
||||
<text class="stock-info">库存: {{ stocks[item.productId] || 0 }}</text>
|
||||
</view>
|
||||
<view class="price-edit">
|
||||
<text class="qty-label">单价</text>
|
||||
@@ -175,6 +180,7 @@
|
||||
|
||||
<script>
|
||||
import orderApi from '@/api/order'
|
||||
import stockApi from '@/api/stock'
|
||||
import productApi from '@/api/product'
|
||||
import customerApi from '@/api/customer'
|
||||
|
||||
@@ -198,6 +204,7 @@ export default {
|
||||
orderItems: [],
|
||||
productList: [],
|
||||
searchKeyword: '',
|
||||
stocks: {}, // 商品库存
|
||||
|
||||
// 金额相关
|
||||
discountRate: 100, // 折扣率(保留逻辑,默认100%不打折)
|
||||
@@ -219,6 +226,7 @@ export default {
|
||||
onLoad(options) {
|
||||
this.loadCustomersByType()
|
||||
this.loadProducts()
|
||||
this.loadStocks()
|
||||
if (options.orderId) {
|
||||
this.editingOrderId = options.orderId
|
||||
// 编辑模式
|
||||
@@ -299,6 +307,20 @@ export default {
|
||||
console.error(e)
|
||||
}
|
||||
},
|
||||
async loadStocks() {
|
||||
try {
|
||||
const res = await stockApi.getStockList({ page: 1, pageSize: 500 })
|
||||
const stockMap = {}
|
||||
if (res.records) {
|
||||
res.records.forEach(s => {
|
||||
stockMap[s.productId] = s.quantity || 0
|
||||
})
|
||||
}
|
||||
this.stocks = stockMap
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
},
|
||||
selectCustomer(e) {
|
||||
this.selectedCustomer = this.filteredCustomers[e.detail.value]
|
||||
},
|
||||
@@ -350,6 +372,38 @@ export default {
|
||||
this.orderItems.splice(index, 1)
|
||||
this.calcAmount()
|
||||
},
|
||||
qtyMinus(index) {
|
||||
const item = this.orderItems[index]
|
||||
const stock = this.stocks[item.productId] || 0
|
||||
if (item.quantity > 1) {
|
||||
item.quantity--
|
||||
this.calcAmount()
|
||||
}
|
||||
},
|
||||
qtyPlus(index) {
|
||||
const item = this.orderItems[index]
|
||||
const stock = this.stocks[item.productId] || 0
|
||||
if (item.quantity < stock) {
|
||||
item.quantity++
|
||||
this.calcAmount()
|
||||
} else {
|
||||
uni.showToast({ title: '库存不足', icon: 'none' })
|
||||
}
|
||||
},
|
||||
onQuantityChange(index) {
|
||||
const item = this.orderItems[index]
|
||||
const stock = this.stocks[item.productId] || 0
|
||||
let qty = parseInt(item.quantity)
|
||||
if (isNaN(qty) || qty < 1) {
|
||||
qty = 1
|
||||
}
|
||||
if (qty > stock) {
|
||||
qty = stock
|
||||
uni.showToast({ title: '库存不足,已调整为最大库存', icon: 'none' })
|
||||
}
|
||||
item.quantity = qty
|
||||
this.calcAmount()
|
||||
},
|
||||
// 核心金额计算逻辑
|
||||
calcAmount() {
|
||||
// 1. 计算原价 = Σ(单价 × 数量)
|
||||
@@ -555,16 +609,50 @@ export default {
|
||||
margin-right: 8rpx;
|
||||
}
|
||||
|
||||
.qty-input, .price-input {
|
||||
width: 100rpx;
|
||||
height: 56rpx;
|
||||
.qty-wrapper {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
background: #f5f5f5;
|
||||
border-radius: 8rpx;
|
||||
}
|
||||
|
||||
.qty-btn {
|
||||
width: 48rpx;
|
||||
height: 48rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 32rpx;
|
||||
color: #667eea;
|
||||
background: #fff;
|
||||
border: 1rpx solid #ddd;
|
||||
border-radius: 8rpx;
|
||||
}
|
||||
|
||||
.qty-btn:first-child {
|
||||
border-radius: 8rpx 0 0 8rpx;
|
||||
}
|
||||
|
||||
.qty-btn:last-child {
|
||||
border-radius: 0 8rpx 8rpx 0;
|
||||
}
|
||||
|
||||
.qty-input {
|
||||
width: 80rpx;
|
||||
height: 48rpx;
|
||||
background: #fff;
|
||||
border: 1rpx solid #ddd;
|
||||
border-left: none;
|
||||
border-right: none;
|
||||
text-align: center;
|
||||
font-size: 24rpx;
|
||||
}
|
||||
|
||||
.stock-info {
|
||||
font-size: 20rpx;
|
||||
color: #999;
|
||||
margin-left: 12rpx;
|
||||
}
|
||||
|
||||
.item-subtotal {
|
||||
flex: 1;
|
||||
text-align: right;
|
||||
|
||||
Reference in New Issue
Block a user