index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. var that;
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. previewImgList: {
  8. type: Array,
  9. value: []
  10. },
  11. previewImg: {
  12. type: null,
  13. value: ''
  14. },
  15. originalArr: {
  16. type: Array,
  17. value: []
  18. },
  19. },
  20. attached: function () {
  21. that = this;
  22. },
  23. /**
  24. * 组件的初始数据
  25. */
  26. data: {
  27. preview_box_top: 0,
  28. left: '0px;',
  29. touchS: [0, 0],
  30. touchE: [0, 0],
  31. index: 0,
  32. imgindex: 0,
  33. previewHideStatus: false,
  34. scale: 1,
  35. scaleObj: {
  36. scale: 1,
  37. x: 0,
  38. y: 0,
  39. yes: true
  40. },
  41. touchStartTime: 0, // 触摸开始时间
  42. touchEndTime: 0, // 触摸结束时间
  43. lastTapTime: 0, // 最后一次单击事件点击发生时间
  44. lastTapTimeoutFunc: null, // 单击事件点击后要触发的函数
  45. },
  46. /**
  47. * 组件的方法列表
  48. */
  49. methods: {
  50. // 查看原图
  51. showOriginal(e) {
  52. wx.showLoading()
  53. let index = this.data.imgindex - 1
  54. this.data.previewImgList[index].url = this.data.originalArr[index]
  55. this.data.previewImgList[index].originalShow = true
  56. this.setData({
  57. previewImgList: this.data.previewImgList
  58. })
  59. },
  60. // 下载原图
  61. downloadOriginal(e) {
  62. let index = this.data.imgindex - 1
  63. let url = this.data.originalArr[index]
  64. wx.showLoading({
  65. title: '图片保存中',
  66. mask: true
  67. })
  68. wx.downloadFile({
  69. url: url,
  70. success: function (res) {
  71. var temp = res.tempFilePath
  72. wx.saveImageToPhotosAlbum({
  73. filePath: temp,
  74. success(res) {
  75. wx.showToast({
  76. title: '保存成功',
  77. duration: 3000
  78. })
  79. },
  80. fail: function (err) {
  81. wx.showToast({
  82. title: '保存失败,请重试',
  83. duration: 3000,
  84. icon: 'none'
  85. })
  86. }
  87. })
  88. },
  89. fail: function (err) {
  90. wx.showToast({
  91. title: '保存失败,请重试',
  92. duration: 3000,
  93. icon: 'none'
  94. })
  95. }
  96. })
  97. },
  98. bindload(e) {
  99. wx.hideLoading()
  100. },
  101. jingzhi(e) {
  102. let curTime = e.timeStamp;
  103. let lastTime = this.data.lastTapDiffTime;
  104. this.data.lastTapDiffTime = curTime;
  105. //两次点击间隔小于300ms, 认为是双击
  106. let diff = curTime - lastTime;
  107. if (diff < 300) {
  108. // console.log("double tap")
  109. clearTimeout(this.data.lastTapTimeoutFunc); // 成功触发双击事件时,取消单击事件的执行
  110. if (that.data.scale == 1) {
  111. that.setData({
  112. scale: 3
  113. })
  114. } else {
  115. that.setData({
  116. scale: 1
  117. })
  118. }
  119. } else {
  120. this.data.lastTapTimeoutFunc = setTimeout(function () {
  121. // console.log("single tap")
  122. if (that.data.scaleObj.yes) {
  123. that.setData({
  124. preview_box_top: '100%'
  125. })
  126. }
  127. }, 300);
  128. }
  129. },
  130. configqxClick(e) {
  131. this.setData({
  132. scopeWritePhotosAlbum: e.detail
  133. })
  134. },
  135. touchStart: function (e) {
  136. this.data.touchStartTime = e.timeStamp //时间点
  137. let sx = e.touches[0].pageX
  138. let sy = e.touches[0].pageY
  139. this.data.touchS = [sx, sy];
  140. },
  141. touchMove: function (e) {
  142. let start = this.data.touchS;
  143. let sx = e.touches[0].pageX;
  144. let sy = e.touches[0].pageY;
  145. this.data.touchE = [sx, sy];
  146. },
  147. touchEnd: function (e) {
  148. this.data.touchEndTime = e.timeStamp //时间点
  149. let start = this.data.touchS
  150. let end = this.data.touchE
  151. let scaleObj = this.data.scaleObj
  152. //如果((start[0] < end[0] - 50) && (scaleObj.scale==1&&scaleObj.x==0&&scaleObj.y==0)) //左滑动
  153. //如果((start[0] > end[0] + 50) && (scaleObj.scale==1&&scaleObj.x==0&&scaleObj.y==0)) //右滑动
  154. if (scaleObj.yes) {
  155. if (end[0] == 0) {
  156. // console.log('点击')
  157. } else if ((start[0] < end[0] - 50) && (scaleObj.scale == 1 && scaleObj.x == 0 && scaleObj.y == 0)) {
  158. if (this.data.index !== 0) {
  159. this.data.index -= 1;
  160. this.data.imgindex -= 1;
  161. this.setData({
  162. index: this.data.index,
  163. left: '-' + this.data.index + '00%;transition: all .5s;',
  164. imgindex: this.data.imgindex
  165. });
  166. }
  167. } else if ((start[0] > end[0] + 50) && (scaleObj.scale == 1 && scaleObj.x == 0 && scaleObj.y == 0)) {
  168. if (this.data.index !== this.data.previewImgList.length - 1) {
  169. this.data.index += 1;
  170. this.data.imgindex += 1;
  171. this.setData({
  172. index: this.data.index,
  173. left: '-' + this.data.index + '00%;transition: all .5s;',
  174. imgindex: this.data.imgindex
  175. });
  176. }
  177. } else {
  178. // console.log('下滑/上滑');
  179. this.setData({
  180. preview_box_top: '100%'
  181. })
  182. }
  183. this.data.touchE = [0, 0];
  184. }
  185. setTimeout(() => {
  186. if (this.data.scaleObj.x == 0 && this.data.scaleObj.y == 0 && this.data.scaleObj.scale == 1) {
  187. // console.log('yes is true');
  188. this.data.scaleObj.yes = true;
  189. }
  190. }, 500)
  191. },
  192. showPreview() {
  193. this.setData({
  194. previewHideStatus: true,
  195. preview_box_top: 0
  196. })
  197. },
  198. hidePreview() {
  199. this.setData({
  200. previewHideStatus: false,
  201. preview_box_top: 0
  202. })
  203. },
  204. onScale(e) {
  205. this.data.scaleObj = {
  206. scale: e.detail.scale,
  207. x: e.detail.x,
  208. y: e.detail.y,
  209. yes: false
  210. }
  211. },
  212. stopPageScroll() {
  213. return
  214. },
  215. },
  216. observers: {
  217. 'previewImgList': function (arr) {
  218. // console.log(arr, 999, this.data.originalArr)
  219. },
  220. 'previewImg': function (img) {
  221. this.data.previewImgList.map((item, index) => {
  222. if (item.url == img) {
  223. let imgindex = index;
  224. imgindex += 1;
  225. this.setData({
  226. index: index,
  227. imgindex: imgindex,
  228. left: '-' + index + '00%;'
  229. })
  230. }
  231. })
  232. }
  233. },
  234. })