dahome.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // subpackages/dahome/dahome.js
  2. // import { models, db, _ } from '../../utils/cloudbase.js'
  3. import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
  4. Page({
  5. data: {
  6. levelOptions: ['初级', '中级', '高级'],
  7. selectedLevelIndex: 0,
  8. typeOptions: ['1', '2', '3'],
  9. selectedTypeIndex: 0,
  10. items: [],
  11. souimg: '',
  12. xiaziimg: '',
  13. showimg: '',
  14. xialaimg: '',
  15. pageNumber: 1,
  16. pageSize: 10,
  17. hasMore: true, // 是否还有更多数据
  18. isLoading: false, // 防止多次触发
  19. searchText: '', // 搜索关键词
  20. inputFocus: false,
  21. // goods_9: '',
  22. },
  23. async onLoad(options) {
  24. // 存储 options 到 this 中
  25. this.options = options;
  26. if (options.autofocus === '1') {
  27. // 延迟设置 focus,让页面完成渲染
  28. setTimeout(() => {
  29. this.setData({
  30. inputFocus: true
  31. });
  32. }, 300); // 300ms 通常足够
  33. }
  34. // 列表数据
  35. this.getdatalist()
  36. // 获取图片
  37. const fileIDs = [
  38. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png',
  39. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiazi.png',
  40. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/show_1.png',
  41. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiala.png',
  42. ];
  43. const fileList = await getTempFileURLs(fileIDs)
  44. this.setData({
  45. homepage_8: fileList[0].tempFileURL,
  46. homepage_9: fileList[1].tempFileURL,
  47. souimg: fileList[2].tempFileURL,
  48. souimg: fileList[0].tempFileURL,
  49. xiaziimg: fileList[1].tempFileURL,
  50. showimg: fileList[2].tempFileURL,
  51. xialaimg: fileList[3].tempFileURL,
  52. // goods_9: fileList[4].tempFileURL,
  53. })
  54. // // 并发下载多个 fileID
  55. // Promise.all(
  56. // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  57. // ).then(results => {
  58. // // 每个 result 对应一个下载结果
  59. // const tempFilePaths = results.map(r => r.tempFilePath);
  60. // console.log('全部下载成功:', tempFilePaths);
  61. // this.setData({
  62. // souimg: tempFilePaths[0],
  63. // xiaziimg: tempFilePaths[1],
  64. // showimg: tempFilePaths[2],
  65. // xialaimg: tempFilePaths[3],
  66. // // goods_9: tempFilePaths[4],
  67. // });
  68. // }).catch(err => {
  69. // console.error('有文件下载失败:', err);
  70. // });
  71. },
  72. onReachBottom() {
  73. // 上拉触底事件的处理函数
  74. this.loadMore();
  75. },
  76. loadMore() {
  77. // 加载更多数据的逻辑
  78. console.log('加载更多');
  79. // this.getdatalist(true);
  80. if (this.data.isLoading || !this.data.hasMore) return;
  81. this.setData({ isLoading: true });
  82. this.getdatalist(true);
  83. },
  84. // 输入即搜索
  85. onSearchInput(e) {
  86. const searchText = e.detail.value;
  87. this.setData({
  88. searchText,
  89. pageNumber: 1,
  90. items: [],
  91. hasMore: true,
  92. isLoading: false
  93. });
  94. this.getdatalist(false);
  95. },
  96. // 列表数据
  97. async getdatalist(isLoadMore = false) {
  98. const models = await getModels()
  99. const { pageNumber, pageSize, selectedLevelIndex, selectedTypeIndex, searchText } = this.data;
  100. // 构造过滤条件
  101. const whereFilter = {
  102. tag_id: 'C05HDKNBDS',
  103. level: Number(selectedTypeIndex) + 1,
  104. dan: Number(selectedLevelIndex),
  105. };
  106. // 添加模糊搜索
  107. if (searchText.trim()) {
  108. whereFilter.name = {
  109. $regex_ci: searchText
  110. };
  111. }
  112. try {
  113. const { data } = await models.file_manage.list({
  114. filter: {
  115. where: whereFilter
  116. },
  117. pageSize,
  118. pageNumber,
  119. getCount: true,
  120. envType: "prod",
  121. });
  122. let collectList = data.records || []; // 注意用 let,不是 const
  123. // 处理云路径图片
  124. const coverFileIDs = collectList
  125. .map(item => item.cover)
  126. .filter(Boolean);
  127. let newList = collectList;
  128. if (coverFileIDs.length > 0) {
  129. const tempFiles = await getTempFileURLs(coverFileIDs);
  130. newList = collectList.map((item, index) => ({
  131. ...item,
  132. cover: tempFiles[index]?.tempFileURL || item.cover
  133. }));
  134. }
  135. this.setData({
  136. items: isLoadMore ? this.data.items.concat(newList) : newList,
  137. pageNumber: pageNumber + 1,
  138. hasMore: collectList.length === pageSize,
  139. isLoading: false
  140. });
  141. } catch (err) {
  142. console.error('数据获取失败:', err);
  143. }
  144. },
  145. handleLevelChange(e) {
  146. const index = Number(e.detail.value);
  147. this.setData({
  148. selectedLevelIndex: index,
  149. pageNumber: 1, // 重置分页,从第一页查
  150. hasMore: true,
  151. isLoading: false,
  152. items: [] // 可选:清空旧数据避免闪烁
  153. });
  154. this.getdatalist(false)
  155. },
  156. handleTypeChange(e) {
  157. const index = Number(e.detail.value);
  158. this.setData({
  159. selectedTypeIndex: index,
  160. pageNumber: 1, // 重置分页,从第一页查
  161. hasMore: true,
  162. isLoading: false,
  163. items: [] // 可选:清空旧数据避免闪烁
  164. });
  165. this.getdatalist(false)
  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: `/subpackages/details/details?item=${itemStr}`
  174. });
  175. }
  176. // // 新增数据 async
  177. // async dianji() {
  178. // const { data } = await models.file_manage.create({
  179. // data: {
  180. // level: 1, // 级别
  181. // range: ['大班'], // 适用范围
  182. // remark: "文本", // 备注
  183. // type: 0, // 文件类型
  184. // url: ['cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/123123.mp4'], // 路径
  185. // download_count: 1, // 下载人数
  186. // dan: 1, // 段位
  187. // cover: "cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/未命名的设计.png", // 封面
  188. // size: "123.12KB", // 文件大小
  189. // name: "动手动脑图库5", // 文件名
  190. // tag_id: "BULYF5VJ9W", // 标签id
  191. // publisher: "李老师", // 发布者
  192. // describe: "文本", // 描述
  193. // },
  194. // // envType: pre 体验环境, prod 正式环境
  195. // envType: "prod",
  196. // });
  197. // // 返回创建的数据 id
  198. // console.log(data);
  199. // },
  200. })