<template>
|
<view class="process-edit">
|
<PageHeader :title="pageTitle"
|
@back="goBack" />
|
<up-form ref="formRef"
|
:model="form"
|
:rules="rules"
|
:errorType="['none']"
|
label-width="110">
|
<up-form-item label="工序编码"
|
prop="no">
|
<up-input v-model="form.no"
|
placeholder="请输入工序编码"
|
clearable />
|
</up-form-item>
|
<up-form-item label="工序名称"
|
prop="name"
|
required>
|
<up-input v-model="form.name"
|
placeholder="请输入工序名称"
|
clearable />
|
</up-form-item>
|
<up-form-item label="工资定额"
|
prop="salaryQuota">
|
<up-input v-model="form.salaryQuota"
|
type="number"
|
placeholder="请输入工资定额"
|
clearable />
|
</up-form-item>
|
<up-form-item label="计费类型"
|
prop="type">
|
<up-input v-model="typeText"
|
placeholder="请选择计费类型"
|
readonly
|
@click="showTypeSheet = true" />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showTypeSheet = true"></up-icon>
|
</template>
|
</up-form-item>
|
<up-form-item label="是否质检"
|
prop="isQuality">
|
<view style="display: flex; justify-content: flex-end; width: 100%;">
|
<up-switch v-model="form.isQuality" />
|
</view>
|
</up-form-item>
|
<up-form-item label="是否生产"
|
prop="isProduction">
|
<view style="display: flex; justify-content: flex-end; width: 100%;">
|
<up-switch v-model="form.isProduction" />
|
</view>
|
</up-form-item>
|
<up-form-item label="关联设备"
|
prop="deviceLedgerId">
|
<up-input v-model="deviceText"
|
placeholder="请选择关联设备"
|
readonly
|
@click="showDeviceSheet = true" />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showDeviceSheet = true"></up-icon>
|
</template>
|
</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="processId ? '保存' : '新增'"
|
@cancel="goBack"
|
@confirm="handleSubmit" />
|
<!-- 计费类型选择 -->
|
<up-action-sheet :show="showTypeSheet"
|
title="选择计费类型"
|
:actions="typeActions"
|
@select="onSelectType"
|
@close="showTypeSheet = false" />
|
<!-- 设备选择 -->
|
<up-action-sheet :show="showDeviceSheet"
|
title="选择关联设备"
|
:actions="deviceActions"
|
@select="onSelectDevice"
|
@close="showDeviceSheet = false" />
|
</view>
|
</template>
|
|
<script setup>
|
import { reactive, ref, computed, onMounted } from "vue";
|
import { onLoad, onReady } from "@dcloudio/uni-app";
|
import FooterButtons from "@/components/FooterButtons.vue";
|
import {
|
add,
|
update,
|
getDeviceLedger,
|
} from "@/api/productionManagement/processManagement";
|
|
const formRef = ref(null);
|
const loading = ref(false);
|
const processId = ref(null);
|
const pageTitle = computed(() => (processId.value ? "编辑工序" : "新增工序"));
|
|
const form = ref({
|
no: "",
|
name: "",
|
salaryQuota: "",
|
isQuality: false,
|
isProduction: false,
|
remark: "",
|
deviceLedgerId: null,
|
type: 0,
|
});
|
|
const rules = {
|
name: [{ required: true, message: "请输入工序名称" }],
|
salaryQuota: [
|
{
|
validator: (rule, value, callback) => {
|
if (value !== "" && value !== null && (isNaN(value) || value < 0)) {
|
callback(new Error("工资定额必须是非负数字"));
|
} else {
|
callback();
|
}
|
},
|
},
|
],
|
};
|
|
const showTypeSheet = ref(false);
|
const typeActions = [
|
{ name: "计时", value: 0 },
|
{ name: "计件", value: 1 },
|
];
|
const typeText = computed(() => {
|
const action = typeActions.find(a => a.value === form.value.type);
|
return action ? action.name : "";
|
});
|
|
const showDeviceSheet = ref(false);
|
const deviceActions = ref([]);
|
const deviceText = ref("");
|
|
const onSelectType = e => {
|
form.value.type = e.value;
|
showTypeSheet.value = false;
|
};
|
|
const onSelectDevice = e => {
|
form.value.deviceLedgerId = e.id;
|
deviceText.value = e.name;
|
showDeviceSheet.value = false;
|
};
|
|
const loadDevices = async () => {
|
try {
|
const { data } = await getDeviceLedger();
|
deviceActions.value = (data || []).map(item => ({
|
name: item.deviceName,
|
id: item.id,
|
}));
|
if (form.value.deviceLedgerId) {
|
const device = deviceActions.value.find(
|
d => d.id === Number(form.value.deviceLedgerId)
|
);
|
if (device) deviceText.value = device.name;
|
}
|
} catch (error) {
|
console.error("加载设备失败", error);
|
}
|
};
|
|
const goBack = () => {
|
uni.navigateBack();
|
};
|
|
const handleSubmit = () => {
|
formRef.value
|
.validate()
|
.then(() => {
|
loading.value = true;
|
const promise = processId.value ? update(form.value) : add(form.value);
|
promise
|
.then(() => {
|
uni.showToast({ title: processId.value ? "保存成功" : "新增成功" });
|
setTimeout(() => {
|
goBack();
|
}, 1500);
|
})
|
.catch(err => {
|
uni.showToast({ title: err.msg || "提交失败", icon: "error" });
|
})
|
.finally(() => {
|
loading.value = false;
|
});
|
})
|
.catch(errors => {
|
if (errors && errors.length > 0) {
|
uni.showToast({
|
title: errors[0].message,
|
icon: "none",
|
});
|
}
|
});
|
};
|
|
onLoad(option => {
|
if (option.item) {
|
const item = JSON.parse(decodeURIComponent(option.item));
|
processId.value = item.id;
|
Object.assign(form.value, item);
|
// 处理类型转换,确保是数字
|
form.value.type = Number(form.value.type);
|
form.value.isQuality = !!form.value.isQuality;
|
form.value.isProduction = !!form.value.isProduction;
|
}
|
});
|
|
onReady(() => {
|
formRef.value.setRules(rules);
|
});
|
|
onMounted(() => {
|
loadDevices();
|
});
|
</script>
|
|
<style scoped lang="scss">
|
@import "@/static/scss/form-common.scss";
|
|
.process-edit {
|
min-height: 100vh;
|
background: #f5f5f5;
|
}
|
</style>
|