¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <div> |
| | | <img v-bind:src="options.img" @click="editCropper()" title="ç¹å»ä¸ä¼ 头å" class="img-circle img-lg" /> |
| | | <el-dialog :title="title" :visible.sync="open" width="800px"> |
| | | <el-row> |
| | | <el-col :xs="24" :md="12" :style="{height: '350px'}"> |
| | | <vue-cropper |
| | | ref="cropper" |
| | | :img="options.img" |
| | | :info="true" |
| | | :autoCrop="options.autoCrop" |
| | | :autoCropWidth="options.autoCropWidth" |
| | | :autoCropHeight="options.autoCropHeight" |
| | | :fixedBox="options.fixedBox" |
| | | @realTime="realTime" |
| | | /> |
| | | </el-col> |
| | | <el-col :xs="24" :md="12" :style="{height: '350px'}"> |
| | | <div class="avatar-upload-preview"> |
| | | <img :src="previews.url" :style="previews.img" /> |
| | | </div> |
| | | </el-col> |
| | | </el-row> |
| | | <br /> |
| | | <el-row> |
| | | <el-col :lg="2" :md="2"> |
| | | <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload"> |
| | | <el-button size="small"> |
| | | ä¸ä¼ |
| | | <i class="el-icon-upload el-icon--right"></i> |
| | | </el-button> |
| | | </el-upload> |
| | | </el-col> |
| | | <el-col :lg="{span: 1, offset: 2}" :md="2"> |
| | | <el-button icon="el-icon-plus" size="small" @click="changeScale(1)"></el-button> |
| | | </el-col> |
| | | <el-col :lg="{span: 1, offset: 1}" :md="2"> |
| | | <el-button icon="el-icon-minus" size="small" @click="changeScale(-1)"></el-button> |
| | | </el-col> |
| | | <el-col :lg="{span: 1, offset: 1}" :md="2"> |
| | | <el-button icon="el-icon-refresh-left" size="small" @click="rotateLeft()"></el-button> |
| | | </el-col> |
| | | <el-col :lg="{span: 1, offset: 1}" :md="2"> |
| | | <el-button icon="el-icon-refresh-right" size="small" @click="rotateRight()"></el-button> |
| | | </el-col> |
| | | <el-col :lg="{span: 2, offset: 6}" :md="2"> |
| | | <el-button type="primary" size="small" @click="uploadImg()">æ 交</el-button> |
| | | </el-col> |
| | | </el-row> |
| | | </el-dialog> |
| | | </div> |
| | | </template> |
| | | |
| | | <script> |
| | | import store from "@/store"; |
| | | import { VueCropper } from "vue-cropper"; |
| | | import { uploadAvatar } from "@/api/system/user"; |
| | | |
| | | export default { |
| | | components: { VueCropper }, |
| | | props: { |
| | | user: { |
| | | type: Object |
| | | } |
| | | }, |
| | | data() { |
| | | return { |
| | | // æ¯å¦æ¾ç¤ºå¼¹åºå± |
| | | open: false, |
| | | // å¼¹åºå±æ é¢ |
| | | title: "ä¿®æ¹å¤´å", |
| | | options: { |
| | | img: store.getters.avatar, //è£åªå¾ççå°å |
| | | autoCrop: true, // æ¯å¦é»è®¤çææªå¾æ¡ |
| | | autoCropWidth: 200, // é»è®¤çææªå¾æ¡å®½åº¦ |
| | | autoCropHeight: 200, // é»è®¤çææªå¾æ¡é«åº¦ |
| | | fixedBox: true // åºå®æªå¾æ¡å¤§å° ä¸å
许æ¹å |
| | | }, |
| | | previews: {} |
| | | }; |
| | | }, |
| | | methods: { |
| | | // ç¼è¾å¤´å |
| | | editCropper() { |
| | | this.open = true; |
| | | }, |
| | | // è¦çé»è®¤çä¸ä¼ è¡ä¸º |
| | | requestUpload() { |
| | | }, |
| | | // åå·¦æè½¬ |
| | | rotateLeft() { |
| | | this.$refs.cropper.rotateLeft(); |
| | | }, |
| | | // åå³æè½¬ |
| | | rotateRight() { |
| | | this.$refs.cropper.rotateRight(); |
| | | }, |
| | | // å¾çç¼©æ¾ |
| | | changeScale(num) { |
| | | num = num || 1; |
| | | this.$refs.cropper.changeScale(num); |
| | | }, |
| | | // ä¸ä¼ é¢å¤ç |
| | | beforeUpload(file) { |
| | | if (file.type.indexOf("image/") == -1) { |
| | | this.msgError("æä»¶æ ¼å¼é误ï¼è¯·ä¸ä¼ å¾çç±»å,å¦ï¼JPGï¼PNGåç¼çæä»¶ã"); |
| | | } else { |
| | | const reader = new FileReader(); |
| | | reader.readAsDataURL(file); |
| | | reader.onload = () => { |
| | | this.options.img = reader.result; |
| | | }; |
| | | } |
| | | }, |
| | | // ä¸ä¼ å¾ç |
| | | uploadImg() { |
| | | this.$refs.cropper.getCropBlob(data => { |
| | | let formData = new FormData(); |
| | | formData.append("avatarfile", data); |
| | | uploadAvatar(formData).then(response => { |
| | | if (response.code === 200) { |
| | | this.open = false; |
| | | this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl; |
| | | this.msgSuccess("ä¿®æ¹æå"); |
| | | } else { |
| | | this.msgError(response.msg); |
| | | } |
| | | this.$refs.cropper.clearCrop(); |
| | | }); |
| | | }); |
| | | }, |
| | | // 宿¶é¢è§ |
| | | realTime(data) { |
| | | this.previews = data; |
| | | } |
| | | } |
| | | }; |
| | | </script> |