cart.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. this.handleCartsFn(res);
  19. })
  20. },
  21. // 处理商品函数
  22. handleCartsFn(value){
  23. let num = 0,checkIds=[];
  24. value.carts.forEach(item=>{
  25. if(item.checkFlag === "1"){
  26. num+=1;
  27. checkIds.push(item.id+'');
  28. }
  29. })
  30. this.setData({
  31. cartList:value.carts,
  32. totalPrice:value.checkedSumAmount,
  33. checkedIds:checkIds,
  34. isAllChecked:num === value.carts.length
  35. })
  36. },
  37. // 购物车选中/取消
  38. handleCheckboxFn(prodId){
  39. let {id} = this.data.cartList.filter(item=>item.id == prodId)[0];
  40. API.checkedCartProds({id}).then(res=>{
  41. this.handleCartsFn(res);
  42. })
  43. },
  44. // 选中/取消选中所有
  45. handleCheckedAll(){
  46. API.handleCheckedAll().then(res=>{
  47. })
  48. },
  49. // 取消选中所有
  50. handleUncheckedAll(){
  51. API.handleUncheckedAll().then(res=>{
  52. })
  53. },
  54. // 删除选中的商品
  55. deleteProd(){
  56. let ids = this.data.checkedIds;
  57. API.deleteCartProds({ids}).then(res=>{
  58. this.handleCartsFn(res);
  59. })
  60. },
  61. // 复选框变化事件
  62. handleCheckboxChange(e) {
  63. const oldCheckIds = this.data.checkedIds;
  64. const checkedIds = e.detail.value; // 选中的商品id数组(如 ['1', '2'])
  65. const cartList = this.data.cartList.map(item => {
  66. item.checkFlag = checkedIds.includes(item.id.toString());
  67. return item;
  68. });
  69. // 取消选中
  70. if(oldCheckIds.length > checkedIds.length){
  71. let id = oldCheckIds.filter(item=>!checkedIds.includes(item));
  72. this.handleCheckboxFn(id[0]);
  73. }else{
  74. this.handleCheckboxFn(checkedIds.at(-1));
  75. }
  76. this.setData({
  77. cartList,
  78. checkedIds,
  79. isAllChecked: checkedIds.length === this.data.cartList.length
  80. });
  81. this.calculateTotal(); // 计算总价
  82. },
  83. // 全选/取消全选
  84. toggleAll() {
  85. const isAllChecked = !this.data.isAllChecked;
  86. const cartList = this.data.cartList.map(item => {
  87. item.checkFlag = isAllChecked;
  88. return item;
  89. });
  90. this.setData({ cartList, isAllChecked });
  91. if(isAllChecked){
  92. this.handleCheckedAll();
  93. }else{
  94. this.handleUncheckedAll();
  95. }
  96. this.calculateTotal();
  97. },
  98. // 增加数量
  99. increaseCount(e) {
  100. const id = e.currentTarget.dataset.id;
  101. const cartList = this.data.cartList.map(item => {
  102. if (item.id == id) item.quantity++;
  103. return item;
  104. });
  105. this.setData({ cartList });
  106. this.calculateTotal();
  107. },
  108. // 减少数量
  109. decreaseCount(e) {
  110. const id = e.currentTarget.dataset.id;
  111. const cartList = this.data.cartList.map(item => {
  112. if (item.id == id && item.quantity > 1) item.quantity--;
  113. return item;
  114. });
  115. this.setData({ cartList });
  116. this.calculateTotal();
  117. },
  118. // 计算总价和选中数量
  119. calculateTotal() {
  120. let totalPrice = 0;
  121. let checkedCount = 0;
  122. this.data.cartList.forEach(item => {
  123. if (item.checkFlag) {
  124. totalPrice += item.price * item.quantity;
  125. checkedCount++;
  126. }
  127. });
  128. this.setData({ totalPrice, checkedCount });
  129. },
  130. // 管理按钮
  131. manageBut(){
  132. this.setData({
  133. isManage:!this.data.isManage
  134. })
  135. },
  136. onLoad(){
  137. this.getCartProds();
  138. }
  139. })