Files
cattleTransportation/pc-cattle-transportation/src/views/userManage/driverDialog.vue

230 lines
8.0 KiB
Vue
Raw Normal View History

2025-10-20 17:32:09 +08:00
<template>
<el-dialog v-model="data.dialogVisible" :title="data.title" :before-close="handleClose" style="width: 650px; padding-bottom: 20px">
<el-form ref="formDataRef" :model="ruleForm" :rules="rules" label-width="auto">
<el-form-item label="司机姓名" prop="username">
<el-input v-model="ruleForm.username" placeholder="请输入司机姓名" clearable></el-input>
</el-form-item>
<el-form-item label="司机手机号" prop="mobile">
<el-input v-model="ruleForm.mobile" placeholder="请输入司机手机号" clearable></el-input>
</el-form-item>
2025-10-27 17:38:20 +08:00
<el-form-item label="驾驶证照片" prop="driver_license">
2025-10-20 17:32:09 +08:00
<el-upload
:limit="2"
list-type="picture-card"
action="/api/common/upload"
2025-10-27 17:38:20 +08:00
:on-success="(response, file, fileList) => handleAvatarSuccess(response, file, fileList, 'driver_license')"
2025-10-20 17:32:09 +08:00
:on-preview="handlePreview"
2025-10-27 17:38:20 +08:00
:on-remove="(file, fileList) => handleRemove(file, fileList, 'driver_license')"
2025-10-20 17:32:09 +08:00
:before-upload="beforeAvatarUpload"
2025-10-27 17:38:20 +08:00
:file-list="ruleForm.driver_license"
2025-10-20 17:32:09 +08:00
:headers="importHeaders"
2025-10-27 17:38:20 +08:00
:on-exceed="() => handleExceed(2)"
2025-10-20 17:32:09 +08:00
>
<el-icon><Plus /></el-icon>
</el-upload>
</el-form-item>
2025-10-27 17:38:20 +08:00
<el-form-item label="身份证照片" prop="id_card">
2025-10-20 17:32:09 +08:00
<el-upload
:limit="2"
list-type="picture-card"
action="/api/common/upload"
2025-10-27 17:38:20 +08:00
:on-success="(response, file, fileList) => handleAvatarSuccess(response, file, fileList, 'id_card')"
2025-10-20 17:32:09 +08:00
:on-preview="handlePreview"
2025-10-27 17:38:20 +08:00
:on-remove="(file, fileList) => handleRemove(file, fileList, 'id_card')"
2025-10-20 17:32:09 +08:00
:before-upload="beforeAvatarUpload"
2025-10-27 17:38:20 +08:00
:file-list="ruleForm.id_card"
2025-10-20 17:32:09 +08:00
:headers="importHeaders"
2025-10-27 17:38:20 +08:00
:on-exceed="() => handleExceed(2)"
2025-10-20 17:32:09 +08:00
>
<el-icon><Plus /></el-icon>
</el-upload>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button :loading="data.saveLoading" type="primary" @click="onClickSave">保存</el-button>
<el-button @click="handleClose">取消</el-button>
</span>
</template>
<el-dialog v-model="data.dialogVisibleImg">
<img w-full :src="data.dialogImageUrl" alt="Preview Image" />
</el-dialog>
</el-dialog>
</template>
<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
2025-10-27 17:38:20 +08:00
import { ElMessage } from 'element-plus';
import { Plus } from '@element-plus/icons-vue';
2025-10-20 17:32:09 +08:00
import { driverAdd, driverEdit } from '@/api/userManage.js';
import { useUserStore } from '../../store/user';
const userStore = useUserStore();
const importHeaders = reactive({ Authorization: userStore.$state.token });
const emits = defineEmits();
2025-10-27 17:38:20 +08:00
2025-10-20 17:32:09 +08:00
const formDataRef = ref();
const data = reactive({
dialogVisible: false,
title: '',
saveLoading: false,
dialogVisibleImg: false,
dialogImageUrl: '',
2025-10-27 17:38:20 +08:00
isEdit: false,
2025-10-20 17:32:09 +08:00
});
2025-10-27 17:38:20 +08:00
2025-10-20 17:32:09 +08:00
const ruleForm = reactive({
2025-10-27 17:38:20 +08:00
id: null,
username: '',
mobile: '',
driver_license: [], // 驾驶证照片
id_card: [], // 身份证照片
2025-10-20 17:32:09 +08:00
});
const rules = reactive({
username: [{ required: true, message: '请输入司机姓名', trigger: 'blur' }],
mobile: [
{
required: true,
validator(rule, value, callback) {
if (!value) {
callback(new Error('请输入司机手机号'));
2025-10-27 17:38:20 +08:00
} else if (!/^1[3-9]\d{9}$/.test(value)) {
2025-10-20 17:32:09 +08:00
callback(new Error('请输入正确的手机号'));
2025-10-27 17:38:20 +08:00
} else {
callback();
2025-10-20 17:32:09 +08:00
}
},
trigger: 'blur',
},
],
2025-10-27 17:38:20 +08:00
driver_license: [{ required: true, message: '请上传驾驶证照片', trigger: 'change' }],
id_card: [{ required: true, message: '请上传身份证照片', trigger: 'change' }],
2025-10-20 17:32:09 +08:00
});
2025-10-27 17:38:20 +08:00
2025-10-20 17:32:09 +08:00
const handleAvatarSuccess = (res, file, fileList, type) => {
console.log('上传成功响应:', res);
if (ruleForm.hasOwnProperty(type)) {
let imageUrl = null;
if (res && res.data && res.data.src) {
imageUrl = res.data.src;
} else if (res && res.data && typeof res.data === 'string') {
imageUrl = res.data;
} else if (res && res.url) {
imageUrl = res.url;
} else if (res && typeof res === 'string') {
imageUrl = res;
}
2025-10-27 17:38:20 +08:00
if (imageUrl) {
// 直接更新 fileList
file.url = imageUrl;
ruleForm[type] = fileList;
console.log(`${type} 上传成功:`, imageUrl, 'fileList:', fileList);
} else {
console.error('无法解析图片URL:', res);
ElMessage.error('上传失败无法获取图片URL');
}
2025-10-20 17:32:09 +08:00
}
};
2025-10-27 17:38:20 +08:00
2025-10-20 17:32:09 +08:00
const handleRemove = (file, fileList, type) => {
if (ruleForm.hasOwnProperty(type)) {
ruleForm[type] = fileList;
}
};
2025-10-27 17:38:20 +08:00
2025-10-20 17:32:09 +08:00
const beforeAvatarUpload = (file) => {
2025-10-27 17:38:20 +08:00
const isImage = file.type.startsWith('image/');
const isLt2M = file.size / 1024 / 1024 < 2;
2025-10-20 17:32:09 +08:00
2025-10-27 17:38:20 +08:00
if (!isImage) {
ElMessage.error('上传文件只能是图片格式!');
2025-10-20 17:32:09 +08:00
}
2025-10-27 17:38:20 +08:00
if (!isLt2M) {
ElMessage.error('上传图片大小不能超过 2MB!');
2025-10-20 17:32:09 +08:00
}
2025-10-27 17:38:20 +08:00
return isImage && isLt2M;
2025-10-20 17:32:09 +08:00
};
2025-10-27 17:38:20 +08:00
const handleExceed = (number) => {
ElMessage.warning(`最多只能上传${number}张图片!`);
2025-10-20 17:32:09 +08:00
};
2025-10-27 17:38:20 +08:00
2025-10-20 17:32:09 +08:00
const handlePreview = (file) => {
data.dialogImageUrl = file.url;
data.dialogVisibleImg = true;
};
2025-10-27 17:38:20 +08:00
2025-10-20 17:32:09 +08:00
const handleClose = () => {
2025-10-27 17:38:20 +08:00
formDataRef.value?.resetFields();
ruleForm.id = null;
ruleForm.username = '';
ruleForm.mobile = '';
ruleForm.driver_license = [];
ruleForm.id_card = [];
2025-10-20 17:32:09 +08:00
data.dialogVisible = false;
2025-10-27 17:38:20 +08:00
data.isEdit = false;
2025-10-20 17:32:09 +08:00
};
const onClickSave = () => {
2025-10-27 17:38:20 +08:00
formDataRef.value.validate((valid) => {
if (valid) {
data.saveLoading = true;
const params = {
id: ruleForm.id,
username: ruleForm.username,
mobile: ruleForm.mobile,
driverLicense: ruleForm.driver_license.length > 0 ? ruleForm.driver_license.map((item) => item.url).join(',') : '',
idCard: ruleForm.id_card.length > 0 ? ruleForm.id_card.map((item) => item.url).join(',') : '',
};
console.log('提交数据:', params);
const apiCall = data.isEdit ? driverEdit(params) : driverAdd(params);
apiCall.then((res) => {
data.saveLoading = false;
if (res.code === 200) {
ElMessage.success(data.isEdit ? '编辑成功' : '新增成功');
handleClose();
emits('success');
2025-10-20 17:32:09 +08:00
} else {
2025-10-27 17:38:20 +08:00
ElMessage.error(res.msg || '操作失败');
2025-10-20 17:32:09 +08:00
}
2025-10-27 17:38:20 +08:00
}).catch((error) => {
data.saveLoading = false;
console.error('保存失败:', error);
ElMessage.error('保存失败,请稍后重试');
});
}
});
2025-10-20 17:32:09 +08:00
};
2025-10-27 17:38:20 +08:00
const onShowDialog = (row = null) => {
2025-10-20 17:32:09 +08:00
data.dialogVisible = true;
2025-10-27 17:38:20 +08:00
data.isEdit = !!row;
data.title = row ? '编辑司机' : '新增司机';
2025-10-20 17:32:09 +08:00
if (row) {
2025-10-27 17:38:20 +08:00
ruleForm.id = row.id;
ruleForm.username = row.username || '';
ruleForm.mobile = row.mobile || '';
ruleForm.driver_license = row.driver_license ? row.driver_license.split(',').map(url => ({ url: url.trim() })) : [];
ruleForm.id_card = row.id_card ? row.id_card.split(',').map(url => ({ url: url.trim() })) : [];
2025-10-20 17:32:09 +08:00
}
};
2025-10-27 17:38:20 +08:00
defineExpose({ onShowDialog });
2025-10-20 17:32:09 +08:00
</script>
2025-10-27 17:38:20 +08:00
<style scoped>
.dialog-footer {
display: flex;
justify-content: flex-end;
}
</style>