cart.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. showAddCartModal:false,
  15. currentProduct:null
  16. },
  17. // 获取购物车商品
  18. getCartProds(){
  19. API.getCartProds().then(res=>{
  20. this.handleCartsFn(res);
  21. })
  22. },
  23. // 处理商品函数
  24. handleCartsFn(value){
  25. let num = 0,checkIds=[];
  26. value.carts.forEach(item=>{
  27. if(item.checkFlag === "1"){
  28. num+=1;
  29. checkIds.push(item.id+'');
  30. }
  31. })
  32. this.setData({
  33. cartList:value.carts,
  34. totalPrice:value.checkedSumAmount,
  35. checkedIds:checkIds,
  36. isAllChecked:num === value.carts.length
  37. })
  38. },
  39. // 购物车选中/取消
  40. handleCheckboxFn(prodId){
  41. let {id} = this.data.cartList.filter(item=>item.id == prodId)[0];
  42. API.checkedCartProds({id}).then(res=>{
  43. this.handleCartsFn(res);
  44. })
  45. },
  46. // 选中/取消选中所有
  47. handleCheckedAll(){
  48. API.handleCheckedAll().then(res=>{
  49. this.handleCartsFn(res);
  50. })
  51. },
  52. // 取消选中所有
  53. handleUncheckedAll(){
  54. API.handleUncheckedAll().then(res=>{
  55. this.handleCartsFn(res);
  56. })
  57. },
  58. // 删除选中的商品
  59. deleteProd(){
  60. let ids = this.data.checkedIds;
  61. API.deleteCartProds({ids}).then(res=>{
  62. this.handleCartsFn(res);
  63. })
  64. },
  65. // 复选框变化事件
  66. handleCheckboxChange(e) {
  67. const oldCheckIds = this.data.checkedIds;
  68. const checkedIds = e.detail.value; // 选中的商品id数组(如 ['1', '2'])
  69. const cartList = this.data.cartList.map(item => {
  70. item.checkFlag = checkedIds.includes(item.id.toString());
  71. return item;
  72. });
  73. // 取消选中
  74. if(oldCheckIds.length > checkedIds.length){
  75. let id = oldCheckIds.filter(item=>!checkedIds.includes(item));
  76. this.handleCheckboxFn(id[0]);
  77. }else{
  78. this.handleCheckboxFn(checkedIds.at(-1));
  79. }
  80. this.setData({
  81. cartList,
  82. checkedIds,
  83. isAllChecked: checkedIds.length === this.data.cartList.length
  84. });
  85. this.calculateTotal(); // 计算总价
  86. },
  87. // 全选/取消全选
  88. toggleAll() {
  89. const isAllChecked = !this.data.isAllChecked;
  90. const cartList = this.data.cartList.map(item => {
  91. item.checkFlag = isAllChecked;
  92. return item;
  93. });
  94. this.setData({ cartList, isAllChecked });
  95. if(isAllChecked){
  96. this.handleCheckedAll();
  97. }else{
  98. this.handleUncheckedAll();
  99. }
  100. this.calculateTotal();
  101. },
  102. // 增加数量
  103. increaseCount(e) {
  104. const id = e.currentTarget.dataset.id;
  105. API.addCartProdCount({id}).then(res=>{
  106. this.handleCartsFn(res);
  107. })
  108. },
  109. // 减少数量
  110. decreaseCount(e) {
  111. const id = e.currentTarget.dataset.id;
  112. API.subCartProdCount({id}).then(res=>{
  113. this.handleCartsFn(res);
  114. })
  115. },
  116. // 计算总价和选中数量
  117. calculateTotal() {
  118. let totalPrice = 0;
  119. let checkedCount = 0;
  120. this.data.cartList.forEach(item => {
  121. if (item.checkFlag) {
  122. totalPrice += item.price * item.quantity;
  123. checkedCount++;
  124. }
  125. });
  126. this.setData({ totalPrice, checkedCount });
  127. },
  128. // 管理按钮
  129. manageBut(){
  130. this.setData({
  131. isManage:!this.data.isManage
  132. })
  133. },
  134. // 确认下单
  135. confirmOrder(){
  136. let prods = this.data.cartList.filter(item=>item.checkFlag === "1");
  137. prods.forEach(item=>{
  138. item.cartsId = item.id;
  139. delete item.id;
  140. })
  141. wx.navigateTo({
  142. url: '/pages/order/confirmOrder/confirmOrder?prods='+encodeURIComponent(JSON.stringify(prods)),
  143. })
  144. },
  145. // 显示商品属性模态框
  146. showAddCartModal(e) {
  147. const product = e.detail;
  148. this.setData({
  149. showAddCartModal: true,
  150. currentProduct:product
  151. });
  152. },
  153. // 关闭属性选择模态框
  154. handleCloseAddCartModal(){
  155. this.setData({
  156. showAddCartModal:false
  157. })
  158. },
  159. onLoad(){
  160. },
  161. onShow(){
  162. this.getCartProds();
  163. }
  164. })