gaoluyang
2026-11-03 2d8b1a28fa5c8b852f0d0078bac4894bfb4abed5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<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>