index.js 6.8 KB

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