index.js 6.7 KB

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