groupbuying.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. this.setData({
  122. goods: isLoadMore ? this.data.goods.concat(collectList) : collectList,
  123. pageNumber: pageNumber + 1,
  124. hasMore: collectList.length === pageSize,
  125. isLoading: false
  126. });
  127. },
  128. // 点击tab分类 async
  129. tabcategories(e) {
  130. const types = e.currentTarget.dataset.type;
  131. const tagId = types.sort === 1 ? null : types._id;
  132. this.setData({
  133. categoriesindex: types.sort,
  134. types_ids: tagId,
  135. goods: [],
  136. pageNumber: 1,
  137. hasMore: true
  138. }, () => {
  139. this.getDatalist(tagId, false); // ✅ 这里也改成直接传 tagId
  140. });
  141. },
  142. navigateToDetail(e) {
  143. const index = e.currentTarget.dataset.index;
  144. const item = this.data.goods[index];
  145. wx.navigateTo({
  146. url: '/subpackages/productdetails/productdetails?data=' + encodeURIComponent(JSON.stringify(item))
  147. });
  148. }
  149. });