cart.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // 获取应用实例
  2. const {API} = require('../../service/api.js');
  3. Page({
  4. data:{
  5. cartList: [
  6. { id: 1, name: "商品A商品A商品A商品A商品A", price: 100, checked: false, count: 1 },
  7. { id: 2, name: "商品B商品B商品B商品B商品B", price: 200, checked: false, count: 2 },
  8. { id: 3, name: "商品C", price: 300, checked: false, count: 1 }
  9. ],
  10. isAllChecked: false, // 全选状态
  11. totalPrice:0,
  12. checkedCount:0,
  13. isManage:false, // 是否进入管理模式
  14. },
  15. // 获取购物车商品
  16. getCartProds(){
  17. API.getCartProds().then(res=>{
  18. let num = 0,checkIds=[];
  19. res.carts.forEach(item=>{
  20. if(item.checkFlag){
  21. num+=1;
  22. checkIds.push(item.id+'');
  23. }
  24. })
  25. this.setData({
  26. cartList:res.carts,
  27. totalPrice:res.checkedSumAmount,
  28. checkedIds:checkIds,
  29. isAllChecked:num === res.carts.length
  30. })
  31. })
  32. },
  33. // 购物车选中/取消
  34. handleCheckboxFn(prodId){
  35. let {mercProdId,id,prodAttrId,quantity} = this.data.cartList.filter(item=>item.id == prodId)[0];
  36. let data = {
  37. mercId:mercProdId,
  38. prodId:id,
  39. prodAttrId,
  40. count:quantity
  41. }
  42. API.checkedCartProds(data).then(res=>{
  43. })
  44. },
  45. // 删除选中的商品
  46. deleteProd(){
  47. let ids = [];
  48. this.data.cartList.map(item=>{
  49. if(item.checkFlag){
  50. ids.push(item.id);
  51. }
  52. });
  53. API.deleteCartProds().then(res=>{
  54. })
  55. },
  56. // 复选框变化事件
  57. handleCheckboxChange(e) {
  58. const oldCheckIds = this.data.checkedIds;
  59. const checkedIds = e.detail.value; // 选中的商品id数组(如 ['1', '2'])
  60. const cartList = this.data.cartList.map(item => {
  61. item.checkFlag = checkedIds.includes(item.id.toString());
  62. return item;
  63. });
  64. console.log("33333333",oldCheckIds,checkedIds);
  65. // 取消选中
  66. if(oldCheckIds.length > checkedIds.length){
  67. let id = oldCheckIds.filter(item=>!checkedIds.includes(item));
  68. this.handleCheckboxFn(id[0]);
  69. }else{
  70. this.handleCheckboxFn(checkedIds.at(-1));
  71. }
  72. this.setData({
  73. cartList,
  74. checkedIds,
  75. isAllChecked: checkedIds.length === this.data.cartList.length
  76. });
  77. this.calculateTotal(); // 计算总价
  78. },
  79. // 全选/取消全选
  80. toggleAll() {
  81. const isAllChecked = !this.data.isAllChecked;
  82. const cartList = this.data.cartList.map(item => {
  83. item.checkFlag = isAllChecked;
  84. return item;
  85. });
  86. this.setData({ cartList, isAllChecked });
  87. this.calculateTotal();
  88. },
  89. // 增加数量
  90. increaseCount(e) {
  91. const id = e.currentTarget.dataset.id;
  92. const cartList = this.data.cartList.map(item => {
  93. if (item.id == id) item.quantity++;
  94. return item;
  95. });
  96. this.setData({ cartList });
  97. this.calculateTotal();
  98. },
  99. // 减少数量
  100. decreaseCount(e) {
  101. const id = e.currentTarget.dataset.id;
  102. const cartList = this.data.cartList.map(item => {
  103. if (item.id == id && item.quantity > 1) item.quantity--;
  104. return item;
  105. });
  106. this.setData({ cartList });
  107. this.calculateTotal();
  108. },
  109. // 计算总价和选中数量
  110. calculateTotal() {
  111. let totalPrice = 0;
  112. let checkedCount = 0;
  113. this.data.cartList.forEach(item => {
  114. if (item.checkFlag) {
  115. totalPrice += item.price * item.quantity;
  116. checkedCount++;
  117. }
  118. });
  119. this.setData({ totalPrice, checkedCount });
  120. },
  121. // 管理按钮
  122. manageBut(){
  123. this.setData({
  124. isManage:!this.data.isManage
  125. })
  126. },
  127. onLoad(){
  128. this.getCartProds();
  129. }
  130. })