teaching.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // subpackages/teaching/teaching.js
  2. import { models, db } from '../../utils/cloudbase.js'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. categoriesindex: 1,
  9. categories: [],
  10. viewType: 'grid',
  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: 2, // 显示位置
  54. // layout_type: 0, // 布局类型
  55. },
  56. },
  57. // envType: pre 体验环境, prod 正式环境
  58. envType: "prod",
  59. });
  60. // 返回查询到的数据
  61. const sortedRecords = data.records.sort((a, b) => {
  62. return a.sort - b.sort; // 升序排列
  63. });
  64. this.setData({
  65. categories: sortedRecords
  66. }, () => {
  67. const type = Number(this.options.type) || 1;
  68. this.data.categories.forEach((item, index) => {
  69. if (item.sort === type) {
  70. this.setData({
  71. categorieslist: item,
  72. categoriesindex: type
  73. });
  74. if (this.data.categorieslist.layout_type === 1) {
  75. this.setData({ viewType: 'list' });
  76. } else {
  77. this.setData({ viewType: 'grid' });
  78. }
  79. // 获取列表数据
  80. this.getdatalist()
  81. }
  82. });
  83. })
  84. },
  85. onReachBottom() {
  86. // 上拉触底事件的处理函数
  87. this.loadMore();
  88. },
  89. loadMore() {
  90. // 加载更多数据的逻辑
  91. console.log('加载更多');
  92. // this.getdatalist(true);
  93. if (this.data.isLoading || !this.data.hasMore) return;
  94. this.setData({ isLoading: true });
  95. this.getdatalist(true);
  96. },
  97. // 模糊搜索
  98. onSearchInput(e) {
  99. const keyword = e.detail.value.trim();
  100. this.setData({
  101. searchText: keyword,
  102. pageNumber: 1,
  103. hasMore: true,
  104. goods: []
  105. }, () => {
  106. this.getdatalist(this.data.types_ids); // 重新加载
  107. });
  108. },
  109. // 列表数据
  110. async getdatalist(isLoadMore = false) {
  111. if (!this.data.categorieslist._id) return; // 防止 _id 为 undefined
  112. const { pageNumber, pageSize, searchText } = this.data;
  113. // 构造 where 条件
  114. const where = {
  115. tag_id: this.data.categorieslist._id,
  116. };
  117. // 模糊搜索关键字
  118. if (searchText) {
  119. where.name = {
  120. $regex_ci: searchText
  121. };
  122. }
  123. const { data } = await models.file_manage.list({
  124. filter: { where },
  125. pageSize,
  126. pageNumber,
  127. getCount: true,
  128. envType: "prod",
  129. });
  130. const collectList = data.records || [];
  131. this.setData({
  132. courseList: isLoadMore ? this.data.courseList.concat(collectList) : collectList,
  133. pageNumber: pageNumber + 1,
  134. hasMore: collectList.length === pageSize,
  135. isLoading: false
  136. });
  137. },
  138. tabcategories(e) {
  139. const sort = Number(e.currentTarget.dataset.type); // 直接转换为数字
  140. const currentCategory = this.data.categories.find(item => item.sort === sort);
  141. if (!currentCategory) return;
  142. const viewType = currentCategory.layout_type === 1 ? 'list' : 'grid';
  143. this.setData({
  144. categoriesindex: sort,
  145. categorieslist: currentCategory,
  146. viewType,
  147. courseList: [], // 重置课程列表
  148. pageNumber: 1, // 重置分页
  149. hasMore: true,
  150. isLoading: false
  151. }, () => {
  152. this.getdatalist(); // 重新加载数据
  153. });
  154. },
  155. goToGoodsList(event) {
  156. // 获取绑定的数据
  157. const item = event.currentTarget.dataset.item;
  158. // 将数据转换为 JSON 字符串并传递
  159. const itemStr = encodeURIComponent(JSON.stringify(item));
  160. wx.navigateTo({
  161. url: `/subpackagestow/details/details?item=${itemStr}`
  162. });
  163. }
  164. })