index.js 6.1 KB

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