fix
13404089107
2025-04-11 eb296bfbd3f18dff5a2ee60b42f8415d51c897b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<template>
  <el-dialog
    :title="type === 'add' ? '新增资料' : type === 'edit' ? '编辑资料' : '资料详情'"
    :visible.sync="dialogVisible"
    width="40%"
    @close="handleClose"
  >
    <el-form ref="uploadForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="镇(街道)" prop="street">
        <el-select :disabled="type === 'detail'" v-model="form.street" placeholder="请选择" @change="handleStreetChange">
          <el-option v-for="item in streetOptions" :key="item.dictCode" :label="item.dictLabel" :value="item.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资料名称" prop="name">
        <el-input v-model="form.name" :disabled="type === 'detail'" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="资料文件" prop="file">
        <el-upload
          :disabled="type === 'detail'"
          ref="upload"
          class="upload-demo"
          drag
          :action="baseUrl + '/common/upload'"
          :headers="{
            'Authorization': 'Bearer ' + token
          }"
          :file-list="fileList"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          :on-change="handleFileChange"
          :limit="1"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">
            支持扩展名:.doc .docx .pdf .jpg .png .xlsx .xls
          </div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="type !== 'detail'" type="primary" @click="submitUpload">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
 
<script>
import { getToken } from '@/utils/auth'
import { add, update } from "@/api/storing-data";
 
export default {
  name: 'UploadDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'add',
    },
    streetOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      token: getToken(),
      dialogVisible: false,
      fileList: [],
      form: {
        street: '',
        name: '',
        file: null,
        attachName: '',
        attachUrl: ''
      },
      baseUrl: process.env.VUE_APP_BASE_API,
      rules: {
        street: [
          { required: true, message: '请选择镇(街道)', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入资料名称', trigger: 'blur' }
        ],
        file: [
          { required: true, message: '请选择上传文件', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val;
    },
    dialogVisible(val) {
      this.$emit('update:visible', val);
    }
  },
  methods: {
    handleClose() {
      this.form = {
        street: '',
        name: '',
        file: null,
        attachName: '',
        attachUrl: ''
      };
      this.$refs.uploadForm && this.$refs.uploadForm.resetFields();
      // 清空文件列表
      this.$refs.upload && this.$refs.upload.clearFiles();
    },
    handleFileChange(file) {
      this.form.file = file.raw;
      this.form.attachName = file.name;
      // 手动触发文件字段验证
      this.$refs.uploadForm.validateField('file');
    },
    // 处理上传成功
    handleUploadSuccess(response, file) {
      if (response.code === 200) {
        this.form.attachUrl = response.url;
        this.$message.success('文件上传成功');
      } else {
        this.$message.error(response.msg || '文件上传失败');
      }
    },
    // 处理上传失败
    handleUploadError(err) {
      this.$message.error('文件上传失败');
    },
    submitUpload() {
      this.$refs.uploadForm.validate(valid => {
        if (!valid) {
          return;
        }
        if (!this.form.attachUrl) {
          this.$message.error('请先上传文件');
          return;
        }
        // 构建提交数据
        const submitData = {
          street: this.form.street,
          name: this.form.name,
          attachName: this.form.attachName,
          attachUrl: this.form.attachUrl
        };
 
        if (this.type === 'add') {
          add(submitData).then(res => {
            this.$emit('success', res.data);
            this.handleClose();
          });
        } else {
          update(submitData).then(res => {
            this.$emit('success', res.data);
            this.handleClose();
          });
        }
      });
    },
    handleStreetChange(value) {
      // 处理镇(街道)选择变化后的逻辑
    }
  }
}
</script>
 
<style scoped>
 
.el-upload {
  width: 100%;
}
.el-upload-dragger {
  width: 100%;
}
</style>