show.js 5.7 KB

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