添加:立即进货 - 购物车添加完成

This commit is contained in:
wuhui_zzw 2024-03-01 16:56:46 +08:00
parent 7c4c64edaa
commit 21b1340734
2 changed files with 240 additions and 72 deletions

View File

@ -16,7 +16,20 @@ export function supplierApplyInfo(data) {
export function supplierGoodsList(data) {
return request.get(`supplier/goods_list`, data);
}
// 进货购物车 - 获取购物车列表
export function withGoodsCartList() {
return request.get(`supplier/cart_list`);
}
// 进货购物车 - 获取购物车所有商品id
export function withGoodsCartIds() {
return request.get(`supplier/cart_ids`);
}
// 进货购物车 - 添加商品
export function withGoodsCartAdd(data) {
return request.post(`supplier/cart_add`, data);
}
// 进货购物车 - 删除商品
export function withGoodsCartDel(data) {
return request.post(`supplier/cart_del`, data);
}

View File

@ -42,7 +42,7 @@
type='number'
@click="buyFlowAddBuyNum('click', item)"
:disabled="Object.values(item.attrValue).length > 1"
@input="buyFlowInputBuyNum($event, item.attrValue[0])"
@input="buyFlowInputBuyNum($event, item.attrValue[0], item)"
/>
<text class="iconfont icon-shangpinshuliang-jia" @click="buyFlowAddBuyNum('add', item)" ></text>
</view>
@ -65,12 +65,13 @@
合计<text class="total-price">¥{{ totalPrice }}</text>
</view>
</view>
<view class="settlement-btn">立即结算</view>
<view class="settlement-btn" @click="settlementCart" v-if="Object.values(buyList).length > 0">立即结算</view>
<view class="settlement-btn not-btn" v-else>立即结算</view>
</view>
</view>
<!-- 授权登录 -->
<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authClose"></authorize>
<!--多规格商品选择弹框-->
<!-- 多规格商品选择弹框 -->
<uni-popup ref="moreSpecsSelect" type="bottom" :is-mask-click="false">
<view class="more-spec-content">
<!--顶部商品信息-->
@ -113,34 +114,29 @@
<view class="num-change">
<text class="iconfont icon-shangpinshuliang-jian" @click="buyFlowChangeNum('reduce', currentGoods.selected)"></text>
<input
:value="buyList[currentGoods.selected.product_id] ? (buyList[currentGoods.selected.product_id].specs ? (buyList[currentGoods.selected.product_id].specs[currentGoods.selected.unique].cart_num || 0) : 0) : 0"
:value="specInputValue"
class="num-input"
type='number'
@input="buyFlowInputBuyNum($event, currentGoods.selected)"
@input="buyFlowInputBuyNum($event, currentGoods.selected, currentGoods)"
/>
<text class="iconfont icon-shangpinshuliang-jia" @click="buyFlowChangeNum('add', currentGoods.selected)"></text>
</view>
<!--单位-->
<view v-if="(batch_list[currentGoods.product_id] || 0) == 1" class="unit">
{{ currentGoods.batch_unit }} {{ (buyList[currentGoods.selected.product_id] ? (buyList[currentGoods.selected.product_id].specs ? (buyList[currentGoods.selected.product_id].specs[currentGoods.selected.unique].cart_num || 0) : 0) : 0) * (buyList[currentGoods.product_id].batch_num || 1) }}{{ currentGoods.unit_name }}
{{ currentGoods.batch_unit }} {{ (specInputValue) * (buyList[currentGoods.product_id].batch_num || 1) }}{{ currentGoods.unit_name }}
</view>
<view v-else class="unit">{{ currentGoods.unit_name }}</view>
</view>
</view>
</uni-popup>
</view>
</template>
<script>
import {mapGetters} from "vuex";
import authorize from '@/components/Authorize';
import {supplierGoodsList} from "@/api/supplier";
import {supplierGoodsList, withGoodsCartAdd} from "@/api/supplier";
import {withGoodsCartDel, withGoodsCartIds, withGoodsCartList, withGoodsChangeNum} from "../../../api/supplier";
export default {
name: 'business',
@ -148,6 +144,16 @@ export default {
authorize,
},
computed: {
specInputValue(){
let selected = this.currentGoods.selected || {};
let productId = selected.product_id || 0;
let unique = selected.unique || '';
let good = this.buyList[productId] || {};
let specs = good.specs || {};
let spec = specs[unique] || {};
return spec.cart_num || 0;
},
...mapGetters(['isLogin', 'uid', 'userInfo', 'viewColor','shopToken', 'shopIsLogin', 'shopMerId'])
},
data() {
@ -166,7 +172,7 @@ export default {
totalPrice: 0.00,
buyList: {},
batch_list: {},
currentGoods: {},
currentGoods: {}
}
},
onLoad(options) {
@ -182,35 +188,30 @@ export default {
'currentGoods.selected_spec': {
handler() {
let good = Object.assign({}, this.currentGoods)
let defaultName = Object.values(good.selected_spec).join(',');
//
this.currentGoods.selected = good.attrValue[defaultName] || {};
if(good.selected_spec){
let defaultName = Object.values(good.selected_spec).join(',');
//
this.$set(this.currentGoods, 'selected', good.attrValue[defaultName] || {});
}else{
this.$set(this.currentGoods, 'selected', {});
}
},
deep: true
},
//
//
'buyList': {
handler() {
let totalNum = 0;
let totalPrice = 0.00;
Object.values(this.buyList).forEach(item => {
totalNum += item.total_num;
Object.values(item.specs).forEach(value => {
let price = value.price * value.cart_num;
let currentBatchType = this.batch_list[value.product_id] || 0;
//
if(currentBatchType == 1) price = price * item.batch_num;
totalPrice += price;
})
})
//
this.totalNum = totalNum;
this.totalPrice = totalPrice;
this.BuyFlowComputeTotal();
},
deep: true
},
//
'batch_list':{
handler() {
this.BuyFlowComputeTotal();
},
deep: true
}
},
methods: {
//
@ -249,6 +250,8 @@ export default {
}
//
this.getGoodsList();
//
this.cartGetList();
},
//
getGoodsList(page = 0) {
@ -268,9 +271,10 @@ export default {
});
},
//
buyFlowInputBuyNum(event, spec){
buyFlowInputBuyNum(event, spec, currentGoods){
//
spec.input_num = Number(event.detail.value || event.target.value);
this.currentGoods = currentGoods;
this.buyFlowChangeNum('input', spec);
},
@ -278,6 +282,7 @@ export default {
buyFlowAddBuyNum(type, item){
let isMoreSpecs = Object.values(item.attrValue).length || 0;
let good = Object.assign({}, item);
good.is_batch = this.batch_list[good.product_id] || 0;
if(Number(isMoreSpecs) > 1){
//
let selected_spec = {};
@ -299,7 +304,8 @@ export default {
},
// - ( || )
buyFlowChangeNum(type, spec){
let currentProduct = this.buyList[spec.product_id] || {};
let isBatch = this.batch_list[spec.product_id] || 0;// 0=1=
let currentProduct = Object.assign({}, this.buyList[spec.product_id] || {});
if(Object.values(currentProduct).length > 0){
//
let currentSpec = currentProduct.specs[spec.unique] || {};
@ -314,18 +320,31 @@ export default {
if(type === 'add') currentSpec.cart_num += 1;
else if(type === 'reduce') currentSpec.cart_num -= 1;
else if(type === 'input') currentSpec.cart_num = spec.input_num || 0;
//
let stock = currentSpec.stock || 0;
if(isBatch == 1) stock = Math.floor(stock / (this.currentGoods.batch_num || ''));//
if(Number(stock) < Number(currentSpec.cart_num)) currentSpec.cart_num = stock;
//
let currentNum = currentSpec.cart_num || 0;
let changeNum = Number(oldNum) - Number(currentNum);
// 0
if(currentSpec.cart_num <= 0) delete currentProduct.specs[spec.unique];
else currentProduct.specs[spec.unique] = currentSpec;
if(currentSpec.cart_num <= 0) {
this.cartDel(currentSpec);
delete currentProduct.specs[spec.unique];
} else {
currentProduct.specs[spec.unique] = currentSpec;
//
currentSpec.batch_num = this.currentGoods.batch_num || '';
currentSpec.is_batch = this.currentGoods.is_batch || 0;
this.cartSubmitEdit(currentSpec);
}
//
if(Object.values(currentProduct.specs).length <= 0){
this.$set(this.buyList, spec.product_id, {});
delete this.buyList[spec.product_id];
}else{
currentProduct.total_num -= changeNum;
this.$set(this.buyList, spec.product_id, currentProduct);
}
}
@ -334,6 +353,10 @@ export default {
if(type === 'add') spec.cart_num = 1;
else if(type === 'reduce') return false;
else if(type === 'input') spec.cart_num = spec.input_num || 0;
//
let stock = spec.stock || 0;
if(isBatch == 1) stock = Math.floor(stock / (this.currentGoods.batch_num || ''));//
if(Number(stock) < Number(spec.cart_num)) spec.cart_num = stock;
//
this.$set(this.buyList, spec.product_id, {
//
@ -348,6 +371,11 @@ export default {
[spec.unique]: spec,
},
});
//
spec.batch_num = this.currentGoods.batch_num || '';
spec.is_batch = this.currentGoods.is_batch || 0;
this.cartSubmitEdit(spec);
}
this.$forceUpdate();
@ -362,41 +390,163 @@ export default {
},
// -
changeBatchType(productId){
//
let currentBatchType = this.batch_list[productId] || 0;
this.batch_list[productId] = currentBatchType == 0 ? 1 : 0;
let negation = currentBatchType == 0 ? 1 : 0;
//
let currentProduct = Object.assign({}, this.buyList[productId] || {});
this.$set(this.batch_list, productId, negation);
if(Object.values(currentProduct).length > 0){
let specs = currentProduct.specs || {};
let totalNum = 0;
Object.values(specs).forEach(item => {
let stock = item.stock || 0;
let cartNum = 0;
if(negation == 1){
//
cartNum = Math.floor(item.cart_num / currentProduct.batch_num);
stock = Math.floor(stock / currentProduct.batch_num);
}else{
//
cartNum = Math.floor(item.cart_num * currentProduct.batch_num);
}
//
cartNum = cartNum <= 0 ? 1 : cartNum;
if(stock < cartNum) cartNum = stock;
totalNum += cartNum;
currentProduct.specs[item.unique].cart_num = cartNum || 1;
//
let spec = currentProduct.specs[item.unique] || {};
spec.batch_num = currentProduct.batch_num || '';
spec.is_batch = negation || 0;
this.cartSubmitEdit(spec);
})
currentProduct.total_num = totalNum;
this.$set(this.buyList, productId, currentProduct);
}
//
this.$forceUpdate();
},
// -
BuyFlowComputeTotal(){
let totalNum = 0;
let totalPrice = 0.00;
Object.values(this.buyList).forEach(item => {
totalNum += item.total_num;
Object.values(item.specs).forEach(value => {
let price = value.price * value.cart_num;
let currentBatchType = this.batch_list[value.product_id] || 0;
//
if(currentBatchType == 1) price = price * item.batch_num;
totalPrice += price;
})
})
//
this.totalNum = totalNum.toFixed(0);
this.totalPrice = totalPrice.toFixed(2);
},
// -
cartGetList(){},
// -
cartGetCount(){},
cartGetList(){
let _this = this;
//
withGoodsCartList().then(res => {
if(res.status == 200){
let data = res.data || {};
let buyList = {};
let batchList = {};
Object.values(data).forEach( item => {
//
let spec = item.productAttr || {};
spec.cart_num = item.cart_num || 0;
//
let good = buyList[item.product_id] || {};
if(Object.values(good).length > 0){
//
buyList[item.product_id].specs[item.product_attr_unique] = spec;
buyList[item.product_id].total_num += item.cart_num;
}else{
//
buyList[item.product_id] = {
//
is_batch: item.is_batch,//
batch_num: item.batch_num,//
unit_name: item.product.unit_name,//
batch_unit: item.product.batch_unit,//
//
product_id: item.product_id,
total_num: item.cart_num,
store_name: item.product.store_name,
specs: {
[item.product_attr_unique]: spec,
},
};
}
//
batchList[item.product_id] = item.is_batch || 0;
});
this.buyList = Object.assign({}, buyList);
this.batch_list = Object.assign({}, batchList);
}
}).catch(err => {
_this.$util.Tips({title: err});
});
},
// -
cartSubmitEdit(){},
// cart_num: 10
// is_new: 0
// product_attr_unique: "598aa5ec4390"
// product_id: "247"
// product_type: 0
// spread_id: ""
//
//
//
//
//
// cart_num: 8
// is_new: 0
// product_attr_unique: "53cd849c89f0"
// product_id: "248"
// product_type: 0
// spread_id: ""
cartSubmitEdit(spec){
let _this = this;
let params = {
cart_num: spec.cart_num,
product_attr_unique: spec.unique,
product_id: spec.product_id,
//
is_batch: spec.is_batch || 0,// 0=1=
batch_num: spec.batch_num || '',//
};
//
withGoodsCartAdd(params).then(res => {
// console.log(res)
}).catch(err => {
_this.cartGetList();
this.$util.Tips({title: err});
});
},
// -
cartDel(spec){
let _this = this;
let params = {
product_attr_unique: spec.unique,
product_id: spec.product_id,
};
//
withGoodsCartDel(params).then(res => {
// console.log(res)
}).catch(err => {
_this.cartGetList();
this.$util.Tips({title: err});
});
},
//
settlementCart(){
let _this = this;
//
withGoodsCartIds().then(res => {
if(res.status == 200){
let data = res.data || {};
if(Object.values(data).length <= 0){
_this.$util.Tips({title: '请选择商品!'});
return false;
}
//
uni.navigateTo({
url: '/pages/users/order_confirm/index?cartId=' + data.join(',')
});
}
}).catch(err => {
_this.$util.Tips({title: err});
});
}
},
//
onReachBottom() {
@ -569,7 +719,7 @@ export default {
.num-input{
border-left: 2rpx solid var(--border-color-);
border-right: 2rpx solid var(--border-color-);
width: 60rpx;
width: 100rpx;
text-align: center;
}
}
@ -640,6 +790,11 @@ export default {
text-align: center;
border-radius: 100rpx;
}
.not-btn{
background-color: #909399!important;
border-color: #909399!important;
color: #fff!important;
}
}
}
//