index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // mask
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. externalClasses: ['l-class', 'l-item-class'],
  7. behaviors: ['wx://form-field'],
  8. properties: {
  9. urls: {
  10. type: Array,
  11. value: []
  12. },
  13. // 最多可以选择的图片张数
  14. count: {
  15. type: [String, Number],
  16. value: 4
  17. },
  18. // 清除urls
  19. clear: {
  20. type: Boolean,
  21. value: false,
  22. observer: function (newVal, oldVal, changedPath) {
  23. if (newVal) {
  24. this.handleClear();
  25. }
  26. }
  27. },
  28. // 每行可显示的个数
  29. size: {
  30. type: [String, Number],
  31. value: 2
  32. },
  33. // 所选的图片的尺寸 ['original', 'compressed']
  34. // 选择图片的来源
  35. // sourceType: {
  36. // type: String,
  37. // value: '',
  38. // },
  39. // 图片裁剪、缩放的模式
  40. mode: {
  41. type: String,
  42. value: 'aspectFit', // 参考微信小程序 image 组件的mode属性列表
  43. },
  44. // 设置是否传入slot
  45. custom: {
  46. type: Boolean,
  47. value: false
  48. },
  49. // 是否可以预览
  50. isPreview: {
  51. type: Boolean,
  52. value: true
  53. }
  54. },
  55. /**
  56. * 组件的初始数据
  57. */
  58. data: {
  59. showBtn: true,
  60. tempFilePath: '',
  61. },
  62. /**
  63. * 组件的方法列表
  64. */
  65. methods: {
  66. handleClear() {
  67. this.setData({
  68. urls: [],
  69. clear: false,
  70. showBtn: true
  71. })
  72. let detail = true;
  73. let option = {};
  74. this.triggerEvent('linclear', detail, option);
  75. },
  76. // 预览 preview
  77. onPreviewTap(e) {
  78. const that = this
  79. const index = e.currentTarget.dataset.index
  80. const tempFilePath = this.data.urls[index]
  81. let detail = {
  82. index, // 下标
  83. current: tempFilePath, // 当前显示图片的http链接
  84. all: that.data.urls // 需要预览的图片http链接列表
  85. };
  86. let option = {};
  87. if (this.data.isPreview === true) {
  88. wx.previewImage({
  89. current: tempFilePath, // 当前显示图片的http链接
  90. urls: that.data.urls // 需要预览的图片http链接列表
  91. })
  92. }
  93. this.triggerEvent('linpreview', detail, option);
  94. },
  95. // 增加 add
  96. onAddTap(e) {
  97. const that = this;
  98. const count = this.data.count - this.data.urls.length
  99. if (count === 0 ) {
  100. return
  101. }
  102. wx.chooseImage({
  103. count,
  104. sizeType: ["compressed"],
  105. sourceType: ['album'],
  106. success(res) {
  107. // tempFilePath可以作为img标签的src属性显示图片
  108. const tempFilePath = res.tempFilePaths;
  109. const newtempFilePaths = that.data.urls.concat(tempFilePath)
  110. // 判断是否还能继续添加图片
  111. if (newtempFilePaths.length === parseInt(that.data.count)) {
  112. that.setData({
  113. showBtn: false
  114. })
  115. }
  116. that.setData({
  117. urls: newtempFilePaths,
  118. value: newtempFilePaths,
  119. tempFilePath
  120. })
  121. let detail = {
  122. current: tempFilePath,
  123. all: newtempFilePaths
  124. }
  125. let option = {};
  126. that.triggerEvent('linchange', detail, option);
  127. }
  128. })
  129. },
  130. // 删除 remove
  131. onDelTap(e) {
  132. const index = e.currentTarget.dataset.index
  133. const urls = this.data.urls
  134. const tempFilePath = urls[index]
  135. const tempFilePaths = this.handleSplice(urls, tempFilePath)
  136. // 判断是否还能继续添加图片
  137. if (tempFilePaths.length < parseInt(this.data.count)) {
  138. this.setData({
  139. showBtn: true
  140. })
  141. }
  142. this.setData({
  143. tempFilePath,
  144. urls: tempFilePaths,
  145. value: tempFilePaths,
  146. })
  147. let detail = {
  148. index,
  149. current: tempFilePath,
  150. all: tempFilePaths
  151. }
  152. let option = {};
  153. this.triggerEvent('linremove', detail, option);
  154. },
  155. handleSplice(arr, current) {
  156. const newArr = arr.filter(item => item!== current)
  157. return newArr
  158. },
  159. },
  160. attached: function () {
  161. },
  162. })