const reqInterface = require("../../../api/models"); const Base64 = require("../../../utils/base64"); const { imagePrefix, getApi } = require("../../../utils/dataHelper"); Component({ /** * 组件的属性列表 */ properties: { images: { type: Array, value: [] }, imagesPath: { type: String, value: '' }, uploadTxt: String, }, observers: { 'images': function (val) { let self = this; if (val) { self.setData({ imageList: val }) } }, 'imagesPath': function (val) { let self = this; if (val) { self.data.imagePathList = val.split(",") self.setData({ imagePathList: self.data.imagePathList }) } }, }, lifetimes: { attached() { this.setData({ 'header.Blade-Auth': `bearer ${wx.getStorageSync('token')}` }) this.setData({ api: getApi() }) } }, data: { styleCss: 'padding: 20rpx 0; line-height: 36rpx font-size: 28rpx; border-bottom: 1rpx dashed #f0f0f0;', api: '', imageList: [], imagePathList: [], tempImageFiles: [], header: { 'Authorization': `Basic ${Base64.Base64.encode('miniapp:miniapp_secret')}`, 'Blade-Auth': `bearer ${wx.getStorageSync('token')}` } }, /** * 组件的方法列表 */ methods: { uploadFiles(e) { let self = this; wx.showActionSheet({ itemList: ['从相册中选择', '拍照'], success(res) { if (res.tapIndex == 0) { self.chooseWxImage('album'); } else if (res.tapIndex == 1) { self.chooseWxImage('camera'); } }, fail(res) { console.log(res.errMsg) } }) }, chooseWxImage(type) { let self = this; wx.chooseMedia({ mediaType: "image", sizeType: ['original', 'compressed'], sourceType: [type], async success(res) { self.setData({ tempImageFiles: res.tempFiles }) for (let i = 0; i < self.data.tempImageFiles.length; i++) { let res = await reqInterface.PostImageUpload(self.data.tempImageFiles[i].tempFilePath); self.data.imageList.push(res.data.url) self.data.imagePathList.push(res.data.path) } self.setData({ imageList: self.data.imageList, imagePathList: self.data.imagePathList }) self.triggerEvent('sumbitImageInfo', { images: self.data.imageList, imagesPath: self.data.imagePathList }) } }) }, delImage(e) { let self = this; self.data.imageList.splice(e.currentTarget.dataset.index, 1) self.data.imagePathList.splice(e.currentTarget.dataset.index, 1) self.setData({ imageList: self.data.imageList, imagePathList: self.data.imagePathList }) self.triggerEvent('sumbitImageInfo', { images: self.data.imageList, imagesPath: self.data.imagePathList }) }, imgPreview(e) { let self = this; wx.previewImage({ urls: [e.currentTarget.dataset.href] }) }, } })