teaching.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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: Number(this.options.type), // 显示位置
  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. // 截取 name 中 "- " 后面的部分
  65. const processedRecords = sortedRecords.map(item => {
  66. let newName = item.name;
  67. if (newName.includes('-')) {
  68. // 按照 '-' 分割,并取第二段(去掉前后空格)
  69. newName = newName.split('-')[1].trim();
  70. }
  71. return {
  72. ...item,
  73. name: newName
  74. };
  75. });
  76. this.setData({
  77. categories: processedRecords
  78. }, () => {
  79. const type = 1;
  80. this.data.categories.forEach((item, index) => {
  81. if (item.sort === type) {
  82. this.setData({
  83. categorieslist: item,
  84. categoriesindex: type
  85. });
  86. if (this.data.categorieslist.layout_type === 1) {
  87. this.setData({ viewType: 'list' });
  88. } else {
  89. this.setData({ viewType: 'grid' });
  90. }
  91. // 获取列表数据
  92. this.getdatalist()
  93. }
  94. });
  95. })
  96. },
  97. onReachBottom() {
  98. // 上拉触底事件的处理函数
  99. this.loadMore();
  100. },
  101. loadMore() {
  102. // 加载更多数据的逻辑
  103. console.log('加载更多');
  104. // this.getdatalist(true);
  105. if (this.data.isLoading || !this.data.hasMore) return;
  106. this.setData({ isLoading: true });
  107. this.getdatalist(true);
  108. },
  109. // 模糊搜索
  110. onSearchInput(e) {
  111. const keyword = e.detail.value.trim();
  112. this.setData({
  113. searchText: keyword,
  114. pageNumber: 1,
  115. hasMore: true,
  116. goods: []
  117. }, () => {
  118. this.getdatalist(this.data.types_ids); // 重新加载
  119. });
  120. },
  121. // 列表数据
  122. async getdatalist(isLoadMore = false) {
  123. if (!this.data.categorieslist._id) return; // 防止 _id 为 undefined
  124. const { pageNumber, pageSize, searchText } = this.data;
  125. // 构造 where 条件
  126. const where = {
  127. tag_id: this.data.categorieslist._id,
  128. };
  129. // 模糊搜索关键字
  130. if (searchText) {
  131. where.name = {
  132. $regex_ci: searchText
  133. };
  134. }
  135. const { data } = await models.file_manage.list({
  136. filter: { where },
  137. pageSize,
  138. pageNumber,
  139. getCount: true,
  140. envType: "prod",
  141. });
  142. const collectList = data.records || [];
  143. this.setData({
  144. courseList: isLoadMore ? this.data.courseList.concat(collectList) : collectList,
  145. pageNumber: pageNumber + 1,
  146. hasMore: collectList.length === pageSize,
  147. isLoading: false
  148. });
  149. },
  150. tabcategories(e) {
  151. const sort = Number(e.currentTarget.dataset.type); // 直接转换为数字
  152. const currentCategory = this.data.categories.find(item => item.sort === sort);
  153. if (!currentCategory) return;
  154. const viewType = currentCategory.layout_type === 1 ? 'list' : 'grid';
  155. this.setData({
  156. categoriesindex: sort,
  157. categorieslist: currentCategory,
  158. viewType,
  159. courseList: [], // 重置课程列表
  160. pageNumber: 1, // 重置分页
  161. hasMore: true,
  162. isLoading: false
  163. }, () => {
  164. this.getdatalist(); // 重新加载数据
  165. });
  166. },
  167. goToGoodsList(event) {
  168. // 获取绑定的数据
  169. const item = event.currentTarget.dataset.item;
  170. // 将数据转换为 JSON 字符串并传递
  171. const itemStr = encodeURIComponent(JSON.stringify(item));
  172. wx.navigateTo({
  173. url: `/subpackagestow/details/details?item=${itemStr}`
  174. });
  175. }
  176. })