imagePicker.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. },
  18. /**
  19. * 组件的方法列表
  20. */
  21. methods: {
  22. chooseImage: function (num, callback) {
  23. wx.chooseImage({
  24. count: num,
  25. sizeType: ['original'],
  26. sourceType: ['album'],
  27. success: function (res) {
  28. callback(res.tempFilePaths)
  29. }.bind(this)
  30. })
  31. },
  32. previewImage: function (e) {
  33. wx.previewImage({
  34. urls: this.data.images,
  35. current: e.currentTarget.dataset.url
  36. })
  37. },
  38. removeImage: function (e) {
  39. for (var i = 0; i < this.properties.images.length; i++) {
  40. if (this.properties.images[i] == e.currentTarget.dataset.url) {
  41. var arr = this.properties.images
  42. arr.splice(i, 1)
  43. this.setData({
  44. images: arr
  45. })
  46. this.triggerEvent("change", {
  47. images: this.properties.images
  48. })
  49. break
  50. }
  51. }
  52. },
  53. addImage: function () {
  54. if (this.properties.images.length >= this.properties.max) {
  55. return
  56. }
  57. this.chooseImage(this.properties.max - this.properties.images.length, (img) => {
  58. var arr = this.properties.images.concat(img)
  59. this.setData({
  60. images: arr
  61. })
  62. this.triggerEvent("change", {
  63. images: this.properties.images
  64. })
  65. })
  66. },
  67. }
  68. })