<template>
|
<view class="basic-parameters-edit">
|
<PageHeader :title="pageTitle"
|
@back="goBack" />
|
<up-form ref="formRef"
|
:model="form"
|
:rules="rules"
|
:errorType="['none']"
|
label-width="110">
|
<up-form-item label="参数编码"
|
prop="paramCode">
|
<up-input v-model="form.paramCode"
|
disabled
|
placeholder="自动生成" />
|
</up-form-item>
|
<up-form-item label="参数名称"
|
prop="paramName"
|
required>
|
<up-input v-model="form.paramName"
|
placeholder="请输入参数名称"
|
clearable />
|
</up-form-item>
|
<up-form-item label="参数类型"
|
prop="paramType"
|
required>
|
<up-input v-model="paramTypeText"
|
placeholder="请选择参数类型"
|
readonly
|
@click="showParamTypeSheet = true" />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showParamTypeSheet = true"></up-icon>
|
</template>
|
</up-form-item>
|
<up-form-item label="单位"
|
prop="unit"
|
:required="form.paramType === 1">
|
<up-input v-model="form.unit"
|
placeholder="请输入单位"
|
clearable />
|
</up-form-item>
|
<up-form-item label="取值格式"
|
v-if="form.paramType === 1 || form.paramType === 2"
|
prop="paramFormat">
|
<up-input v-model="form.paramFormat"
|
placeholder="请输入取值格式"
|
clearable />
|
</up-form-item>
|
<up-form-item label="下拉字典"
|
v-else-if="form.paramType === 3"
|
prop="paramFormat">
|
<up-input v-model="dictTypeText"
|
placeholder="请选择下拉字典"
|
readonly
|
@click="showDictTypeSheet = true" />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showDictTypeSheet = true"></up-icon>
|
</template>
|
</up-form-item>
|
<up-form-item label="时间格式"
|
v-else-if="form.paramType === 4"
|
prop="paramFormat">
|
<up-input v-model="form.paramFormat"
|
placeholder="请选择时间格式"
|
readonly
|
@click="showTimeFormatSheet = true" />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showTimeFormatSheet = true"></up-icon>
|
</template>
|
</up-form-item>
|
<up-form-item label="是否必填"
|
prop="isRequired">
|
<view style="display: flex; justify-content: flex-end; width: 100%;">
|
<up-switch v-model="form.isRequired"
|
:activeValue="1"
|
:inactiveValue="0" />
|
</view>
|
</up-form-item>
|
<up-form-item label="备注"
|
prop="remark">
|
<up-textarea v-model="form.remark"
|
placeholder="请输入备注"
|
autoHeight />
|
</up-form-item>
|
</up-form>
|
<FooterButtons :loading="loading"
|
:confirmText="paramId ? '保存' : '新增'"
|
@cancel="goBack"
|
@confirm="handleSubmit" />
|
<!-- 参数类型选择 -->
|
<up-action-sheet :show="showParamTypeSheet"
|
title="选择参数类型"
|
:actions="paramTypeActions"
|
@select="onSelectParamType"
|
@close="showParamTypeSheet = false" />
|
<!-- 下拉字典选择 -->
|
<up-action-sheet :show="showDictTypeSheet"
|
title="选择下拉字典"
|
:actions="dictTypeActions"
|
@select="onSelectDictType"
|
@close="showDictTypeSheet = false" />
|
<!-- 时间格式选择 -->
|
<up-action-sheet :show="showTimeFormatSheet"
|
title="选择时间格式"
|
:actions="timeFormatActions"
|
@select="onSelectTimeFormat"
|
@close="showTimeFormatSheet = false" />
|
</view>
|
</template>
|
|
<script setup>
|
import { computed, nextTick, onMounted, ref } from "vue";
|
import { onLoad, onReady } from "@dcloudio/uni-app";
|
import FooterButtons from "@/components/FooterButtons.vue";
|
import PageHeader from "@/components/PageHeader.vue";
|
import {
|
addBaseParam,
|
editBaseParam,
|
} from "@/api/basicData/parameterMaintenance";
|
import { listType } from "@/api/system/dict/type";
|
|
const formRef = ref();
|
const loading = ref(false);
|
const paramId = ref("");
|
const showParamTypeSheet = ref(false);
|
const showDictTypeSheet = ref(false);
|
const showTimeFormatSheet = ref(false);
|
const dictTypes = ref([]);
|
|
const form = ref({
|
id: null,
|
paramCode: "",
|
paramName: "",
|
paramType: "",
|
unit: "",
|
remark: "",
|
isRequired: 0,
|
paramFormat: "",
|
});
|
|
const rules = {
|
paramName: [{ required: true, message: "请输入参数名称" }],
|
paramType: [{ required: true, message: "请选择参数类型" }],
|
unit: [
|
{
|
validator: (rule, value, callback) => {
|
if (form.value.paramType === 1 && !value) {
|
callback(new Error("数值类型必须填写单位"));
|
} else {
|
callback();
|
}
|
},
|
},
|
],
|
};
|
|
const paramTypeActions = [
|
{ name: "数值格式", value: 1 },
|
{ name: "文本格式", value: 2 },
|
{ name: "下拉选项", value: 3 },
|
{ name: "时间格式", value: 4 },
|
];
|
|
const timeFormatActions = [
|
{ name: "YYYY-MM-DD", value: "YYYY-MM-DD" },
|
{ name: "YYYY-MM-DD HH:mm:ss", value: "YYYY-MM-DD HH:mm:ss" },
|
];
|
|
const dictTypeActions = computed(() => {
|
return dictTypes.value.map(item => ({
|
name: item.dictName,
|
value: item.dictType,
|
}));
|
});
|
|
const pageTitle = computed(() => (paramId.value ? "编辑参数" : "新增参数"));
|
|
const paramTypeText = computed(() => {
|
const action = paramTypeActions.find(
|
item => item.value === form.value.paramType
|
);
|
return action ? action.name : "";
|
});
|
|
const dictTypeText = computed(() => {
|
const action = dictTypes.value.find(
|
item => item.dictType === form.value.paramFormat
|
);
|
return action ? action.dictName : form.value.paramFormat || "";
|
});
|
|
const goBack = () => {
|
uni.navigateBack();
|
};
|
|
const getDictTypes = () => {
|
listType({ pageNum: 1, pageSize: 1000 }).then(res => {
|
dictTypes.value = res.rows || [];
|
});
|
};
|
|
const onSelectParamType = action => {
|
form.value.paramType = action.value;
|
if (action.value === 1) {
|
form.value.paramFormat = "#.00000";
|
} else if (action.value === 4) {
|
form.value.paramFormat = "YYYY-MM-DD HH:mm:ss";
|
} else {
|
form.value.paramFormat = "";
|
}
|
showParamTypeSheet.value = false;
|
};
|
|
const onSelectDictType = action => {
|
form.value.paramFormat = action.value;
|
showDictTypeSheet.value = false;
|
};
|
|
const onSelectTimeFormat = action => {
|
form.value.paramFormat = action.value;
|
showTimeFormatSheet.value = false;
|
};
|
|
const handleSubmit = () => {
|
formRef.value
|
.validate()
|
.then(() => {
|
if (form.value.paramType === 3 && !form.value.paramFormat) {
|
uni.showToast({ title: "请选择下拉字典", icon: "none" });
|
return;
|
}
|
|
loading.value = true;
|
const action = paramId.value ? editBaseParam : addBaseParam;
|
action({ ...form.value, id: paramId.value || undefined })
|
.then(() => {
|
uni.showToast({ title: "保存成功", icon: "success" });
|
setTimeout(() => {
|
goBack();
|
}, 1500);
|
})
|
.catch(() => {
|
uni.showToast({ title: "保存失败", icon: "none" });
|
})
|
.finally(() => {
|
loading.value = false;
|
});
|
})
|
.catch(errors => {
|
if (errors && errors.length > 0) {
|
uni.showToast({
|
title: errors[0].message,
|
icon: "none",
|
});
|
}
|
});
|
};
|
|
onReady(() => {
|
if (formRef.value) {
|
formRef.value.setRules(rules);
|
}
|
});
|
|
onMounted(() => {
|
getDictTypes();
|
});
|
|
onLoad(options => {
|
if (options?.item) {
|
const item = JSON.parse(decodeURIComponent(options.item));
|
paramId.value = item.id;
|
if (item.paramType) {
|
item.paramType = Number(item.paramType);
|
}
|
Object.assign(form.value, item);
|
}
|
});
|
</script>
|
|
<style scoped lang="scss">
|
@import "@/static/scss/form-common.scss";
|
|
.basic-parameters-edit {
|
min-height: 100vh;
|
background: #f5f5f5;
|
}
|
</style>
|