import { minioBaseUrl, clientId, clientSecret } from '@/common/setting' import { Base64 } from '@/utils/base64.js'; export default { data() { return { form: { images: [] }, uploadConfig: { acceptImg: "image", acceptVideo: 'video', capture: ['album', 'camera'], multiple: true, maxCount: "5", previewFullImage: true, uploadText: "上传中", url: minioBaseUrl + "/blade-resource/oss/endpoint/put-file-attach", header: {}, }, } }, created() { this.getHeader() }, methods: { //获取头部 getHeader() { let accessToken = uni.getStorageSync('accessToken'); let myHeader = {} if (accessToken) { myHeader['Blade-Auth'] = 'bearer ' + accessToken; } // 客户端认证参数 myHeader['Authorization'] = 'Basic ' + Base64.encode(clientId + ':' + clientSecret); this.uploadConfig.header = myHeader }, //上传方法 uploadFilePromise(url) { return new Promise((resolve, reject) => { let a = uni.uploadFile({ url: this.uploadConfig.url, //接口地址 filePath: url, name: 'file', header: this.uploadConfig.header, success: (res) => { let data = JSON.parse(res.data) setTimeout(() => { resolve(data) }, 1000) } }); }) }, //上传成功后对返回数据进行处理 async afterReadImg(event) { this.showLoading() var that = this; // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式 let lists = [].concat(event.file) let fileListLen = this.form.images.length lists.map((item) => { this.form.images.push({ ...item, status: 'uploading', message: '上传中' }) }) for (let i = 0; i < lists.length; i++) { const result = await this.uploadFilePromise(lists[i].url) that.form.images.splice(fileListLen, 1, Object.assign({}, { url: result.data.link, name:result.data.name })) fileListLen++ } this.loadingClose() }, //删除图片 deletePic(event) { this.form.images.splice(event.index, 1) }, showLoading() { uni.showLoading({ mask: true, title: '上传中' }) }, loadingClose() { uni.hideLoading() }, } }