details.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. // 转换云文件ID为临时链接
  29. if (item.url && item.url.length > 0) {
  30. wx.cloud.getTempFileURL({
  31. fileList: [item.url[0]],
  32. success: res => {
  33. if (res.fileList && res.fileList.length > 0) {
  34. this.setData({
  35. 'itemlist.playUrl': res.fileList[0].tempFileURL
  36. });
  37. }
  38. },
  39. fail: err => {
  40. console.error('获取临时链接失败', err);
  41. }
  42. });
  43. }
  44. // 收藏
  45. // this.getcollect()
  46. // 相关推荐
  47. this.getcourseList()
  48. });
  49. // 获取图片
  50. const fileIDs = [
  51. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/shouchang.png',
  52. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show_1.png',
  53. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiazi.png',
  54. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xia_1.png'
  55. ];
  56. // 并发下载多个 fileID
  57. Promise.all(
  58. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  59. ).then(results => {
  60. // 每个 result 对应一个下载结果
  61. const tempFilePaths = results.map(r => r.tempFilePath);
  62. console.log('全部下载成功:', tempFilePaths);
  63. this.setData({
  64. shouchang: tempFilePaths[0],
  65. show_1: tempFilePaths[1],
  66. xiazi: tempFilePaths[2],
  67. xia: tempFilePaths[3],
  68. shouchangs: tempFilePaths[4],
  69. });
  70. }).catch(err => {
  71. console.error('有文件下载失败:', err);
  72. });
  73. },
  74. // 相关课件推荐
  75. async getcourseList() {
  76. const { data } = await models.file_manage.list({
  77. filter: {
  78. where: {
  79. level: this.data.itemlist.level,
  80. dan: this.data.itemlist.dan,
  81. tag_id: this.data.itemlist.tag_id
  82. }
  83. },
  84. pageSize: 20, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  85. pageNumber: 1, // 第几页
  86. getCount: true, // 开启用来获取总数
  87. // envType: pre 体验环境, prod 正式环境
  88. envType: "prod",
  89. });
  90. // 返回查询到的数据列表 records 和 总数 total
  91. console.log(data, 'data');
  92. this.setData({
  93. courseList: data.records
  94. })
  95. },
  96. // 下载
  97. goToGoodsLists(event) {
  98. // 获取绑定的数据
  99. const item = event.currentTarget.dataset.item;
  100. // 将数据转换为 JSON 字符串并传递
  101. const itemStr = encodeURIComponent(JSON.stringify(item));
  102. wx.navigateTo({
  103. url: `/subpackages/down/down?item=${itemStr}`
  104. });
  105. },
  106. // 相关课件点击详情
  107. goToGoodsList(event) {
  108. // 获取绑定的数据
  109. const item = event.currentTarget.dataset.item;
  110. // 将数据转换为 JSON 字符串并传递
  111. const itemStr = encodeURIComponent(JSON.stringify(item));
  112. wx.navigateTo({
  113. url: `/subpackagestow/details/details?item=${itemStr}`
  114. });
  115. },
  116. // // 是否收藏
  117. // async getcollect() {
  118. // const { data } = await models.wx_collect.get({
  119. // filter: {
  120. // where: {
  121. // file_manager_id: this.data.itemlist._id
  122. // }
  123. // },
  124. // // envType: pre 体验环境, prod 正式环境
  125. // envType: "prod",
  126. // });
  127. // // 返回查询到的数据
  128. // console.log(data, '123321');
  129. // // 判断是否有值
  130. // if (data && Object.keys(data).length > 0) {
  131. // this.setData({
  132. // isCollected: true // 设置标志为 true
  133. // });
  134. // } else {
  135. // this.setData({
  136. // isCollected: false // 设置标志为 false
  137. // });
  138. // }
  139. // },
  140. // // 收藏
  141. // async goTocollect() {
  142. // if (this.data.isCollected) {
  143. // const { data } = await models.wx_collect.delete({
  144. // filter: {
  145. // where: {
  146. // file_manager_id: _.eq(this.data.itemlist._id), // 收藏文件id
  147. // }
  148. // },
  149. // // envType: pre 体验环境, prod 正式环境
  150. // envType: "prod",
  151. // });
  152. // // 返回删除成功的条数
  153. // console.log(data, '删除');
  154. // wx.showToast({ title: '取消收藏成功', icon: 'success' });
  155. // this.setData({
  156. // isCollected: false
  157. // })
  158. // } else {
  159. // const userInfo = wx.getStorageSync('userInfo');
  160. // const { data } = await models.wx_collect.create({
  161. // data: {
  162. // wx_user_id: userInfo._id, // 收藏人_id
  163. // file_manager_id: this.data.itemlist._id, // 收藏文件id
  164. // remark: "备注备注备注备注备注备注", // 备注
  165. // },
  166. // // envType: pre 体验环境, prod 正式环境
  167. // envType: "prod",
  168. // });
  169. // // 返回创建的数据 id
  170. // console.log(data);
  171. // wx.showToast({ title: '收藏成功', icon: 'success' });
  172. // this.setData({
  173. // isCollected: true // 设置标志为 true
  174. // });
  175. // }
  176. // },
  177. // 预览
  178. previewPDF() {
  179. wx.cloud.downloadFile({
  180. fileID: this.data.itemlist.url[0],
  181. success: res => {
  182. const filePath = res.tempFilePath;
  183. const extension = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
  184. console.log('文件后缀:', extension);
  185. // 根据文件类型处理
  186. if (['pdf', 'ppt', 'pptx'].includes(extension)) {
  187. wx.openDocument({
  188. filePath: filePath,
  189. fileType: extension,
  190. success: () => {
  191. console.log('文档打开成功');
  192. },
  193. fail: err => {
  194. console.error('文档打开失败', err);
  195. }
  196. });
  197. } else if (['mp3', 'aac', 'wav'].includes(extension)) {
  198. console.log('这是一个音频文件,可以跳转到播放页面或使用音频组件');
  199. this.toggleAudio()
  200. } else if (['mp4', 'mov'].includes(extension)) {
  201. console.log('这是一个视频文件,可以跳转到视频播放页');
  202. this.toggleVideo()
  203. } else {
  204. wx.showToast({
  205. title: '暂不支持该文件类型预览',
  206. icon: 'none'
  207. });
  208. }
  209. },
  210. fail: err => {
  211. console.error('文件下载失败', err);
  212. }
  213. });
  214. },
  215. // 视频播放还是暂停
  216. toggleVideo() {
  217. const videoContext = wx.createVideoContext('myVideo', this);
  218. if (this.data.isPlaying) {
  219. videoContext.pause();
  220. this.setData({ isPlaying: false });
  221. } else {
  222. videoContext.play();
  223. this.setData({ isPlaying: true });
  224. }
  225. },
  226. // 音频的播放暂停
  227. toggleAudio() {
  228. // 如果已经有临时播放链接,直接用它
  229. if (this.data.itemlist.playUrl) {
  230. this._playAudio(this.data.itemlist.playUrl);
  231. } else {
  232. // 否则判断 url 是否是 cloud:// 开头,需要转临时链接
  233. const cloudUrl = this.data.itemlist.url[0];
  234. if (cloudUrl && cloudUrl.startsWith('cloud://')) {
  235. wx.cloud.getTempFileURL({
  236. fileList: [cloudUrl],
  237. success: res => {
  238. if (res.fileList && res.fileList.length > 0) {
  239. const tempUrl = res.fileList[0].tempFileURL;
  240. this.setData({ 'itemlist.playUrl': tempUrl }, () => {
  241. this._playAudio(tempUrl);
  242. });
  243. }
  244. },
  245. fail: err => {
  246. wx.showToast({ title: '获取播放链接失败', icon: 'none' });
  247. console.error('获取临时链接失败', err);
  248. }
  249. });
  250. } else {
  251. // 不是 cloud:// 开头,直接播放原链接
  252. this._playAudio(cloudUrl);
  253. }
  254. }
  255. },
  256. _playAudio(url) {
  257. if (!this.audioContext) {
  258. this.audioContext = wx.createInnerAudioContext();
  259. this.audioContext.onPlay(() => this.setData({ isAudioPlaying: true }));
  260. this.audioContext.onPause(() => this.setData({ isAudioPlaying: false }));
  261. this.audioContext.onStop(() => this.setData({ isAudioPlaying: false }));
  262. this.audioContext.onEnded(() => this.setData({ isAudioPlaying: false }));
  263. this.audioContext.onError((res) => {
  264. wx.showToast({ title: '音频播放错误', icon: 'none' });
  265. this.setData({ isAudioPlaying: false });
  266. console.error('音频播放错误', res);
  267. });
  268. }
  269. if (this.data.isAudioPlaying) {
  270. this.audioContext.pause();
  271. } else {
  272. this.audioContext.src = url;
  273. this.audioContext.play();
  274. }
  275. },
  276. onUnload() {
  277. if (this.audioContext) {
  278. this.audioContext.stop();
  279. this.audioContext.destroy();
  280. this.audioContext = null;
  281. }
  282. },
  283. onHide() {
  284. if (this.audioContext) {
  285. this.audioContext.pause();
  286. }
  287. },
  288. })