马宇豪
2024-01-26 c694cffc8541d921e5256d33e14e3237454de950
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
<template>
  <div class="system-add-user-container">
    <el-dialog :title="title" v-model="isShowDialog" width="50%">
      <el-form :model="form" size="default" ref="formRef" :rules="rules" label-width="120px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="模板">
              <el-button type="primary" @click="downloadFile">点击下载模板</el-button>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="文件上传">
              <el-upload accept=".xlsx,.xls" :auto-upload="false" method="post" :on-exceed="showTip" :limit='1' v-model:file-list="fileList" :before-upload="picSize" :on-remove="handleRemove" :before-remove="beforeRemove">
                <el-button type="primary">点击上传</el-button>
                <template #tip>
                  <div class="el-upload__tip">尺寸小于2M,最多可上传1份</div>
                </template>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
            <el-button @click="isShowDialog = !isShowDialog" size="default">取 消</el-button>
            <el-button type="primary" v-throttle @click="onSubmit" size="default">确认导入</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
 
<script lang="ts">
import { reactive, toRefs, onMounted, defineComponent, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import axios from "axios";
import Cookies from "js-cookie";
import * as XLSX from "xlsx";
import {judgeReportApi} from "/@/api/dataUpload/saftyBaseInfo/judgeReport";
import { lineApi } from "/@/api/intelligentLine/index";
 
// 定义接口来定义对象的类型
interface DataState {
  title: string;
  isShowDialog: boolean;
  form: {
    files: any
  }
  rules:{},
  fileList: [],
  uploadUrl: string,
  header: {}
}
 
export default defineComponent({
  name: 'reportDialog',
  setup(props, context) {
    const formRef = ref()
    const state = reactive<DataState>({
      title: '',
      isShowDialog: false,
      form: {
        files: ''
      },
      rules:{
      },
      fileList: [],
      uploadUrl: '',
      header: {
        uid: Cookies.get('uid'),
        Authorization: Cookies.get('token')
      }
    })
 
    // 页面加载时
    onMounted(() => {
 
    })
    // 打开弹窗
    const open = (type: string) => {
      state.isShowDialog = true;
      if (type === '任务') {
        state.title = '巡检单元导入';
      }
      if(type == 'id'){
        state.title = '巡检RFID导入';
      }
      if(type == '区域'){
        state.title = '巡检区域导入';
      }
      if(type == '点'){
        state.title = '巡检点导入';
      }
      state.form = {
        files: ''
      }
      state.fileList = []
    };
 
 
 
    // 图片上传
    const showTip =()=>{
      ElMessage({
        type: 'warning',
        message: '超出文件上传数量'
      });
    }
 
    const picSize = async(rawFile: any) => {
      if(rawFile.size / 1024 / 1024 > 2){
        ElMessage({
          type: 'warning',
          message: '文件大小不能超过2M'
        });
        return false
      }
    }
 
 
    const handleRemove = (file, uploadFiles,type) => {
      state.fileList = []
    }
 
    const downloadFile=()=>{
      let filePath = ''
      let name = ''
      if(state.title == '巡检单元导入'){
        filePath = 'excelFiles/missionUpload.xlsx'
        name = '巡检单元导入模板.xlsx'
      }
      if(state.title == '巡检RFID导入'){
        filePath = 'excelFiles/RFIDUpload.xlsx'
        name = '巡检RFID导入模板.xlsx'
      }
      if(state.title == '巡检区域导入'){
        filePath = 'excelFiles/regionUpload.xlsx'
        name = '巡检区域导入模板.xlsx'
      }
      if(state.title == '巡检点导入'){
        filePath = 'excelFiles/pointUpload.xlsx'
        name = '巡检点导入模板.xlsx'
      }
      const link = document.createElement('a')
      link.href = filePath
      link.target = '_blank'
      link.download = name
      link.click()
    }
 
    // 新增修改
    const onSubmit = async () => {
      if(state.fileList.length == 0){
        ElMessage({
          type: 'warning',
          message: '请先选择导入文件'
        })
        return
      }else{
        let res = {}
        if(state.title == '巡检单元导入'){
          res = await lineApi().uploadMission({file: state.fileList[0].raw})
        }
        if(state.title == '巡检RFID导入'){
          res = await lineApi().uploadRfid({file: state.fileList[0].raw})
        }
        if(state.title == '巡检区域导入'){
          res = await lineApi().uploadRegion({file: state.fileList[0].raw})
        }
        if(state.title == '巡检点导入'){
          res = await lineApi().uploadPoint({file: state.fileList[0].raw})
        }
        if(res.data.code == 200){
          ElMessage({
            type: 'success',
            message: res.data.msg,
            duration: 4000
          })
          state.fileList = []
          state.isShowDialog = false
          context.emit('refresh')
        }else{
          ElMessage({
            type: 'warning',
            message: res.data.msg,
            duration: 4000
          })
        }
      }
    }
 
    return {
      formRef,
      downloadFile,
      showTip,
      picSize,
      handleRemove,
      open,
      onSubmit,
      ...toRefs(state)
    };
  }
});
</script>