|
|
|
@ -2,7 +2,7 @@
|
|
|
|
|
* @Author: donghao donghao@supervision.ltd
|
|
|
|
|
* @Date: 2024-08-07 14:47:44
|
|
|
|
|
* @LastEditors: donghao donghao@supervision.ltd
|
|
|
|
|
* @LastEditTime: 2024-08-12 15:34:22
|
|
|
|
|
* @LastEditTime: 2024-08-14 16:12:34
|
|
|
|
|
* @FilePath: \General-AI-Platform-Web-Client\src\views\deviceSetting\components\add.vue
|
|
|
|
|
* @Description: 新建位置
|
|
|
|
|
-->
|
|
|
|
@ -11,56 +11,85 @@
|
|
|
|
|
import historyAlarm from "@/assets/history_alarm.png";
|
|
|
|
|
import { UploadFilled } from "@element-plus/icons-vue";
|
|
|
|
|
import { message } from "@/utils/message";
|
|
|
|
|
import { addWorkshopsApi } from "@/api/workshops";
|
|
|
|
|
import { isSuccessApi } from "@/utils/forApi";
|
|
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
|
name: "DeviceSettingAdd"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits(["finishAdd"]);
|
|
|
|
|
const formData = ref({
|
|
|
|
|
name: "",
|
|
|
|
|
file: null as File | null
|
|
|
|
|
picture: null
|
|
|
|
|
});
|
|
|
|
|
const formRef = ref(null);
|
|
|
|
|
|
|
|
|
|
const uploadRef = ref(null);
|
|
|
|
|
const imagePreview = ref<string | null>(null); // 预览图像的 URL
|
|
|
|
|
const dialogVisible = ref<boolean>(false);
|
|
|
|
|
const fileList = ref([]);
|
|
|
|
|
const rules = {
|
|
|
|
|
name: [{ required: true, message: "请输入位置名称", trigger: "blur" }],
|
|
|
|
|
file: [{ required: true, message: "请上传图片文件", trigger: "change" }]
|
|
|
|
|
picture: [{ required: true, message: "请上传图片文件", trigger: "change" }]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const uploadAction = ""; // 可以设置为实际的上传地址
|
|
|
|
|
|
|
|
|
|
const openDialog = () => {
|
|
|
|
|
dialogVisible.value = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleFileChange = (file: any) => {
|
|
|
|
|
// 使用 FileReader 读取文件内容并生成预览
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = e => {
|
|
|
|
|
imagePreview.value = e.target?.result as string;
|
|
|
|
|
};
|
|
|
|
|
reader.readAsDataURL(file.raw);
|
|
|
|
|
console.log(file, "handleFileChange");
|
|
|
|
|
|
|
|
|
|
formData.value = {
|
|
|
|
|
...formData.value,
|
|
|
|
|
file: "https://img.cgmodel.com/image/2020/1010/big/1537169-1390622992.jpg"
|
|
|
|
|
picture: file.raw
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
// 手动触发上传
|
|
|
|
|
const removeFile = () => {
|
|
|
|
|
imagePreview.value = null;
|
|
|
|
|
formData.value = {
|
|
|
|
|
...formData.value,
|
|
|
|
|
picture: null
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const beforeUpload = (file: File) => {
|
|
|
|
|
console.log("beforeUpload_file", file);
|
|
|
|
|
const isJPG = file.type === "image/jpeg" || file.type === "image/png";
|
|
|
|
|
const isLt500K = file.size / 1024 / 1024 < 0.5;
|
|
|
|
|
const isLt500K = file.size / 1024 / 1024 < 20;
|
|
|
|
|
if (!isJPG) {
|
|
|
|
|
ElMessage.error("上传图片只能是 JPG/PNG 格式!");
|
|
|
|
|
}
|
|
|
|
|
if (!isLt500K) {
|
|
|
|
|
ElMessage.error("上传图片大小不能超过 500KB!");
|
|
|
|
|
ElMessage.error("上传图片大小不能超过 20MB!");
|
|
|
|
|
}
|
|
|
|
|
return isJPG && isLt500K;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function fetchAddPoint() {
|
|
|
|
|
const formParams = new FormData();
|
|
|
|
|
formParams.append("name", formData.value.name);
|
|
|
|
|
formParams.append("picture", formData.value.picture);
|
|
|
|
|
const resp = await addWorkshopsApi(formParams);
|
|
|
|
|
console.log(resp, "fetchAddPoint_resp");
|
|
|
|
|
if (isSuccessApi(resp)) {
|
|
|
|
|
message(resp.msg || "添加成功", { type: "success" });
|
|
|
|
|
emit("finishAdd", resp.data);
|
|
|
|
|
dialogVisible.value = false;
|
|
|
|
|
} else {
|
|
|
|
|
message(resp.msg || "添加失败", { type: "error" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const submitForm = () => {
|
|
|
|
|
formRef.validate((valid: boolean) => {
|
|
|
|
|
formRef.value.validate((valid: boolean) => {
|
|
|
|
|
if (valid) {
|
|
|
|
|
message("提交成功", { type: "success" });
|
|
|
|
|
dialogVisible.value = false;
|
|
|
|
|
fetchAddPoint();
|
|
|
|
|
} else {
|
|
|
|
|
message("验证失败", { type: "error" });
|
|
|
|
|
return false;
|
|
|
|
@ -68,11 +97,6 @@ const submitForm = () => {
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// const resetForm = (formEl: FormInstance | undefined) => {
|
|
|
|
|
// if (!formEl) return;
|
|
|
|
|
// formEl.resetFields();
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
|
openDialog
|
|
|
|
|
});
|
|
|
|
@ -113,28 +137,30 @@ defineExpose({
|
|
|
|
|
<el-form-item
|
|
|
|
|
label="上传图片(请上传png或jpeg格式图片文件,尺寸不超过4096*3112px,容量不超过20M)"
|
|
|
|
|
class="w-full"
|
|
|
|
|
prop="file"
|
|
|
|
|
prop="picture"
|
|
|
|
|
>
|
|
|
|
|
<el-upload
|
|
|
|
|
ref="uploadRef"
|
|
|
|
|
class="w-full"
|
|
|
|
|
drag
|
|
|
|
|
:action="uploadAction"
|
|
|
|
|
:on-change="handleFileChange"
|
|
|
|
|
:before-upload="beforeUpload"
|
|
|
|
|
:on-remove="removeFile"
|
|
|
|
|
:auto-upload="false"
|
|
|
|
|
:file-list="fileList"
|
|
|
|
|
:limit="1"
|
|
|
|
|
accept="image/*"
|
|
|
|
|
>
|
|
|
|
|
<div>
|
|
|
|
|
<div v-if="!formData?.file">
|
|
|
|
|
<div v-if="!imagePreview">
|
|
|
|
|
<el-icon class="el-icon--upload"><upload-filled /></el-icon>
|
|
|
|
|
<div class="el-upload__text">
|
|
|
|
|
拖拽图片到这里,或点此添加 <em>点此添加</em>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<el-image
|
|
|
|
|
v-else
|
|
|
|
|
:src="formData?.file || ''"
|
|
|
|
|
v-if="imagePreview"
|
|
|
|
|
:src="imagePreview || ''"
|
|
|
|
|
:fit="'contain'"
|
|
|
|
|
class="w-full"
|
|
|
|
|
/>
|
|
|
|
|