import { zconfig } from'../../api/ztb' const util = require('../../utils/util') const app = getApp() Component({ options: { addGlobalClass: true, multipleSlots: true }, data: { path: null }, methods: { chooseImage: function(e) { this.triggerEvent('before', null) var that = this wx.chooseImage({ count: 1, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], //从相册选择 success: (res) => { that.setData({ path: res.tempFilePaths[0] }) that.uploadFile(res.tempFilePaths[0]) } }) }, uploadFile: function (path) { var that = this var header = { ...zconfig.header, 'X-APP-ID': app.xAppId, appversion: app.version, 'Content-Type': 'multipart/form-data' } wx.uploadFile({ url: zconfig.baseUrl + '/recycle-user-center/file-uploading/upload/image', filePath: path, name: 'image', header, success: function (result) { var resp = null if (!util.isEmpty(result.data)) { resp = JSON.parse(result.data) } if (resp && resp.code === 0) { that.triggerEvent('uploaded', {id: that.data.id, url: resp.data}) } else { util.showToast('图片上传失败,请重新上传') } }, fail: function (res) { util.showToast('图片上传失败,请重新上传') } }) }, viewImage: function(e) { this.setData({ focus: false }) wx.previewImage({ urls: [this.data.path], current: this.data.path }) }, deleteImg: function(e) { this.setData({ path: null }) } } })