zhouwenxuan
2024-02-01 b91be1b5fcf1f5e571d3126969e6c6bc785719f3
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<template>
  <div class="notice">
    <el-dialog
        v-model="dialogVisible"
        :title="state.title"
        width="550px"
        :before-close="handleClose"
    >
      <el-form :model="state.form" size="default" ref="formRef" :rules="state.formRules" label-width="110px" >
        <el-form-item label="整改时间:" prop="rectifyTime" >
          <el-date-picker
              style="width: 100%"
              v-model="state.form.rectifyTime"
              type="date"
              value-format="YYYY-MM-DD 00:00:00"
              placeholder="请选择日期"
              size="large"
          />
        </el-form-item>
        <el-form-item label="整改人:"  prop="rectifyPerson">
          <el-input v-model="state.form.rectifyPerson" show-word-limit type="text" size="large" placeholder="请输入整改人" />
        </el-form-item>
        <el-form-item label="整改说明:" prop="reason">
          <el-input v-model="state.form.reason" show-word-limit type="text" size="large" placeholder="请输入整改说明"/>
        </el-form-item>
        <el-form-item prop="fileList">
          <el-upload accept=".pdf"
                     :action="state.uploadUrl"
                     :disabled="state.disabled"
                     :headers="state.header"
                     method="post"
                     :limit="state.imgLimit"
                     :before-upload="beforeUpload"
                     :on-success="handleAvatarSuccess"
                     v-model:file-list="state.form.fileList"
                     :on-remove="handleRemove"
                     :data="state.uploadData"
                     :on-preview="handlePreview"
          >
            <el-button type="primary">附件上传</el-button>
            <template #tip>
              <div class="el-upload__tip">
                只能上传pdf类型,且只能上传一个
              </div>
            </template>
          </el-upload>
 
        </el-form-item>
      </el-form>
      <template #footer >
        <span class="dialog-footer">
            <el-button @click="handleClose" size="default">取 消</el-button>
            <el-button type="primary"  @click="onSubmit" size="default" v-preReClick>确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import {defineEmits, nextTick, reactive, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import {getToken} from "@/utils/auth";
import {delAccessoryFile} from "@/api/projectManage/project";
import {addRate, addRecitification, editRecitification} from "@/api/projectManage/riskAnalysis";
const emit = defineEmits(["getList"]);
const formRef = ref(null)
const state = reactive({
  title: '',
  form: {
    id: null,
  },
  formRules:{
    rectifyTime: [{ required: true, message: '请选择整改时间', trigger: 'blur' }],
    rectifyPerson: [{ required: true, message: '请输入整改人', trigger: 'blur' }],
    fileList: [{ required: true, message: '请上传附件', trigger: 'blur' }],
    reason:[{ required: true, message: '请输入整改说明', trigger: 'blur' }],
  },
  imgLimit: 1,
  uploadUrl: import.meta.env.VITE_APP_BASE_API + '/manage/accessory-file/uploadFile',
  header: {
    Authorization: getToken()
  },
  uploadData: {
    moduleType: 11
  },
 
})
const dialogVisible = ref(false)
 
const openDialog = (type, value) => {
  state.uploadData.projectId = value.projectId;
  state.title = type === 'add' ? '新增' :  '编辑' ;
  if(state.title === '编辑'){
    state.form = value;
    state.form.fileList = [value.accessoryFile]
    state.form.fileList[0].name = value.accessoryFile.originName
  }
  dialogVisible.value = true
}
 
const beforeUpload = (file) => {
  let fileType = file.name.substring(file.name.lastIndexOf(".") + 1);
  if (fileType === 'pdf') {
  } else {
    ElMessage.error("文件类型必须为pdf格式")
    return false
  }
}
const handleAvatarSuccess = (res) => {
  if(res.code === 200){
    console.log("file",state.form.fileList)
    ElMessage({
      type: 'success',
      message: '文件上传成功'
    })
  }else {
    ElMessage({
      type: 'warning',
      message: '文件上传失败'
    })
  }
}
 
const handlePreview = (file) => {
  let path = "";
  if(file.path){
    path = file.path
  }else {
    path = file.response.data.path
 
  }
  const url = import.meta.env.VITE_APP_BASE_API + '/' + path
  axios.get( url,{
        headers:
            {
              'Content-Type': 'application/json',
              'Authorization':getToken(),
            },
        responseType: 'blob'
      }
  ).then(res=>{
    if (res) {
      const link = document.createElement('a')
      let blob = new Blob([res.data],{type: res.data.type})
      link.style.display = "none";
      link.href = URL.createObjectURL(blob); // 创建URL
      link.setAttribute("download", file.name);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    } else {
      this.$message.error('获取文件失败')
    }
  })
}
const onSubmit = async () => {
  const valid = await formRef.value.validate();
  if(valid){
    state.form.fileId = state.form.fileList ? state.form.fileList[0].response ? state.form.fileList[0].response.data.id : state.form.fileList[0].id : '';
    if(state.title === '新增'){
      const {id,fileList, ...data} = JSON.parse(JSON.stringify(state.form))
      data.projectId = state.uploadData.projectId;
      console.log("Add",data)
      const res = await addRecitification(data);
      if (res.code == 200) {
        ElMessage.success('保存成功')
        handleClose();
        emit('getList');
      } else {
        ElMessage.warning(res.message)
      }
    }else if(state.title === '编辑'){
      const {ileList, ...data} = JSON.parse(JSON.stringify(state.form))
      data.projectId = state.uploadData.projectId;
      const res = await editRecitification(data);
      if (res.code == 200) {
        ElMessage.success('编辑成功')
       handleClose();
        emit('getList');
      } else {
        ElMessage.warning(res.message)
      }
    }
  }
}
const handleRemove = async (file, uploadFiles) => {
  console.log("file",file)
  let accessoryFileId = "";
  if(file.id){
    accessoryFileId = file.id
  }else {
    accessoryFileId = file.response.data.id
 
  }
  const res = await delAccessoryFile(accessoryFileId)
  if(res.code == 200){
    ElMessage({
      type: 'success',
      message: '文件已删除'
    })
  }else{
    ElMessage({
      type: 'warning',
      message: res.message
    })
  }
}
 
const handleClose = () => {
  state.form = {
    id: null,
    rectifyTime: '',
    rectifyPerson: '',
    reason: '',
    fileList: []
  }
  formRef.value.clearValidate();
  dialogVisible.value = false;
 
}
defineExpose({
  openDialog
});
</script>
 
<style scoped lang="scss">
 
</style>