index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // miniprogram/pages/marvellous/index.js
  2. Page({
  3. imgList: [],
  4. downCount: 0,
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. pageList: [],
  10. select: { count: 0 },
  11. pageType: "",
  12. showSelect: false,
  13. showVideo: "",
  14. showAnVideo: true
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: async function (options) {
  20. wx.setNavigationBarTitle({
  21. title: options.title || "精彩瞬间"
  22. })
  23. const db = wx.cloud.database();
  24. const _ = db.command;
  25. const $ = db.command.aggregate
  26. this.imgList = [];
  27. let pageList = {};
  28. wx.showLoading();
  29. let list = await db.collection('data_asset').where({
  30. type: _.eq(options.type || "img")
  31. }).get();
  32. wx.hideLoading();
  33. if (!list.data.length) return wx.showToast({
  34. title: '暂无数据',
  35. icon: "none"
  36. })
  37. for (let i = 0; i < list.data.length; i++) {
  38. const v = list.data[i];
  39. v.index = i;
  40. pageList[v.creat_time] ? pageList[v.creat_time].list.push(v) : pageList[v.creat_time] = { list: [v], time: this.format(v.creat_time) };
  41. this.imgList.push(v.url);
  42. }
  43. console.log(pageList)
  44. this.setData({
  45. pageList: pageList,
  46. pageType: options.type || "img"
  47. })
  48. },
  49. /**
  50. * 生命周期函数--监听页面初次渲染完成
  51. */
  52. onReady: function () {
  53. },
  54. /**
  55. * 生命周期函数--监听页面显示
  56. */
  57. onShow: function () {
  58. },
  59. /**
  60. * 生命周期函数--监听页面隐藏
  61. */
  62. onHide: function () {
  63. },
  64. /**
  65. * 生命周期函数--监听页面卸载
  66. */
  67. onUnload: function () {
  68. },
  69. /**
  70. * 页面相关事件处理函数--监听用户下拉动作
  71. */
  72. onPullDownRefresh: function () {
  73. },
  74. /**
  75. * 页面上拉触底事件的处理函数
  76. */
  77. onReachBottom: function () {
  78. },
  79. /**
  80. * 用户点击右上角分享
  81. */
  82. onShareAppMessage: function () {
  83. },
  84. showImg(e) {
  85. if (this.data.showSelect) return this.btnSelect(e);
  86. if(this.data.pageType === "video") return this.showVideo(e);
  87. if(this.data.pageType === "img") return this.showImage(e);
  88. },
  89. showVideo:function(e){
  90. this.setData({
  91. showVideo: this.imgList[e.currentTarget.dataset.index]
  92. })
  93. },
  94. showImage:function(e){
  95. wx.previewImage({
  96. urls: this.imgList,
  97. current: this.imgList[e.currentTarget.dataset.index],
  98. })
  99. },
  100. longtap: function (e) {
  101. this.setData({
  102. showSelect: true
  103. })
  104. },
  105. longClose: function (e) {
  106. let pageList = this.data.pageList;
  107. let select = this.data.select;
  108. let keys = Object.keys(select);
  109. for (let i = 0; i < keys.length; i++) {
  110. let v = keys[i];
  111. if (v === 'count') continue;
  112. let li = v.split("-");
  113. pageList[li[0]].list[li[1]].select = false;
  114. }
  115. this.setData({
  116. showSelect: false,
  117. pageList,
  118. select: {
  119. count: 0
  120. }
  121. })
  122. },
  123. btnSelect: function (e) {
  124. if (this.data.select.count > 9) return;
  125. let d = e.currentTarget.dataset;
  126. let pageList = this.data.pageList;
  127. let select = this.data.select;
  128. pageList[d.key].list[d.o].select = !pageList[d.key].list[d.o].select;
  129. if (pageList[d.key].list[d.o].select) {
  130. select[d.key + "-" + d.o] = d.url;
  131. select.count++;
  132. } else {
  133. select[d.key + "-" + d.o] = undefined;
  134. select.count--;
  135. }
  136. this.setData({
  137. pageList: pageList,
  138. select
  139. })
  140. },
  141. format: function (res) {
  142. if(!res) return ""
  143. let T = new Date(res || 0);
  144. let year = T.getFullYear();
  145. let month = T.getMonth() + 1;
  146. let day = T.getDate();
  147. let hour = T.getHours();
  148. let min = T.getMinutes() + 1;
  149. let sec = T.getSeconds();
  150. return year + "-" + (month > 9 ? month : "0" + month) + "-" + day + " " + (hour > 9 ? hour : '0' + hour) + ":" + (min > 9 ? min : '0' + min) + ":" + (sec > 9 ? sec : '0' + sec);
  151. },
  152. saveAsset: function () {
  153. if (this.data.select.count === 0) return;
  154. let _this = this;
  155. // 权限判断
  156. wx.getSetting({
  157. success: function(res){
  158. if (!res['scope.writePhotosAlbum']) {
  159. wx.authorize({
  160. scope: 'scope.writePhotosAlbum',
  161. success: function(res){
  162. wx.showLoading();
  163. let keys = Object.keys(_this.data.select);
  164. _this.downCount = keys.length - 1;
  165. for (let i = 0; i < keys.length; i++) {
  166. const v = keys[i];
  167. if (v === 'count') continue;
  168. _this.downFile(v);
  169. }
  170. },
  171. fail:function(err){
  172. wx.showToast({
  173. title: '未获取权限',
  174. icon: "none"
  175. })
  176. }
  177. })
  178. return
  179. }
  180. wx.showLoading();
  181. let keys = Object.keys(this.data.select);
  182. _this.downCount = keys.length - 1;
  183. for (let i = 0; i < keys.length; i++) {
  184. const v = keys[i];
  185. if (v === 'count') continue;
  186. _this.downFile(v);
  187. }
  188. },
  189. fail:function(err){
  190. wx.showToast({
  191. title: '未获取权限',
  192. icon: "none"
  193. })
  194. }
  195. })
  196. },
  197. downFile: function (v) {
  198. let _this = this;
  199. wx.cloud.downloadFile({
  200. fileID: _this.data.select[v],
  201. success: function (res) {
  202. if (_this.data.pageType === "img") {
  203. _this.saveImage(res.tempFilePath)
  204. } else {
  205. _this.savevideo(res.tempFilePath)
  206. }
  207. },
  208. fail: function (err) {
  209. wx.showToast({
  210. title: "下载失败:" + _this.data.select[v],
  211. icon: "none"
  212. })
  213. _this.downCount--;
  214. if (_this.downCount <= 0) {
  215. wx.hideLoading();
  216. }
  217. }
  218. })
  219. },
  220. saveImage: function (tempFilePath) {
  221. let _this = this;
  222. wx.saveImageToPhotosAlbum({
  223. filePath: tempFilePath,
  224. success: function (res) {
  225. _this.downEnd();
  226. },
  227. fail: function (err) {
  228. wx.showToast({
  229. title: '下载失败',
  230. icon: "none"
  231. })
  232. _this.downEnd();
  233. }
  234. })
  235. },
  236. savevideo: function (tempFilePath) {
  237. wx.saveVideoToPhotosAlbum({
  238. filePath: tempFilePath,
  239. success: function (res) {
  240. _this.downEnd();
  241. },
  242. fail: function (err) {
  243. wx.showToast({
  244. title: '下载失败',
  245. icon: "none"
  246. })
  247. _this.downEnd();
  248. }
  249. })
  250. },
  251. downEnd: function () {
  252. this.downCount--;
  253. if (this.downCount <= 0) {
  254. wx.hideLoading();
  255. }
  256. },
  257. closeVideo: function(){
  258. this.setData({
  259. showVideo: ""
  260. })
  261. },
  262. closeAnvido:function(){
  263. this.setData({
  264. showAnVideo: false
  265. })
  266. }
  267. })