down.js 10 KB

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