productdetails.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. this.getbrowse()
  18. });
  19. const fileIDs = [
  20. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show.png',
  21. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/gouwuc_img.png'
  22. ];
  23. // 并发下载多个 fileID
  24. Promise.all(
  25. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  26. ).then(results => {
  27. // 每个 result 对应一个下载结果
  28. const tempFilePaths = results.map(r => r.tempFilePath);
  29. console.log('全部下载成功:', tempFilePaths);
  30. this.setData({
  31. showimg: tempFilePaths[0],
  32. gouwucimg: tempFilePaths[1]
  33. });
  34. }).catch(err => {
  35. console.error('有文件下载失败:', err);
  36. });
  37. },
  38. // 新增浏览量
  39. async getbrowse() {
  40. const { data } = await models.wx_merchandise.update({
  41. data: {
  42. browse: this.data.item.browse + 1, // 浏览数
  43. },
  44. filter: {
  45. where: {
  46. $and: [
  47. {
  48. _id: {
  49. $eq: this.data.item._id, // 推荐传入_id数据标识进行操作
  50. },
  51. },
  52. ]
  53. }
  54. },
  55. // envType: pre 体验环境, prod 正式环境
  56. envType: "prod",
  57. });
  58. // 返回更新成功的条数
  59. console.log(data, 'datadata');
  60. this.setData({
  61. 'item.browse': this.data.item.browse + 1
  62. })
  63. },
  64. previewImage(e) {
  65. const index = e.currentTarget.dataset.index;
  66. const images = this.data.item.detail_images;
  67. wx.previewImage({
  68. current: images[index], // 当前预览的图片
  69. urls: images // 所有可预览的图片列表
  70. });
  71. },
  72. // 跳转购物车
  73. gotogwc() {
  74. wx.switchTab({
  75. url: `/pages/shoppingcart/shoppingcart`
  76. });
  77. },
  78. // 规格选择
  79. setspecs(e) {
  80. const index = e.currentTarget.dataset.item;
  81. this.setData({
  82. gaoliao: index
  83. })
  84. },
  85. // 购买数量
  86. onChange(e) {
  87. const num = e.detail
  88. this.setData({
  89. steppernum:num
  90. })
  91. },
  92. // 加入购物车
  93. async addToCart() {
  94. // 加入购物车逻辑
  95. console.log('加入购物车', this.data.item, this.data.item._id);
  96. const userInfo = wx.getStorageSync('userInfo');
  97. const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID
  98. const { data } = await models.shopping_cart.get({
  99. filter: {
  100. where: {
  101. user_id: { $eq: userId },
  102. merchandise_id: { $eq: this.data.item._id }, // 推荐传入_id数据标识进行操作
  103. specs_index: { $eq: this.data.gaoliao }
  104. }
  105. },
  106. envType: "prod",
  107. });
  108. // 返回查询到的数据
  109. console.log(data);
  110. const datalist = data || {}
  111. if (!datalist || Object.keys(datalist).length === 0) {
  112. console.log('购物车中没有该商品,准备新增');
  113. // 执行插入操作
  114. const { data } = await models.shopping_cart.create({
  115. data: {
  116. user_id: userId,
  117. merchandise_id: this.data.item._id,
  118. num: this.data.steppernum, // 商品数量
  119. specs_index: this.data.gaoliao, // 规格下标
  120. },
  121. envType: "prod",
  122. });
  123. // 判断是否有 id 返回
  124. if (data && (data.id || data.Id)) {
  125. console.log('加入购物车成功:', data);
  126. wx.showToast({
  127. title: '加入购物车成功',
  128. icon: 'success',
  129. duration: 1500
  130. });
  131. } else {
  132. // 数据结构异常,视为失败
  133. console.error('加入购物车失败:无返回 id', data);
  134. wx.showToast({
  135. title: '加入失败',
  136. icon: 'error',
  137. duration: 1500
  138. });
  139. }
  140. } else {
  141. console.log('购物车已有该商品,准备更新数量');
  142. // 执行更新操作
  143. const { data } = await models.shopping_cart.update({
  144. data: {
  145. num: datalist.num + 1, // 商品数量
  146. },
  147. filter: {
  148. where: {
  149. $and: [
  150. {
  151. _id: {
  152. $eq: datalist._id, // 推荐传入_id数据标识进行操作
  153. },
  154. },
  155. ]
  156. }
  157. },
  158. envType: "prod",
  159. });
  160. // 返回更新成功的条数
  161. console.log(data);
  162. // 判断是否有 id 返回
  163. if (data.count > 0) {
  164. console.log('加入购物车成功:', data);
  165. wx.showToast({
  166. title: '加入购物车成功',
  167. icon: 'success',
  168. duration: 1500
  169. });
  170. } else {
  171. // 数据结构异常,视为失败
  172. console.error('加入购物车失败:无返回 id', data);
  173. wx.showToast({
  174. title: '加入失败',
  175. icon: 'error',
  176. duration: 1500
  177. });
  178. }
  179. }
  180. },
  181. buyNow() {
  182. // 立即购买逻辑
  183. console.log('立即购买');
  184. }
  185. });