123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <template>
- <div id="upload">
- <el-upload
- v-if="url"
- ref="upload"
- :action="url"
- :list-type="listType"
- :file-list="fileList"
- :limit="limit"
- :on-exceed="outLimit"
- :on-preview="handlePictureCardPreview"
- :on-remove="handleRemove"
- :on-success="onSuccess"
- :show-file-list="showList"
- :accept="accept"
- >
- <el-button size="small" type="primary" v-if="isBtn">点击上传</el-button>
- <template v-else>
- <i class="el-icon-plus"></i>
- </template>
- <!-- <template #tip v-if="tip">
- {{ tip }}
- </template> -->
- </el-upload>
- <el-dialog :visible.sync="dialogVisible">
- <img width="100%" :src="dialogImageUrl" alt="" />
- </el-dialog>
- </div>
- </template>
- <script>
- import _ from 'lodash';
- export default {
- name: 'upload',
- props: {
- url: { type: null },
- limit: { type: Number },
- data: { type: null },
- type: { type: String },
- isBtn: { type: Boolean, default: false },
- showList: { type: Boolean, default: true },
- accept: { type: String },
- tip: { type: String, default: undefined },
- listType: { type: String, default: 'picture-card' },
- },
- components: {},
- data: () => ({
- dialogVisible: false,
- dialogImageUrl: '',
- fileList: [],
- }),
- created() {
- if (this.data) {
- this.defalutProcess(this.data);
- }
- },
- watch: {
- data: {
- handler(val) {
- this.defalutProcess(val);
- },
- },
- },
- computed: {},
- methods: {
- handlePictureCardPreview(file) {
- this.dialogImageUrl = file.url;
- this.dialogVisible = true;
- },
- handleRemove(file) {
- this.$emit('upload', { type: this.type, data: file });
- return true;
- },
- outLimit() {
- this.$message.error('只允许上传1个文件');
- },
- onSuccess(response, file, fileList) {
- //将文件整理好传回父组件
- this.$emit('upload', { type: this.type, data: response });
- },
- defalutProcess(val) {
- if (typeof val === 'object' && _.get(val, length) !== undefined && val.length > 0) {
- let newArr = [];
- val.map(item => {
- let object = {};
- object.name = item.name;
- object.url = `${item.uri}`;
- newArr.push(object);
- });
- this.$set(this, `fileList`, newArr);
- } else if (typeof val === 'object' && _.get(val, length) === undefined) {
- let object = {};
- object.name = val.name;
- object.url = `${val.uri}`;
- this.$set(this, `fileList`, [object]);
- } else if (typeof val === 'string') {
- this.$set(this, `fileList`, [{ name: '附件', url: val }]);
- }
- },
- },
- };
- </script>
- <style lang="less" scoped></style>
|