| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import { API } from "../../service/api";
- // components/addCartModel/addCartModel.js
- Component({
- properties: {
- visible: {
- type: Boolean,
- value: false
- },
- product: {
- type: Object,
- value: {}
- }
- },
-
- data: {
- selectedSpecs: [], // 选择的规格索引 [0,1]表示第一个规格的第0个选项,第二个规格的第1个选项
- quantity: 1,
- selectIndex:0, // 属性索引
- },
-
- observers: {
- 'product': function(specs) {
- // console.log("888888888888",specs);
- specs && this.getProdDetail(specs);
- }
- },
-
- methods: {
- // 获取商品详情
- getProdDetail(e){
- let data = {
- pageNum:1,
- pageSize:1,
- mercId:e.mercId,
- prodId:e.prodId,
- mercProdId:e.mercProdId
- }
- API.getProdDetail(data).then(res=>{
- this.setData({
- productDetail:res
- })
- })
- },
- // 关闭模态框
- handleClose() {
- this.triggerEvent('close');
- },
-
- // 选择属性
- selectSpec(e) {
- let index = e.currentTarget.dataset.index;
- let {availQty} = this.data.productDetail.prodAttrList[index];
- this.setData({
- selectIndex:index,
- quantity:availQty > 0?1:0
- });
- },
-
- // 添加数量
- increaseQuantity() {
- const { quantity, productDetail,selectIndex } = this.data;
- if (quantity < productDetail.prodAttrList[selectIndex].availQty) {
- this.setData({ quantity: quantity + 1 });
- }
- },
-
- // 减少数量
- decreaseQuantity() {
- const { quantity } = this.data;
- if (quantity > 1) {
- this.setData({ quantity: quantity - 1 });
- }
- },
-
- handleAddToCart() {
- let {mercId,prodAttrList,prodId} = this.data.productDetail;
- if(this.data.product.buy){
- let prod = this.data.productDetail;
- let {attrName,prodAttrPrice} = prodAttrList[this.data.selectIndex];
- prod.attrName = attrName;
- prod.quantity = this.data.quantity;
- prod.price = prodAttrPrice.price;
- wx.navigateTo({
- url: '/pages/order/confirmOrder/confirmOrder?prods='+encodeURIComponent(JSON.stringify([prod])),
- })
- this.triggerEvent('close');
- return;
- }
- const cartItem = {
- prodAttrId: prodAttrList[this.data.selectIndex].prodAttrId,
- mercId,
- prodId,
- count:this.data.quantity
- };
- API.addCarts(cartItem).then(res=>{
- // 可以显示添加成功的提示
- wx.showToast({
- title: '已加入购物车',
- icon: 'success'
- });
- this.triggerEvent('close', { item: cartItem });
- })
- }
- },
-
- computed: {
-
- }
- });
|