|
@@ -0,0 +1,154 @@
|
|
|
+<template>
|
|
|
+ <div class="clearfix">
|
|
|
+ <a-upload
|
|
|
+ :action="action"
|
|
|
+ :fileList="fileList"
|
|
|
+ multiple
|
|
|
+ @change="handleChange"
|
|
|
+ :customRequest="customRequest"
|
|
|
+ :remove="handleRemove"
|
|
|
+ :beforeUpload="beforeUpload">
|
|
|
+ <div class="file-btn" v-if="fileList.length < maxSize">
|
|
|
+ <a-icon type="paper-clip" />
|
|
|
+ <div class="ant-upload-text">点击上传</div>
|
|
|
+ </div>
|
|
|
+ </a-upload>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+import { uploadFile } from '@/api/upms/file'
|
|
|
+export default {
|
|
|
+ name: 'Upload',
|
|
|
+ model: {
|
|
|
+ prop: 'value',
|
|
|
+ event: 'change'
|
|
|
+ },
|
|
|
+ data () {
|
|
|
+ return {
|
|
|
+ previewVisible: false,
|
|
|
+ previewImage: '',
|
|
|
+ fileList: [],
|
|
|
+ isWatch: true,
|
|
|
+ action: process.env.VUE_APP_API_BASE_URL + '/upms/files/upload'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ props: {
|
|
|
+ maxSize: {
|
|
|
+ type: Number,
|
|
|
+ default: 1
|
|
|
+ },
|
|
|
+ value: {
|
|
|
+ type: Array
|
|
|
+ }
|
|
|
+ },
|
|
|
+ watch: {
|
|
|
+ fileList: {
|
|
|
+ handler (newV, oldV) {
|
|
|
+ this.fileList = newV
|
|
|
+ this.$emit('change', newV)
|
|
|
+ },
|
|
|
+ deep: true,
|
|
|
+ immediate: true
|
|
|
+ },
|
|
|
+ value: {
|
|
|
+ deep: true,
|
|
|
+ immediate: true,
|
|
|
+ handler: function (newV) {
|
|
|
+ // 数据为空的三种情况
|
|
|
+ if (this.isWatch) {
|
|
|
+ this.isWatch = false
|
|
|
+ if (newV === null || newV === '' || newV === undefined) {
|
|
|
+ this.fileList = []
|
|
|
+ return
|
|
|
+ }
|
|
|
+ this.fileList = this.BaseTool.UPLOAD.transImg(newV)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ base (fileList) {
|
|
|
+ this.fileList = fileList
|
|
|
+ },
|
|
|
+ handleCancel () {
|
|
|
+ this.previewVisible = false
|
|
|
+ },
|
|
|
+ handlePreview (file) {
|
|
|
+ this.previewImage = file.url || file.thumbUrl
|
|
|
+ this.previewVisible = true
|
|
|
+ },
|
|
|
+ handleChange ({ fileList }) {
|
|
|
+ // this.fileList = fileList
|
|
|
+ // this.$emit('catchFile', fileList)
|
|
|
+ },
|
|
|
+ async customRequest (data) {
|
|
|
+ const formData = new FormData()
|
|
|
+ formData.append('file', data.file)
|
|
|
+ data.onProgress()
|
|
|
+ this.$loading.show()
|
|
|
+ uploadFile(formData).then(res => {
|
|
|
+ this.$loading.hide()
|
|
|
+ data.onSuccess()
|
|
|
+ this.fileList.push({
|
|
|
+ uid: '-1',
|
|
|
+ name: res.data.name,
|
|
|
+ status: 'done',
|
|
|
+ url: this.BaseTool.Constant.FILE_URL + res.data.url
|
|
|
+ })
|
|
|
+ this.$emit('catchFile', this.fileList)
|
|
|
+ })
|
|
|
+ },
|
|
|
+ handleRemove (file) {
|
|
|
+ const index = this.fileList.indexOf(file)
|
|
|
+ const newFileList = this.fileList.slice()
|
|
|
+ newFileList.splice(index, 1)
|
|
|
+ this.fileList = newFileList
|
|
|
+ this.$emit('catchFile', this.fileList)
|
|
|
+ return true
|
|
|
+ },
|
|
|
+ beforeUpload (file, fileList) {
|
|
|
+ const reg = /\.(doc|docx|html|pdf|txt|mht|xlsx|xls|jpg|png|zip|png|jpg|gif|jpeg|webp|zip|rar|7z|dxf)(\?.*)?$/
|
|
|
+ // if (this.fileType === 1) {
|
|
|
+ // reg = /\.(jpg|png|gif)(\?.*)?$/
|
|
|
+ // }
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ if (reg.test(file.name)) {
|
|
|
+ resolve()
|
|
|
+ } else {
|
|
|
+ this.$message.error(`请上传正确的文件格式`)
|
|
|
+ reject(new Error('请上传正确的文件格式'))
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+</script>
|
|
|
+<style>
|
|
|
+ /* you can make up upload button and sample style by using stylesheets */
|
|
|
+ .ant-upload-select-picture-card i {
|
|
|
+ font-size: 32px;
|
|
|
+ color: #999;
|
|
|
+ }
|
|
|
+
|
|
|
+ .ant-upload-select-picture-card .ant-upload-text {
|
|
|
+ margin-top: 8px;
|
|
|
+ color: #666;
|
|
|
+ }
|
|
|
+ .ant-upload-btn {
|
|
|
+ margin-top: 20px;
|
|
|
+ }
|
|
|
+</style>
|
|
|
+<style lang="less" scoped>
|
|
|
+.file-btn {
|
|
|
+ border-radius: 3px;
|
|
|
+ background-color: rgba(242, 247, 255, 1);
|
|
|
+ color: rgba(8, 123, 232, 1);
|
|
|
+ border: 1px dashed rgba(8, 123, 232, 1);
|
|
|
+ display: flex;
|
|
|
+ width: 129px;
|
|
|
+ height: 40px;
|
|
|
+ justify-content: center;
|
|
|
+ align-items: center;
|
|
|
+}
|
|
|
+</style>
|