| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- // components/imagePicker/imagePicker.js
- Component({
- behaviors: ['wx://form-field'],
- /**
- * 组件的属性列表
- */
- properties: {
- value: Array,
- max: Number,
- imageWidth: Number,
- imageHeight: Number,
- readonly: Boolean
- },
- /**
- * 组件的初始数据
- */
- data: {
- imageMargin: 0
- },
- observers: {
- 'value, max, readonly': function (value, max, readonly) {
- this.setData({
- imageMargin: value.length + (value.length < max && !readonly) > 1 ? 5 : 0
- })
- }
- },
- /**
- * 组件的方法列表
- */
- methods: {
- chooseImage: function (num, callback) {
- wx.chooseImage({
- count: num,
- sizeType: ['original'],
- sourceType: ['album']
- }).then(res => {
- callback(res.tempFilePaths)
- })
- },
- previewImage: function (e) {
- wx.previewImage({
- urls: this.data.value,
- current: e.currentTarget.dataset.url
- })
- },
- removeImage: function (e) {
- this.data.value.splice(this.data.value.indexOf(e.currentTarget.dataset.url), 1)
- this.setData({
- value: this.data.value
- })
- this.triggerEvent("change", {
- value: this.data.value
- })
- },
- addImage: function () {
- this.chooseImage(this.data.max - this.data.value.length, (imgs) => {
- this.data.value = this.data.value.concat(imgs)
- this.setData({
- value: this.data.value
- })
- this.triggerEvent("change", {
- value: this.data.value
- })
- })
- },
- }
- })
|