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

<script>

    import { getFilePolicy }           from '@/api/AlCloud.js';
    //  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) {
                if (!params) {
                    return;
                }
                const suffixList = params.file.name.split('.').pop();
                const options = {
                    prefix: 'resource/portal/',
                    suffix: suffixList,
                };
                const loading = this.$loading({background:"rgba(0, 0, 0, 0.7)"});
                getFilePolicy(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);

                        this.$axios.post(multipartParams.url, FormDataForAl).then(alRes => {
                            if (alRes.status === 200) {
                                this.afterUploadImg = `${multipartParams.url}/${multipartParams.key}`;
                                this.$emit('getFileUrl', this.afterUploadImg);
                                loading.close();
                            }else {
                                loading.close();
                            }
                        }).catch(e => {
                            loading.close();
                        });
                    }else {
                        loading.close();
                    }
                });
            }
        },
    }
</script>

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

        .avatar-uploader .el-upload {
            background: #fff;
            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>
