productdetails.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { models, db, _ } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. item: {},
  5. quantity: 1,
  6. showimg: '',
  7. gouwucimg: '',
  8. gaoliao: 0,
  9. steppernum: 1,
  10. },
  11. onLoad(options) {
  12. const itemData = decodeURIComponent(options.data);
  13. const item = JSON.parse(itemData);
  14. this.setData({
  15. item: item
  16. });
  17. const fileIDs = [
  18. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show.png',
  19. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/gouwuc_img.png'
  20. ];
  21. // 并发下载多个 fileID
  22. Promise.all(
  23. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  24. ).then(results => {
  25. // 每个 result 对应一个下载结果
  26. const tempFilePaths = results.map(r => r.tempFilePath);
  27. console.log('全部下载成功:', tempFilePaths);
  28. this.setData({
  29. showimg: tempFilePaths[0],
  30. gouwucimg: tempFilePaths[1]
  31. });
  32. }).catch(err => {
  33. console.error('有文件下载失败:', err);
  34. });
  35. },
  36. // 跳转购物车
  37. gotogwc() {
  38. wx.switchTab({
  39. url: `/pages/shoppingcart/shoppingcart`
  40. });
  41. },
  42. // 规格选择
  43. setspecs(e) {
  44. const index = e.currentTarget.dataset.item;
  45. this.setData({
  46. gaoliao: index
  47. })
  48. },
  49. // 购买数量
  50. onChange(e) {
  51. const num = e.detail
  52. this.setData({
  53. steppernum:num
  54. })
  55. },
  56. // 加入购物车
  57. async addToCart() {
  58. // 加入购物车逻辑
  59. console.log('加入购物车', this.data.item, this.data.item._id);
  60. const { data } = await models.shopping_cart.get({
  61. filter: {
  62. where: {
  63. merchandise_id: { $eq: this.data.item._id }, // 推荐传入_id数据标识进行操作
  64. specs_index: { $eq: this.data.gaoliao }
  65. }
  66. },
  67. envType: "prod",
  68. });
  69. // 返回查询到的数据
  70. console.log(data);
  71. const datalist = data || {}
  72. if (!datalist || Object.keys(datalist).length === 0) {
  73. console.log('购物车中没有该商品,准备新增');
  74. // 执行插入操作
  75. const { data } = await models.shopping_cart.create({
  76. data: {
  77. merchandise_id: this.data.item._id,
  78. num: this.data.steppernum, // 商品数量
  79. specs_index: this.data.gaoliao, // 规格下标
  80. },
  81. envType: "prod",
  82. });
  83. // 判断是否有 id 返回
  84. if (data && (data.id || data.Id)) {
  85. console.log('加入购物车成功:', data);
  86. wx.showToast({
  87. title: '加入购物车成功',
  88. icon: 'success',
  89. duration: 1500
  90. });
  91. } else {
  92. // 数据结构异常,视为失败
  93. console.error('加入购物车失败:无返回 id', data);
  94. wx.showToast({
  95. title: '加入失败',
  96. icon: 'error',
  97. duration: 1500
  98. });
  99. }
  100. } else {
  101. console.log('购物车已有该商品,准备更新数量');
  102. // 执行更新操作
  103. const { data } = await models.shopping_cart.update({
  104. data: {
  105. num: datalist.num + 1, // 商品数量
  106. },
  107. filter: {
  108. where: {
  109. $and: [
  110. {
  111. _id: {
  112. $eq: datalist._id, // 推荐传入_id数据标识进行操作
  113. },
  114. },
  115. ]
  116. }
  117. },
  118. envType: "prod",
  119. });
  120. // 返回更新成功的条数
  121. console.log(data);
  122. // 判断是否有 id 返回
  123. if (data.count > 0) {
  124. console.log('加入购物车成功:', data);
  125. wx.showToast({
  126. title: '加入购物车成功',
  127. icon: 'success',
  128. duration: 1500
  129. });
  130. } else {
  131. // 数据结构异常,视为失败
  132. console.error('加入购物车失败:无返回 id', data);
  133. wx.showToast({
  134. title: '加入失败',
  135. icon: 'error',
  136. duration: 1500
  137. });
  138. }
  139. }
  140. },
  141. buyNow() {
  142. // 立即购买逻辑
  143. console.log('立即购买');
  144. }
  145. });