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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<script setup lang="ts">
  import { computed, reactive, ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import { getSimpleDictDataList, type DictData } from "../../../../api/crm";
  import {
    createProcess,
    createProcessContent,
    deleteProcessContent,
    generateAutoCode,
    getProcess,
    getProcessContentList,
    updateProcess,
    updateProcessContent,
    type ProProcess,
    type ProProcessContent,
  } from "../../../../api/process-design";
  import { useAuth } from "../../../../stores/auth";
  import { DICT, RULE, dateText, dictLabel, dictOptions } from "../shared";
 
  const { hasPermission, state } = useAuth();
  const id = ref<number>();
  const mode = ref<"edit" | "detail">("edit");
  const saving = ref(false);
  const generating = ref(false);
  const stepSaving = ref(false);
  const stepLoading = ref(false);
  const dicts = ref<DictData[]>([]);
  const steps = ref<ProProcessContent[]>([]);
  const stepVisible = ref(false);
  const stepForm = reactive<ProProcessContent>({});
 
  const readonly = computed(() => mode.value === "detail");
  const editable = computed(() => !readonly.value && hasPermission("mes:pro-process:update"));
  const canSave = computed(() =>
    id.value ? hasPermission("mes:pro-process:update") : hasPermission("mes:pro-process:create"),
  );
  const showSave = computed(() => !readonly.value && canSave.value);
  const statusOptions = computed(() => dictOptions(dicts.value, DICT.COMMON_STATUS));
  const stepTitle = computed(() => (stepForm.id ? "编辑工序步骤" : "新增工序步骤"));
  const form = reactive<ProProcess>({ code: "", name: "", status: 0, attention: "", remark: "" });
 
  function selectStatus(event: { detail: { value: number } }) {
    const row = statusOptions.value[Number(event.detail.value)];
    form.status = row?.value === undefined ? 0 : Number(row.value);
  }
 
  async function generateCode() {
    if (!state.accessToken || generating.value || readonly.value) return;
    generating.value = true;
    try {
      form.code = await generateAutoCode(state.accessToken, RULE.PRO_PROCESS_CODE);
    } catch (error) {
      uni.showToast({ title: error instanceof Error ? error.message : "编码生成失败", icon: "none" });
    } finally {
      generating.value = false;
    }
  }
 
  async function loadSteps() {
    if (!state.accessToken || !id.value) return;
    stepLoading.value = true;
    try {
      steps.value = (await getProcessContentList(state.accessToken, id.value)) || [];
    } catch (error) {
      uni.showToast({ title: error instanceof Error ? error.message : "操作步骤加载失败", icon: "none" });
    } finally {
      stepLoading.value = false;
    }
  }
 
  function openStep(row?: ProProcessContent) {
    if (!editable.value) return;
    Object.keys(stepForm).forEach(key => delete (stepForm as Record<string, unknown>)[key]);
    if (row) {
      Object.assign(stepForm, row);
    } else {
      const maxSort = Math.max(0, ...steps.value.map(item => item.sort || 0));
      Object.assign(stepForm, { processId: id.value, sort: maxSort + 1 });
    }
    stepVisible.value = true;
  }
 
  function closeStep() {
    stepVisible.value = false;
  }
 
  async function saveStep() {
    if (!state.accessToken || !id.value || stepSaving.value) return;
    stepSaving.value = true;
    try {
      const data: ProProcessContent = {
        ...stepForm,
        processId: id.value,
        sort: stepForm.sort === undefined || stepForm.sort === null ? undefined : Number(stepForm.sort),
      };
      if (stepForm.id) await updateProcessContent(state.accessToken, data);
      else await createProcessContent(state.accessToken, data);
      stepVisible.value = false;
      uni.showToast({ title: "保存成功", icon: "success" });
      await loadSteps();
    } catch (error) {
      uni.showToast({ title: error instanceof Error ? error.message : "保存失败", icon: "none" });
    } finally {
      stepSaving.value = false;
    }
  }
 
  async function removeStep(row: ProProcessContent) {
    if (!state.accessToken || !row.id || !editable.value) return;
    const ok = await new Promise<boolean>(resolve =>
      uni.showModal({
        title: "确认删除",
        content: "确定删除该工序步骤吗?",
        success: res => resolve(res.confirm),
      }),
    );
    if (!ok) return;
    try {
      await deleteProcessContent(state.accessToken, row.id);
      uni.showToast({ title: "删除成功", icon: "success" });
      await loadSteps();
    } catch (error) {
      uni.showToast({ title: error instanceof Error ? error.message : "删除失败", icon: "none" });
    }
  }
 
  async function save() {
    if (!form.code?.trim()) return uni.showToast({ title: "请填写工序编码", icon: "none" });
    if (!form.name?.trim()) return uni.showToast({ title: "请填写工序名称", icon: "none" });
    if (!state.accessToken || saving.value || !showSave.value) return;
    saving.value = true;
    try {
      const data: ProProcess = {
        ...form,
        id: id.value,
        code: form.code.trim(),
        name: form.name.trim(),
        status: form.status === undefined || form.status === null ? 0 : Number(form.status),
      };
      if (id.value) {
        await updateProcess(state.accessToken, data);
        uni.showToast({ title: "保存成功", icon: "success" });
        setTimeout(() => uni.navigateBack(), 500);
        return;
      }
      const newId = await createProcess(state.accessToken, data);
      uni.showToast({ title: "保存成功", icon: "success" });
      if (typeof newId === "number") {
        // 新增后停留在当前页并切到编辑模式,便于继续维护操作步骤
        id.value = newId;
        mode.value = "edit";
        Object.assign(form, { id: newId });
        await loadSteps();
      } else {
        setTimeout(() => uni.navigateBack(), 500);
      }
    } catch (error) {
      uni.showToast({ title: error instanceof Error ? error.message : "保存失败", icon: "none" });
    } finally {
      saving.value = false;
    }
  }
 
  onLoad(async query => {
    id.value = query.id ? Number(query.id) : undefined;
    mode.value = query.mode === "detail" ? "detail" : "edit";
    if (!state.accessToken) return;
    try {
      dicts.value = await getSimpleDictDataList(state.accessToken);
      if (id.value) {
        Object.assign(form, await getProcess(state.accessToken, id.value));
        await loadSteps();
      }
    } catch (error) {
      uni.showToast({ title: error instanceof Error ? error.message : "数据加载失败", icon: "none" });
    }
  });
</script>
 
<template>
  <view class="page">
    <view class="form-card">
      <view class="field code-field">
        <text>工序编码 <text class="required">*</text></text>
        <input v-model="form.code" :disabled="readonly" placeholder="请输入工序编码" />
        <button v-if="!readonly" :loading="generating" :disabled="generating" @click="generateCode">{{ generating ? '生成中' : '生成' }}</button>
      </view>
      <view class="field"><text>工序名称 <text class="required">*</text></text><input v-model="form.name" :disabled="readonly" placeholder="请输入工序名称" /></view>
      <view class="field">
        <text>状态</text>
        <picker v-if="!readonly" :range="statusOptions" range-key="label" @change="selectStatus">
          <view class="picker"><text>{{ dictLabel(dicts, DICT.COMMON_STATUS, form.status, '请选择状态') }}</text><text>›</text></view>
        </picker>
        <text v-else class="value">{{ dictLabel(dicts, DICT.COMMON_STATUS, form.status) }}</text>
      </view>
      <view v-if="readonly" class="field"><text>创建时间</text><text class="value">{{ dateText(form.createTime) }}</text></view>
      <view class="field column"><text>工序说明</text><textarea v-model="form.attention" :disabled="readonly" placeholder="请输入工序说明" /></view>
      <view class="field column"><text>备注</text><textarea v-model="form.remark" :disabled="readonly" placeholder="请输入备注" /></view>
    </view>
 
    <view v-if="id" class="form-card">
      <view class="section">
        <text>操作步骤</text>
        <button v-if="editable" @click="openStep()">新增步骤</button>
      </view>
      <view v-if="steps.length">
        <view v-for="step in steps" :key="step.id" class="step">
          <view class="step-head">
            <view><text class="step-sort">序号 {{ step.sort ?? '--' }}</text><text class="step-content">{{ step.content || '未填写步骤说明' }}</text></view>
          </view>
          <view class="grid">
            <text>辅助设备:{{ step.device || '--' }}</text>
            <text>辅助材料:{{ step.material || '--' }}</text>
            <text>材料文档:{{ step.docUrl || '--' }}</text>
            <text>备注:{{ step.remark || '--' }}</text>
          </view>
          <view v-if="editable" class="step-actions">
            <button @click="openStep(step)">编辑</button>
            <button class="danger" @click="removeStep(step)">删除</button>
          </view>
        </view>
      </view>
      <view v-else class="state compact">{{ stepLoading ? '加载中...' : '暂无操作步骤' }}</view>
    </view>
 
    <button v-if="showSave" class="save" :loading="saving" :disabled="saving" @click="save">保存</button>
    <button v-else-if="readonly && hasPermission('mes:pro-process:update')" class="save" @click="mode='edit'">编辑</button>
    <view v-else-if="!readonly" class="state">暂无保存权限</view>
 
    <view v-if="stepVisible" class="mask" @click="closeStep">
      <view class="modal" @click.stop>
        <view class="modal-title">{{ stepTitle }}</view>
        <view class="field"><text>序号</text><input v-model="stepForm.sort" type="number" placeholder="请输入序号" /></view>
        <view class="field column"><text>步骤说明</text><textarea v-model="stepForm.content" placeholder="请输入步骤说明" /></view>
        <view class="field"><text>辅助设备</text><input v-model="stepForm.device" placeholder="请输入辅助设备" /></view>
        <view class="field"><text>辅助材料</text><input v-model="stepForm.material" placeholder="请输入辅助材料" /></view>
        <view class="field"><text>材料文档 URL</text><input v-model="stepForm.docUrl" placeholder="请输入材料文档 URL" /></view>
        <view class="field column"><text>备注</text><textarea v-model="stepForm.remark" placeholder="请输入备注" /></view>
        <view class="modal-actions">
          <button class="ghost" @click="closeStep">取消</button>
          <button class="primary" :loading="stepSaving" :disabled="stepSaving" @click="saveStep">保存</button>
        </view>
      </view>
    </view>
  </view>
</template>
 
<style lang="scss" scoped>
.page{min-height:100vh;padding:24rpx 30rpx 70rpx;box-sizing:border-box;background:#f6f7f9}.form-card{margin-bottom:18rpx;overflow:hidden;background:#fff;border-radius:16rpx}.field{display:flex;align-items:center;min-height:96rpx;padding:0 24rpx;gap:20rpx;border-bottom:1rpx solid #eef1f5}.field>text:first-child{width:190rpx;flex:none;color:#33455d;font-size:27rpx}.field input,.field picker,.picker,.value{min-width:0;flex:1;color:#1e3049;font-size:27rpx}.picker{display:flex;align-items:center;justify-content:space-between;color:#65758a}.picker text:first-child,.value{overflow:hidden;text-align:right;text-overflow:ellipsis;white-space:nowrap}.picker text:last-child{margin-left:16rpx;color:#8092a7;font-size:36rpx}.required{color:#e23b3b}.code-field input{min-width:0}.code-field button{flex:none;min-width:136rpx;padding:0 16rpx;margin:0;color:var(--color-primary, #1769d8);font-size:24rpx;line-height:56rpx;background:#fff;border:1rpx solid var(--color-primary, #1769d8);border-radius:8rpx}.code-field button::after,.save::after,.step-actions button::after,.modal-actions button::after,.section button::after{border:0}.column{align-items:flex-start;flex-direction:column;padding:20rpx 24rpx}.column>text:first-child{width:auto}.column textarea{width:100%;height:140rpx;font-size:27rpx}.section{display:flex;align-items:center;justify-content:space-between;height:88rpx;padding:0 24rpx;color:#10233f;font-size:29rpx;font-weight:600}.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}.step{margin:0 20rpx 18rpx;border:1rpx solid #edf1f6;border-radius:12rpx;overflow:hidden}.step-head{padding:18rpx 20rpx;background:#f9fbff}.step-head>view{display:flex;min-width:0;flex-direction:column;gap:8rpx}.step-sort{color:var(--color-primary, #1769d8);font-size:22rpx;font-weight:600}.step-content{overflow:hidden;color:#10233f;font-size:27rpx;text-overflow:ellipsis;white-space:nowrap}.grid{display:grid;gap:10rpx;padding:16rpx 20rpx;color:#66778c;font-size:23rpx;border-top:1rpx solid #edf1f6}.grid text{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.step-actions{display:flex;justify-content:flex-end;gap:12rpx;padding:12rpx 20rpx;border-top:1rpx solid #f2f5f9}.step-actions button{height:50rpx;min-height:50rpx;padding:0 18rpx;margin:0;color:var(--color-primary, #1769d8);font-size:23rpx;line-height:48rpx;background:var(--color-primary-soft, #f5f8ff);border:1rpx solid var(--color-primary-border, #8ab3ee);border-radius:8rpx}.danger{color:#c65050;background:#fff7f7;border:1rpx solid #e3a0a0}.save{margin-top:20rpx;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}.mask{position:fixed;top:0;right:0;bottom:0;left:0;z-index:900;display:flex;align-items:center;justify-content:center;padding:0 40rpx;background:rgba(16,35,63,.45)}.modal{width:100%;max-height:84vh;padding-bottom:20rpx;overflow-y:auto;background:#fff;border-radius:20rpx}.modal-title{padding:28rpx 28rpx 12rpx;color:#10233f;font-size:30rpx;font-weight:600}.modal-actions{display:flex;justify-content:flex-end;gap:16rpx;padding:20rpx 24rpx 0}.modal-actions button{height:72rpx;min-height:72rpx;padding:0 34rpx;margin:0;font-size:26rpx;line-height:70rpx;border-radius:36rpx}.ghost{color:var(--color-primary, #1769d8);background:#f2f6fc;border:1rpx solid #cfe0f7}.primary{color:#fff;background:var(--color-primary, #1769d8)}
</style>