购物车:接入修改数量 API,剩余所有

pull/1/MERGE
YunaiV 2023-08-30 19:14:46 +08:00
parent 6bdbebe2a0
commit e45372d4b6
2 changed files with 263 additions and 292 deletions

View File

@ -19,6 +19,13 @@ export function createFavorite(spuId) {
}); });
} }
// 添加多个商品收藏
export function createFavoriteList(spuIds) {
return request.post('app-api/product/favorite/create-list', {
spuIds
});
}
// 取消商品收藏 // 取消商品收藏
export function deleteFavorite(spuId) { export function deleteFavorite(spuId) {
return request.delete('app-api/product/favorite/delete', { return request.delete('app-api/product/favorite/delete', {

View File

@ -43,7 +43,6 @@
<text v-for="property in item.sku.properties" style="padding-left: 2px">{{property.valueName}}</text> <text v-for="property in item.sku.properties" style="padding-left: 2px">{{property.valueName}}</text>
</view> </view>
<view class='money' v-if="item.canChecked">{{ fen2yuan(item.sku.price) }}</view> <view class='money' v-if="item.canChecked">{{ fen2yuan(item.sku.price) }}</view>
<!-- TODO 芋艿重选 -->
<view class="reElection acea-row row-between-wrapper" v-else> <view class="reElection acea-row row-between-wrapper" v-else>
<view class="title">请重新选择商品规格</view> <view class="title">请重新选择商品规格</view>
<view class="reBnt cart-color acea-row row-center-wrapper" @click.stop="reElection(item)">重选</view> <view class="reBnt cart-color acea-row row-center-wrapper" @click.stop="reElection(item)">重选</view>
@ -97,16 +96,16 @@
</view> </view>
</view> </view>
</view> </view>
<!-- TODO --> <!-- 热门推荐 -->
<view class='noCart' v-if="cartList.valid.length === 0 && cartList.invalid.length === 0 && canShow"> <view class='noCart' v-if="cartList.valid.length === 0 && cartList.invalid.length === 0 && canShow">
<view class='pictrue'> <view class='pictrue'>
<image src='../../static/images/noCart.png'></image> <image src='../../static/images/noCart.png' />
</view> </view>
<recommend :hostProduct='hostProduct'></recommend> <recommend :hostProduct='hostProduct'></recommend>
</view> </view>
</view> </view>
</view> </view>
<!-- TODO --> <!-- 管理操作 -->
<view class='footer acea-row row-between-wrapper' v-if="cartList.valid.length > 0"> <view class='footer acea-row row-between-wrapper' v-if="cartList.valid.length > 0">
<view> <view>
<checkbox-group @change="checkboxAllChange"> <checkbox-group @change="checkboxAllChange">
@ -129,10 +128,18 @@
</form> </form>
</view> </view>
</view> </view>
<!-- SKU 不可用的商品重新选择 SKU TODO --> <!-- SKU 不可用的商品重新选择 SKU -->
<productWindow :attr="attr" :isShow='1' :iSplus='1' :iScart='1' @myevent="onMyEvent" @ChangeAttr="ChangeAttr" <productWindow
@ChangeCartNum="ChangeCartNum" @attrVal="attrVal" @iptCartNum="iptCartNum" @goCat="reGoCat" :attr="attr"
id='product-window'></productWindow> :isShow='1'
:iSplus='1'
:iScart='1'
@ChangeAttr="ChangeAttr"
@ChangeCartNum="ChangeCartNum"
@iptCartNum="iptCartNum"
@close="closeAttr"
@goCat="reGoCat"
/>
<view class="uni-p-b-96" /> <view class="uni-p-b-96" />
<view class="uni-p-b-98" /> <view class="uni-p-b-98" />
</view> </view>
@ -140,14 +147,16 @@
<script> <script>
let sysHeight = 0 let sysHeight = 0
import { cartDel, getResetCart } from '@/api/order.js';
import { getProductHot, collectAll, getProductDetail } from '@/api/store.js';
import { toLogin } from '@/libs/login.js'; import { toLogin } from '@/libs/login.js';
import { mapGetters } from "vuex"; import { mapGetters } from "vuex";
import recommend from '@/components/recommend'; import recommend from '@/components/recommend';
import productWindow from '@/components/productWindow'; import productWindow from '@/components/productWindow';
import * as TradeCartApi from '@/api/trade/cart.js'; import * as TradeCartApi from '@/api/trade/cart.js';
import * as ProductSpuApi from '@/api/product/spu.js'
import * as ProductFavoriteApi from '@/api/product/favorite.js';
import * as Util from '@/utils/util.js'; import * as Util from '@/utils/util.js';
import * as ProductUtil from '@/utils/product.js';
import * as PromotionActivityApi from '@/api/promotion/activity.js';
export default { export default {
components: { components: {
recommend, recommend,
@ -155,6 +164,7 @@
}, },
data() { data() {
return { return {
sysHeight: sysHeight,
goodsHidden: false, goodsHidden: false,
footerswitch: true, footerswitch: true,
hostProduct: [], hostProduct: [],
@ -162,6 +172,7 @@
hotLimit: 10, hotLimit: 10,
hotScroll: false, hotScroll: false,
//
cartList: { // cartList: { //
valid: [], // valid: [], //
invalid: [] // invalid: [] //
@ -170,22 +181,20 @@
selectValue: [], // selectValue: [], //
selectCountPrice: 0.00, // selectCountPrice: 0.00, //
cartCount: 0, // cartCount: 0, //
canShow: false, //
loading: false, //
loadTitle: '加载更多', // cartId: 0, // cart id
attr: { product_id: 0, // SPU
cartAttr: false, attr: { // productWindow 使
productAttr: [], cartAttr: false, //
productSelect: {} // id = name = values[].id = values[].name = index =
properties: [],
productSelect: {} // SKU
}, },
productValue: [], // spu: {}, // SPU
productInfo: {}, skuMap: [], // SKU Map
attrValue: '', // attrValue: '', // ,
attrTxt: '请选择', //
cartId: 0,
product_id: 0,
sysHeight: sysHeight,
canShow: false
}; };
}, },
computed: mapGetters(['isLogin']), computed: mapGetters(['isLogin']),
@ -203,6 +212,7 @@
this.hostProduct = []; this.hostProduct = [];
this.hotScroll = false; this.hotScroll = false;
this.loadend = false; this.loadend = false;
this.getHostProduct()
// //
this.footerswitch = true; this.footerswitch = true;
@ -219,236 +229,203 @@
} }
}, },
methods: { methods: {
// /**
* 修改购物车
*/
reGoCat: function() { reGoCat: function() {
let that = this, const productSelect = this.skuMap[this.attrValue];
productSelect = that.productValue[this.attrValue];
// ,, // ,,
if ( if (this.attr.properties.length &&
that.attr.productAttr.length && productSelect === undefined) {
productSelect === undefined return this.$util.Tips({
)
return that.$util.Tips({
title: "产品库存不足,请选择其它" title: "产品库存不足,请选择其它"
}); });
let q = {
id: that.cartId,
productId: that.product_id,
num: that.attr.productSelect.cart_num,
unique: that.attr.productSelect !== undefined ?
that.attr.productSelect.unique : that.productInfo.id
};
getResetCart(q)
.then(function(res) {
that.attr.cartAttr = false;
that.$util.Tips({
title: "添加购物车成功",
success: () => {
that.loadend = false;
that.cartList.valid = [];
that.getCartList();
} }
TradeCartApi.resetCart({
id: this.cartId,
skuId: this.attr.productSelect.id,
count: this.attr.productSelect.cart_num
}).then(res => {
this.attr.cartAttr = false;
this.$util.Tips({
title: this.$t(`添加购物车成功`)
}); });
}) this.cartList.valid = [];
.catch(res => { this.getCartList();
return that.$util.Tips({ }).catch(res => {
return this.$util.Tips({
title: res title: res
}); });
}); });
}, },
onMyEvent: function() { /**
this.$set(this.attr, 'cartAttr', false); * 重选 SKU
}, */
reElection: function(item) { reElection: function(item) {
this.getGoodsDetails(item) this.getGoodsDetails(item)
}, },
/** /**
* 获取产品详情 * 获取产品详情
*
*/ */
getGoodsDetails: function(item) { getGoodsDetails: function(item) {
uni.showLoading({ uni.showLoading({
title: '加载中', title: '加载中',
mask: true mask: true
}); });
let that = this; this.cartId = item.id;
that.cartId = item.id; this.product_id = item.spu.id;
that.product_id = item.productId; ProductSpuApi.getSpuDetail(item.spu.id).then(res => {
getProductDetail(item.productId).then(res => {
uni.hideLoading(); uni.hideLoading();
that.attr.cartAttr = true; this.attr.cartAttr = true;
let productInfo = res.data.productInfo; let spu = res.data;
that.$set(that, 'productInfo', productInfo); let properties = ProductUtil.convertProductPropertyList(res.data.skus);
that.$set(that.attr, 'productAttr', res.data.productAttr); let skuMap = ProductUtil.convertProductSkuMap(res.data.skus);
that.$set(that, 'productValue', res.data.productValue); //
that.DefaultSelect(); this.$set(this, 'spu', spu);
this.$set(this.attr, 'properties', properties);
this.$set(this, 'skuMap', skuMap);
this.selectDefaultSku();
}).catch(err => { }).catch(err => {
uni.hideLoading(); uni.hideLoading();
}) })
}, },
/** /**
* 属性变动赋值 * 关闭 productWindow 弹窗
*
*/ */
ChangeAttr: function(res) { closeAttr: function () {
let productSelect = this.productValue[res]; this.$set(this.attr, "cartAttr", false);
if (productSelect && productSelect.stock > 0) {
this.$set(this.attr.productSelect, "image", productSelect.image);
this.$set(this.attr.productSelect, "price", productSelect.price);
this.$set(this.attr.productSelect, "stock", productSelect.stock);
this.$set(this.attr.productSelect, "unique", productSelect.id);
this.$set(this.attr.productSelect, "cart_num", 1);
this.$set(this, "attrValue", res);
this.$set(this, "attrTxt", "已选择");
} else {
this.$set(this.attr.productSelect, "image", this.productInfo.image);
this.$set(this.attr.productSelect, "price", this.productInfo.price);
this.$set(this.attr.productSelect, "stock", 0);
this.$set(this.attr.productSelect, "unique", this.productInfo.id);
this.$set(this.attr.productSelect, "cart_num", 0);
this.$set(this, "attrValue", "");
this.$set(this, "attrTxt", "请选择");
}
}, },
/** /**
* 默认选中属性 * 属性变动赋值
* *
* @param newSkuKey 新的 skuKey
* @param propertyIndex properties 的下标
* @param valueIndex values 的下标
*/ */
DefaultSelect: function() { ChangeAttr: function(newSkuKey, propertyIndex, valueIndex) {
let productAttr = this.attr.productAttr; // SKU
let value = []; let sku = this.skuMap[newSkuKey];
for (let key in this.productValue) { if (!sku) {
if (this.productValue[key].stock > 0) { return;
value = this.attr.productAttr.length ? key.split(",") : []; }
this.$set(this.attr.productSelect, "id", sku.id);
this.$set(this.attr.productSelect, "picUrl", sku.picUrl);
this.$set(this.attr.productSelect, "price", sku.price);
this.$set(this.attr.productSelect, "stock", sku.stock);
this.$set(this.attr.productSelect, "cart_num", 1);
// SKU
this.$set(this.attr.properties[propertyIndex], 'index',
this.attr.properties[propertyIndex].values[valueIndex].name);
this.$set(this, "attrValue", newSkuKey);
},
/**
* 查找默认选中的 sku设置到 attr.productSelect
*
* 先找有库存的 SKU否则找第一个 SKU
*/
selectDefaultSku: function() {
const properties = this.attr.properties;
// "," skuKey = ["", ""]
let skuKey = undefined;
for (let key in this.skuMap) {
if (this.skuMap[key].stock > 0) {
skuKey = key.split(",");
break; break;
} }
} }
for (let i = 0; i < productAttr.length; i++) { if (!skuKey) { //
this.$set(productAttr[i], "index", value[i]); skuKey = Object.keys(this.skuMap)[0].split(",");
} }
//sort();:-- // 使 index
let productSelect = this.productValue[value.sort().join(",")]; for (let i = 0; i < properties.length; i++) {
if (productSelect && productAttr.length) { this.$set(properties[i], "index", skuKey[i]);
this.$set(
this.attr.productSelect,
"storeName",
this.productInfo.storeName
);
this.$set(this.attr.productSelect, "image", productSelect.image);
this.$set(this.attr.productSelect, "price", productSelect.price);
this.$set(this.attr.productSelect, "stock", productSelect.stock);
this.$set(this.attr.productSelect, "unique", productSelect.id);
this.$set(this.attr.productSelect, "cart_num", 1);
this.$set(this, "attrValue", value.sort().join(","));
this.$set(this, "attrTxt", "已选择");
} else if (!productSelect && productAttr.length) {
this.$set(
this.attr.productSelect,
"storeName",
this.productInfo.storeName
);
this.$set(this.attr.productSelect, "image", this.productInfo.image);
this.$set(this.attr.productSelect, "price", this.productInfo.price);
this.$set(this.attr.productSelect, "stock", 0);
this.$set(this.attr.productSelect, "unique", this.productInfo.id);
this.$set(this.attr.productSelect, "cart_num", 0);
this.$set(this, "attrValue", "");
this.$set(this, "attrTxt", "请选择");
} else if (!productSelect && !productAttr.length) {
this.$set(
this.attr.productSelect,
"storeName",
this.productInfo.storeName
);
this.$set(this.attr.productSelect, "image", this.productInfo.image);
this.$set(this.attr.productSelect, "price", this.productInfo.price);
this.$set(this.attr.productSelect, "stock", this.productInfo.stock);
this.$set(
this.attr.productSelect,
"unique",
this.productInfo.id || ""
);
this.$set(this.attr.productSelect, "cart_num", 1);
this.$set(this, "attrValue", "");
this.$set(this, "attrTxt", "请选择");
} }
},
attrVal(val) { let sku = this.skuMap[skuKey.join(",")];
this.$set(this.attr.productAttr[val.indexw], 'index', this.attr.productAttr[val.indexw].attrValues[val if (!sku) {
.indexn]); return
}
this.$set(this.attr.productSelect, "spuName", this.spu.name);
this.$set(this.attr.productSelect, "id", sku.id);
this.$set(this.attr.productSelect, "picUrl", sku.picUrl);
this.$set(this.attr.productSelect, "price", sku.price);
this.$set(this.attr.productSelect, "stock", sku.stock);
this.$set(this.attr.productSelect, "cart_num", 1);
this.$set(this, "attrValue", skuKey.join(","));
}, },
/** /**
* 购物车数量加和数量减 * 购物车数量加和数量减
* *
* @param changeValue true 增加false 减少
*/ */
ChangeCartNum: function(changeValue) { ChangeCartNum: function(changeValue) {
//changeValue: | /// sku
// let sku = this.attr.productSelect;
let productSelect = this.productValue[this.attrValue]; if (!sku) {
//, return;
if (productSelect === undefined && !this.attr.productAttr.length) }
productSelect = this.attr.productSelect;
//0 //
if (productSelect === undefined) return; let stock = sku.stock || 0;
let stock = productSelect.stock || 0;
let num = this.attr.productSelect;
if (changeValue) { if (changeValue) {
num.cart_num++; sku.cart_num++;
if (num.cart_num > stock) { if (sku.cart_num > stock) {
this.$set(this.attr.productSelect, "cart_num", stock ? stock : 1); this.$set(this.attr.productSelect, "cart_num", stock);
this.$set(this, "cart_num", stock ? stock : 1);
} }
} else { } else {
num.cart_num--; sku.cart_num--;
if (num.cart_num < 1) { if (sku.cart_num < 1) {
this.$set(this.attr.productSelect, "cart_num", 1); this.$set(this.attr.productSelect, "cart_num", 1);
this.$set(this, "cart_num", 1);
} }
} }
}, },
/** /**
* 购物车手动填写 * 购物车手动填写
*
*/ */
iptCartNum: function(e) { iptCartNum: function(number) {
this.$set(this.attr.productSelect, 'cart_num', e); this.$set(this.attr.productSelect, 'cart_num', number ? number : 1);
}, },
/**
* 删除选中的商品
*/
subDel: function(event) { subDel: function(event) {
let that = this, const selectValue = this.selectValue;
selectValue = that.selectValue; if (selectValue.length === 0) {
if (selectValue.length > 0) return this.$util.Tips({
cartDel(selectValue).then(res => {
that.loadend = false;
that.cartList.valid = [];
that.getCartList();
});
else
return that.$util.Tips({
title: '请选择产品' title: '请选择产品'
});
}
TradeCartApi.deleteCart(selectValue).then(res => {
this.getCartList();
}); });
}, },
getSelectValueProductId: function() { getSelectValueProductId: function() {
let that = this; const validList = this.cartList.valid;
let validList = that.cartList.valid; const selectValue = this.selectValue;
let selectValue = that.selectValue;
let productId = []; let productId = [];
if (selectValue.length > 0) { if (selectValue.length > 0) {
for (let index in validList) { for (let index in validList) {
if (that.inArray(validList[index].id, selectValue)) { if (this.inArray(validList[index].id, selectValue)) {
productId.push(validList[index].productId); productId.push(validList[index].spu.id);
} }
} }
} }
return productId; return productId;
}, },
/**
* 收藏选中的商品
*/
subCollect: function(event) { subCollect: function(event) {
let that = this, const spuIds = this.getSelectValueProductId();
selectValue = that.selectValue; if (spuIds.length === 0) {
if (selectValue.length > 0) { return this.$util.Tips({
let selectValueProductId = that.getSelectValueProductId(); title: '请选择产品'
collectAll(that.getSelectValueProductId()).then(res => { });
return that.$util.Tips({ }
ProductFavoriteApi.createFavoriteList(spuIds).then(res => {
return this.$util.Tips({
title: '收藏成功', title: '收藏成功',
icon: 'success' icon: 'success'
}); });
@ -457,24 +434,20 @@
title: err title: err
}); });
}); });
} else {
return that.$util.Tips({
title: '请选择产品'
});
}
}, },
// /**
* 立即下单
*/
subOrder: function(event) { subOrder: function(event) {
const selectValue = this.selectValue;
let that = this, if (selectValue.length === 0) {
selectValue = that.selectValue; return this.$util.Tips({
if (selectValue.length > 0) {
that.getPreOrder();
} else {
return that.$util.Tips({
title: '请选择产品' title: '请选择产品'
}); });
} }
uni.navigateTo({
url: '/pages/users/order_confirm/index?cartIds=' + selectValue.join(',')
});
}, },
/** /**
* 预下单 * 预下单
@ -487,6 +460,9 @@
}) })
this.$Order.getPreOrder("shoppingCart", shoppingCartId); this.$Order.getPreOrder("shoppingCart", shoppingCartId);
}, },
/**
* 全选 / 全不选
*/
checkboxAllChange: function(event) { checkboxAllChange: function(event) {
let value = event.detail.value; let value = event.detail.value;
if (value.length > 0) { if (value.length > 0) {
@ -565,7 +541,6 @@
} else { } else {
isAllSelect = false; isAllSelect = false;
} }
continue;
} else { } else {
if (!this.footerswitch && item.selected) { if (!this.footerswitch && item.selected) {
selectValue.push(item.id); selectValue.push(item.id);
@ -592,23 +567,6 @@
this.selectCountPrice = selectCountPrice; this.selectCountPrice = selectCountPrice;
this.cartCount = cartCount; this.cartCount = cartCount;
}, },
/**
* 购物车手动填写
*/
iptCartNum: function(index) {
let item = this.cartList.valid[index];
if (item.cartNum) {
this.setCartNum(item.id, item.cartNum);
}
this.switchSelect();
},
blurInput: function(index) {
let item = this.cartList.valid[index];
if (!item.cartNum) {
item.cartNum = 1;
this.$set(this.cartList, 'valid', this.cartList.valid)
}
},
/** /**
* 减少购买数量 * 减少购买数量
*/ */
@ -654,7 +612,7 @@
* 修改购物项为指定数量 * 修改购物项为指定数量
*/ */
setCartNum(cartId, cartNum) { setCartNum(cartId, cartNum) {
TradeCartApi.updateCart({ TradeCartApi.updateCartCount({
id: cartId, id: cartId,
count: cartNum count: cartNum
}).then(res => { }).then(res => {
@ -667,7 +625,6 @@
TradeCartApi.getCartList().then((res) => { TradeCartApi.getCartList().then((res) => {
resolve(res.data); resolve(res.data);
}).catch(function(err) { }).catch(function(err) {
this.loading = false;
this.canShow = true; this.canShow = true;
this.$util.Tips({ this.$util.Tips({
title: err title: err
@ -709,25 +666,35 @@
this.$set(this.cartList, 'invalid', invalidList); this.$set(this.cartList, 'invalid', invalidList);
// //
this.loading = false;
this.canShow = true; this.canShow = true;
uni.hideLoading(); uni.hideLoading();
}, },
/**
* 获得热门推荐
*/
getHostProduct: function() { getHostProduct: function() {
let that = this; ProductSpuApi.getSpuPage({
if (that.hotScroll) return recommendType: 'hot',
getProductHot( pageNo: this.hotPage,
that.hotPage, pageSize: this.hotLimit
that.hotLimit, }).then(res => {
).then(res => { const good_list = res.data.list;
that.hotPage++ this.hotPage++
that.hotScroll = res.data.list.length < that.hotLimit this.hotScroll = good_list.length < this.hotLimit
that.hostProduct = that.hostProduct.concat(res.data.list)
//
const spuIds = good_list.map(item => item.id);
if (spuIds.length > 0) {
PromotionActivityApi.getActivityListBySpuIds(spuIds).then(res => {
ProductUtil.setActivityList(good_list, res.data);
this.hostProduct = this.hostProduct.concat(good_list) // Vue
});
}
}); });
}, },
goodsOpen: function() { goodsOpen: function() {
let that = this; this.goodsHidden = !this.goodsHidden;
that.goodsHidden = !that.goodsHidden;
}, },
/** /**
* 切换到管理 * 切换到管理
@ -736,20 +703,20 @@
this.footerswitch = !this.footerswitch; this.footerswitch = !this.footerswitch;
this.switchSelect(); this.switchSelect();
}, },
/**
* 清空
*/
unsetCart: function() { unsetCart: function() {
let that = this, const ids = [];
ids = []; for (let i = 0, len = this.cartList.invalid.length; i < len; i++) {
for (let i = 0, len = that.cartList.invalid.length; i < len; i++) { ids.push(this.cartList.invalid[i].id);
ids.push(that.cartList.invalid[i].id);
} }
cartDel(ids).then(res => { TradeCartApi.deleteCart(ids).then(res => {
that.$util.Tips({ this.$util.Tips({
title: '清除成功' title: '清除成功'
}); });
that.$set(that.cartList, 'invalid', []); this.getCartList()
that.getHostProduct();
}).catch(res => { }).catch(res => {
}); });
}, },
@ -766,13 +733,10 @@
}, },
}, },
onReachBottom() { onReachBottom() {
// TODO if (this.cartList.valid.length === 0
if (true) { && this.cartList.invalid.length === 0
return; && this.hotPage != 1) {
} this.getHostProduct();
let that = this;
if (that.cartList.valid.length == 0 && that.cartList.invalid.length == 0 && this.hotPage != 1) {
that.getHostProduct();
} }
} }
} }