details.js 7.0 KB

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