groupbuying.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.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. 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. },
  75. // 模糊搜索
  76. onSearchInput(e) {
  77. const keyword = e.detail.value.trim();
  78. this.setData({
  79. searchText: keyword,
  80. pageNumber: 1,
  81. hasMore: true,
  82. goods: []
  83. }, () => {
  84. this.getDatalist(this.data.types_ids); // 重新加载
  85. });
  86. },
  87. // 列表数据
  88. async getDatalist(tag_id = null, isLoadMore = false) {
  89. const userInfo = wx.getStorageSync('userInfo');
  90. // 构造 where 条件
  91. const where = {
  92. school_id: _.in(userInfo.school_id)
  93. };
  94. if (tag_id) {
  95. where.tag_id = tag_id; // 仅在 tag_id 有值时添加
  96. }
  97. const { pageNumber, pageSize, searchText } = this.data;
  98. // 模糊搜索关键字
  99. if (searchText) {
  100. where.name = {
  101. $regex_ci: searchText
  102. };
  103. }
  104. const { data } = await models.wx_merchandise.list({
  105. filter: { where },
  106. pageSize,
  107. pageNumber,
  108. getCount: true,
  109. envType: "prod",
  110. });
  111. const collectList = data.records || [];
  112. this.setData({
  113. goods: isLoadMore ? this.data.goods.concat(collectList) : collectList, // ✅ 用 goods 拼接
  114. pageNumber: pageNumber + 1,
  115. hasMore: collectList.length === pageSize,
  116. isLoading: false
  117. });
  118. },
  119. // 点击tab分类 async
  120. tabcategories(e) {
  121. const types = e.currentTarget.dataset.type;
  122. const tagId = types.sort === 1 ? null : types._id;
  123. this.setData({
  124. categoriesindex: types.sort,
  125. types_ids: tagId,
  126. goods: [],
  127. pageNumber: 1,
  128. hasMore: true
  129. }, () => {
  130. this.getDatalist(tagId, false); // ✅ 这里也改成直接传 tagId
  131. });
  132. },
  133. navigateToDetail(e) {
  134. const index = e.currentTarget.dataset.index;
  135. const item = this.data.goods[index];
  136. wx.navigateTo({
  137. url: '/subpackages/productdetails/productdetails?data=' + encodeURIComponent(JSON.stringify(item))
  138. });
  139. }
  140. });