groupbuying.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. },
  15. onLoad(options) {
  16. // 获取tab数据
  17. this.getTabdata();
  18. const fileIDs = [
  19. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png',
  20. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/gouwuc_img.png'
  21. ];
  22. // 并发下载多个 fileID
  23. Promise.all(
  24. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  25. ).then(results => {
  26. // 每个 result 对应一个下载结果
  27. const tempFilePaths = results.map(r => r.tempFilePath);
  28. console.log('全部下载成功:', tempFilePaths);
  29. this.setData({
  30. souimg: tempFilePaths[0],
  31. gouwucimg: tempFilePaths[1]
  32. });
  33. }).catch(err => {
  34. console.error('有文件下载失败:', err);
  35. });
  36. },
  37. // tab数据
  38. async getTabdata() {
  39. const { data } = await models.tab.list({
  40. filter: {
  41. where: {
  42. position: 4, // 显示位置
  43. },
  44. },
  45. envType: "prod",
  46. });
  47. // 返回查询到的数据
  48. const sortedRecords = data.records.sort((a, b) => {
  49. return a.sort - b.sort; // 升序排列
  50. });
  51. this.setData({
  52. categories: sortedRecords,
  53. goods: [],
  54. pageNumber: 1,
  55. hasMore: true,
  56. }, () => {
  57. this.getDatalist()
  58. })
  59. },
  60. onReachBottom() {
  61. // 上拉触底事件的处理函数
  62. this.loadMore();
  63. },
  64. loadMore() {
  65. // 加载更多数据的逻辑
  66. console.log('加载更多');
  67. this.getDatalist(this.data.types_ids, true);
  68. },
  69. // 列表数据
  70. async getDatalist(tag_id = null, isLoadMore = false) {
  71. const userInfo = wx.getStorageSync('userInfo');
  72. // 构造 where 条件
  73. const where = {
  74. school_id: _.in(userInfo.school_id)
  75. };
  76. if (tag_id) {
  77. where.tag_id = tag_id; // 仅在 tag_id 有值时添加
  78. }
  79. const { pageNumber, pageSize } = this.data;
  80. const { data } = await models.wx_merchandise.list({
  81. filter: { where },
  82. pageSize,
  83. pageNumber,
  84. getCount: true,
  85. envType: "prod",
  86. });
  87. const collectList = data.records || [];
  88. this.setData({
  89. goods: isLoadMore ? this.data.goods.concat(collectList) : collectList, // ✅ 用 goods 拼接
  90. pageNumber: pageNumber + 1,
  91. hasMore: collectList.length === pageSize,
  92. isLoading: false
  93. });
  94. },
  95. // 点击tab分类 async
  96. tabcategories(e) {
  97. const types = e.currentTarget.dataset.type;
  98. const tagId = types.sort === 1 ? null : types._id;
  99. this.setData({
  100. categoriesindex: types.sort,
  101. types_ids: tagId,
  102. goods: [],
  103. pageNumber: 1,
  104. hasMore: true
  105. }, () => {
  106. this.getDatalist(tagId, false); // ✅ 这里也改成直接传 tagId
  107. });
  108. },
  109. navigateToDetail(e) {
  110. const index = e.currentTarget.dataset.index;
  111. const item = this.data.goods[index];
  112. wx.navigateTo({
  113. url: '/subpackages/productdetails/productdetails?data=' + encodeURIComponent(JSON.stringify(item))
  114. });
  115. }
  116. });