<script setup lang="ts">
|
import { computed, ref } from "vue";
|
import { onLoad } from "@dcloudio/uni-app";
|
import { getSimpleDictDataList, type DictData } from "../../../../api/crm";
|
import {
|
getParameterPage,
|
getProcessParamListByProcess,
|
updateProcessParams,
|
type PdParameter,
|
type ProProcessParam,
|
} from "../../../../api/process-design";
|
import { useAuth } from "../../../../stores/auth";
|
import PickerSheet from "../components/PickerSheet.vue";
|
import { DICT, dictLabel, type SheetOption } from "../shared";
|
|
const { hasPermission, state } = useAuth();
|
const processId = ref<number>();
|
const processName = ref("");
|
const saving = ref(false);
|
const loading = ref(false);
|
const loadingParams = ref(false);
|
const dicts = ref<DictData[]>([]);
|
const boundList = ref<ProProcessParam[]>([]);
|
const parameterOptions = ref<SheetOption[]>([]);
|
const pickerVisible = ref(false);
|
const parameterCache = new Map<number, PdParameter>();
|
|
const canUpdate = computed(() => hasPermission("mes:pro-process:update"));
|
const pickedIds = computed(() =>
|
boundList.value.map(item => item.processParamId).filter((value): value is number => value !== undefined && value !== null),
|
);
|
|
async function loadBound() {
|
if (!state.accessToken || !processId.value) return;
|
loading.value = true;
|
try {
|
boundList.value = (await getProcessParamListByProcess(state.accessToken, processId.value)) || [];
|
} catch (error) {
|
uni.showToast({ title: error instanceof Error ? error.message : "已绑定参数加载失败", icon: "none" });
|
} finally {
|
loading.value = false;
|
}
|
}
|
|
/** 加载工艺参数候选项 */
|
async function loadParameters(keyword = "") {
|
if (!state.accessToken) return;
|
loadingParams.value = true;
|
try {
|
const page = await getParameterPage(state.accessToken, {
|
pageNo: 1,
|
pageSize: 200,
|
paramName: keyword || undefined,
|
});
|
parameterOptions.value = (page.list || [])
|
.filter(item => item.id !== undefined)
|
.map(item => ({
|
key: item.id as number,
|
label: item.paramName || "未命名参数",
|
sub: [item.paramCode, item.unitMeasureName].filter(Boolean).join(" · ") || "暂无编码",
|
extra: dictLabel(dicts.value, DICT.MES_PD_PARAM_TYPE, item.paramType, ""),
|
}));
|
parameterCache.clear();
|
(page.list || []).forEach(item => {
|
if (item.id !== undefined) parameterCache.set(item.id, item);
|
});
|
} catch (error) {
|
uni.showToast({ title: error instanceof Error ? error.message : "工艺参数加载失败", icon: "none" });
|
} finally {
|
loadingParams.value = false;
|
}
|
}
|
|
function openPicker() {
|
pickerVisible.value = true;
|
}
|
|
/** 接收选中的参数,跳过已绑定项后追加(与 web 端 onParametersSelected 一致) */
|
function onParametersSelected(keys: Array<number | string>) {
|
const existingIds = new Set(
|
boundList.value.map(item => item.processParamId).filter((value): value is number => value !== undefined && value !== null),
|
);
|
const newRows = keys
|
.map(key => parameterCache.get(Number(key)))
|
.filter((item): item is PdParameter => Boolean(item) && item.id !== undefined && !existingIds.has(item.id as number))
|
.map((item, index) => ({
|
processParamId: item.id,
|
paramCode: item.paramCode,
|
paramName: item.paramName,
|
paramType: item.paramType,
|
unitMeasureName: item.unitMeasureName,
|
required: Boolean(item.required),
|
sort: boundList.value.length + index + 1,
|
remark: item.remark,
|
}));
|
boundList.value = [...boundList.value, ...newRows];
|
}
|
|
function removeItem(index: number) {
|
boundList.value.splice(index, 1);
|
}
|
|
async function save() {
|
if (!state.accessToken || !processId.value || saving.value || !canUpdate.value) return;
|
saving.value = true;
|
try {
|
await updateProcessParams(
|
state.accessToken,
|
processId.value,
|
boundList.value
|
.filter(item => item.processParamId !== undefined && item.processParamId !== null)
|
.map((item, index) => ({
|
processParamId: item.processParamId as number,
|
required: Boolean(item.required),
|
sort: item.sort === undefined || item.sort === null ? index + 1 : Number(item.sort),
|
remark: item.remark,
|
})),
|
);
|
uni.showToast({ title: "保存成功", icon: "success" });
|
await loadBound();
|
} catch (error) {
|
uni.showToast({ title: error instanceof Error ? error.message : "保存失败", icon: "none" });
|
} finally {
|
saving.value = false;
|
}
|
}
|
|
function safeDecode(value: string) {
|
try {
|
return decodeURIComponent(value);
|
} catch {
|
return value;
|
}
|
}
|
|
onLoad(async query => {
|
processId.value = query.processId ? Number(query.processId) : undefined;
|
processName.value = query.processName ? safeDecode(String(query.processName)) : "";
|
if (!state.accessToken) return;
|
try {
|
dicts.value = await getSimpleDictDataList(state.accessToken);
|
await loadBound();
|
} catch (error) {
|
uni.showToast({ title: error instanceof Error ? error.message : "数据加载失败", icon: "none" });
|
}
|
});
|
</script>
|
|
<template>
|
<view class="page">
|
<view class="hero">
|
<text>工艺参数绑定</text>
|
<text>{{ processName || '生产工序' }}</text>
|
</view>
|
|
<view class="card">
|
<view class="section">
|
<text>已绑定工艺参数</text>
|
<button v-if="canUpdate" @click="openPicker">添加参数</button>
|
</view>
|
<view v-if="boundList.length">
|
<view v-for="(item, index) in boundList" :key="item.processParamId || index" class="bind">
|
<view class="bind-head">
|
<view>
|
<text class="bind-name">{{ item.paramName || '未命名参数' }}</text>
|
<text class="bind-code">{{ item.paramCode || '暂无编码' }}</text>
|
</view>
|
<text class="bind-type">{{ dictLabel(dicts, DICT.MES_PD_PARAM_TYPE, item.paramType, '未分类') }}</text>
|
</view>
|
<view class="bind-row"><text>单位</text><text class="bind-value">{{ item.unitMeasureName || '--' }}</text></view>
|
<view class="bind-row"><text>是否必填</text><switch :checked="!!item.required" :disabled="!canUpdate" @change="item.required = $event.detail.value" /></view>
|
<view class="bind-row"><text>排序</text><input v-model="item.sort" type="number" :disabled="!canUpdate" placeholder="请输入排序" /></view>
|
<view class="bind-row"><text>备注</text><input v-model="item.remark" :disabled="!canUpdate" placeholder="请输入备注" /></view>
|
<view v-if="canUpdate" class="bind-actions"><button class="danger" @click="removeItem(index)">删除</button></view>
|
</view>
|
</view>
|
<view v-else class="state compact">{{ loading ? '加载中…' : '暂未绑定参数' }}</view>
|
</view>
|
|
<button v-if="canUpdate" class="save" :loading="saving" :disabled="saving" @click="save">保存</button>
|
<view v-else class="state">暂无工艺参数绑定权限</view>
|
|
<PickerSheet
|
v-model:show="pickerVisible"
|
title="工艺参数选择"
|
multiple
|
:items="parameterOptions"
|
:selected-keys="pickedIds"
|
:loading="loadingParams"
|
search-placeholder="搜索参数名称"
|
empty-text="暂无工艺参数"
|
@search="loadParameters"
|
@confirm="onParametersSelected"
|
/>
|
</view>
|
</template>
|
|
<style lang="scss" scoped>
|
.page{min-height:100vh;padding:24rpx 30rpx 70rpx;box-sizing:border-box;background:#f6f7f9}.hero{display:flex;flex-direction:column;gap:8rpx;margin-bottom:18rpx;padding:26rpx 24rpx;color:#fff;background:var(--color-primary, #1769d8);border-radius:16rpx;box-shadow:0 14rpx 26rpx rgba(23,105,216,.2)}.hero text:first-child{font-size:31rpx;font-weight:700}.hero text:last-child{color:#d7e8ff;font-size:23rpx}.card{overflow:hidden;background:#fff;border-radius:16rpx}.section{display:flex;align-items:center;justify-content:space-between;height:92rpx;padding:0 24rpx;color:#10233f;font-size:29rpx;font-weight:600;border-bottom:1rpx solid #eef1f5}.section button{height:56rpx;min-height:56rpx;padding:0 20rpx;margin:0;color:var(--color-primary, #1769d8);font-size:24rpx;line-height:54rpx;background:var(--color-primary-soft, #f5f8ff);border:1rpx solid var(--color-primary-border, #8ab3ee);border-radius:8rpx}.section button::after,.bind-actions button::after,.save::after{border:0}.bind{margin:18rpx 20rpx;border:1rpx solid #edf1f6;border-radius:12rpx;overflow:hidden}.bind-head{display:flex;align-items:center;justify-content:space-between;gap:14rpx;padding:18rpx 20rpx;background:#f9fbff}.bind-head>view{display:flex;min-width:0;flex-direction:column;gap:8rpx}.bind-name{overflow:hidden;color:#10233f;font-size:27rpx;font-weight:600;text-overflow:ellipsis;white-space:nowrap}.bind-code{color:#8494a8;font-size:22rpx}.bind-type{flex:none;padding:6rpx 14rpx;color:var(--color-primary, #1769d8);font-size:22rpx;background:var(--color-primary-soft, #edf5ff);border:1rpx solid var(--color-primary-border, #bfdbff);border-radius:999rpx}.bind-row{display:flex;align-items:center;gap:16rpx;min-height:78rpx;padding:0 20rpx;border-top:1rpx solid #f2f5f9}.bind-row>text:first-child{width:150rpx;flex:none;color:#617287;font-size:25rpx}.bind-row input{min-width:0;flex:1;color:#1e3049;font-size:26rpx;text-align:right}.bind-value{min-width:0;flex:1;color:#1e3049;font-size:26rpx;text-align:right}.bind-actions{display:flex;justify-content:flex-end;padding:12rpx 20rpx;border-top:1rpx solid #f2f5f9}.bind-actions button{height:50rpx;min-height:50rpx;padding:0 18rpx;margin:0;font-size:23rpx;line-height:48rpx;border-radius:8rpx}.danger{color:#c65050;background:#fff7f7;border:1rpx solid #e3a0a0}.save{margin-top:28rpx;color:#fff;font-size:30rpx;background:var(--color-primary, #1769d8);border-radius:12rpx}.state{padding:38rpx;color:#8795a8;font-size:26rpx;text-align:center}.compact{padding:44rpx 0}
|
</style>
|