123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- // subpackagestow/down/down.js
- import { models, db, _ } from '../../utils/cloudbase.js'
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- fuzhi: '',
- itemlist: {},
- itemtempFileURL: '',
- countdown: '00:05:00', // 倒计时文本
- countdownTime: 300, // 倒计时秒数(5分钟)
- timer: null // 定时器句柄
- },
- onLoad(options) {
- // 获取传递过来的数据
- const itemStr = decodeURIComponent(options.item);
- const item = JSON.parse(itemStr);
- // 设置到页面数据中
- this.setData({
- itemlist: item
- }, () => {
- wx.cloud.getTempFileURL({
- fileList: [
- {
- fileID: this.data.itemlist.url[0]
- }
- ],
- maxAge: 300 // 5分钟,单位秒
- })
- .then(res => {
- console.log(res.fileList, 'fileList');
- this.setData({
- itemtempFileURL: res.fileList[0].tempFileURL
- })
- this.startCountdown(); // 启动倒计时
- })
- .catch(err => {
- console.error(err);
- });
- });
- const fileIDs = [
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/fuzhi.png',
- ];
-
- // 并发下载多个 fileID
- Promise.all(
- fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
- ).then(results => {
- // 每个 result 对应一个下载结果
- const tempFilePaths = results.map(r => r.tempFilePath);
- console.log('全部下载成功:', tempFilePaths);
- this.setData({
- fuzhi: tempFilePaths[0],
- });
- }).catch(err => {
- console.error('有文件下载失败:', err);
- });
- },
- // 五分钟倒计时
- startCountdown() {
- const interval = setInterval(() => {
- let time = this.data.countdownTime;
- if (time <= 0) {
- clearInterval(interval);
- this.setData({
- countdown: '00:00:00',
- timer: null
- });
- return;
- }
-
- time--;
- const min = String(Math.floor(time / 60)).padStart(2, '0');
- const sec = String(time % 60).padStart(2, '0');
- const formatted = `00:${min}:${sec}`;
-
- this.setData({
- countdown: formatted,
- countdownTime: time,
- timer: interval
- });
- }, 1000);
- },
- // 分享到微信
- onShareAppMessage() {
- return {
- title: this.data.itemlist.name || '文件分享',
- path: `/subpackagestow/down/down?item=${encodeURIComponent(JSON.stringify(this.data.itemlist))}`,
- imageUrl: this.data.itemlist.cover, // 可选:自定义分享图
- }
- },
- handleShareTap() {
- wx.showToast({
- title: '请点击右上角“···”分享',
- icon: 'none',
- duration: 3000
- });
- this.getaddlishi()
- },
- // 保存链接地址
- handleCopyLink() {
- const link = this.data.itemtempFileURL;
- if (!link) {
- wx.showToast({
- title: '暂无可复制链接',
- icon: 'none'
- });
- return;
- }
- const that = this; // 保存页面上下文
-
- wx.setClipboardData({
- data: link,
- success() {
- that.getaddlishi()
- wx.showToast({
- title: '链接已复制',
- icon: 'success'
- });
- },
- fail() {
- wx.showToast({
- title: '复制失败',
- icon: 'none'
- });
- }
- });
- },
- // 下载
- handleDownloadFile() {
- const url = this.data.itemtempFileURL;
- if (!url) {
- wx.showToast({
- title: '文件链接不存在',
- icon: 'none'
- });
- return;
- }
-
- wx.showLoading({
- title: '下载中...',
- mask: true
- });
-
- wx.downloadFile({
- url: url,
- success: (res) => {
- wx.hideLoading();
-
- if (res.statusCode === 200) {
- const tempFilePath = res.tempFilePath;
- const fileExt = url.split('.').pop().toLowerCase();
- const that = this; // 保存页面上下文
- if (['pdf', 'ppt', 'pptx', 'doc', 'docx', 'xls', 'xlsx'].includes(fileExt)) {
- // 打开文档
- wx.openDocument({
- filePath: tempFilePath,
- showMenu: true,
- success() {
- console.log('打开文档成功');
- that.getaddlishi()
- },
- fail(err) {
- wx.showToast({
- title: '打开文档失败',
- icon: 'none'
- });
- console.error('文档打开失败', err);
- }
- });
-
- } else if (['mp4', 'mov', 'avi'].includes(fileExt)) {
- // 视频:先检查权限再保存
- wx.getSetting({
- success: (res) => {
- if (!res.authSetting['scope.writePhotosAlbum']) {
- wx.authorize({
- scope: 'scope.writePhotosAlbum',
- success: () => {
- this.saveVideo(tempFilePath);
- },
- fail: () => {
- wx.showModal({
- title: '提示',
- content: '保存视频需要开启“保存到相册”权限,请前往设置开启。',
- showCancel: true,
- success(result) {
- if (result.confirm) {
- wx.openSetting();
- }
- }
- });
- }
- });
- } else {
- this.saveVideo(tempFilePath);
- }
- }
- });
-
- } else if (['mp3', 'wav', 'aac'].includes(fileExt)) {
- // 音频保存到本地缓存
- wx.saveFile({
- tempFilePath,
- success(result) {
- wx.showToast({
- title: '音频已保存',
- icon: 'success'
- });
- that.getaddlishi()
- console.log('音频保存路径:', result.savedFilePath);
- },
- fail(err) {
- wx.showToast({
- title: '保存失败',
- icon: 'none'
- });
- console.error('音频保存失败:', err);
- }
- });
-
- } else {
- wx.showToast({
- title: '暂不支持的文件类型',
- icon: 'none'
- });
- }
-
- } else {
- wx.showToast({
- title: '下载失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- wx.hideLoading();
- wx.showToast({
- title: '下载失败',
- icon: 'none'
- });
- console.error('下载出错:', err);
- }
- });
- },
- saveVideo(filePath) {
- const that = this; // 保存页面上下文
- wx.saveVideoToPhotosAlbum({
- filePath,
- success() {
- wx.showToast({
- title: '视频已保存',
- icon: 'success'
- });
- that.getaddlishi()
- },
- fail(err) {
- wx.showToast({
- title: '保存失败',
- icon: 'none'
- });
- console.error('视频保存失败:', err);
- }
- });
- },
- // 新增下载历史
- async getaddlishi() {
- const userInfo = wx.getStorageSync('userInfo')
- const { data } = await models.parent_download_history.create({
- data: {
- wx_user_id: userInfo._id, // 用户id
- delete: 0, // 逻辑删除
- file_manage_id: this.data.itemlist._id, // 课件id
- },
- // envType: pre 体验环境, prod 正式环境
- envType: "prod",
- });
-
- // 返回更新成功的条数
- console.log(this.data.itemlist._id, 'this.data.itemlist._id');
- },
- // 清除定时器
- onUnload() {
- if (this.data.timer) {
- clearInterval(this.data.timer);
- this.setData({ timer: null });
- }
- }
- })
|