index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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: 9
  17. },
  18. // 清除urls
  19. clear: {
  20. type: Boolean,
  21. value: false,
  22. observer: function (newVal) {
  23. if (newVal) {
  24. this.handleClear();
  25. }
  26. }
  27. },
  28. // 每行可显示的个数
  29. size: {
  30. type: [String, Number],
  31. value: 3
  32. },
  33. // 所选的图片的尺寸 ['original', 'compressed']
  34. sizeType: {
  35. type: String,
  36. value: 'original',
  37. },
  38. // 图片裁剪、缩放的模式
  39. mode: {
  40. type: String,
  41. value: 'aspectFit', // 参考微信小程序 image 组件的mode属性列表
  42. },
  43. // 设置是否传入slot
  44. custom: {
  45. type: Boolean,
  46. value: false
  47. },
  48. // 是否可以预览
  49. preview: {
  50. type: Boolean,
  51. value: true
  52. },
  53. // 所选图片最大限制,单位字节
  54. maxImageSize: {
  55. type: Number,
  56. value: 10000000,
  57. }
  58. },
  59. /**
  60. * 组件的初始数据
  61. */
  62. data: {
  63. showBtn: true,
  64. tempFilePath: '',
  65. },
  66. lifetimes: {
  67. attached: function () {
  68. // 在组件实例进入页面节点树时执行
  69. const newOrOld = this.judgeNewOrOld();
  70. this.setData({
  71. newOrOld
  72. });
  73. if (newOrOld == 'old') {
  74. console.warn('image-picker组件已经升级,建议使用最新版本,当前用法会在后续版本中暂停支持');
  75. }
  76. },
  77. },
  78. /**
  79. * 组件的方法列表
  80. */
  81. methods: {
  82. handleClear() {
  83. let urls = this.data.urls;
  84. this.setData({
  85. urls: [],
  86. clear: false,
  87. showBtn: true
  88. });
  89. let info = {
  90. all: urls,
  91. current: urls,
  92. };
  93. let option = {};
  94. this.triggerEvent('linclear', info, option);
  95. },
  96. // 预览 preview
  97. onPreviewTap(e) {
  98. const index = e.currentTarget.dataset.index;
  99. const urls = this.data.urls;
  100. let tempFilePath = '';
  101. let previewImageList = [];
  102. const newOrOld = this.data.newOrOld;
  103. if (newOrOld == 'old') {
  104. tempFilePath = this.data.urls[index];
  105. previewImageList = this.data.urls;
  106. } else {
  107. tempFilePath = this.data.urls[index].url;
  108. for (let i = 0; i < urls.length; i++) {
  109. previewImageList.push(urls[i].url);
  110. }
  111. }
  112. let detail = {
  113. index, // 下标
  114. current: urls[index], // 当前显示图片的http链接
  115. all: urls // 需要预览的图片http链接列表
  116. };
  117. let option = {};
  118. if (this.data.preview === true) {
  119. wx.previewImage({
  120. current: tempFilePath, // 当前显示图片的http链接
  121. urls: previewImageList // 需要预览的图片http链接列表
  122. });
  123. }
  124. this.triggerEvent('linpreview', detail, option);
  125. },
  126. // 增加 add
  127. onAddTap() {
  128. const that = this;
  129. const count = this.data.count - this.data.urls.length;
  130. if (count === 0) {
  131. return;
  132. }
  133. const newOrOld = this.data.newOrOld;
  134. wx.chooseImage({
  135. count,
  136. sizeType: this.data.sizeType,
  137. sourceType: ['album', 'camera'],
  138. success(res) {
  139. // tempFilePath可以作为img标签的src属性显示图片
  140. let tempFilePath = [];
  141. if (newOrOld == 'old') {
  142. tempFilePath = res.tempFilePaths;
  143. } else {
  144. for (let i = 0; i < res.tempFilePaths.length; i++) {
  145. tempFilePath.push({
  146. url: res.tempFilePaths[i],
  147. // key: null
  148. imageSize: res.tempFiles[i].size
  149. });
  150. if (res.tempFiles[i].size > that.data.maxImageSize) {
  151. tempFilePath[i].overSize = true;
  152. } else {
  153. tempFilePath[i].overSize = false;
  154. }
  155. }
  156. }
  157. const newtempFilePaths = that.data.urls.concat(tempFilePath);
  158. // 判断是否还能继续添加图片
  159. if (newtempFilePaths.length === parseInt(that.data.count)) {
  160. that.setData({
  161. showBtn: false
  162. });
  163. }
  164. that.setData({
  165. urls: newtempFilePaths,
  166. value: newtempFilePaths,
  167. tempFilePath
  168. });
  169. let detail = {
  170. current: tempFilePath,
  171. all: newtempFilePaths
  172. };
  173. let option = {};
  174. that.triggerEvent('linchange', detail, option);
  175. that.triggerEvent('linpush', detail, option);
  176. // 超过大小的image集合
  177. let overSizeList = [];
  178. for (let n = 0; n < newtempFilePaths.length; n++) {
  179. if (newtempFilePaths[n].overSize) {
  180. overSizeList.push(newtempFilePaths[n]);
  181. }
  182. }
  183. if (overSizeList.length > 0) {
  184. let detail = {
  185. current: tempFilePath,
  186. all: newtempFilePaths,
  187. overSizeList: overSizeList,
  188. };
  189. that.triggerEvent('linoversize', detail, option);
  190. }
  191. }
  192. });
  193. },
  194. // 删除 remove
  195. onDelTap(e) {
  196. const index = e.currentTarget.dataset.index;
  197. const urls = this.data.urls;
  198. const tempFilePath = urls[index];
  199. const tempFilePaths = this.handleSplice(urls, tempFilePath);
  200. // 判断是否还能继续添加图片
  201. if (tempFilePaths.length < parseInt(this.data.count)) {
  202. this.setData({
  203. showBtn: true
  204. });
  205. }
  206. this.setData({
  207. tempFilePath,
  208. urls: tempFilePaths,
  209. value: tempFilePaths,
  210. });
  211. let detail = {
  212. index,
  213. current: tempFilePath,
  214. all: tempFilePaths
  215. };
  216. let option = {};
  217. this.triggerEvent('linremove', detail, option);
  218. },
  219. handleSplice(arr, current) {
  220. const newArr = arr.filter(item => item !== current);
  221. return newArr;
  222. },
  223. judgeNewOrOld: function () {
  224. const urls = this.data.urls;
  225. if (urls.length != 0) {
  226. if (typeof (urls[0]) != 'object') {
  227. return 'old';
  228. }
  229. return 'new';
  230. }
  231. return 'new';
  232. }
  233. },
  234. });