| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <template>
- <div class="singleImageUpload2 upload-container">
- <el-upload
- v-show="uploaderShow"
- v-loading="uploaderLoading"
- class="image-uploader"
- :action="uploaderRequestUrl"
- name="file"
- :headers="uploaderHeaders"
- :data="uploaderFormData"
- :show-file-list="false"
- :before-upload="uploaderHandleBefore"
- :on-success="uploaderHandleSuccess"
- :disabled="uploaderDisabled"
- :style="isImgUpload ? `width:${width};min-height:${height};` : ''"
- >
- <template v-if="isImgUpload">
- <el-image
- v-if="uploaderImageUrl"
- :src="uploaderImageUrl"
- class="image-preview"
- fit="contain"
- :style="`width:${width};min-height:${height};display: block;`"
- ></el-image>
- <i
- v-else
- class="el-icon-plus uploader-icon"
- :style="`width:${width};height:${height};line-height:${height};`"
- />
- </template>
- <template v-else>
- <el-button type="primary">{{ uploadBtnTitle }}</el-button>
- </template>
- </el-upload>
- <template v-if="isImgUpload">
- <div v-show="!uploaderShow" class="image-show">
- <el-image :src="uploaderImageUrl" alt="" :width="width" :height="height"></el-image>
- </div>
- </template>
- <div class="previewImg">
- <el-button v-show="uploaderImageUrl" type="info" class="previewImgButton" @click="previewImg">{{ $t('shop.imagePreview') }}</el-button>
- </div>
- <el-dialog append-to-body top="5vh" :visible.sync="dialogVisible" custom-class="custom-dialog">
- <div class="previewImage">
- <el-image width="60%" :src="uploaderImageUrl" alt=""></el-image>
- </div>
- </el-dialog>
- </div>
- </template>
- <script>
- import { getToken } from '@/api/upload'
- export default {
- name: 'Uploader',
- props: {
- isImgUpload: {
- type: Boolean,
- default: true
- },
- uploadBtnTitle: {
- type: String,
- default: '上传'
- },
- requestRoute: {
- type: String,
- required: true
- },
- defaultImageUrl: {
- type: String,
- default: null
- },
- uploaderSuccessCanChange: {
- type: Boolean,
- default: true
- },
- width: {
- type: String,
- default: '100px'
- },
- height: {
- type: String,
- default: '100%'
- }
- },
- data() {
- return {
- uploaderShow: true,
- uploaderLoading: false,
- uploaderFormData: {
- 'uploadToken': ''
- },
- uploaderRequestUrl: process.env.VUE_APP_BASE_API + `/${this.requestRoute}`,
- uploaderHeaders: {
- 'Device-Type': 'pc',
- 'Suppress-Response-Code': '1',
- 'Authorization': ''
- },
- uploaderDisabled: false,
- successImageUrl: null,
- uploaderImage: null,
- dialogVisible: false,
- uploadedImages: []
- }
- },
- computed: {
- uploaderImageUrl() {
- if (this.uploaderImage != null) {
- return this.uploaderImage
- }
- if (this.successImageUrl !== null) {
- return this.successImageUrl
- } else if (this.defaultImageUrl !== null) {
- // this.uploaderShow = false
- // this.uploaderDisabled = true
- // this.uploaderLoading = false
- return this.defaultImageUrl
- } else {
- return ''
- }
- }
- },
- watch: {
- defaultImageUrl(newVal, oldVal) {
- this.uploaderImage = newVal
- // console.log(oldVal)
- }
- },
- created() {
- console.log(this.uploaderImage)
- },
- methods: {
- previewImg(){
- this.dialogVisible = true;
- },
- uploaderHandleBefore() {
- const auth_token = localStorage.accessToken
- this.uploaderHeaders.Authorization = 'Bearer ' + auth_token
- return new Promise((resolve, reject) => {
- getToken().then(response => {
- console.log(response)
- this.uploaderFormData.uploadToken = response.data
- resolve(true)
- }).catch(() => {
- reject(false)
- })
- })
- },
- uploaderHandleSuccess(response, file) {
- // console.log(response)
- if (response.success) {
- this.$message({
- message: 'Successful',
- type: 'success'
- })
- this.successImageUrl = URL.createObjectURL(file.raw)
- if (!this.uploaderSuccessCanChange) {
- this.uploaderShow = false
- this.uploaderDisabled = true
- this.uploaderLoading = false
- }
- this.$emit('on-success', response.data)
- } else {
- this.$message({
- message: response.data.message,
- type: 'warning'
- })
- this.uploaderLoading = false
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .upload-container {
- width: 100%;
- height: 100%;
- position: relative;
- margin-bottom: 50px;
- .image-uploader {
- height: 100%;
- }
- .image-preview {
- width: 100%;
- height: 100%;
- position: absolute;
- left: 0px;
- top: 0px;
- border: 1px dashed #d9d9d9;
- .image-preview-wrapper {
- position: relative;
- width: 100%;
- height: 100%;
- img {
- width: 100%;
- height: 100%;
- }
- }
- .image-preview-action {
- position: absolute;
- width: 100%;
- height: 100%;
- left: 0;
- top: 0;
- cursor: default;
- text-align: center;
- color: #fff;
- opacity: 0;
- font-size: 20px;
- background-color: rgba(0, 0, 0, .5);
- transition: opacity .3s;
- cursor: pointer;
- text-align: center;
- line-height: 200px;
- .el-icon-delete {
- font-size: 36px;
- }
- }
- &:hover {
- .image-preview-action {
- opacity: 1;
- }
- }
- }
- }
- .previewImage{
- display: flex;
- justify-content: center;
- align-items: center;
- }
- .previewImg{
- display: flex;
- justify-content: center;
- .previewImgButton{
- position: absolute;
- bottom: -50px;
- }
- }
- .custom-dialog {
- //background: rgba(0, 0, 0, 0); /* 半透明的背景 */
- background-color: transparent!important;
- }
- .custom-dialog {
- background-color: transparent; /* 使对话框的背景透明 */
- box-shadow: none; /* 移除阴影 */
- }
- .custom-dialog .el-dialog__header,
- .custom-dialog .el-dialog__body,
- .custom-dialog .el-dialog__footer {
- background-color: transparent; /* 透明的头部、内容和底部 */
- }
- .custom-dialog .el-dialog__header {
- border-bottom: none; /* 移除对话框头部的边框 */
- }
- .custom-dialog .el-dialog__footer {
- border-top: none; /* 移除底部的边框 */
- }
- </style>
|