addCartModel.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { API } from "../../service/api";
  2. // components/addCartModel/addCartModel.js
  3. Component({
  4. properties: {
  5. visible: {
  6. type: Boolean,
  7. value: false
  8. },
  9. product: {
  10. type: Object,
  11. value: {}
  12. }
  13. },
  14. data: {
  15. selectedSpecs: [], // 选择的规格索引 [0,1]表示第一个规格的第0个选项,第二个规格的第1个选项
  16. quantity: 1,
  17. selectIndex:0, // 属性索引
  18. },
  19. observers: {
  20. 'product': function(specs) {
  21. // console.log("888888888888",specs);
  22. specs && this.getProdDetail(specs);
  23. }
  24. },
  25. methods: {
  26. // 获取商品详情
  27. getProdDetail(e){
  28. let data = {
  29. pageNum:1,
  30. pageSize:1,
  31. mercId:e.mercId,
  32. prodId:e.prodId,
  33. mercProdId:e.mercProdId
  34. }
  35. API.getProdDetail(data).then(res=>{
  36. this.setData({
  37. productDetail:res
  38. })
  39. })
  40. },
  41. // 关闭模态框
  42. handleClose() {
  43. this.triggerEvent('close');
  44. },
  45. // 选择属性
  46. selectSpec(e) {
  47. let index = e.currentTarget.dataset.index;
  48. let {availQty} = this.data.productDetail.prodAttrList[index];
  49. this.setData({
  50. selectIndex:index,
  51. quantity:availQty > 0?1:0
  52. });
  53. },
  54. // 添加数量
  55. increaseQuantity() {
  56. const { quantity, productDetail,selectIndex } = this.data;
  57. if (quantity < productDetail.prodAttrList[selectIndex].availQty) {
  58. this.setData({ quantity: quantity + 1 });
  59. }
  60. },
  61. // 减少数量
  62. decreaseQuantity() {
  63. const { quantity } = this.data;
  64. if (quantity > 1) {
  65. this.setData({ quantity: quantity - 1 });
  66. }
  67. },
  68. handleAddToCart() {
  69. let {mercId,prodAttrList,prodId} = this.data.productDetail;
  70. if(this.data.product.buy){
  71. let prod = this.data.productDetail;
  72. let {attrName,prodAttrPrice} = prodAttrList[this.data.selectIndex];
  73. prod.attrName = attrName;
  74. prod.quantity = this.data.quantity;
  75. prod.price = prodAttrPrice.price;
  76. wx.navigateTo({
  77. url: '/pages/order/confirmOrder/confirmOrder?prods='+encodeURIComponent(JSON.stringify([prod])),
  78. })
  79. return;
  80. }
  81. const cartItem = {
  82. prodAttrId: prodAttrList[this.data.selectIndex].prodAttrId,
  83. mercId,
  84. prodId,
  85. count:this.data.quantity
  86. };
  87. API.addCarts(cartItem).then(res=>{
  88. // 可以显示添加成功的提示
  89. wx.showToast({
  90. title: '已加入购物车',
  91. icon: 'success'
  92. });
  93. this.triggerEvent('close', { item: cartItem });
  94. })
  95. }
  96. },
  97. computed: {
  98. }
  99. });