course.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // subpackages/course/course.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. // { id: 1, title: '趣味识字 – 动物世界', type: 'PDF', grade: '小班', subject: '语文', views: 128 },
  13. // { id: 2, title: '数字王国探险记', type: '视频', grade: '中班', subject: '数学', views: 128 },
  14. // { id: 3, title: '儿歌大全 – 春天在哪里', type: '音频', grade: '大班', subject: '音乐', views: 128 },
  15. // { id: 4, title: '创意绘画 – 我的小房子', type: 'PPT', grade: '中班', subject: '美术', views: 128 }
  16. ],
  17. sou: '',
  18. show_1: '',
  19. xiazi: '',
  20. goods_9: '',
  21. categorieslist: {}
  22. },
  23. onLoad(options) {
  24. // 存储 options 到 this 中
  25. this.options = options;
  26. // 获取tab数据
  27. this.getTabdata();
  28. // 获取图片
  29. const fileIDs = [
  30. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png',
  31. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show_1.png',
  32. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiazi.png',
  33. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/goods/goods_9.jpg'
  34. ];
  35. // 并发下载多个 fileID
  36. Promise.all(
  37. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  38. ).then(results => {
  39. // 每个 result 对应一个下载结果
  40. const tempFilePaths = results.map(r => r.tempFilePath);
  41. console.log('全部下载成功:', tempFilePaths);
  42. this.setData({
  43. sou: tempFilePaths[0],
  44. show_1: tempFilePaths[1],
  45. xiazi: tempFilePaths[2],
  46. goods_9: tempFilePaths[3],
  47. });
  48. }).catch(err => {
  49. console.error('有文件下载失败:', err);
  50. });
  51. },
  52. // tab数据 async
  53. async getTabdata() {
  54. const { data } = await models.tab.list({
  55. filter: {
  56. where: {
  57. position: 0, // 显示位置
  58. // layout_type: 0, // 布局类型
  59. },
  60. },
  61. // envType: pre 体验环境, prod 正式环境
  62. envType: "prod",
  63. });
  64. // 返回查询到的数据
  65. // console.log(data);
  66. const sortedRecords = data.records.sort((a, b) => {
  67. return a.sort - b.sort; // 升序排列
  68. });
  69. this.setData({
  70. categories: sortedRecords
  71. }, () => {
  72. // 在 setData 回调中执行逻辑,确保 categories 已经更新
  73. const type = Number(this.options.type) || 1;
  74. this.data.categories.forEach((item, index) => {
  75. if (item.sort === type) {
  76. this.setData({
  77. categorieslist: item,
  78. categoriesindex: type
  79. });
  80. // 获取列表数据
  81. this.getdatalist()
  82. }
  83. });
  84. })
  85. },
  86. // 列表数据
  87. async getdatalist() {
  88. console.log(this.data.categorieslist, '123123');
  89. const { data } = await models.file_manage.list({
  90. filter: {
  91. where: {
  92. tag_id: this.data.categorieslist._id,
  93. }
  94. },
  95. pageSize: 10, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  96. pageNumber: 1, // 第几页
  97. getCount: true, // 开启用来获取总数
  98. // envType: pre 体验环境, prod 正式环境
  99. envType: "prod",
  100. });
  101. // 返回查询到的数据列表 records 和 总数 total
  102. console.log(data, '123123');
  103. this.setData({
  104. courseList: data.records
  105. })
  106. },
  107. // async
  108. tabcategories(e) {
  109. const types = e.currentTarget.dataset.type;
  110. // if (type === 2) {
  111. // this.setData({ viewType: 'list' });
  112. // } else {
  113. // this.setData({ viewType: 'grid' });
  114. // }
  115. this.setData({
  116. categoriesindex: types
  117. }, () => {
  118. // 在 setData 回调中执行逻辑,确保 categories 已经更新
  119. const type = Number(types) || 1;
  120. this.data.categories.forEach((item, index) => {
  121. if (item.sort === type) {
  122. this.setData({
  123. categorieslist: item,
  124. categoriesindex: type
  125. });
  126. // 获取列表数据
  127. this.getdatalist()
  128. }
  129. });
  130. });
  131. // const { data } = await models.file_manage.create({
  132. // data: {
  133. // level: 1, // 级别
  134. // range: ['中班'], // 适用范围
  135. // remark: "备注文本字段", // 备注
  136. // type: 3, // 文件类型 0-视频 1-音频 2-pdf 3-ppt 4-图文
  137. // url: ['cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/newfile.ppt'], // 路径
  138. // download_count: 1, // 下载人数
  139. // dan: 0, // 段位
  140. // cover: "cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/未命名的设计.png", // 封面
  141. // size: "123.12KB", // 文件大小
  142. // name: "PPT-文件名33", // 文件名
  143. // tag_id: "BT6QJSE5RS", // 标签id
  144. // describe: "描述文本字段", // 描述
  145. // },
  146. // // envType: pre 体验环境, prod 正式环境
  147. // envType: "prod",
  148. // });
  149. // // 返回创建的数据 id
  150. // console.log(data, '234234');
  151. },
  152. goToGoodsList(event) {
  153. // 获取绑定的数据
  154. const item = event.currentTarget.dataset.item;
  155. // 将数据转换为 JSON 字符串并传递
  156. const itemStr = encodeURIComponent(JSON.stringify(item));
  157. wx.navigateTo({
  158. url: `/subpackagestow/details/details?item=${itemStr}`
  159. });
  160. }
  161. })