down.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // subpackagestow/down/down.js
  2. // import { models, db, _ } from '../../utils/cloudbase.js'
  3. import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. fuzhi: '',
  10. itemlist: {},
  11. itemtempFileURL: '',
  12. countdown: '00:05:00', // 倒计时文本
  13. countdownTime: 300, // 倒计时秒数(5分钟)
  14. timer: null // 定时器句柄
  15. },
  16. async onLoad(options) {
  17. // 获取传递过来的数据
  18. const itemStr = decodeURIComponent(options.item);
  19. const item = JSON.parse(itemStr);
  20. // 设置到页面数据中
  21. this.setData({
  22. itemlist: item
  23. }, async () => { // 这里直接加 async
  24. this.getimgsm()
  25. });
  26. const fileIDs = [
  27. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/fuzhi.png',
  28. ];
  29. const fileList = await getTempFileURLs(fileIDs)
  30. this.setData({
  31. fuzhi: fileList[0].tempFileURL,
  32. })
  33. // // 并发下载多个 fileID
  34. // Promise.all(
  35. // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  36. // ).then(results => {
  37. // // 每个 result 对应一个下载结果
  38. // const tempFilePaths = results.map(r => r.tempFilePath);
  39. // console.log('全部下载成功:', tempFilePaths);
  40. // this.setData({
  41. // fuzhi: tempFilePaths[0],
  42. // });
  43. // }).catch(err => {
  44. // console.error('有文件下载失败:', err);
  45. // });
  46. },
  47. async getimgsm() {
  48. try {
  49. const res = await getTempFileURLs([this.data.itemlist.url[0]]);
  50. console.log(res, 'fileList');
  51. this.setData({
  52. itemtempFileURL: res[0].fileID
  53. });
  54. console.log(this.data.itemtempFileURL,'itemtempFileURLitemtempFileURLitemtempFileURL');
  55. this.startCountdown(); // 启动倒计时
  56. } catch (err) {
  57. console.error('获取临时链接失败', err);
  58. }
  59. },
  60. // 五分钟倒计时
  61. startCountdown() {
  62. const interval = setInterval(() => {
  63. let time = this.data.countdownTime;
  64. if (time <= 0) {
  65. clearInterval(interval);
  66. this.setData({
  67. countdown: '00:00:00',
  68. timer: null
  69. });
  70. return;
  71. }
  72. time--;
  73. const min = String(Math.floor(time / 60)).padStart(2, '0');
  74. const sec = String(time % 60).padStart(2, '0');
  75. const formatted = `00:${min}:${sec}`;
  76. this.setData({
  77. countdown: formatted,
  78. countdownTime: time,
  79. timer: interval
  80. });
  81. }, 1000);
  82. },
  83. // 分享到微信
  84. onShareAppMessage() {
  85. return {
  86. title: this.data.itemlist.name || '文件分享',
  87. path: `/subpackagestow/down/down?item=${encodeURIComponent(JSON.stringify(this.data.itemlist))}`,
  88. imageUrl: this.data.itemlist.cover, // 可选:自定义分享图
  89. }
  90. },
  91. handleShareTap() {
  92. wx.showToast({
  93. title: '请点击右上角“···”分享',
  94. icon: 'none',
  95. duration: 3000
  96. });
  97. this.getaddlishi()
  98. },
  99. // 保存链接地址
  100. handleCopyLink() {
  101. const link = this.data.itemtempFileURL;
  102. if (!link) {
  103. wx.showToast({
  104. title: '暂无可复制链接',
  105. icon: 'none'
  106. });
  107. return;
  108. }
  109. const that = this; // 保存页面上下文
  110. wx.setClipboardData({
  111. data: link,
  112. success() {
  113. that.getaddlishi()
  114. wx.showToast({
  115. title: '链接已复制',
  116. icon: 'success'
  117. });
  118. },
  119. fail() {
  120. wx.showToast({
  121. title: '复制失败',
  122. icon: 'none'
  123. });
  124. }
  125. });
  126. },
  127. // 下载
  128. handleDownloadFile() {
  129. const link = this.data.itemtempFileURL;
  130. if (!link) {
  131. wx.showToast({
  132. title: '下载链接为空',
  133. icon: 'none'
  134. });
  135. return;
  136. }
  137. // 复制到剪贴板
  138. wx.setClipboardData({
  139. data: link,
  140. success: () => {
  141. wx.showModal({
  142. title: '提示',
  143. content: '下载链接已复制,请到浏览器粘贴并下载',
  144. showCancel: false, // 不显示取消按钮
  145. confirmText: '我知道了',
  146. success(res) {
  147. if (res.confirm) {
  148. console.log('用户点击确定,弹框关闭');
  149. }
  150. }
  151. });
  152. }
  153. });
  154. // const url = this.data.itemtempFileURL;
  155. // if (!url) {
  156. // wx.showToast({
  157. // title: '文件链接不存在',
  158. // icon: 'none'
  159. // });
  160. // return;
  161. // }
  162. // wx.showLoading({
  163. // title: '下载中...',
  164. // mask: true
  165. // });
  166. // wx.downloadFile({
  167. // url: url,
  168. // success: (res) => {
  169. // wx.hideLoading();
  170. // if (res.statusCode === 200) {
  171. // const tempFilePath = res.tempFilePath;
  172. // const fileExt = url.split('.').pop().toLowerCase();
  173. // const that = this; // 保存页面上下文
  174. // if (['pdf', 'ppt', 'pptx', 'doc', 'docx', 'xls', 'xlsx'].includes(fileExt)) {
  175. // // 打开文档
  176. // wx.openDocument({
  177. // filePath: tempFilePath,
  178. // showMenu: true,
  179. // success() {
  180. // console.log('打开文档成功');
  181. // that.getaddlishi()
  182. // },
  183. // fail(err) {
  184. // wx.showToast({
  185. // title: '打开文档失败',
  186. // icon: 'none'
  187. // });
  188. // console.error('文档打开失败', err);
  189. // }
  190. // });
  191. // } else if (['mp4', 'mov', 'avi'].includes(fileExt)) {
  192. // // 视频:先检查权限再保存
  193. // wx.getSetting({
  194. // success: (res) => {
  195. // if (!res.authSetting['scope.writePhotosAlbum']) {
  196. // wx.authorize({
  197. // scope: 'scope.writePhotosAlbum',
  198. // success: () => {
  199. // this.saveVideo(tempFilePath);
  200. // },
  201. // fail: () => {
  202. // wx.showModal({
  203. // title: '提示',
  204. // content: '保存视频需要开启“保存到相册”权限,请前往设置开启。',
  205. // showCancel: true,
  206. // success(result) {
  207. // if (result.confirm) {
  208. // wx.openSetting();
  209. // }
  210. // }
  211. // });
  212. // }
  213. // });
  214. // } else {
  215. // this.saveVideo(tempFilePath);
  216. // }
  217. // }
  218. // });
  219. // } else if (['mp3', 'wav', 'aac'].includes(fileExt)) {
  220. // // 音频保存到本地缓存
  221. // wx.saveFile({
  222. // tempFilePath,
  223. // success(result) {
  224. // wx.showToast({
  225. // title: '音频已保存',
  226. // icon: 'success'
  227. // });
  228. // that.getaddlishi()
  229. // console.log('音频保存路径:', result.savedFilePath);
  230. // },
  231. // fail(err) {
  232. // wx.showToast({
  233. // title: '保存失败',
  234. // icon: 'none'
  235. // });
  236. // console.error('音频保存失败:', err);
  237. // }
  238. // });
  239. // } else {
  240. // wx.showToast({
  241. // title: '暂不支持的文件类型',
  242. // icon: 'none'
  243. // });
  244. // }
  245. // } else {
  246. // wx.showToast({
  247. // title: '下载失败',
  248. // icon: 'none'
  249. // });
  250. // }
  251. // },
  252. // fail: (err) => {
  253. // wx.hideLoading();
  254. // wx.showToast({
  255. // title: '下载失败',
  256. // icon: 'none'
  257. // });
  258. // console.error('下载出错:', err);
  259. // }
  260. // });
  261. },
  262. // saveVideo(filePath) {
  263. // const that = this; // 保存页面上下文
  264. // wx.saveVideoToPhotosAlbum({
  265. // filePath,
  266. // success() {
  267. // wx.showToast({
  268. // title: '视频已保存',
  269. // icon: 'success'
  270. // });
  271. // that.getaddlishi()
  272. // },
  273. // fail(err) {
  274. // wx.showToast({
  275. // title: '保存失败',
  276. // icon: 'none'
  277. // });
  278. // console.error('视频保存失败:', err);
  279. // }
  280. // });
  281. // },
  282. // 新增下载历史
  283. async getaddlishi() {
  284. const models = await getModels()
  285. const userInfo = wx.getStorageSync('userInfo')
  286. const { data } = await models.parent_download_history.create({
  287. data: {
  288. wx_user_id: userInfo._id, // 用户id
  289. delete: 0, // 逻辑删除
  290. file_manage_id: this.data.itemlist._id, // 课件id
  291. },
  292. // envType: pre 体验环境, prod 正式环境
  293. envType: "prod",
  294. });
  295. // 返回更新成功的条数
  296. console.log(this.data.itemlist._id, 'this.data.itemlist._id');
  297. },
  298. // 清除定时器
  299. onUnload() {
  300. if (this.data.timer) {
  301. clearInterval(this.data.timer);
  302. this.setData({ timer: null });
  303. }
  304. }
  305. })