|
@@ -1,301 +0,0 @@
|
|
|
-// miniprogram/pages/marvellous/index.js
|
|
|
-Page({
|
|
|
- imgList: [],
|
|
|
- downCount: 0,
|
|
|
- /**
|
|
|
- * 页面的初始数据
|
|
|
- */
|
|
|
- data: {
|
|
|
- pageList: [],
|
|
|
- select: { count: 0 },
|
|
|
- pageType: "",
|
|
|
- showSelect: false,
|
|
|
- showVideo: "",
|
|
|
- showAnVideo: true
|
|
|
- },
|
|
|
- /**
|
|
|
- * 生命周期函数--监听页面加载
|
|
|
- */
|
|
|
- onLoad: function (options) {
|
|
|
- wx.setNavigationBarTitle({
|
|
|
- title: options.title || "精彩瞬间"
|
|
|
- })
|
|
|
- this.init(options);
|
|
|
- },
|
|
|
- async init(options) {
|
|
|
- const db = wx.cloud.database();
|
|
|
- const _ = db.command;
|
|
|
- if (!options) options = { type: this.data.pageType }
|
|
|
- this.imgList = [];
|
|
|
- let pageList = {};
|
|
|
- wx.showLoading();
|
|
|
- let allList = await db.collection('data_asset').where({
|
|
|
- type: _.eq(options.type || "img")
|
|
|
- }).count();
|
|
|
- const totalpage = Math.ceil(allList.total / 20)
|
|
|
- let list = [];
|
|
|
- for (let i = 0; i < totalpage; i++) {
|
|
|
- let end = allList.total > (i + 1) * 20 ? (i + 1) * 20 : allList.total;
|
|
|
- let li = await db.collection('data_asset').where({
|
|
|
- type: _.eq(options.type || "img")
|
|
|
- }).skip(i * 20)
|
|
|
- .limit(end)
|
|
|
- .get() || { data: [] };
|
|
|
- list = list.concat(...li.data)
|
|
|
- }
|
|
|
-
|
|
|
- wx.hideLoading();
|
|
|
- if (!list.length) return wx.showToast({
|
|
|
- title: '暂无数据',
|
|
|
- icon: "none"
|
|
|
- })
|
|
|
- list.sort((a, b) => {
|
|
|
- return b.create_time - a.create_time
|
|
|
- })
|
|
|
- for (let i = 0; i < list.length; i++) {
|
|
|
- const v = list[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
|
|
|
- })
|
|
|
- }
|
|
|
-})
|