details.js 12 KB

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