<template>
    <div class="response-uploadAlCloud">
        <el-upload
                :http-request="uploadFun"
                :show-file-list="false"
                action="aaa"
                class="avatar-uploader"
                ref="upload">
            <img :src="imageUrl" alt="头像" class="avatar" v-if="imageUrl">
            <i class="el-icon-plus avatar-uploader-icon" v-else></i>
        </el-upload>
        <el-button @click="btnClick(btnType)" class="delete-btn" size="small" type="primary" v-if="showBtnFlag&&btnType==='delete'">删除</el-button>
    </div>
</template>

<script>

    import { getUploadImg }           from '@/api/AlCloud.js';
    import { mapGetters, mapActions } from 'vuex';
    import axios                      from 'axios';
    //  describe: 上传组件 author: Wgy date:2020-03-30
    /*
    * showBtnFlag:是否显现按钮
    * btnType:按钮类型 显示按钮 默认type为 'delete'
    *imageUrl:入参图片
    * 父页面 example：<upload-file  :imageUrl="imageUrl1" @btnFun="btnFun" @getFileUrl="getFileFun"></upload-file>
    * */

    export default {
        name: "UploadAlCloud",
        props: {
            showBtnFlag: {
                type: Boolean,
                default: true
            },
            btnType: {
                type: String,
                default: 'delete',
            },
            imageUrl: {
                type: String,
                default: '',
            },
        },
        data() {
            return {
                afterUploadImg: '',
            };
        },
        methods: {
            //按钮
            btnClick(data){
                let response ={
                    type:data,
                    imageUrl:this.imageUrl
                }
                this.$emit('btnFun', response);
            },
            // 自定义上传
            uploadFun(params) {
                console.log(params);
                if (!params) {
                    return;
                }
                const suffixList = params.file.name.split('.').pop();
                const options = {
                    prefix: 'resource/' + this.getTenantCode + '/',
                    suffix: suffixList,
                };
                const loading = this.$loading({background:"rgba(0, 0, 0, 0.7)"});
                getUploadImg(options).then((res) => {
                    if (res.code === 0) {
                        // 二进制文件通过forData对象进行传递
                        const FormDataForAl = new FormData();
                        const multipartParams = Object.assign({}, res.data, {
                            Filename:              `images/${params.filename}`,
                            success_action_status: '200',
                        });
                        // 参数数据
                        FormDataForAl.append('key', multipartParams.key);
                        FormDataForAl.append('policy', multipartParams.policy);
                        FormDataForAl.append('signature', multipartParams.signature);
                        FormDataForAl.append('OSSAccessKeyId', multipartParams.accessid);
                        FormDataForAl.append('success_action_status', multipartParams.success_action_status);
                        // OSS要求, file放到最后
                        FormDataForAl.append('file', params.file);

                        axios.post(multipartParams.uploadUrl, FormDataForAl).then(alRes => {
                            if (alRes.status === 200) {
                                this.afterUploadImg = `${multipartParams.downloadUrl}/${multipartParams.key}`;
                                this.$emit('getFileUrl', this.afterUploadImg);
                                loading.close();
                            }else {
                                loading.close();
                            }
                        });
                    }else {
                        loading.close();
                    }
                });
            }
        },
        computed:   {
            getcode() {
                return this.getTenantCode;
            },
            ...mapGetters(['getTenantCode']),
        },
    }
</script>

<style lang="scss">
    .uploadAlCloud {
        display: block;

        .avatar-uploader .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 {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        @media screen and (max-width: 1440px){
            .avatar{
                width: 140px; // 必须指定宽高
                height: 140px;
                line-height: 140px;
            }
            .avatar-uploader-icon{
                width: 140px;
                height: 140px;
                line-height: 140px;
            }
        }
    }
</style>
