// miniprogram/pages/marvellous/index.js Page({ imgList: [], downCount: 0, /** * 页面的初始数据 */ data: { pageList: [], select: { count: 0 }, pageType: "", showSelect: false, showVideo: "", showAnVideo: true }, /** * 生命周期函数--监听页面加载 */ onLoad: async function (options) { wx.setNavigationBarTitle({ title: options.title || "精彩瞬间" }) const db = wx.cloud.database(); const _ = db.command; const $ = db.command.aggregate this.imgList = []; let pageList = {}; wx.showLoading(); let list = await db.collection('data_asset').where({ type: _.eq(options.type || "img") }).get(); wx.hideLoading(); if (!list.data.length) return wx.showToast({ title: '暂无数据', icon: "none" }) for (let i = 0; i < list.data.length; i++) { const v = list.data[i]; v.index = i; pageList[v.creat_time] ? pageList[v.creat_time].list.push(v) : pageList[v.creat_time] = { list: [v], time: this.format(v.creat_time) }; this.imgList.push(v.url); } console.log(pageList) this.setData({ pageList: pageList, pageType: options.type || "img" }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, showImg(e) { if (this.data.showSelect) return this.btnSelect(e); if(this.data.pageType === "video") return this.showVideo(e); if(this.data.pageType === "img") return this.showImage(e); }, showVideo:function(e){ this.setData({ showVideo: this.imgList[e.currentTarget.dataset.index] }) }, showImage:function(e){ wx.previewImage({ urls: this.imgList, current: this.imgList[e.currentTarget.dataset.index], }) }, longtap: function (e) { this.setData({ showSelect: true }) }, longClose: function (e) { let pageList = this.data.pageList; let select = this.data.select; let keys = Object.keys(select); for (let i = 0; i < keys.length; i++) { let v = keys[i]; if (v === 'count') continue; let li = v.split("-"); pageList[li[0]].list[li[1]].select = false; } this.setData({ showSelect: false, pageList, select: { count: 0 } }) }, btnSelect: function (e) { if (this.data.select.count > 9) return; let d = e.currentTarget.dataset; let pageList = this.data.pageList; let select = this.data.select; pageList[d.key].list[d.o].select = !pageList[d.key].list[d.o].select; if (pageList[d.key].list[d.o].select) { select[d.key + "-" + d.o] = d.url; select.count++; } else { select[d.key + "-" + d.o] = undefined; select.count--; } this.setData({ pageList: pageList, select }) }, format: function (res) { if(!res) return "" let T = new Date(res || 0); let year = T.getFullYear(); let month = T.getMonth() + 1; let day = T.getDate(); let hour = T.getHours(); let min = T.getMinutes() + 1; let sec = T.getSeconds(); return year + "-" + (month > 9 ? month : "0" + month) + "-" + day + " " + (hour > 9 ? hour : '0' + hour) + ":" + (min > 9 ? min : '0' + min) + ":" + (sec > 9 ? sec : '0' + sec); }, saveAsset: function () { if (this.data.select.count === 0) return; let _this = this; // 权限判断 wx.getSetting({ success: function(res){ if (!res['scope.writePhotosAlbum']) { wx.authorize({ scope: 'scope.writePhotosAlbum', success: function(res){ wx.showLoading(); let keys = Object.keys(_this.data.select); _this.downCount = keys.length - 1; for (let i = 0; i < keys.length; i++) { const v = keys[i]; if (v === 'count') continue; _this.downFile(v); } }, fail:function(err){ wx.showToast({ title: '未获取权限', icon: "none" }) } }) return } wx.showLoading(); let keys = Object.keys(this.data.select); _this.downCount = keys.length - 1; for (let i = 0; i < keys.length; i++) { const v = keys[i]; if (v === 'count') continue; _this.downFile(v); } }, fail:function(err){ wx.showToast({ title: '未获取权限', icon: "none" }) } }) }, downFile: function (v) { let _this = this; wx.cloud.downloadFile({ fileID: _this.data.select[v], success: function (res) { if (_this.data.pageType === "img") { _this.saveImage(res.tempFilePath) } else { _this.savevideo(res.tempFilePath) } }, fail: function (err) { wx.showToast({ title: "下载失败:" + _this.data.select[v], icon: "none" }) _this.downCount--; if (_this.downCount <= 0) { wx.hideLoading(); } } }) }, saveImage: function (tempFilePath) { let _this = this; wx.saveImageToPhotosAlbum({ filePath: tempFilePath, success: function (res) { _this.downEnd(); }, fail: function (err) { wx.showToast({ title: '下载失败', icon: "none" }) _this.downEnd(); } }) }, savevideo: function (tempFilePath) { wx.saveVideoToPhotosAlbum({ filePath: tempFilePath, success: function (res) { _this.downEnd(); }, fail: function (err) { wx.showToast({ title: '下载失败', icon: "none" }) _this.downEnd(); } }) }, downEnd: function () { this.downCount--; if (this.downCount <= 0) { wx.hideLoading(); } }, closeVideo: function(){ this.setData({ showVideo: "" }) }, closeAnvido:function(){ this.setData({ showAnVideo: false }) } })