imagePicker.js 1.6 KB

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