productdetails.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. this.setData({
  18. item: item,
  19. from
  20. }, () => {
  21. this.getbrowse()
  22. });
  23. const fileIDs = [
  24. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show.png',
  25. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/fenxianglj.png'
  26. // 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/gouwuc_img.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. let title = '';
  100. let path = '';
  101. if (shareType === 'teacher') {
  102. title = '老师推荐的优质商品,快来看!';
  103. path = `/subpackages/productdetails/productdetails?data=${encodeURIComponent(JSON.stringify(item))}&from=teacher`;
  104. } else {
  105. title = '家长推荐的宝藏商品,别错过!';
  106. path = `/subpackages/productdetails/productdetails?data=${encodeURIComponent(JSON.stringify(item))}&from=parent`;
  107. }
  108. return {
  109. title,
  110. path,
  111. imageUrl: item.detail_images?.[0] || ''
  112. };
  113. },
  114. // handleSpecSelect(e) {
  115. // const spec = e.currentTarget.dataset.spec;
  116. // this.setData({ selectedSpec: spec });
  117. // },
  118. // handleQuantityChange(e) {
  119. // const action = e.currentTarget.dataset.action;
  120. // let quantity = this.data.quantity;
  121. // if (action === 'plus') {
  122. // quantity++;
  123. // } else if (action === 'minus' && quantity > 1) {
  124. // quantity--;
  125. // }
  126. // this.setData({ quantity });
  127. // },
  128. // addToCart() {
  129. // // 加入购物车逻辑
  130. // console.log('加入购物车');
  131. // },
  132. // buyNow() {
  133. // // 立即购买逻辑
  134. // console.log('立即购买');
  135. // }
  136. });