shoppingcart.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. onShow() {
  26. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  27. this.getTabBar().setSelected(2); // 比如首页就是 0
  28. }
  29. },
  30. onLoad() {
  31. this.updateTotalPrice();
  32. const fileIDs = [
  33. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png',
  34. ];
  35. // 并发下载多个 fileID
  36. Promise.all(
  37. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  38. ).then(results => {
  39. // 每个 result 对应一个下载结果
  40. const tempFilePaths = results.map(r => r.tempFilePath);
  41. console.log('全部下载成功:', tempFilePaths);
  42. this.setData({
  43. souimg: tempFilePaths[0],
  44. });
  45. }).catch(err => {
  46. console.error('有文件下载失败:', err);
  47. });
  48. },
  49. handleCheckboxChange(e) {
  50. const { value } = e.detail;
  51. const cartItems = this.data.cartItems.map(item => ({
  52. ...item,
  53. checked: value.includes(item.id)
  54. }));
  55. this.setData({ cartItems });
  56. this.updateTotalPrice();
  57. },
  58. handleAllCheckboxChange(e) {
  59. const { value } = e.detail;
  60. const allChecked = value.includes('all');
  61. const cartItems = this.data.cartItems.map(item => ({
  62. ...item,
  63. checked: allChecked
  64. }));
  65. this.setData({ cartItems, allChecked });
  66. this.updateTotalPrice();
  67. },
  68. updateTotalPrice() {
  69. const totalPrice = this.data.cartItems.reduce((total, item) => {
  70. if (item.checked) {
  71. return total + item.price * item.quantity;
  72. }
  73. return total;
  74. }, 0);
  75. this.setData({ totalPrice });
  76. },
  77. handleCheckout() {
  78. // 处理结算逻辑
  79. console.log('结算');
  80. },
  81. onChange(e) {
  82. const { id } = e.currentTarget.dataset;
  83. const { detail } = e;
  84. const cartItems = this.data.cartItems.map(item => {
  85. if (item.id === id) {
  86. return { ...item, quantity: detail };
  87. }
  88. return item;
  89. });
  90. this.setData({ cartItems }, () => {
  91. this.updateTotalPrice();
  92. });
  93. }
  94. });