show.js 5.2 KB

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