shoppingcart.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. Page({
  2. data: {
  3. cartItems: [
  4. {
  5. id: '1',
  6. image: 'https://img1.baidu.com/it/u=2052658756,3021621759&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  7. title: '幼儿启蒙绘本 10册套装',
  8. description: '适合3-6岁儿童,培养创造力',
  9. price: 134.00,
  10. quantity: 1,
  11. checked: true
  12. },
  13. {
  14. id: '2',
  15. image: 'https://img1.baidu.com/it/u=2052658756,3021621759&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
  16. title: '幼儿启蒙绘本 10册套装',
  17. description: '适合3-6岁儿童,培养创造力',
  18. price: 134.00,
  19. quantity: 1,
  20. checked: true
  21. }
  22. ],
  23. souimg: '',
  24. },
  25. onLoad() {
  26. this.updateTotalPrice();
  27. const fileIDs = [
  28. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png',
  29. ];
  30. // 并发下载多个 fileID
  31. Promise.all(
  32. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  33. ).then(results => {
  34. // 每个 result 对应一个下载结果
  35. const tempFilePaths = results.map(r => r.tempFilePath);
  36. console.log('全部下载成功:', tempFilePaths);
  37. this.setData({
  38. souimg: tempFilePaths[0],
  39. });
  40. }).catch(err => {
  41. console.error('有文件下载失败:', err);
  42. });
  43. },
  44. handleCheckboxChange(e) {
  45. const { value } = e.detail;
  46. const cartItems = this.data.cartItems.map(item => ({
  47. ...item,
  48. checked: value.includes(item.id)
  49. }));
  50. this.setData({ cartItems });
  51. this.updateTotalPrice();
  52. },
  53. handleAllCheckboxChange(e) {
  54. const { value } = e.detail;
  55. const allChecked = value.includes('all');
  56. const cartItems = this.data.cartItems.map(item => ({
  57. ...item,
  58. checked: allChecked
  59. }));
  60. this.setData({ cartItems, allChecked });
  61. this.updateTotalPrice();
  62. },
  63. updateTotalPrice() {
  64. const totalPrice = this.data.cartItems.reduce((total, item) => {
  65. if (item.checked) {
  66. return total + item.price * item.quantity;
  67. }
  68. return total;
  69. }, 0);
  70. this.setData({ totalPrice });
  71. },
  72. handleCheckout() {
  73. // 处理结算逻辑
  74. console.log('结算');
  75. },
  76. onChange(e) {
  77. const { id } = e.currentTarget.dataset;
  78. const { detail } = e;
  79. const cartItems = this.data.cartItems.map(item => {
  80. if (item.id === id) {
  81. return { ...item, quantity: detail };
  82. }
  83. return item;
  84. });
  85. this.setData({ cartItems }, () => {
  86. this.updateTotalPrice();
  87. });
  88. }
  89. });