<template>
    <div>
        <a-upload
            :action="action"
            :show-file-list="false"
            :auto-upload="true"
            :headers="headers"
            multiple
            @progress="progress"
            @success="upload"
        >
            <template #upload-button>
                <a-button size="small" type="text">
                    <template #icon>
                        <icon-loading v-if="loading" />
                        <icon-upload v-else />
                    </template>
                    <template #default> 上传文件</template>
                </a-button>
            </template>
        </a-upload>
    </div>
</template>

<script>
export default {
    props: {
        action: {
            type: String,
            default: '/api/oss/upload'
        },
        headers: {
          type:Object,
          default(){
            return  { Authorization: 'Bearer ' + localStorage.getItem('token') }
          }
        }},
    data() {
        return {
            loading: false,

        }
    },
    methods: {
        progress(e, progress) {
            if (progress.loaded > 0) {
                this.loading = true
            }
        },
        upload(res) {
            const code = res.response.code
            this.loading = false
            if (code === 200) {
                this.$emit('ok', res.response.data.file)
            } else {
                this.$notification.error('上传错误')
            }
        }
    }
}
</script>

<style lang="scss" scoped></style>
