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="item-edit">
|
||||||
<view class="quantity-edit">
|
<view class="quantity-edit">
|
||||||
<text class="qty-label">数量</text>
|
<text class="qty-label">数量</text>
|
||||||
<input
|
<view class="qty-wrapper">
|
||||||
class="qty-input"
|
<text class="qty-btn" @click="qtyMinus(index)">-</text>
|
||||||
type="number"
|
<input
|
||||||
v-model="item.quantity"
|
class="qty-input"
|
||||||
@change="calcAmount"
|
type="number"
|
||||||
/>
|
v-model="item.quantity"
|
||||||
|
@change="onQuantityChange(index)"
|
||||||
|
/>
|
||||||
|
<text class="qty-btn" @click="qtyPlus(index)">+</text>
|
||||||
|
</view>
|
||||||
|
<text class="stock-info">库存: {{ stocks[item.productId] || 0 }}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="price-edit">
|
<view class="price-edit">
|
||||||
<text class="qty-label">单价</text>
|
<text class="qty-label">单价</text>
|
||||||
@@ -175,6 +180,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import orderApi from '@/api/order'
|
import orderApi from '@/api/order'
|
||||||
|
import stockApi from '@/api/stock'
|
||||||
import productApi from '@/api/product'
|
import productApi from '@/api/product'
|
||||||
import customerApi from '@/api/customer'
|
import customerApi from '@/api/customer'
|
||||||
|
|
||||||
@@ -198,6 +204,7 @@ export default {
|
|||||||
orderItems: [],
|
orderItems: [],
|
||||||
productList: [],
|
productList: [],
|
||||||
searchKeyword: '',
|
searchKeyword: '',
|
||||||
|
stocks: {}, // 商品库存
|
||||||
|
|
||||||
// 金额相关
|
// 金额相关
|
||||||
discountRate: 100, // 折扣率(保留逻辑,默认100%不打折)
|
discountRate: 100, // 折扣率(保留逻辑,默认100%不打折)
|
||||||
@@ -219,6 +226,7 @@ export default {
|
|||||||
onLoad(options) {
|
onLoad(options) {
|
||||||
this.loadCustomersByType()
|
this.loadCustomersByType()
|
||||||
this.loadProducts()
|
this.loadProducts()
|
||||||
|
this.loadStocks()
|
||||||
if (options.orderId) {
|
if (options.orderId) {
|
||||||
this.editingOrderId = options.orderId
|
this.editingOrderId = options.orderId
|
||||||
// 编辑模式
|
// 编辑模式
|
||||||
@@ -299,6 +307,20 @@ export default {
|
|||||||
console.error(e)
|
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) {
|
selectCustomer(e) {
|
||||||
this.selectedCustomer = this.filteredCustomers[e.detail.value]
|
this.selectedCustomer = this.filteredCustomers[e.detail.value]
|
||||||
},
|
},
|
||||||
@@ -350,6 +372,38 @@ export default {
|
|||||||
this.orderItems.splice(index, 1)
|
this.orderItems.splice(index, 1)
|
||||||
this.calcAmount()
|
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() {
|
calcAmount() {
|
||||||
// 1. 计算原价 = Σ(单价 × 数量)
|
// 1. 计算原价 = Σ(单价 × 数量)
|
||||||
@@ -555,16 +609,50 @@ export default {
|
|||||||
margin-right: 8rpx;
|
margin-right: 8rpx;
|
||||||
}
|
}
|
||||||
|
|
||||||
.qty-input, .price-input {
|
.qty-wrapper {
|
||||||
width: 100rpx;
|
display: flex;
|
||||||
height: 56rpx;
|
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;
|
background: #fff;
|
||||||
border: 1rpx solid #ddd;
|
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;
|
text-align: center;
|
||||||
font-size: 24rpx;
|
font-size: 24rpx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.stock-info {
|
||||||
|
font-size: 20rpx;
|
||||||
|
color: #999;
|
||||||
|
margin-left: 12rpx;
|
||||||
|
}
|
||||||
|
|
||||||
.item-subtotal {
|
.item-subtotal {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
text-align: right;
|
text-align: right;
|
||||||
|
|||||||
Reference in New Issue
Block a user