groupbuying.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // import { models, db, _ } from '../../utils/cloudbase.js'
  2. import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
  3. const app = getApp();
  4. Page({
  5. data: {
  6. categoriesindex: 1,
  7. categories: [],
  8. goods: [],
  9. souimg: '',
  10. gouwucimg: '',
  11. pageNumber: 1,
  12. pageSize: 10,
  13. hasMore: true, // 是否还有更多数据
  14. isLoading: false, // 防止多次触发
  15. types_ids: null, // 当前选中的分类 tag_id
  16. searchText: '', // 搜索内容
  17. },
  18. onShow() {
  19. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  20. this.getTabBar().setSelected(1); // 比如首页就是 0
  21. }
  22. // 获取tab数据
  23. this.getTabdata();
  24. },
  25. async onLoad(options) {
  26. const fileIDs = [
  27. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png',
  28. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/gouwuc_img.png'
  29. ];
  30. const fileList = await getTempFileURLs(fileIDs)
  31. this.setData({
  32. souimg: fileList[0].tempFileURL,
  33. gouwucimg: fileList[1].tempFileURL
  34. })
  35. // // 并发下载多个 fileID
  36. // Promise.all(
  37. // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  38. // ).then(results => {
  39. // // 每个 result 对应一个下载结果
  40. // const tempFilePaths = results.map(r => r.tempFilePath);
  41. // console.log('全部下载成功:', tempFilePaths);
  42. // this.setData({
  43. // souimg: tempFilePaths[0],
  44. // gouwucimg: tempFilePaths[1]
  45. // });
  46. // }).catch(err => {
  47. // console.error('有文件下载失败:', err);
  48. // });
  49. },
  50. // tab数据
  51. async getTabdata() {
  52. const models = await getModels()
  53. const { data } = await models.tab.list({
  54. filter: {
  55. where: {
  56. position: 4, // 显示位置
  57. },
  58. },
  59. envType: "prod",
  60. });
  61. // 返回查询到的数据
  62. const sortedRecords = data.records.sort((a, b) => {
  63. return a.sort - b.sort; // 升序排列
  64. });
  65. this.setData({
  66. categories: sortedRecords,
  67. goods: [],
  68. pageNumber: 1,
  69. hasMore: true,
  70. }, () => {
  71. this.getDatalist()
  72. })
  73. },
  74. onReachBottom() {
  75. // 上拉触底事件的处理函数
  76. this.loadMore();
  77. },
  78. loadMore() {
  79. // 加载更多数据的逻辑
  80. console.log('加载更多');
  81. // this.getDatalist(this.data.types_ids, true);
  82. if (this.data.isLoading || !this.data.hasMore) return;
  83. this.setData({ isLoading: true });
  84. this.getDatalist(this.data.types_ids, true);
  85. },
  86. //模糊搜搜
  87. onSearchInput(e) {
  88. const searchText = e.detail.value.trim();
  89. this.setData({
  90. searchText,
  91. goods: [],
  92. pageNumber: 1,
  93. hasMore: true
  94. }, () => {
  95. this.getDatalist(this.data.types_ids);
  96. });
  97. },
  98. // 点击搜搜
  99. onSearchConfirm() {
  100. this.setData({
  101. goods: [],
  102. pageNumber: 1,
  103. hasMore: true
  104. }, () => {
  105. this.getDatalist(this.data.types_ids);
  106. });
  107. },
  108. // 列表数据
  109. async getDatalist(tag_id = null, isLoadMore = false) {
  110. const models = await getModels()
  111. const _ = await getCommand()
  112. const userInfo = wx.getStorageSync('userInfo');
  113. const { pageNumber, pageSize, searchText } = this.data;
  114. const where = {
  115. school_id: _.in(userInfo.school_id)
  116. };
  117. if (tag_id) {
  118. where.tag_id = tag_id;
  119. }
  120. if (searchText) {
  121. where.name = {
  122. $regex_ci: searchText
  123. };
  124. }
  125. const { data } = await models.wx_merchandise.list({
  126. filter: { where },
  127. pageSize,
  128. pageNumber,
  129. getCount: true,
  130. envType: "prod",
  131. });
  132. const collectList = data.records || [];
  133. // 循环查 groupbuy 表
  134. for (let item of collectList) {
  135. try {
  136. const res = await models.wx_groupbuy_groupbuy.list({
  137. filter: { where: { groupbuy_id: item._id } },
  138. envType: "prod",
  139. });
  140. item.specList = res.data.records || [];
  141. } catch (err) {
  142. console.error(`获取 groupbuy 失败,商品ID: ${item._id}`, err);
  143. item.specList = [];
  144. }
  145. }
  146. // 批量获取商品图片临时 URL
  147. const fileIDs = collectList.map(item => item.img).filter(f => f)
  148. let tempFiles = []
  149. if (fileIDs.length > 0) {
  150. tempFiles = await getTempFileURLs(fileIDs)
  151. }
  152. // 将临时 URL 替换原 img
  153. const goodsList = collectList.map((item, index) => ({
  154. ...item,
  155. img: tempFiles[index]?.tempFileURL || item.img
  156. }))
  157. this.setData({
  158. goods: isLoadMore ? this.data.goods.concat(goodsList) : goodsList,
  159. pageNumber: pageNumber + 1,
  160. hasMore: goodsList.length === pageSize,
  161. isLoading: false
  162. });
  163. },
  164. // 点击tab分类 async
  165. tabcategories(e) {
  166. const types = e.currentTarget.dataset.type;
  167. const tagId = types.sort === 1 ? null : types._id;
  168. this.setData({
  169. categoriesindex: types.sort,
  170. types_ids: tagId,
  171. goods: [],
  172. pageNumber: 1,
  173. hasMore: true
  174. }, () => {
  175. this.getDatalist(tagId, false); // ✅ 这里也改成直接传 tagId
  176. });
  177. },
  178. navigateToDetail(e) {
  179. const index = e.currentTarget.dataset.index;
  180. const item = this.data.goods[index];
  181. wx.navigateTo({
  182. url: '/subpackages/productdetails/productdetails?data=' + encodeURIComponent(JSON.stringify(item))
  183. });
  184. }
  185. });