productdetails.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. fenxiangljimg: '',
  10. shareType: '' // 用于区分是老师还是家长
  11. },
  12. onLoad(options) {
  13. const from = options.from || '';
  14. console.log('进了');
  15. const itemData = decodeURIComponent(options.data);
  16. const item = JSON.parse(itemData);
  17. console.log(item, 'itemitemitemitemitemitemitem');
  18. this.setData({
  19. item: item,
  20. from
  21. }, () => {
  22. this.getbrowse()
  23. });
  24. const fileIDs = [
  25. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show.png',
  26. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/fenxianglj.png'
  27. // 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/gouwuc_img.png'
  28. ];
  29. // 并发下载多个 fileID
  30. Promise.all(
  31. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  32. ).then(results => {
  33. // 每个 result 对应一个下载结果
  34. const tempFilePaths = results.map(r => r.tempFilePath);
  35. console.log('全部下载成功:', tempFilePaths);
  36. this.setData({
  37. showimg: tempFilePaths[0],
  38. fenxiangljimg: tempFilePaths[1]
  39. // gouwucimg: tempFilePaths[1]
  40. });
  41. }).catch(err => {
  42. console.error('有文件下载失败:', err);
  43. });
  44. },
  45. // 新增浏览量
  46. async getbrowse() {
  47. const { data } = await models.wx_merchandise.update({
  48. data: {
  49. browse: this.data.item.browse + 1, // 浏览数
  50. },
  51. filter: {
  52. where: {
  53. $and: [
  54. {
  55. _id: {
  56. $eq: this.data.item._id, // 推荐传入_id数据标识进行操作
  57. },
  58. },
  59. ]
  60. }
  61. },
  62. // envType: pre 体验环境, prod 正式环境
  63. envType: "prod",
  64. });
  65. // 返回更新成功的条数
  66. console.log(data, 'datadata');
  67. this.setData({
  68. 'item.browse': this.data.item.browse + 1
  69. })
  70. },
  71. previewImage(e) {
  72. const index = e.currentTarget.dataset.index;
  73. const images = this.data.item.detail_images;
  74. wx.previewImage({
  75. current: images[index], // 当前预览的图片
  76. urls: images // 所有可预览的图片列表
  77. });
  78. },
  79. // 点击分享图标
  80. onShareTap() {
  81. wx.showActionSheet({
  82. itemList: ['分享给老师', '分享给家长'],
  83. success: (res) => {
  84. const shareType = res.tapIndex === 0 ? 'teacher' : 'parent';
  85. this.setData({ shareType });
  86. wx.showModal({
  87. title: '提示',
  88. content: '请点击右上角“···”按钮,选择“发送给朋友”进行分享',
  89. showCancel: false
  90. });
  91. },
  92. fail: (err) => {
  93. console.log('取消选择', err);
  94. }
  95. });
  96. },
  97. // 配置微信分享信息
  98. onShareAppMessage() {
  99. const { item, shareType } = this.data;
  100. let title = '';
  101. let path = '';
  102. if (shareType === 'teacher') {
  103. title = '老师推荐的优质商品,快来看!';
  104. path = `/subpackages/productdetails/productdetails?data=${encodeURIComponent(JSON.stringify(item))}&from=teacher`;
  105. } else {
  106. title = '家长推荐的宝藏商品,别错过!';
  107. path = `/subpackages/productdetails/productdetails?data=${encodeURIComponent(JSON.stringify(item))}&from=parent`;
  108. }
  109. return {
  110. title,
  111. path,
  112. imageUrl: item.detail_images?.[0] || ''
  113. };
  114. },
  115. // 点击切换规格
  116. selectSpec(e) {
  117. const specIndex = e.currentTarget.dataset.specindex;
  118. this.setData({
  119. gaoliao: specIndex
  120. })
  121. }
  122. // handleSpecSelect(e) {
  123. // const spec = e.currentTarget.dataset.spec;
  124. // this.setData({ selectedSpec: spec });
  125. // },
  126. // handleQuantityChange(e) {
  127. // const action = e.currentTarget.dataset.action;
  128. // let quantity = this.data.quantity;
  129. // if (action === 'plus') {
  130. // quantity++;
  131. // } else if (action === 'minus' && quantity > 1) {
  132. // quantity--;
  133. // }
  134. // this.setData({ quantity });
  135. // },
  136. // addToCart() {
  137. // // 加入购物车逻辑
  138. // console.log('加入购物车');
  139. // },
  140. // buyNow() {
  141. // // 立即购买逻辑
  142. // console.log('立即购买');
  143. // }
  144. });