¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <div> |
| | | <el-upload |
| | | class="avatar-uploader" |
| | | accept=".png,.jpg,.jpeg" |
| | | :style="customStyle" |
| | | action="" |
| | | :auto-upload="false" |
| | | :show-file-list="false" |
| | | :on-change='openUpdateIcon'> |
| | | <img v-if=" file.imgurlfull" style="width: 100%;" :src="file.imgurlfull" :style="customStyle" class="avatar"> |
| | | <div v-else :style="customStyle"> |
| | | <i class="el-icon-plus avatar-uploader-icon"></i> |
| | | <div class="tips-style">{{ tipsLabel }}</div> |
| | | </div> |
| | | </el-upload> |
| | | <el-dialog |
| | | append-to-body |
| | | :close-on-click-modal="false" |
| | | title="ä¸ä¼ å¾ç" |
| | | :visible.sync="updateImg" |
| | | width="1000px" |
| | | class="icon-dialog-wrapper dialong-com-style"> |
| | | <ImageCropper ref="iconShot" v-if="updateImg" :imgSrc="img" > |
| | | </ImageCropper> |
| | | <span slot="footer" class="dialog-footer"> |
| | | <el-button v-if="loading">å æ¶</el-button> |
| | | <el-button v-else @click="updateImg = false">å æ¶</el-button> |
| | | <el-button :loading="loading" type="primary" @click="uploadIcon">ç¡® å®</el-button> |
| | | </span> |
| | | </el-dialog> |
| | | </div> |
| | | </template> |
| | | |
| | | <script> |
| | | import ImageCropper from '@/components/common/ImageCropper' |
| | | import { upload } from '@/api/system/common' |
| | | export default { |
| | | components: { ImageCropper }, |
| | | props: { |
| | | file: { |
| | | type: Object, |
| | | default: () => {} |
| | | }, |
| | | tipsLabel: '', |
| | | customStyle: { |
| | | type: String, |
| | | default: 'width: 90px; height: 90px;' |
| | | }, |
| | | uploadData: Object |
| | | }, |
| | | data () { |
| | | return { |
| | | loading: false, |
| | | fileInfo:{}, |
| | | img: null, |
| | | updateImg: false, |
| | | imageSrc: null, |
| | | uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/visitsAdmin/cloudService/public/upload' |
| | | } |
| | | }, |
| | | |
| | | methods: { |
| | | uploadIcon () { |
| | | // è·åè£åªåçå¾ç |
| | | this.$refs.iconShot.getImagecropper().getCropBlob((fileData) => { // è·åå½åè£åªå¥½çæ°æ® |
| | | // æ³¨æ¤æ¶çdataæ¯ä¸ä¸ªBlobæ°æ®ï¼é¨åæ¥å£æ¥æ¶çæ¯File转åçFormDataæ°æ® |
| | | console.log(fileData) |
| | | const formData = new FormData() |
| | | |
| | | formData.append('folder', 'member') |
| | | if(this.uploadData.isFace || this.uploadData.isFace == 0){ |
| | | formData.append('isFace', 0) |
| | | } |
| | | formData.append( |
| | | 'file', |
| | | new File( |
| | | [fileData], // å°Blobç±»å转åæFileç±»å |
| | | this.fileInfo.name, // 设置Fileç±»åçæä»¶åç§° |
| | | { type: this.fileInfo.type } // 设置Fileç±»åçæä»¶ç±»å |
| | | ) |
| | | ) |
| | | this.loading = true |
| | | upload(formData).then(res => { |
| | | this.loading = false |
| | | console.log(res) |
| | | this.file.imgurl = res.imgaddr |
| | | this.file.imgurlfull = res.url |
| | | this.$message.success('ä¸ä¼ æå') |
| | | this.imageSrc = res.url |
| | | this.updateImg = false |
| | | this.$emit('uploadSuccess', { imgurl: res.imgaddr, imgurlfull: res.url, name: res.originname }) |
| | | this.$emit('uploadEnd') |
| | | }, () => { |
| | | this.loading = false |
| | | }) |
| | | }) |
| | | }, |
| | | |
| | | // ä¸ä¼ å¾ç |
| | | openUpdateIcon (file, fileList) { |
| | | const isJPG = file.raw.type === 'image/jpeg' || file.raw.type === 'image/png' |
| | | const isLt2M = file.size / 1024 / 1024 < 5 |
| | | if (!isJPG) { |
| | | this.$message.error('ä¸ä¼ 头åå¾çåªè½æ¯ JPG/PNG æ ¼å¼!') |
| | | return false |
| | | } |
| | | if (!isLt2M) { |
| | | this.$message.error('ä¸ä¼ 头åå¾ç大å°ä¸è½è¶
è¿ 5MB!') |
| | | return false |
| | | } |
| | | // ä¸ä¼ æååå°å¾çå°åèµå¼ç»è£åªæ¡æ¾ç¤ºå¾ç |
| | | this.$nextTick(async () => { |
| | | // base64æ¹å¼ |
| | | // this.option.img = await fileByBase64(file.raw) |
| | | this.fileInfo.name = file.name |
| | | this.fileInfo.type = file.type |
| | | console.log(file, fileList) |
| | | this.img = URL.createObjectURL(file.raw) |
| | | // this.loading = false |
| | | this.updateImg = true |
| | | }) |
| | | }, |
| | | handleAvatarSuccess (res, file) { |
| | | if (res.code == 200) { |
| | | const { data } = res |
| | | this.file.imgurl = data.imgaddr |
| | | this.file.imgurlfull = data.url |
| | | this.$message.success('ä¸ä¼ æå') |
| | | this.imageSrc = data.url |
| | | this.updateImg = true |
| | | // this.$emit('uploadSuccess', { imgurl: data.imgaddr, imgurlfull: data.url, name: data.originname }) |
| | | } else { |
| | | this.$message.error('ä¸ä¼ 失败') |
| | | } |
| | | this.$emit('uploadEnd') |
| | | }, |
| | | uploadError () { |
| | | this.$message.error('ä¸ä¼ 失败') |
| | | this.$emit('uploadEnd') |
| | | }, |
| | | // // æ¦æª |
| | | beforeAvatarUpload (file) { |
| | | this.$emit('uploadBegin') |
| | | return true |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | $image-width: 90px; |
| | | .avatar-uploader { |
| | | width: $image-width; |
| | | height: $image-width; |
| | | } |
| | | ::v-deep .el-upload { |
| | | border: 1px dashed #d9d9d9; |
| | | border-radius: 6px; |
| | | cursor: pointer; |
| | | position: relative; |
| | | overflow: hidden; |
| | | } |
| | | .avatar-uploader .el-upload:hover { |
| | | border-color: #409EFF; |
| | | } |
| | | .avatar-uploader-icon { |
| | | line-height: 90px; |
| | | font-size: 28px; |
| | | color: #8c939d; |
| | | width: $image-width; |
| | | height: $image-width; |
| | | text-align: center; |
| | | } |
| | | .avatar { |
| | | width: 100% !important; |
| | | height: auto !important; |
| | | display: block; |
| | | } |
| | | .tips-style { |
| | | height: 13px; |
| | | font-size: 13px; |
| | | font-weight: 400; |
| | | color: #999999; |
| | | line-height: 13px; |
| | | } |
| | | </style> |
| | | <style lang="scss" scoped> |
| | | ::v-deep .el-upload--picture-card{ |
| | | width: 90px !important; |
| | | height: 90px !important; |
| | | } |
| | | ::v-deep .el-upload-list__item { |
| | | width: 90px !important; |
| | | height: 90px !important; |
| | | } |
| | | .icon { |
| | | -webkit-transform: translate(-50%,-50%); |
| | | -ms-transform: translate(-50%,-50%); |
| | | transform: translate(0%, -85%); |
| | | } |
| | | ::v-deep .el-upload-list__item { |
| | | width: 90px !important; |
| | | height: 90px !important; |
| | | } |
| | | </style> |