<template>
|
<view class="hazard-source-detail">
|
<PageHeader :title="isEdit ? '编辑危险源' : '新增危险源'"
|
@back="goBack" />
|
<u-form @submit="handleSubmit"
|
ref="formRef"
|
label-width="110">
|
<!-- 危险源信息 -->
|
<u-cell-group title="危险源信息">
|
<u-form-item label="危险源名称"
|
prop="name"
|
required
|
border-bottom>
|
<u-input v-model="form.name"
|
placeholder="请输入危险源名称" />
|
</u-form-item>
|
<u-form-item label="危险源编码"
|
prop="code"
|
required
|
border-bottom>
|
<u-input v-model="form.code"
|
placeholder="请输入危险源编码" />
|
</u-form-item>
|
<u-form-item label="危险源类型"
|
prop="type"
|
required
|
border-bottom>
|
<u-input v-model="typeName"
|
placeholder="请选择危险源类型"
|
@click="showTypeSheet"
|
readonly />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showTypeSheet"></up-icon>
|
</template>
|
</u-form-item>
|
<u-form-item label="风险等级"
|
prop="riskLevel"
|
required
|
border-bottom>
|
<u-input v-model="riskLevelName"
|
placeholder="请选择风险等级"
|
@click="showRiskLevelSheet"
|
readonly />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showRiskLevelSheet"></up-icon>
|
</template>
|
</u-form-item>
|
<u-form-item label="所在位置"
|
prop="location"
|
required
|
border-bottom>
|
<u-input v-model="form.location"
|
placeholder="请输入所在位置" />
|
</u-form-item>
|
<u-form-item label="管控措施"
|
prop="controlMeasures"
|
required
|
border-bottom>
|
<u-textarea v-model="form.controlMeasures"
|
placeholder="请输入管控措施"
|
:maxlength="200"
|
count
|
:autoHeight="true" />
|
</u-form-item>
|
<u-form-item label="库存数量"
|
prop="stockQty"
|
border-bottom>
|
<u-input v-model="form.stockQty"
|
type="number"
|
min="0"
|
@blur="validateStockQty"
|
placeholder="请输入库存数量" />
|
</u-form-item>
|
<u-form-item label="管控责任人"
|
prop="principalUser"
|
required
|
border-bottom>
|
<u-input v-model="form.principalUser"
|
placeholder="请选择管控责任人"
|
@click="showPrincipalSheet"
|
readonly />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showPrincipalSheet"></up-icon>
|
</template>
|
</u-form-item>
|
<u-form-item label="责任人联系电话"
|
prop="principalMobile"
|
required
|
border-bottom>
|
<u-input v-model="form.principalMobile"
|
placeholder="请输入责任人联系电话" />
|
</u-form-item>
|
<u-form-item label="规格 / 风险描述"
|
prop="specInfo"
|
border-bottom>
|
<u-textarea v-model="form.specInfo"
|
placeholder="请输入规格 / 风险描述"
|
:maxlength="200"
|
count
|
:autoHeight="true" />
|
</u-form-item>
|
</u-cell-group>
|
<!-- 提交按钮 -->
|
<view class="footer-btns">
|
<u-button class="cancel-btn"
|
@click="goBack">取消</u-button>
|
<u-button class="sign-btn"
|
type="primary"
|
@click="handleSubmit"
|
:loading="loading">{{ isEdit ? '保存修改' : '提交' }}</u-button>
|
</view>
|
</u-form>
|
<!-- 危险源类型选择器 -->
|
<up-action-sheet :show="typeSheetVisible"
|
:actions="typeOptions"
|
@select="handleTypeSelect"
|
title="选择危险源类型" />
|
<!-- 风险等级选择器 -->
|
<up-action-sheet :show="riskLevelSheetVisible"
|
:actions="riskLevelOptions"
|
@select="handleRiskLevelSelect"
|
title="选择风险等级" />
|
<!-- 管控责任人选择器 -->
|
<up-action-sheet :show="principalSheetVisible"
|
:actions="principalOptions"
|
@select="handlePrincipalSelect"
|
title="选择管控责任人" />
|
</view>
|
</template>
|
|
<script setup>
|
// 替换 toast 方法
|
defineOptions({ name: "hazard-source-detail" });
|
const showToast = message => {
|
uni.showToast({
|
title: message,
|
icon: "none",
|
});
|
};
|
|
import { ref, onMounted, nextTick } from "vue";
|
import PageHeader from "@/components/PageHeader.vue";
|
import {
|
safeHazardAdd,
|
safeHazardUpdate,
|
} from "@/api/safeProduction/hazardSourceLedger";
|
import { userListNoPageByTenantId } from "@/api/system/user";
|
import useUserStore from "@/store/modules/user";
|
import { useDict } from "@/utils/dict";
|
import { onLoad } from "@dcloudio/uni-app";
|
|
// 获取字典数据
|
const { hazard_source_type } = useDict("hazard_source_type");
|
|
const userStore = useUserStore();
|
|
// 表单数据
|
const form = ref({
|
name: "",
|
code: "",
|
type: "",
|
riskLevel: "",
|
location: "",
|
controlMeasures: "",
|
stockQty: "",
|
principalUser: "",
|
principalUserId: "",
|
principalMobile: "",
|
specInfo: "",
|
});
|
|
// 页面状态
|
const loading = ref(false);
|
const formRef = ref(null);
|
const isEdit = ref(false);
|
|
// 危险源类型选择器
|
const typeSheetVisible = ref(false);
|
const typeName = ref("");
|
const typeOptions = ref([]);
|
const showTypeSheet = () => {
|
typeSheetVisible.value = true;
|
};
|
const handleTypeSelect = item => {
|
form.value.type = item.value;
|
typeName.value = item.name;
|
typeSheetVisible.value = false;
|
};
|
const validateStockQty = val => {
|
let numVal = Number(val);
|
if (isNaN(numVal)) {
|
numVal = 0;
|
}
|
if (numVal < 0) {
|
showToast("库存数量不能小于0");
|
numVal = 0;
|
}
|
// 使用 nextTick 确保视图更新
|
nextTick(() => {
|
form.value.stockQty = numVal;
|
});
|
};
|
|
// 风险等级选择器
|
const riskLevelSheetVisible = ref(false);
|
const riskLevelName = ref("");
|
const riskLevelOptions = ref([]);
|
const showRiskLevelSheet = () => {
|
riskLevelSheetVisible.value = true;
|
};
|
const handleRiskLevelSelect = item => {
|
form.value.riskLevel = item.value;
|
riskLevelName.value = item.name;
|
riskLevelSheetVisible.value = false;
|
};
|
|
// 管控责任人选择器
|
const principalSheetVisible = ref(false);
|
const userList = ref([]);
|
const principalOptions = ref([]);
|
const showPrincipalSheet = () => {
|
if (principalOptions.value.length === 0) {
|
getUserList();
|
} else {
|
principalSheetVisible.value = true;
|
}
|
};
|
const handlePrincipalSelect = item => {
|
console.log(item, "item");
|
form.value.principalUser = item.name;
|
form.value.principalUserId = item.value;
|
form.value.principalMobile = item.phonenumber;
|
principalSheetVisible.value = false;
|
};
|
|
// 获取用户列表
|
const getUserList = () => {
|
userListNoPageByTenantId().then(res => {
|
if (res.code === 200) {
|
userList.value = res.data;
|
principalOptions.value = res.data.map(user => ({
|
value: user.userId,
|
name: user.nickName,
|
phonenumber: user.phonenumber,
|
}));
|
principalSheetVisible.value = true;
|
}
|
});
|
};
|
|
// 返回上一页
|
const goBack = () => {
|
// 返回时清除本地存储的数据
|
uni.removeStorageSync("hazardSourceLedger");
|
uni.navigateBack();
|
};
|
|
// 提交表单
|
const handleSubmit = async () => {
|
if (!form.value.name) {
|
showToast("请输入危险源名称");
|
return;
|
}
|
|
if (!form.value.code) {
|
showToast("请输入危险源编码");
|
return;
|
}
|
|
if (!form.value.type) {
|
showToast("请选择危险源类型");
|
return;
|
}
|
|
if (!form.value.riskLevel) {
|
showToast("请选择风险等级");
|
return;
|
}
|
|
if (!form.value.location) {
|
showToast("请输入所在位置");
|
return;
|
}
|
|
if (!form.value.controlMeasures) {
|
showToast("请输入管控措施");
|
return;
|
}
|
|
if (!form.value.principalUser) {
|
showToast("请输入管控责任人");
|
return;
|
}
|
|
if (!form.value.principalMobile) {
|
showToast("请输入责任人联系电话");
|
return;
|
}
|
|
try {
|
loading.value = true;
|
|
// 使用安全浅拷贝,避免对象展开在某些运行时抛错
|
const source =
|
form.value && typeof form.value === "object" ? form.value : {};
|
const submitData = {};
|
Object.keys(source).forEach(k => {
|
submitData[k] = source[k];
|
});
|
console.log("submitData", submitData);
|
if (isEdit.value) {
|
const { code } = await safeHazardUpdate(submitData);
|
if (code === 200) {
|
showToast("修改成功");
|
setTimeout(() => {
|
goBack();
|
}, 500);
|
} else {
|
loading.value = false;
|
showToast("修改失败,请重试");
|
}
|
} else {
|
const { code } = await safeHazardAdd(submitData);
|
if (code === 200) {
|
showToast("新增成功");
|
setTimeout(() => {
|
goBack();
|
}, 500);
|
} else {
|
loading.value = false;
|
showToast("新增失败,请重试");
|
}
|
}
|
} catch (e) {
|
loading.value = false;
|
console.error("提交失败:", e);
|
showToast("提交失败,请重试");
|
}
|
};
|
|
onLoad(() => {
|
// 编辑危险源时,从本地存储获取数据
|
const hazardSource = uni.getStorageSync("hazardSourceLedger");
|
console.log("hazardSource", hazardSource);
|
if (hazardSource.id) {
|
form.value = hazardSource;
|
isEdit.value = true;
|
console.log("form.value", form.value);
|
} else {
|
isEdit.value = false;
|
}
|
});
|
|
onMounted(() => {
|
// 初始化选项数据
|
typeOptions.value = hazard_source_type.value.map(item => ({
|
value: item.value,
|
name: item.label,
|
}));
|
riskLevelOptions.value = [
|
{ value: "低风险", name: "低风险" },
|
{ value: "一般风险", name: "一般风险" },
|
{ value: "较大风险", name: "较大风险" },
|
{ value: "重大风险", name: "重大风险" },
|
];
|
|
// 设置已选值的显示文本
|
if (form.value.type) {
|
typeName.value =
|
typeOptions.value.find(item => item.value == form.value.type)?.name || "";
|
}
|
if (form.value.riskLevel) {
|
riskLevelName.value =
|
riskLevelOptions.value.find(item => item.value == form.value.riskLevel)
|
?.name || "";
|
}
|
});
|
</script>
|
|
<style scoped lang="scss">
|
@import "@/static/scss/form-common.scss";
|
.client-visit {
|
min-height: 100vh;
|
background: #f8f9fa;
|
padding-bottom: 5rem;
|
}
|
|
.footer-btns {
|
position: fixed;
|
left: 0;
|
right: 0;
|
bottom: 0;
|
background: #fff;
|
display: flex;
|
justify-content: space-around;
|
align-items: center;
|
padding: 0.75rem 0;
|
box-shadow: 0 -0.125rem 0.5rem rgba(0, 0, 0, 0.05);
|
z-index: 1000;
|
}
|
|
.cancel-btn {
|
font-weight: 400;
|
font-size: 1rem;
|
color: #666;
|
background: #f5f5f5;
|
border: 1px solid #ddd;
|
width: 45%;
|
height: 2.5rem;
|
border-radius: 2.5rem 2.5rem 2.5rem 2.5rem;
|
}
|
|
.sign-btn {
|
font-weight: 500;
|
font-size: 1rem;
|
color: #fff;
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
border: none;
|
width: 45%;
|
height: 2.5rem;
|
border-radius: 2.5rem 2.5rem 2.5rem 2.5rem;
|
}
|
|
.location-icon {
|
color: #1989fa;
|
font-size: 1.2rem;
|
}
|
</style>
|