groupbuying.js 4.5 KB

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