details.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // subpackagestow/details/details.js
  2. import { models, db, _ } from '../../utils/cloudbase.js'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. fileType: 'video',
  9. viewType: 'grid',
  10. courseList: [],
  11. shouchang: '',
  12. show_1: '',
  13. xiazi: '',
  14. xia: '',
  15. itemlist: {},
  16. // isCollected: false,
  17. isPlaying: false,
  18. isAudioPlaying: false,
  19. },
  20. onLoad(options) {
  21. // 获取传递过来的数据
  22. const itemStr = decodeURIComponent(options.item);
  23. const item = JSON.parse(itemStr);
  24. // 设置到页面数据中
  25. this.setData({
  26. itemlist: item
  27. }, () => {
  28. // 收藏
  29. // this.getcollect()
  30. // 相关推荐
  31. this.getcourseList()
  32. });
  33. // 获取图片
  34. const fileIDs = [
  35. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/shouchang.png',
  36. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show_1.png',
  37. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiazi.png',
  38. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xia_1.png'
  39. ];
  40. // 并发下载多个 fileID
  41. Promise.all(
  42. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  43. ).then(results => {
  44. // 每个 result 对应一个下载结果
  45. const tempFilePaths = results.map(r => r.tempFilePath);
  46. console.log('全部下载成功:', tempFilePaths);
  47. this.setData({
  48. shouchang: tempFilePaths[0],
  49. show_1: tempFilePaths[1],
  50. xiazi: tempFilePaths[2],
  51. xia: tempFilePaths[3],
  52. shouchangs: tempFilePaths[4],
  53. });
  54. }).catch(err => {
  55. console.error('有文件下载失败:', err);
  56. });
  57. },
  58. // 相关课件推荐
  59. async getcourseList() {
  60. const { data } = await models.file_manage.list({
  61. filter: {
  62. where: {
  63. level: this.data.itemlist.level,
  64. dan: this.data.itemlist.dan,
  65. tag_id: this.data.itemlist.tag_id
  66. }
  67. },
  68. pageSize: 20, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  69. pageNumber: 1, // 第几页
  70. getCount: true, // 开启用来获取总数
  71. // envType: pre 体验环境, prod 正式环境
  72. envType: "prod",
  73. });
  74. // 返回查询到的数据列表 records 和 总数 total
  75. console.log(data, 'data');
  76. this.setData({
  77. courseList: data.records
  78. })
  79. },
  80. // 下载
  81. goToGoodsLists(event) {
  82. // 获取绑定的数据
  83. const item = event.currentTarget.dataset.item;
  84. // 将数据转换为 JSON 字符串并传递
  85. const itemStr = encodeURIComponent(JSON.stringify(item));
  86. wx.navigateTo({
  87. url: `/subpackages/down/down?item=${itemStr}`
  88. });
  89. },
  90. // 相关课件点击详情
  91. goToGoodsList(event) {
  92. // 获取绑定的数据
  93. const item = event.currentTarget.dataset.item;
  94. // 将数据转换为 JSON 字符串并传递
  95. const itemStr = encodeURIComponent(JSON.stringify(item));
  96. wx.navigateTo({
  97. url: `/subpackagestow/details/details?item=${itemStr}`
  98. });
  99. },
  100. // // 是否收藏
  101. // async getcollect() {
  102. // const { data } = await models.wx_collect.get({
  103. // filter: {
  104. // where: {
  105. // file_manager_id: this.data.itemlist._id
  106. // }
  107. // },
  108. // // envType: pre 体验环境, prod 正式环境
  109. // envType: "prod",
  110. // });
  111. // // 返回查询到的数据
  112. // console.log(data, '123321');
  113. // // 判断是否有值
  114. // if (data && Object.keys(data).length > 0) {
  115. // this.setData({
  116. // isCollected: true // 设置标志为 true
  117. // });
  118. // } else {
  119. // this.setData({
  120. // isCollected: false // 设置标志为 false
  121. // });
  122. // }
  123. // },
  124. // // 收藏
  125. // async goTocollect() {
  126. // if (this.data.isCollected) {
  127. // const { data } = await models.wx_collect.delete({
  128. // filter: {
  129. // where: {
  130. // file_manager_id: _.eq(this.data.itemlist._id), // 收藏文件id
  131. // }
  132. // },
  133. // // envType: pre 体验环境, prod 正式环境
  134. // envType: "prod",
  135. // });
  136. // // 返回删除成功的条数
  137. // console.log(data, '删除');
  138. // wx.showToast({ title: '取消收藏成功', icon: 'success' });
  139. // this.setData({
  140. // isCollected: false
  141. // })
  142. // } else {
  143. // const userInfo = wx.getStorageSync('userInfo');
  144. // const { data } = await models.wx_collect.create({
  145. // data: {
  146. // wx_user_id: userInfo._id, // 收藏人_id
  147. // file_manager_id: this.data.itemlist._id, // 收藏文件id
  148. // remark: "备注备注备注备注备注备注", // 备注
  149. // },
  150. // // envType: pre 体验环境, prod 正式环境
  151. // envType: "prod",
  152. // });
  153. // // 返回创建的数据 id
  154. // console.log(data);
  155. // wx.showToast({ title: '收藏成功', icon: 'success' });
  156. // this.setData({
  157. // isCollected: true // 设置标志为 true
  158. // });
  159. // }
  160. // },
  161. // 预览
  162. previewPDF() {
  163. wx.cloud.downloadFile({
  164. fileID: this.data.itemlist.url[0],
  165. success: res => {
  166. const filePath = res.tempFilePath;
  167. const extension = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
  168. console.log('文件后缀:', extension);
  169. // 根据文件类型处理
  170. if (['pdf', 'ppt', 'pptx'].includes(extension)) {
  171. wx.openDocument({
  172. filePath: filePath,
  173. fileType: extension,
  174. success: () => {
  175. console.log('文档打开成功');
  176. },
  177. fail: err => {
  178. console.error('文档打开失败', err);
  179. }
  180. });
  181. } else if (['mp3', 'aac', 'wav'].includes(extension)) {
  182. console.log('这是一个音频文件,可以跳转到播放页面或使用音频组件');
  183. // this.toggleAudio()
  184. } else if (['mp4', 'mov'].includes(extension)) {
  185. console.log('这是一个视频文件,可以跳转到视频播放页');
  186. this.toggleVideo()
  187. } else {
  188. wx.showToast({
  189. title: '暂不支持该文件类型预览',
  190. icon: 'none'
  191. });
  192. }
  193. },
  194. fail: err => {
  195. console.error('文件下载失败', err);
  196. }
  197. });
  198. },
  199. // 视频播放还是暂停
  200. toggleVideo() {
  201. const videoContext = wx.createVideoContext('myVideo', this);
  202. if (this.data.isPlaying) {
  203. videoContext.pause();
  204. this.setData({ isPlaying: false });
  205. } else {
  206. videoContext.play();
  207. this.setData({ isPlaying: true });
  208. }
  209. },
  210. // 音频的播放暂停
  211. toggleAudio() {
  212. const audioContext = wx.createAudioContext('myAudio');
  213. if (this.data.isAudioPlaying) {
  214. audioContext.pause();
  215. this.setData({ isAudioPlaying: false });
  216. } else {
  217. audioContext.play();
  218. this.setData({ isAudioPlaying: true });
  219. }
  220. }
  221. })