imagePicker.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // components/imagePicker/imagePicker.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. images: Array,
  8. max: Number,
  9. imageWidth: Number,
  10. imageHeight: Number,
  11. readonly: Boolean
  12. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. imageMargin: 0
  18. },
  19. observers: {
  20. 'images, max, readonly': function (images, max, readonly) {
  21. this.setData({
  22. imageMargin: images.length + (images.length < max && !readonly) > 1 ? 5 : 0
  23. })
  24. }
  25. },
  26. /**
  27. * 组件的方法列表
  28. */
  29. methods: {
  30. chooseImage: function (num, callback) {
  31. wx.chooseImage({
  32. count: num,
  33. sizeType: ['original'],
  34. sourceType: ['album']
  35. }).then(res => {
  36. callback(res.tempFilePaths)
  37. })
  38. },
  39. previewImage: function (e) {
  40. wx.previewImage({
  41. urls: this.data.images,
  42. current: e.currentTarget.dataset.url
  43. })
  44. },
  45. removeImage: function (e) {
  46. this.properties.images.splice(this.properties.images.indexOf(e.currentTarget.dataset.url), 1)
  47. this.triggerEvent("change", {
  48. images: this.properties.images
  49. })
  50. },
  51. addImage: function () {
  52. this.chooseImage(this.properties.max - this.properties.images.length, (imgs) => {
  53. this.properties.images = this.properties.images.concat(imgs)
  54. this.triggerEvent("change", {
  55. images: this.properties.images
  56. })
  57. })
  58. },
  59. }
  60. })