123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- // 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"
- })
- let li = list.data || [];
- for (let i = 0; i < li.length; i++) {
- const v = li[i];
- v.index = i;
- // pageList[v.create_time] ? pageList[v.create_time].list.push(v) : pageList[v.create_time] = { list: [v], time: this.format(v.create_time) };
- pageList[0] ? pageList[0].list.push(v) : pageList[0] = { list: [v], time: 0 };
- this.imgList.push(v.url);
- }
- this.setData({
- pageList: pageList,
- pageType: options.type || "img",
- showAnVideo: options.type === "video"
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- 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
- })
- }
- })
|