productdetails.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // import { models, db, _ } from '../../utils/cloudbase.js'
  2. import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
  3. Page({
  4. data: {
  5. item: {},
  6. quantity: 1,
  7. showimg: '',
  8. gouwucimg: '',
  9. gaoliao: 0,
  10. steppernum: 1,
  11. outTradeNo: '',
  12. transactionId: ''
  13. },
  14. async onLoad(options) {
  15. const itemData = decodeURIComponent(options.data);
  16. const item = JSON.parse(itemData);
  17. console.log(item, 'itemitemitemitemitem');
  18. // 处理 detail_images 云路径 => 临时 URL
  19. if (item.specList && item.specList.length > 0) {
  20. for (let i = 0; i < item.specList.length; i++) {
  21. const images = item.specList[i].detail_images || [];
  22. if (images.length > 0) {
  23. const tempFiles = await getTempFileURLs(images);
  24. // 替换原数组为 tempFileURL
  25. item.specList[i].detail_images = tempFiles.map(f => f.tempFileURL);
  26. }
  27. }
  28. }
  29. this.setData({
  30. item: item
  31. }, () => {
  32. this.getbrowse()
  33. });
  34. const fileIDs = [
  35. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/show.png',
  36. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/gouwuc_img.png'
  37. ];
  38. const fileList = await getTempFileURLs(fileIDs)
  39. this.setData({
  40. showimg: fileList[0].tempFileURL,
  41. gouwucimg: fileList[1].tempFileURL,
  42. })
  43. // // 并发下载多个 fileID
  44. // Promise.all(
  45. // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  46. // ).then(results => {
  47. // // 每个 result 对应一个下载结果
  48. // const tempFilePaths = results.map(r => r.tempFilePath);
  49. // console.log('全部下载成功:', tempFilePaths);
  50. // this.setData({
  51. // showimg: tempFilePaths[0],
  52. // gouwucimg: tempFilePaths[1]
  53. // });
  54. // }).catch(err => {
  55. // console.error('有文件下载失败:', err);
  56. // });
  57. },
  58. // 新增浏览量
  59. async getbrowse() {
  60. const models = await getModels()
  61. const { data } = await models.wx_merchandise.update({
  62. data: {
  63. browse: this.data.item.browse + 1, // 浏览数
  64. },
  65. filter: {
  66. where: {
  67. $and: [
  68. {
  69. _id: {
  70. $eq: this.data.item._id, // 推荐传入_id数据标识进行操作
  71. },
  72. },
  73. ]
  74. }
  75. },
  76. // envType: pre 体验环境, prod 正式环境
  77. envType: "prod",
  78. });
  79. // 返回更新成功的条数
  80. console.log(data, 'datadata');
  81. this.setData({
  82. 'item.browse': this.data.item.browse + 1
  83. })
  84. },
  85. previewImage(e) {
  86. const index = e.currentTarget.dataset.index;
  87. const images = this.data.item.detail_images;
  88. wx.previewImage({
  89. current: images[index], // 当前预览的图片
  90. urls: images // 所有可预览的图片列表
  91. });
  92. },
  93. // 跳转购物车
  94. gotogwc() {
  95. wx.switchTab({
  96. url: `/pages/shoppingcart/shoppingcart`
  97. });
  98. },
  99. // 点击切换规格
  100. selectSpec(e) {
  101. const specIndex = e.currentTarget.dataset.specindex;
  102. this.setData({
  103. gaoliao: specIndex
  104. })
  105. },
  106. // 购买数量
  107. onChange(e) {
  108. const num = e.detail
  109. this.setData({
  110. steppernum:num
  111. })
  112. },
  113. // 加入购物车
  114. async addToCart() {
  115. // 加入购物车逻辑
  116. console.log('加入购物车', this.data.item, this.data.item._id);
  117. const userInfo = wx.getStorageSync('userInfo');
  118. const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID
  119. const models = await getModels()
  120. const { data } = await models.shopping_cart.get({
  121. filter: {
  122. where: {
  123. user_id: { $eq: userId },
  124. merchandise_id: { $eq: this.data.item._id }, // 推荐传入_id数据标识进行操作
  125. specs_index: { $eq: this.data.gaoliao },
  126. }
  127. },
  128. envType: "prod",
  129. });
  130. // 返回查询到的数据
  131. console.log(data);
  132. const datalist = data || {}
  133. if (!datalist || Object.keys(datalist).length === 0) {
  134. console.log('购物车中没有该商品,准备新增');
  135. // 执行插入操作
  136. const { data } = await models.shopping_cart.create({
  137. data: {
  138. user_id: userId,
  139. merchandise_id: this.data.item._id,
  140. num: this.data.steppernum, // 商品数量
  141. specs_index: this.data.gaoliao, // 规格下标
  142. },
  143. envType: "prod",
  144. });
  145. // 判断是否有 id 返回
  146. if (data && (data.id || data.Id)) {
  147. console.log('加入购物车成功:', data);
  148. wx.showToast({
  149. title: '加入购物车成功',
  150. icon: 'success',
  151. duration: 1500
  152. });
  153. } else {
  154. // 数据结构异常,视为失败
  155. console.error('加入购物车失败:无返回 id', data);
  156. wx.showToast({
  157. title: '加入失败',
  158. icon: 'error',
  159. duration: 1500
  160. });
  161. }
  162. } else {
  163. console.log('购物车已有该商品,准备更新数量');
  164. // 执行更新操作
  165. const { data } = await models.shopping_cart.update({
  166. data: {
  167. num: datalist.num + 1, // 商品数量
  168. },
  169. filter: {
  170. where: {
  171. $and: [
  172. {
  173. _id: {
  174. $eq: datalist._id, // 推荐传入_id数据标识进行操作
  175. },
  176. },
  177. ]
  178. }
  179. },
  180. envType: "prod",
  181. });
  182. // 返回更新成功的条数
  183. console.log(data);
  184. // 判断是否有 id 返回
  185. if (data.count > 0) {
  186. console.log('加入购物车成功:', data);
  187. wx.showToast({
  188. title: '加入购物车成功',
  189. icon: 'success',
  190. duration: 1500
  191. });
  192. } else {
  193. // 数据结构异常,视为失败
  194. console.error('加入购物车失败:无返回 id', data);
  195. wx.showToast({
  196. title: '加入失败',
  197. icon: 'error',
  198. duration: 1500
  199. });
  200. }
  201. }
  202. },
  203. async buyNow() {
  204. // 将数据存入本地缓存
  205. const selectedItems = [
  206. {
  207. ...this.data.item, // 复制原始 item 对象
  208. specs_index: this.data.gaoliao, // 新增字段
  209. num: this.data.steppernum
  210. }
  211. ];
  212. console.log(selectedItems,'selectedItems');
  213. wx.setStorageSync('checkoutItems', selectedItems);
  214. wx.navigateTo({
  215. url: '/subpackages/submitorder/submitorder'
  216. });
  217. },
  218. });