<template>
|
<view class="hazard-source-detail">
|
<PageHeader :title="isEdit ? '归还' : '领用危险源'"
|
@back="goBack" />
|
<u-form @submit="handleSubmit"
|
ref="formRef"
|
label-width="110">
|
<!-- 危险源信息 -->
|
<u-cell-group v-if="!isEdit"
|
title="危险源信息">
|
<u-form-item v-if="!isEdit"
|
label="危险源名称"
|
prop="name"
|
required
|
border-bottom>
|
<u-input v-model="form.name"
|
placeholder="请选择危险源"
|
@click="showHazardSourceSheet"
|
readonly />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showHazardSourceSheet"></up-icon>
|
</template>
|
</u-form-item>
|
<u-form-item label="危险源编码"
|
prop="code"
|
border-bottom>
|
<u-input v-model="form.code"
|
disabled
|
placeholder="自动带出"
|
readonly />
|
</u-form-item>
|
<u-form-item v-if="!isEdit"
|
label="危险源类型"
|
prop="type"
|
border-bottom>
|
<u-input v-model="form.type"
|
disabled
|
placeholder="自动带出"
|
readonly />
|
</u-form-item>
|
<u-form-item v-if="!isEdit"
|
label="风险等级"
|
prop="riskLevel"
|
border-bottom>
|
<u-input v-model="form.riskLevel"
|
disabled
|
placeholder="自动带出"
|
readonly />
|
</u-form-item>
|
<u-form-item v-if="!isEdit"
|
label="所在位置"
|
prop="location"
|
border-bottom>
|
<u-input v-model="form.location"
|
disabled
|
placeholder="自动带出"
|
readonly />
|
</u-form-item>
|
<u-form-item v-if="!isEdit"
|
label="领用时间"
|
prop="applyTime"
|
border-bottom>
|
<u-input v-model="form.applyTime"
|
disabled />
|
</u-form-item>
|
<u-form-item v-if="!isEdit"
|
label="领用人"
|
prop="applyUserName"
|
border-bottom>
|
<u-input v-model="form.applyUserName"
|
disabled />
|
</u-form-item>
|
<u-form-item v-if="!isEdit"
|
label="领用用途"
|
prop="applyPurpose"
|
required
|
border-bottom>
|
<u-input v-model="form.applyPurpose"
|
placeholder="请输入领用用途" />
|
</u-form-item>
|
<u-form-item v-if="!isEdit"
|
label="领用数量"
|
prop="applyQty"
|
required
|
border-bottom>
|
<u-input v-model="form.applyQty"
|
type="number"
|
@blur="validateApplyQty"
|
min="1"
|
placeholder="请输入领用数量" />
|
</u-form-item>
|
</u-cell-group>
|
<u-cell-group v-if="isEdit"
|
title="归还信息">
|
<u-form-item label="归还人"
|
prop="returnUserName"
|
border-bottom>
|
<u-input v-model="form.returnUserName"
|
disabled />
|
</u-form-item>
|
<u-form-item label="归还时间"
|
prop="returnTime"
|
border-bottom>
|
<u-input v-model="form.returnTime"
|
disabled />
|
</u-form-item>
|
<u-form-item label="归还情况说明"
|
prop="returnRemark"
|
required
|
border-bottom>
|
<u-textarea v-model="form.returnRemark"
|
placeholder="请输入归还情况说明"
|
:rows="4"
|
: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="hazardSourceSheetVisible"
|
:actions="hazardSourceOptions"
|
@select="handleHazardSourceSelect"
|
@close="hazardSourceSheetVisible = false"
|
title="选择危险源" />
|
<!-- 时间选择器 -->
|
<up-datetime-picker :show="applyTimeVisible"
|
v-model="applyTime"
|
@confirm="onApplyTimeConfirm"
|
@cancel="applyTimeVisible = false"
|
mode="datetime" />
|
<up-datetime-picker :show="returnTimeVisible"
|
v-model="returnTime"
|
@confirm="onReturnTimeConfirm"
|
@cancel="returnTimeVisible = false"
|
mode="date" />
|
</view>
|
</template>
|
|
<script setup>
|
// 替换 toast 方法
|
defineOptions({ name: "hazard-source-detail" });
|
const showToast = message => {
|
uni.showToast({
|
title: message,
|
icon: "none",
|
});
|
};
|
|
import { ref, onMounted } from "vue";
|
import PageHeader from "@/components/PageHeader.vue";
|
import {
|
safeHazardRecordAdd,
|
safeHazardRecordUpdate,
|
} from "@/api/safeProduction/hazardousMaterialsControl";
|
import { safeHazardListPage } from "@/api/safeProduction/hazardSourceLedger";
|
import useUserStore from "@/store/modules/user";
|
import dayjs from "dayjs";
|
import { onLoad } from "@dcloudio/uni-app";
|
|
const userStore = useUserStore();
|
|
// 表单数据
|
const form = ref({
|
name: "",
|
code: "",
|
type: "",
|
riskLevel: "",
|
location: "",
|
applyPurpose: "",
|
applyTime: "",
|
applyQty: "",
|
returnTime: "",
|
returnRemark: "",
|
safeHazardId: "",
|
});
|
|
// 页面状态
|
const loading = ref(false);
|
const formRef = ref(null);
|
const isEdit = ref(false);
|
|
// 危险源选择器
|
const hazardSourceSheetVisible = ref(false);
|
const hazardSourceOptions = ref([]);
|
const hazardSourceList = ref([]);
|
const showHazardSourceSheet = () => {
|
if (hazardSourceOptions.value.length === 0) {
|
getHazardSourceList();
|
} else {
|
hazardSourceSheetVisible.value = true;
|
}
|
};
|
const stockQty = ref(0);
|
const handleHazardSourceSelect = item => {
|
const hazardSource = hazardSourceList.value.find(h => h.id === item.value);
|
if (hazardSource) {
|
form.value.name = hazardSource.name;
|
form.value.code = hazardSource.code;
|
form.value.type = hazardSource.type;
|
form.value.riskLevel = hazardSource.riskLevel;
|
form.value.location = hazardSource.location;
|
form.value.safeHazardId = hazardSource.id;
|
stockQty.value = hazardSource.stockQty || 0;
|
}
|
hazardSourceSheetVisible.value = false;
|
};
|
const validateApplyQty = () => {
|
if (!form.value.applyQty) {
|
showToast("请输入领用数量");
|
return false;
|
}
|
if (isNaN(form.value.applyQty)) {
|
showToast("领用数量必须是数字");
|
form.value.applyQty = 0;
|
return false;
|
}
|
if (form.value.applyQty < 0) {
|
showToast("领用数量必须大于等于1");
|
form.value.applyQty = 0;
|
return false;
|
}
|
if (form.value.applyQty > stockQty.value) {
|
showToast("领用数量不能大于库存数量");
|
form.value.applyQty = 0;
|
return false;
|
}
|
return true;
|
};
|
|
// 获取危险源列表
|
const getHazardSourceList = () => {
|
const params = {
|
current: -1,
|
size: -1,
|
};
|
safeHazardListPage(params).then(res => {
|
if (res.code === 200) {
|
hazardSourceList.value = res.records || res.data?.records || [];
|
// 过滤掉库存数量小于等于0的选项
|
const validHazardSources = hazardSourceList.value.filter(
|
item => item.stockQty > 0
|
);
|
hazardSourceOptions.value = validHazardSources.map(item => ({
|
value: item.id,
|
name: item.name,
|
subname: `库存: ${item.stockQty}`,
|
}));
|
hazardSourceSheetVisible.value = true;
|
}
|
});
|
};
|
|
// 时间选择器
|
const applyTimeVisible = ref(false);
|
const applyTime = ref(Date.now());
|
const showApplyTimePicker = () => {
|
applyTimeVisible.value = true;
|
};
|
const onApplyTimeConfirm = e => {
|
form.value.applyTime = dayjs(e.value).format("YYYY-MM-DD HH:mm:ss");
|
applyTime.value = e.value;
|
applyTimeVisible.value = false;
|
};
|
|
const returnTimeVisible = ref(false);
|
const returnTime = ref(Date.now());
|
const showReturnTimePicker = () => {
|
returnTimeVisible.value = true;
|
};
|
const onReturnTimeConfirm = e => {
|
form.value.returnTime = dayjs(e.value).format("YYYY-MM-DD");
|
returnTime.value = e.value;
|
returnTimeVisible.value = false;
|
};
|
|
// 返回上一页
|
const goBack = () => {
|
// 返回时清除本地存储的数据
|
uni.removeStorageSync("hazardousMaterialsControl");
|
uni.navigateBack();
|
};
|
|
// 提交表单
|
const handleSubmit = async () => {
|
if (!form.value.safeHazardId) {
|
showToast("请选择危险源");
|
return;
|
}
|
|
if (!form.value.applyPurpose) {
|
showToast("请输入领用用途");
|
return;
|
}
|
|
if (!form.value.applyQty) {
|
showToast("请输入领用数量");
|
return;
|
}
|
|
if (isEdit.value) {
|
if (!form.value.returnTime) {
|
showToast("请选择归还时间");
|
return;
|
}
|
|
if (!form.value.returnRemark) {
|
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 safeHazardRecordUpdate(submitData);
|
if (code === 200) {
|
showToast("归还成功");
|
setTimeout(() => {
|
goBack();
|
}, 500);
|
} else {
|
loading.value = false;
|
showToast("归还失败,请重试");
|
}
|
} else {
|
const { code } = await safeHazardRecordAdd(submitData);
|
if (code === 200) {
|
showToast("领用成功");
|
setTimeout(() => {
|
goBack();
|
}, 500);
|
} else {
|
loading.value = false;
|
showToast("领用失败,请重试");
|
}
|
}
|
} catch (e) {
|
loading.value = false;
|
console.error("提交失败:", e);
|
showToast("提交失败,请重试");
|
}
|
};
|
|
onLoad(() => {
|
// 编辑危险源时,从本地存储获取数据
|
const hazardousMaterials = uni.getStorageSync("hazardousMaterialsControl");
|
console.log("hazardousMaterials", hazardousMaterials);
|
if (hazardousMaterials.id) {
|
form.value = hazardousMaterials;
|
isEdit.value = true;
|
userStore.getInfo().then(res => {
|
form.value.returnUserId = res.user.userId;
|
form.value.returnUserName = res.user.nickName;
|
});
|
console.log("form.value", form.value);
|
} else {
|
userStore.getInfo().then(res => {
|
form.value.applyUserId = res.user.userId;
|
form.value.applyUserName = res.user.nickName;
|
});
|
isEdit.value = false;
|
}
|
});
|
|
onMounted(() => {
|
// 设置默认时间
|
if (!isEdit.value) {
|
form.value.applyTime = dayjs().format("YYYY-MM-DD");
|
applyTime.value = Date.now();
|
} else {
|
form.value.returnTime = dayjs().format("YYYY-MM-DD");
|
returnTime.value = Date.now();
|
}
|
});
|
</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>
|