1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| <template> <div> <el-upload list-type="picture-card" class="avatar-uploader" action="http://daichongwbe.oss-cn-beijing.aliyuncs.com" :data="dataObj" accept=".jpeg, .png, .jpg" :multiple="false" :before-upload="beforeUpload" :on-success="handleUploadSuccess" :limit="limit" :show-file-list="false" ref="upload" > <img v-if="imageUrl" :src="imageUrl" class="avatar" /> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </div> </template> <script> import { getPolicy } from "@/api/oss/token"; export default { name: "ossSingleUpload", props: { limit: Number, imgCover: String }, data() { return { dataObj: { policy: "", signature: "", key: "", ossaccessKeyId: "", dir: "", host: "", callback: "" }, imageUrl: "" }; }, created() { setTimeout(() => { this.imageUrl = this.imgCover; }, 500); }, methods: { emitInput(val) { this.$emit("input", val); }, beforeUpload(file) { let _self = this; return new Promise((resolve, reject) => { getPolicy() .then(response => { _self.dataObj.policy = response.data.policy; _self.dataObj.signature = response.data.signature; _self.dataObj.ossaccessKeyId = response.data.accessid; _self.dataObj.key = response.data.dir + "${filename}"; _self.dataObj.dir = response.data.dir; _self.dataObj.host = response.data.host; _self.dataObj.callback = response.data.callback; resolve(true); }) .catch(err => { console.log(err); reject(false); }); }); }, handleUploadSuccess(res, file) { this.$refs.upload.clearFiles(); this.imageUrl = this.dataObj.host + "/" + this.dataObj.dir + file.name; this.$emit("getMessage", this.imageUrl); } } }; </script>
|