productdetails.js 4.5 KB

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