index.js 7.2 KB

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