index.js 6.5 KB

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