zhouwx
2026-06-18 5612bbd431d10589a06c7ed3584de2cad1b5ea0a
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<template>
  <div class="notice">
    <el-dialog
        v-model="dialogVisible"
        :title="title"
        width="850px"
        :before-close="handleClose"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
    >
      <el-form :model="state.form" size="default" ref="busRef" :rules="state.formRules" label-width="150px" >
        <el-form-item required label= "危化品列表" >
          <div style="display: flex;width: 100%;margin-top: 5px">
            <el-table :data="state.form.subscribeHazmats" :border="true">
              <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
              <el-table-column label="危化品名称" align="center" width="200"  >
                <template #default="{row,$index}">
                  <el-form-item :prop="'subscribeHazmats.' + '[' + $index + ']' + '.hazmatName'" :rules="state.formRules.hazmatName">
                    <el-select
                        clearable
                        v-model="row.hazmatName"
                        filterable
                        remote
                        @change="selectValueHazmat(row.hazmatName,$index)"
                        reserve-keyword
                        placeholder="请输入危化品名称"
                        remote-show-suffix
                        :remote-method="getHazmaList"
                        style="width: 100%"
                    >
                      <el-option
                          v-for="item in state.hazmatList"
                          :key="item.id"
                          :label="item.name"
                          :value="item.name"
                      />
                    </el-select>
<!--                    <el-input type="textarea" :rows="1" v-model="row.hazmatName" placeholder="请输入危化品名称"></el-input>-->
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="数量" align="center"  >
                <template #default="{row,$index}">
                  <el-form-item :prop="'subscribeHazmats.' + '[' + $index + ']' + '.hazmatCount'" :rules="state.formRules.hazmatCount">
                    <el-input   v-model="row.hazmatCount" @input="row.hazmatCount = row.hazmatCount.replace(/[^0-9]/g,'')" placeholder="请输入数量"></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="title !== '查看'" >
                <template #default="scope" >
                  <el-button link type="danger"  @click="handleDelete(scope.row)" >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-button
                v-if="title !== '查看'"
                style="margin-left: 20px"
                type="primary"
                @click="addObject"
            >新增</el-button>
          </div>
        </el-form-item>
        <el-form-item label="审批人:" prop="checkId" >
          <el-select
              clearable
              v-model="state.form.checkName"
              filterable
              remote
              @change="selectValue"
              reserve-keyword
              placeholder="请输入审批人名称"
              remote-show-suffix
              :remote-method="getPeopleList"
              style="width: 50%"
          >
            <el-option
                v-for="item in state.peopleList"
                :key="item.id"
                :label="item.name"
                :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="申购文件:" prop="filePath">
          <el-upload accept=".doc,.docx,.xls,.xlsx,.pdf" v-if="title!=='查看'" :action="state.uploadUrl" :headers="state.header" method="post" :on-success="(res, uploadFile)=>handleAvatarSuccess(res, uploadFile)" :on-exceed="showTip" :limit='state.fileLimit' v-model:file-list="state.fileList" :before-upload="picSize" :on-remove="(file)=>handleRemove(file)" >
            <el-button type="primary">点击上传</el-button>
            <template #tip>
              <div class="el-upload__tip">支持上传.doc、.docx、.pdf、.xls、.xlsx格式文档,尺寸小于50M,最多可上传1个</div>
            </template>
          </el-upload>
          <div v-else>
            <el-link v-if="state.form.fileName" style="" type="primary" @click="openFile(state.form.filePath)">{{state.form.fileName}}</el-link>
          </div>
        </el-form-item>
        <el-form-item label="审批意见:" v-if="title === '查看' && state.form.status == 2" style="width: 70%">
          <el-input v-model="state.form.opinion" type="textarea" :rows="5"></el-input>
        </el-form-item>
      </el-form>
      <template #footer v-if="title !=='查看'">
        <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 {reactive, ref, toRefs} from 'vue'
import {ElMessage, ElMessageBox} from "element-plus";
import {addCompany, checkName, distributeCompany, editCompany} from "@/api/hazardousChemicals/company";
import {verifyPhone, verifyPwd} from "@/utils/validate";
import {getUser} from "@/api/hazardousChemicals/user";
import {getToken} from "@/utils/auth";
import {renderAsync} from "docx-preview";
import {addSubscribe, editSubscribe} from "@/api/hazardousChemicals/subscribeApply";
import {getBasicList} from "@/api/hazardousChemicals/basicInfo";
 
const dialogVisible = ref(false);
const title = ref("");
const busRef = ref();
const length = ref()
const emit = defineEmits(["getList"]);
const state = reactive({
  form: {
    id: '',
    checkId: null,
    checkName:'',
    filePath: '',
    fileName: '',
    subscribeHazmats:[],
  },
  formRules:{
    checkId: [{ required: true, message: '请选择审核人', trigger: 'blur' }],
    filePath:[{ required: true, message: '请上传文件', trigger: 'blur' }],
    hazmatName: [{ required: true, message: '', trigger: 'blur' }],
    hazmatCount: [{ required: true, message: '', trigger: 'blur' }],
  },
 
  peopleList:[],
  hazmatList:[],
  fileList:[],
  uploadUrl: import.meta.env.VITE_APP_BASE_API + '/system/common/uploadFile',
  header: {
    Authorization: getToken()
  },
  fileLimit: 1,
})
 
 
const openDialog = async (type, value) => {
  await getPeopleList("")
  await getHazmaList("")
  title.value = type === 'add' ? '申购申请' : type ==='edit' ? '申购编辑' : '查看' ;
  if(type === 'edit' || type === 'view') {
    state.form = JSON.parse(JSON.stringify(value));
    if(state.form.filePath) {
      const obj = {
        url: value.filePath,
        name: value.fileName
      }
      state.fileList = [obj]
    }
  }
  dialogVisible.value = true;
}
 
const onSubmit = async () => {
  const valid = await busRef.value.validate();
  if(valid){
    if(title.value === '申购申请'){
      const {id, ...data} = JSON.parse(JSON.stringify(state.form))
      const res = await addSubscribe(data)
      if(res.code === 200){
        ElMessage({
          type: 'success',
          message: '申请成功'
        });
      }else{
        ElMessage.warning(res.message)
      }
      emit("getList")
      busRef.value.clearValidate();
      reset();
      dialogVisible.value = false;
    }else if(title.value === '申购编辑'){
      const {...data} = JSON.parse(JSON.stringify(state.form))
      const res = await editSubscribe(data)
      if(res.code === 200){
        ElMessage({
          type: 'success',
          message: '编辑成功'
        });
      }else{
        ElMessage.warning(res.message)
      }
      emit("getList")
      busRef.value.clearValidate();
      reset();
      dialogVisible.value = false;
    }
  }
}
 
const handleClose = () => {
  busRef.value.clearValidate();
  reset();
  dialogVisible.value = false;
  emit("getList")
}
const reset = () => {
  state.form = {
    id: '',
    checkId: null,
    filePath: '',
    fileName: '',
    subscribeHazmats:[],
  }
  state.fileList = []
}
const addObject = () => {
 
  state.form.subscribeHazmats.push({hazmatName:'',hazmatCount:null})
}
const handleDelete = (val) => {
  state.form.subscribeHazmats = state.form.subscribeHazmats.filter(item=> item != val)
}
const selectValue =  (val) => {
  if(!val){
    state.form.checkId = null
  }
  state.peopleList.forEach(item => {
    if(item.name === val){
      state.form.checkId  = item.id
      state.form.checkName = item.name
    }
  })
}
 
const getPeopleList = async (val)=>{
  let queryParams = {}
  if(val != ""){
    queryParams = {
      name: val
    }
  }else {
    queryParams = {
      pageNum: 1,
      pageSize: 10
    }
  }
  const res = await getUser(queryParams)
  if (res.code == 200) {
    state.peopleList = res.data.list
  } else {
    ElMessage.warning(res.message)
  }
}
const selectValueHazmat =  (val,index) => {
  if(!val){
    state.form.subscribeHazmats.forEach((item, i) => i === index && (item.basicId = null));
  }
  const match = state.hazmatList.find(item => item.name === val);
  if (match) {
    state.form.subscribeHazmats[index].basicId = match.id;
    state.form.subscribeHazmats[index].hazmatName = match.name;
  }
}
const getHazmaList = async (val)=>{
  let queryParams = {}
  if(val != ""){
    queryParams = {
      name: val
    }
  }else {
    queryParams = {
      pageNum: 1,
      pageSize: 10
    }
  }
  const res = await getBasicList(queryParams)
  if(res.code == 200){
    state.hazmatList = res.data.list
  }else{
    ElMessage.warning(res.message)
  }
}
const handleAvatarSuccess = (res, uploadFile) => {
  if(res.code == 200){
    state.form.fileName = res.data.originName
    state.form.filePath = res.data.path
    state.form.format = '.' + res.data.filename.split('.')[1]
  }else{
    state.fileList = []
    ElMessage({
      type: 'warning',
      message: '文件上传失败'
    })
  }
}
 
const showTip =()=>{
  ElMessage({
    type: 'warning',
    message: '超出文件上传数量'
  });
}
const picSize = async (rawFile) => {
  if(rawFile.size / 1024 / 1024 > 50){
    ElMessage({
      type: 'warning',
      message: '文件大小不能超过50M'
    });
    return false
  }
};
const handleRemove = async (file) => {
  const index = state.fileList.indexOf(file)
  const newFileList = state.fileList.slice()
  newFileList.splice(index, 1)
  state.fileList = newFileList;
}
const openFile = async(path)=>{
  const ext = path.split('.').pop().toLowerCase();
  if (ext === 'doc' || ext === 'xls' || ext === 'xlsx' || ext === 'ppt' || ext === 'pptx') {
    ElMessageBox.confirm(`暂不支持线上预览.${ext}文件,是否下载查看?`, '提示', { confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning' }).then(() => {
      window.open(`${import.meta.env.VITE_APP_BASE_API}/${path}`, '_blank');
    }).catch(() => {
      console.log('取消预览')
    });
    return
  }else if(ext === 'pdf' || ext === 'jpg'||  ext === 'jpeg' || ext === 'png' ){
    window.open(`${import.meta.env.VITE_APP_BASE_API}/${path}`, '_blank');
  }else{
    try {
      // 1. 获取文件
      const response = await fetch(import.meta.env.VITE_APP_BASE_API + '/' + path);
      const arrayBuffer = await response.arrayBuffer();
      // 2. 创建新窗口
      const win = window.open('', '_blank');
      win.document.write(`
      <!DOCTYPE html>
      <html>
        <head>
          <title>预览</title>
          <style>
            body { margin: 20px; font-family: Arial; }
            .docx-container { width: 100%; height: 100%; }
          </style>
        </head>
        <body>
          <div id="container" class="docx-container"></div>
        </body>
      </html>
    `);
      // 3. 渲染 DOCX
      await renderAsync(arrayBuffer, win.document.getElementById('container'));
 
    } catch (error) {
      console.error('预览失败:', error);
      alert(`预览失败: ${error.message}`);
    }
  }
}
defineExpose({
  openDialog
});
 
 
</script>
 
<style scoped lang="scss">
.notice{
  :deep(.el-form .el-form-item__label) {
    font-size: 15px;
  }
  .file {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>