training.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // subpackages/training/training.js
  2. import { models, db } from '../../utils/cloudbase.js'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. categoriesindex: 1,
  9. categories: [],
  10. viewType: 'list',
  11. courseList: [],
  12. sou: '',
  13. show_1: '',
  14. xiazi: '',
  15. categorieslist: {},
  16. pageNumber: 1,
  17. pageSize: 10,
  18. hasMore: true, // 是否还有更多数据
  19. isLoading: false, // 防止多次触发
  20. searchText: '', // 搜索关键词
  21. },
  22. onLoad(options) {
  23. // 存储 options 到 this 中
  24. this.options = options;
  25. // 获取tab数据
  26. this.getTabdata();
  27. const fileIDs = [
  28. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png',
  29. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/show_1.png',
  30. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiazi.png',
  31. ];
  32. // 并发下载多个 fileID
  33. Promise.all(
  34. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  35. ).then(results => {
  36. // 每个 result 对应一个下载结果
  37. const tempFilePaths = results.map(r => r.tempFilePath);
  38. console.log('全部下载成功:', tempFilePaths);
  39. this.setData({
  40. sou: tempFilePaths[0],
  41. show_1: tempFilePaths[1],
  42. xiazi: tempFilePaths[2],
  43. });
  44. }).catch(err => {
  45. console.error('有文件下载失败:', err);
  46. });
  47. },
  48. // tab数据
  49. async getTabdata() {
  50. const { data } = await models.tab.list({
  51. filter: {
  52. where: {
  53. position: 1, // 显示位置
  54. // layout_type: 0, // 布局类型
  55. },
  56. },
  57. // envType: pre 体验环境, prod 正式环境
  58. envType: "prod",
  59. });
  60. // 返回查询到的数据
  61. // console.log(data);
  62. const sortedRecords = data.records.sort((a, b) => {
  63. return a.sort - b.sort; // 升序排列
  64. });
  65. this.setData({
  66. categories: sortedRecords
  67. }, () => {
  68. const type = Number(this.options.type) || 1;
  69. this.data.categories.forEach((item, index) => {
  70. if (item.sort === type) {
  71. this.setData({
  72. categorieslist: item,
  73. categoriesindex: type
  74. });
  75. if (this.data.categorieslist.layout_type === 1) {
  76. this.setData({ viewType: 'list' });
  77. } else {
  78. this.setData({ viewType: 'grid' });
  79. }
  80. // 获取列表数据
  81. this.getdatalist()
  82. }
  83. });
  84. })
  85. },
  86. onReachBottom() {
  87. // 上拉触底事件的处理函数
  88. this.loadMore();
  89. },
  90. loadMore() {
  91. // 加载更多数据的逻辑
  92. console.log('加载更多');
  93. // this.getdatalist(true);
  94. if (this.data.isLoading || !this.data.hasMore) return;
  95. this.setData({ isLoading: true });
  96. this.getdatalist(true);
  97. },
  98. // 模糊搜索
  99. onSearchInput(e) {
  100. const keyword = e.detail.value.trim();
  101. this.setData({
  102. searchText: keyword,
  103. pageNumber: 1,
  104. hasMore: true,
  105. goods: []
  106. }, () => {
  107. this.getdatalist(this.data.types_ids); // 重新加载
  108. });
  109. },
  110. // 列表数据
  111. async getdatalist(isLoadMore = false) {
  112. if (!this.data.categorieslist._id) return; // 防止 _id 为 undefined
  113. const { pageNumber, pageSize, searchText } = this.data;
  114. // 构造 where 条件
  115. const where = {
  116. tag_id: this.data.categorieslist._id,
  117. };
  118. // 模糊搜索关键字
  119. if (searchText) {
  120. where.name = {
  121. $regex_ci: searchText
  122. };
  123. }
  124. const { data } = await models.file_manage.list({
  125. filter: { where },
  126. pageSize,
  127. pageNumber,
  128. getCount: true, // 开启用来获取总数
  129. // envType: pre 体验环境, prod 正式环境
  130. envType: "prod",
  131. });
  132. const collectList = data.records || [];
  133. this.setData({
  134. courseList: isLoadMore ? this.data.courseList.concat(collectList) : collectList,
  135. pageNumber: pageNumber + 1,
  136. hasMore: collectList.length === pageSize,
  137. isLoading: false
  138. });
  139. },
  140. tabcategories(e) {
  141. const sort = Number(e.currentTarget.dataset.type); // 直接转换为数字
  142. const currentCategory = this.data.categories.find(item => item.sort === sort);
  143. if (!currentCategory) return;
  144. const viewType = currentCategory.layout_type === 1 ? 'list' : 'grid';
  145. this.setData({
  146. categoriesindex: sort,
  147. categorieslist: currentCategory,
  148. viewType,
  149. courseList: [], // 重置课程列表
  150. pageNumber: 1, // 重置分页
  151. hasMore: true,
  152. isLoading: false
  153. }, () => {
  154. this.getdatalist(); // 重新加载数据
  155. });
  156. },
  157. goToGoodsList(event) {
  158. // 获取绑定的数据
  159. const item = event.currentTarget.dataset.item;
  160. // 将数据转换为 JSON 字符串并传递
  161. const itemStr = encodeURIComponent(JSON.stringify(item));
  162. wx.navigateTo({
  163. url: `/subpackagestow/details/details?item=${itemStr}`
  164. });
  165. }
  166. })