groupbuying.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { models, db, _ } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. categoriesindex: 1,
  5. categories: [],
  6. goods: [],
  7. souimg: '',
  8. gouwucimg: '',
  9. pageNumber: 1,
  10. pageSize: 10,
  11. hasMore: true, // 是否还有更多数据
  12. isLoading: false, // 防止多次触发
  13. types_ids: null, // 当前选中的分类 tag_id
  14. searchText: '', // 搜索关键词
  15. },
  16. onShow() {
  17. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  18. this.getTabBar().setSelected(1); // 比如首页就是 0
  19. }
  20. // 获取tab数据
  21. this.getTabdata();
  22. },
  23. onLoad(options) {
  24. const fileIDs = [
  25. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png',
  26. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/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. souimg: tempFilePaths[0],
  37. gouwucimg: tempFilePaths[1]
  38. });
  39. }).catch(err => {
  40. console.error('有文件下载失败:', err);
  41. });
  42. },
  43. // tab数据
  44. async getTabdata() {
  45. const { data } = await models.tab.list({
  46. filter: {
  47. where: {
  48. position: 4, // 显示位置
  49. },
  50. },
  51. envType: "prod",
  52. });
  53. // 返回查询到的数据
  54. const sortedRecords = data.records.sort((a, b) => {
  55. return a.sort - b.sort; // 升序排列
  56. });
  57. this.setData({
  58. categories: sortedRecords,
  59. goods: [],
  60. pageNumber: 1,
  61. hasMore: true,
  62. }, () => {
  63. this.getDatalist()
  64. })
  65. },
  66. onReachBottom() {
  67. // 上拉触底事件的处理函数
  68. this.loadMore();
  69. },
  70. loadMore() {
  71. // 加载更多数据的逻辑
  72. console.log('加载更多');
  73. // this.getDatalist(this.data.types_ids, true);
  74. if (this.data.isLoading || !this.data.hasMore) return;
  75. this.setData({ isLoading: true });
  76. this.getDatalist(this.data.types_ids, true);
  77. },
  78. // 模糊搜索
  79. onSearchInput(e) {
  80. const keyword = e.detail.value.trim();
  81. this.setData({
  82. searchText: keyword,
  83. pageNumber: 1,
  84. hasMore: true,
  85. goods: []
  86. }, () => {
  87. this.getDatalist(this.data.types_ids); // 重新加载
  88. });
  89. },
  90. // 列表数据
  91. async getDatalist(tag_id = null, isLoadMore = false) {
  92. const userInfo = wx.getStorageSync('userInfo');
  93. // 构造 where 条件
  94. const where = {
  95. school_id: _.in(userInfo.school_id)
  96. };
  97. if (tag_id) {
  98. where.tag_id = tag_id; // 仅在 tag_id 有值时添加
  99. }
  100. const { pageNumber, pageSize, searchText } = this.data;
  101. // 模糊搜索关键字
  102. if (searchText) {
  103. where.name = {
  104. $regex_ci: searchText
  105. };
  106. }
  107. const { data } = await models.wx_merchandise.list({
  108. filter: { where },
  109. pageSize,
  110. pageNumber,
  111. getCount: true,
  112. envType: "prod",
  113. });
  114. const collectList = data.records || [];
  115. // 循环去查 groupbuy 表
  116. for (let item of collectList) {
  117. try {
  118. const res = await models.wx_groupbuy_groupbuy.list({
  119. filter: { where: { groupbuy_id: item._id } },
  120. envType: "prod",
  121. });
  122. // 把查到的记录存入 specList,保证是数组
  123. item.specList = res.data.records || [];
  124. } catch (err) {
  125. console.error(`获取 groupbuy 失败,商品ID: ${item._id}`, err);
  126. item.specList = [];
  127. }
  128. }
  129. console.log(collectList, '+++++++++++++++++++++++++++++');
  130. this.setData({
  131. goods: isLoadMore ? this.data.goods.concat(collectList) : collectList, // ✅ 用 goods 拼接
  132. pageNumber: pageNumber + 1,
  133. hasMore: collectList.length === pageSize,
  134. isLoading: false
  135. });
  136. },
  137. // 点击tab分类 async
  138. tabcategories(e) {
  139. const types = e.currentTarget.dataset.type;
  140. const tagId = types.sort === 1 ? null : types._id;
  141. this.setData({
  142. categoriesindex: types.sort,
  143. types_ids: tagId,
  144. goods: [],
  145. pageNumber: 1,
  146. hasMore: true
  147. }, () => {
  148. this.getDatalist(tagId, false); // ✅ 这里也改成直接传 tagId
  149. });
  150. },
  151. navigateToDetail(e) {
  152. const index = e.currentTarget.dataset.index;
  153. const item = this.data.goods[index];
  154. wx.navigateTo({
  155. url: '/subpackages/productdetails/productdetails?data=' + encodeURIComponent(JSON.stringify(item))
  156. });
  157. }
  158. });