123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <template>
- <div>
- <el-upload
- v-if="!isCheck"
- ref="fileUpload"
- :data="data"
- :auto-upload="autoUpload"
- :class="{ 'el-upload-hidden': !chooseState }"
- :action="action"
- :headers="headers"
- :on-success="uploadSuccess"
- :before-upload="beforeUpload"
- :on-error="uploadError"
- :limit="limit"
- :multiple="multiple"
- :accept="accept"
- :file-list="fileList"
- >
- <div v-if="tip" slot="tip" class="el-upload__tip">{{ tip }}</div>
- <el-button v-if="!isCheck" size="mini" type="primary">选择文件</el-button>
- </el-upload>
- <div v-for="(file, index) in dataList" :key="index" class="file-list">
- <span>{{ file.name }} </span>
- <div>
- <el-button type="text" size="small" @click="handlePreview(file.response)">预览</el-button>
- <el-button type="text" size="small" style="color: red" @click="handleRemove(index)">删除</el-button>
- </div>
- </div>
- </div>
- </template>
- <script>
- import { mapGetters } from 'vuex'
- export default {
- name: 'UploadFile',
- props: {
- tip: {
- type: String,
- default: ''
- },
- multiple: {
- type: Boolean,
- default: false
- },
- limit: {
- type: Number,
- default: 1
- },
- accept: {
- type: String,
- default: '.docx,.pptx,.pdf'
- },
- listType: {
- type: String,
- default: 'picture-card'
- },
- fileList: {
- type: Array,
- default: () => []
- },
- uuid: {
- type: Number,
- default: 0
- },
- beforeUpload: {
- type: Function,
- default: () => true
- },
- autoUpload: {
- type: Boolean,
- default: true
- },
- data: {
- type: Object,
- default: () => ({})
- },
- mode: {
- type: String,
- default: 'brand'
- },
- list: {
- type: Array,
- default: () => []
- },
- isCheck: {
- type: Boolean,
- default: () => true
- }
- },
- data() {
- return {
- dialogVisible: false,
- dialogImageUrl: '',
- dataList: [],
- iframUrl: ''
- }
- },
- computed: {
- ...mapGetters(['token']),
- chooseState() {
- return this.dataList.length < this.limit
- },
- action() {
- return process.env.VUE_APP_BASE_API + '/formData/MultiPictareaddData'
- },
- headers() {
- return {
- 'X-Token': this.token
- }
- }
- },
- watch: {
- list: {
- handler(val) {
- console.log(val)
- this.dataList = val
- },
- deep: true
- }
- },
- methods: {
- // 上传成功
- uploadSuccess(response, file, fileList) {
- if (file.response.data) {
- this.dataList.push(file)
- console.log(response, file)
- this.$emit('success', { fileList: this.dataList })
- } else {
- this.$message({
- type: 'error',
- message: '上传失败'
- })
- }
- },
- // 删除
- handleRemove(index) {
- this.dataList = this.dataList.filter((_, i) => index !== i)
- this.$emit('remove', { fileList: this.dataList })
- },
- // 变化
- handleChange(file, fileList) {
- this.dataList = fileList
- this.$emit('change', { fileList: this.dataList })
- },
- // 上传失败
- uploadError(err, file, fileList) {
- this.dataList = fileList
- this.$emit('error', { err, file, fileList })
- },
- handlePreview(f) {
- if (f.data.indexOf('.docx') !== -1 || f.data.indexOf('.pptx') !== -1) {
- window.open(`https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(f.data)}`)
- } else {
- window.open(f.data)
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- ::v-deep .el-upload-list {
- display: none;
- }
- ::v-deep .el-upload__tip {
- position: static;
- }
- .file-list {
- display: flex;
- justify-content: space-between;
- padding-right: 30px;
- margin-top: 10px;
- }
- </style>
|