<template>
|
<view class="safe-qualifications-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="version"
|
required
|
border-bottom>
|
<u-input v-model="form.version"
|
placeholder="请输入版本号" />
|
</u-form-item>
|
<u-form-item label="有效期"
|
prop="effectiveTime"
|
required
|
border-bottom>
|
<u-input v-model="form.effectiveTime"
|
placeholder="请选择有效期"
|
@click="showTimePicker" />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showTimePicker"></up-icon>
|
</template>
|
</u-form-item>
|
<u-form-item label="备注"
|
prop="remark"
|
border-bottom>
|
<u-textarea v-model="form.remark"
|
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-datetime-picker :show="showTime"
|
v-model="currentTime"
|
@confirm="onTimeConfirm"
|
@cancel="showTime = false"
|
mode="date" />
|
<!-- 规程资质类型选择器 -->
|
<up-action-sheet :show="typeSheetVisible"
|
:actions="typeOptions"
|
@select="handleTypeSelect"
|
title="选择规程资质类型" />
|
</view>
|
</template>
|
|
<script setup>
|
// 替换 toast 方法
|
defineOptions({ name: "safe-qualifications-detail" });
|
const showToast = message => {
|
uni.showToast({
|
title: message,
|
icon: "none",
|
});
|
};
|
|
import { ref, onMounted } from "vue";
|
import PageHeader from "@/components/PageHeader.vue";
|
import {
|
safeCertificationAdd,
|
safeCertificationUpdate,
|
} from "@/api/safeProduction/safeQualifications";
|
import useUserStore from "@/store/modules/user";
|
import { useDict } from "@/utils/dict";
|
import dayjs from "dayjs";
|
import { onLoad } from "@dcloudio/uni-app";
|
|
// 获取规程资质类型字典
|
const { type_qualification } = useDict("type_qualification");
|
|
const userStore = useUserStore();
|
|
// 表单数据
|
const form = ref({
|
name: "",
|
code: "",
|
type: "",
|
version: "",
|
remark: "",
|
effectiveTime: "",
|
});
|
|
// 页面状态
|
const loading = ref(false);
|
const formRef = ref(null);
|
const isEdit = ref(false);
|
|
// 时间相关
|
const currentTime = ref(Date.now());
|
const showTime = ref(false);
|
|
// 规程资质类型选择器
|
const typeSheetVisible = ref(false);
|
const showTypeSheet = () => {
|
typeSheetVisible.value = true;
|
};
|
const typeName = ref("");
|
const handleTypeSelect = item => {
|
form.value.type = item.value;
|
typeName.value = item.name;
|
typeSheetVisible.value = false;
|
};
|
|
// 返回上一页
|
const goBack = () => {
|
// 返回时清除本地存储的数据
|
uni.removeStorageSync("safeQualifications");
|
uni.navigateBack();
|
};
|
|
// 显示时间选择器
|
const showTimePicker = () => {
|
showTime.value = true;
|
};
|
|
// 确认时间选择
|
const onTimeConfirm = e => {
|
form.value.effectiveTime = dayjs(e.value).format("YYYY-MM-DD");
|
currentTime.value = e.value;
|
showTime.value = false;
|
};
|
|
// 提交表单
|
const handleSubmit = async () => {
|
if (!form.value.name) {
|
showToast("请输入规程资质名称");
|
return;
|
}
|
|
if (!form.value.code) {
|
showToast("请输入规程资质编号");
|
return;
|
}
|
|
if (!form.value.type) {
|
showToast("请输入规程资质类型");
|
return;
|
}
|
|
if (!form.value.version) {
|
showToast("请输入版本号");
|
return;
|
}
|
|
if (!form.value.effectiveTime) {
|
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 safeCertificationUpdate(submitData);
|
if (code === 200) {
|
showToast("修改成功");
|
setTimeout(() => {
|
goBack();
|
}, 500);
|
} else {
|
loading.value = false;
|
showToast("修改失败,请重试");
|
}
|
} else {
|
const { code } = await safeCertificationAdd(submitData);
|
if (code === 200) {
|
showToast("新增成功");
|
setTimeout(() => {
|
goBack();
|
}, 500);
|
} else {
|
loading.value = false;
|
showToast("新增失败,请重试");
|
}
|
}
|
} catch (e) {
|
loading.value = false;
|
console.error("提交失败:", e);
|
showToast("提交失败,请重试");
|
}
|
};
|
|
onLoad(() => {
|
// 编辑规程资质时,从本地存储获取数据
|
const qualification = uni.getStorageSync("safeQualifications");
|
if (qualification) {
|
form.value = qualification;
|
isEdit.value = true;
|
|
console.log("form.value", form.value);
|
} else {
|
isEdit.value = false;
|
}
|
});
|
|
// 初始化页面数据
|
const initPageData = () => {
|
// 设置默认有效期为当前时间
|
if (!isEdit.value) {
|
form.value.effectiveTime = dayjs().format("YYYY-MM-DD");
|
currentTime.value = Date.now();
|
}
|
};
|
const typeOptions = ref([]);
|
onMounted(() => {
|
initPageData();
|
typeOptions.value = type_qualification.value.map(item => ({
|
value: item.value,
|
name: item.label,
|
}));
|
if (form.value.type) {
|
typeName.value =
|
typeOptions.value.find(item => item.value == form.value.type)?.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>
|