details.js 11 KB

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