batman
2023-03-13 70c6eb1d5fe7d56fa4f1948e26bf6a86e863602b
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
<template>
    <div class="system-add-menu-container">
        <el-dialog :title="title" v-model="isShowRFIDDialog" width="600px">
            <el-form :model="RFIDForm" :rules="RFIDFormRules" ref="RFIDFormRef" size="default" 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="RFID名称" prop="rfidName">
                            <el-input class="input-add" v-model.trim="RFIDForm.rfidName" placeholder="请输入RFID名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="RFID编码" prop="rfid">
                            <el-input class="input-add" v-model.trim="RFIDForm.rfid" placeholder="请输入RFID编码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="处理人所属车间" prop="exceptionHandlerDepId">
                            <el-cascader @change="getUser" :options="departmentData" :props="{ emitPath: false, checkStrictly: true, value: 'depId', label: 'depName' }" placeholder="请输入RFID所属车间" clearable class="input-add" v-model="RFIDForm.exceptionHandlerDepId"> </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="异常处理人" prop="exceptionHandlerId">
                            <el-select v-model="RFIDForm.exceptionHandlerId" filterable class="input-add" placeholder="请输入异常处理人">
                                <el-option v-for="item in userList" :key="item.uid" :label="item.realName" :value="item.uid"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="RFID所属车间" prop="rfidDepartmentId">
                            <el-cascader :options="departmentData" :props="{ emitPath: false, checkStrictly: true, value: 'depId', label: 'depName' }" placeholder="请输入RFID所属车间" clearable class="input-add" v-model="RFIDForm. rfidDepartmentId"> </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                        <el-form-item label="上传图片" prop="rfidImage">
                            <el-upload accept="image/*" :on-preview="handlePictureCardPreview" :limits="1" v-model:file-list="fileList" :http-request="upload" :action="uploadUrl" list-type="picture-card" :on-remove="handleRemove" :before-remove="beforeRemove" :before-upload="getUploadUrl">
                                <el-icon><Plus /></el-icon>
                                <template #tip>
                                    <div class="el-upload__tip">上传 jpg/png 图片尺寸小于500KB</div>
                                </template>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="isShowRFIDDialog = !isShowRFIDDialog" size="default">取 消</el-button>
                    <el-button type="primary" @click="submitRFID" v-throttle size="default">确 定</el-button>
                </span>
            </template>
        </el-dialog>
        <el-dialog v-model="dialogVisible">
            <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
    </div>
</template>
 
<script lang="ts">
import {userApi} from "/@/api/systemManage/user";
 
interface stateType {
    isShowRFIDDialog: Boolean;
    RFIDForm: {
        rfid: string;
        rfidName: string;
        rfidImage: string | null;
        rfidDepartmentId: number | null,
        exceptionHandlerId: number | null,
        exceptionHandlerDepId: number | null,
    };
    title: string;
    dialogVisible: Boolean;
    dialogImageUrl: string | null;
    RFIDFormRules: {};
    uploadUrl: string;
    fileList: Array<file>;
    departmentData: [];
    userList: [];
}
interface file {
    url: string;
}
 
import {reactive, toRefs, ref, onMounted} from 'vue';
import { RFIDApi } from '/@/api/intellectInspectSystem/RFID';
import { ElMessage, ElMessageBox } from 'element-plus';
import type { UploadProps, UploadUserFile } from 'element-plus';
import axios from 'axios';
export default {
    name: 'RFIDDialog',
    setup(props: any, context: any) {
        const RFIDFormRef = ref();
        const state = reactive<stateType>({
            title: '',
            isShowRFIDDialog: false,
            RFIDForm: {
                rfid: '',
                rfidName: '',
                rfidDepartmentId: null,
                exceptionHandlerId: null,
                exceptionHandlerDepId: null,
                rfidImage: null
            },
            uploadUrl: '',
            dialogVisible: false,
            dialogImageUrl: null,
            RFIDFormRules: {
                rfid: [{ required: true, message: '请填写RFID编码', trigger: 'blur' }],
                rfidName: [{ required: true, message: '请填写RFID名称', trigger: 'change' }],
                rfidDepartmentId: [{ required: true, message: '请选择', trigger: 'change' }],
                exceptionHandlerDepId: [{ required: true, message: '请选择', trigger: 'change' }],
                exceptionHandlerId: [{ required: true, message: '请选择', trigger: 'change' }]
            },
 
            fileList: [],
            departmentData: [],
            userList: []
        });
        //打开模态框
        const openRFIDDialog = async (type: string, value: object, departmentList: []) => {
            state.fileList = [];
            state.isShowRFIDDialog = true;
            state.departmentData = departmentList;
            setTimeout(() => {
                RFIDFormRef.value.clearValidate();
            });
            if (type === '新增') {
                state.title = '新增RFID';
                state.RFIDForm = {
                    rfid: '',
                    rfidName: '',
                    rfidDepartmentId: null,
                    exceptionHandlerId: null,
                    exceptionHandlerDepId: null,
                    rfidImage: ''
                };
            } else {
                state.title = '修改RFID';
                let res = await RFIDApi().getRFIDById({id:parseInt(value.id)})
                state.RFIDForm = res.data.data
                getUser()
                if (state.RFIDForm.rfidImage === null) return;
                state.fileList = [{ url: state.RFIDForm.rfidImage }];
            }
        };
 
        // // 图片上传
        // const fileList = ref<UploadUserFile[]>([]);
        //
        // const handleRemove = () => {
        //     ElMessageBox.confirm(`此操作将永久删除该图片,是否继续?`, '提示', {
        //         confirmButtonText: '确认',
        //         cancelButtonText: '取消',
        //         type: 'warning'
        //     })
        //         .then(() => {
        //             state.RFIDForm.rfidImage = null;
        //             state.fileList = [];
        //         })
        //         .catch(() => {});
        // };
 
        const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
            console.log(uploadFile);
        };
 
        const getUploadUrl = async (rawFile: any) => {
            const res = await RFIDApi().getUploadUrl(rawFile.name);
            state.RFIDForm.rfidImage = res.data.rfidImage;
            state.uploadUrl = res.data.uploadUrl;
        };
 
        const upload = async (params: any) => {
            // const formData = new FormData();
            // formData.append('file', state.fileList[0].raw);
            let reader = new FileReader();
            reader.readAsArrayBuffer(params.file);
            reader.onload = async () => {
                axios
                    .put(state.uploadUrl, reader.result, {
                        header: { 'Content-Type': 'multipart/form-data' }
                    })
                    .then(() => {
                        if (state.fileList.length === 2) {
                            state.fileList.splice(0, 1);
                        }
                    });
                // let res = await RFIDApi().uploadFile(state.uploadUrl, reader.result);
                // if (res.data.code === '200') {
                // } else {
                //     ElMessage({
                //         type: 'warning',
                //         message: res.data.msg
                //     });
                // }
            };
        };
 
        const beforeRemove = (file: {}, fileList: []) => {
            const result = new Promise((resolve, reject) => {
                ElMessageBox.confirm('此操作将删除该图片, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(() => {
                        state.RFIDForm.rfidImage = null;
                        state.fileList = [];
                        // 请求删除接口
                    })
                    .catch(() => {
                        reject(false);
                    });
            });
            return result;
        };
 
        //新增修改提交
        const submitRFID = async () => {
            RFIDFormRef.value.validate(async (valid: Boolean) => {
                if (valid) {
                    if (state.title === '新增RFID') {
                        let res = await RFIDApi().addRFID(state.RFIDForm);
                        if (res.data.code === '200') {
                            ElMessage({
                                type: 'success',
                                message: 'RFID新增成功',
                                duration: 2000
                            });
                            state.isShowRFIDDialog = false;
                            context.emit('refreshRFID');
                        } else {
                            ElMessage({
                                type: 'warning',
                                message: res.data.msg
                            });
                        }
                    } else {
                        let res = await RFIDApi().modRFID(state.RFIDForm);
                        if (res.data.code === '200') {
                            ElMessage({
                                type: 'success',
                                message: 'RFID修改成功',
                                duration: 2000
                            });
                            state.isShowRFIDDialog = false;
                            context.emit('refreshRFID');
                        } else {
                            ElMessage({
                                type: 'warning',
                                message: res.data.msg
                            });
                        }
                    }
                } else {
                    ElMessage({
                        type: 'warning',
                        message: '请完善基本信息'
                    });
                }
            });
        };
 
        //获取用户列表
        const getUser = async () => {
            let res = await userApi().getUserLByDepartment(state.RFIDForm.exceptionHandlerDepId);
            if (res.data.code === '200') {
                state.userList = res.data.data;
            } else {
                ElMessage({
                    type: 'warning',
                    message: res.data.msg
                });
            }
        };
 
        const handlePictureCardPreview = (uploadFile: { url: string }) => {
            state.dialogImageUrl = uploadFile.url!;
            state.dialogVisible = true;
        };
 
        return {
            ...toRefs(state),
            RFIDFormRef,
            // fileList,
            upload,
            getUploadUrl,
            getUser,
            handlePreview,
            beforeRemove,
            submitRFID,
            openRFIDDialog,
            handlePictureCardPreview
        };
    }
};
</script>
 
<style scoped></style>