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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<script setup lang="ts">
  /**
   * BPM 动态表单渲染器
   *
   * 负责把 form-parser 解析出的字段用 uni 组件渲染。
   * 只做渲染与取值,提交逻辑由父页面负责。
   */
  import { computed } from "vue";
  import type { FormField, FormFieldOption } from "../form-parser";
 
  const props = defineProps<{
    fields: FormField[];
    /** 双向绑定的表单值:{ [field]: value } */
    modelValue: Record<string, any>;
    /** 字段权限:'1' 只读 / '2' 可写 / '3' 隐藏 */
    permissions?: Record<string, string>;
    disabled?: boolean;
    /**
     * 动态选项映射:字段名 -> 选项列表。
     * UserSelect / DeptSelect / DictSelect 的候选数据来自接口,解析阶段拿不到,
     * 所以由父页面(加载过人员/部门/字典的页面)把「值 -> 名称」的映射注入进来,
     * 否则回显时只能显示后端的原始 id(如「122」)。
     */
    fieldOptions?: Record<string, Array<{ key: number | string; label: string }>>;
  }>();
 
  const emit = defineEmits<{
    (e: "update:modelValue", value: Record<string, any>): void;
    (e: "change", field: string, value: any): void;
    /** 需要弹外部选择面板的字段(用户/部门/字典/省市区),由父页面接管 */
    (e: "pick", field: FormField): void;
  }>();
 
  function visibleFields() {
    return props.fields.filter(f => props.permissions?.[f.field] !== "3");
  }
 
  function isDisabled(field: FormField) {
    if (props.disabled) return true;
    // '1' 只读;其余(含无权限信息)可编辑
    return props.permissions?.[field.field] === "1";
  }
 
  function setValue(field: string, value: any) {
    const next = { ...props.modelValue, [field]: value };
    emit("update:modelValue", next);
    emit("change", field, value);
  }
 
  function getValue(field: FormField) {
    return props.modelValue[field.field];
  }
 
  /**
   * 字段的候选选项:优先用父页面注入的动态选项(人员/部门/字典来自接口),
   * 没有时退回解析阶段得到的静态 options。
   * 注入的是 SheetOption(key/label),这里统一归一化成 FormFieldOption(value/label)。
   */
  function optionsOf(field: FormField): FormFieldOption[] {
    const injected = props.fieldOptions?.[field.field];
    if (injected?.length) {
      return injected.map(item => ({ value: item.key, label: item.label }));
    }
    return field.options;
  }
 
  /** picker 选中:按选项索引取值 */
  function onOptionPick(field: FormField, index: number) {
    const option = field.options[index];
    if (!option) return;
    if (field.multiple) {
      const current: Array<number | string> = Array.isArray(getValue(field))
        ? [...getValue(field)]
        : [];
      const pos = current.findIndex(item => String(item) === String(option.value));
      if (pos >= 0) current.splice(pos, 1);
      else current.push(option.value);
      setValue(field.field, current);
      return;
    }
    setValue(field.field, option.value);
  }
 
  /**
   * 单个值的展示文本。
   * 后端给值有两种形态:标量(id / 枚举值)或对象({id,name}),两种都要能显示。
   * 标量优先按候选选项反查名称,查不到再退回原始值。
   */
  function valueText(field: FormField, item: any) {
    if (item === undefined || item === null || item === "") return "";
    if (typeof item === "object") {
      return item.label ?? item.name ?? item.url ?? JSON.stringify(item);
    }
    const hit = optionsOf(field).find(o => String(o.value) === String(item));
    return hit ? hit.label : String(item);
  }
 
  /** 值 -> 展示文本(数组用「、」连接) */
  function valueListText(field: FormField, value: any) {
    if (Array.isArray(value)) return value.map(item => valueText(field, item)).join("、");
    return valueText(field, value);
  }
 
  /** 选择器展示文案:无值给占位(灰字),有值给名称 */
  function displayText(field: FormField) {
    if (!hasValue(field)) return field.placeholder;
    return valueListText(field, getValue(field));
  }
 
  /** 是否为「选择类」字段(右侧展示箭头,走 picker 交互) */
  function isSelectLike(field: FormField) {
    return (
      field.kind === "select" ||
      field.kind === "radio" ||
      field.kind === "checkbox" ||
      field.kind === "date" ||
      field.kind === "datetime" ||
      field.kind === "time" ||
      field.kind === "user" ||
      field.kind === "dept" ||
      field.kind === "dict" ||
      field.kind === "area"
    );
  }
 
  /** 是否已选值 */
  function hasValue(field: FormField) {
    const value = getValue(field);
    return !(
      value === undefined || value === null || value === "" ||
      (Array.isArray(value) && value.length === 0)
    );
  }
 
  /** 选择器的展示文案:无值时用占位(灰字),有值时用选项/值文本 */
  function pickerText(field: FormField) {
    return displayText(field);
  }
 
  /** 多选/单选是否选中 */
  function isOptionActive(field: FormField, value: number | string) {
    const current = getValue(field);
    if (field.multiple && Array.isArray(current)) {
      return current.some(item => String(item) === String(value));
    }
    return String(current) === String(value);
  }
 
  /** 日期时间选择:uni picker 的 value 需要字符串 */
  function pickerValue(field: FormField) {
    const value = getValue(field);
    if (typeof value === "string") return value;
    return "";
  }
 
  /** 只读展示的文本 */
  function readonlyText(field: FormField) {
    const value = getValue(field);
    if (value === undefined || value === null || value === "") return "--";
    const text = valueListText(field, value);
    return text === "" ? "--" : text;
  }
</script>
 
<template>
  <view class="bpm-form">
    <view v-for="field in visibleFields()" :key="field.field" class="field">
      <view class="field-label">
        <text>{{ field.title }}</text>
        <text v-if="field.required" class="required">*</text>
      </view>
 
      <!-- 只读态统一走文本展示 -->
      <view v-if="isDisabled(field)" class="readonly">
        {{ readonlyText(field) }}
      </view>
 
      <!-- 单行文本 -->
      <input
        v-else-if="field.kind === 'input'"
        class="input"
        :value="getValue(field) ?? ''"
        :placeholder="field.placeholder"
        @input="setValue(field.field, $event.detail.value)"
      />
 
      <!-- 多行文本 / 富文本降级 -->
      <textarea
        v-else-if="field.kind === 'textarea'"
        class="textarea"
        :value="getValue(field) ?? ''"
        :placeholder="field.placeholder"
        @input="setValue(field.field, $event.detail.value)"
      />
 
      <!-- 数字 -->
      <input
        v-else-if="field.kind === 'number'"
        class="input"
        type="digit"
        :value="getValue(field) === undefined || getValue(field) === null ? '' : String(getValue(field))"
        :placeholder="field.placeholder"
        @input="setValue(field.field, $event.detail.value === '' ? '' : Number($event.detail.value))"
      />
 
      <!-- 下拉 / 单选:单选走 picker,多选走 picker(多选模式) -->
      <picker
        v-else-if="field.kind === 'select' || field.kind === 'radio'"
        :range="field.options"
        range-key="label"
        @change="onOptionPick(field, Number($event.detail.value))"
      >
        <view class="picker" :class="{ placeholder: !hasValue(field) }">
          <text class="picker-text">{{ pickerText(field) }}</text>
          <text class="arrow">›</text>
        </view>
      </picker>
 
      <!-- 多选下拉 -->
      <picker
        v-else-if="field.kind === 'checkbox' || field.multiple"
        :range="field.options"
        range-key="label"
        @change="onOptionPick(field, Number($event.detail.value))"
      >
        <view class="picker" :class="{ placeholder: !hasValue(field) }">
          <text class="picker-text">{{ pickerText(field) }}</text>
          <text class="arrow">›</text>
        </view>
      </picker>
 
      <!-- 开关 -->
      <switch
        v-else-if="field.kind === 'switch'"
        :checked="Boolean(getValue(field))"
        @change="setValue(field.field, $event.detail.value)"
      />
 
      <!-- 日期 -->
      <picker
        v-else-if="field.kind === 'date'"
        mode="date"
        :value="pickerValue(field)"
        @change="setValue(field.field, $event.detail.value)"
      >
        <view class="picker" :class="{ placeholder: !hasValue(field) }">
          <text class="picker-text">{{ pickerText(field) }}</text>
          <text class="arrow">›</text>
        </view>
      </picker>
 
      <!-- 日期时间 -->
      <picker
        v-else-if="field.kind === 'datetime'"
        mode="multiSelector"
        :range="[[], [], [], [], [], []]"
        @change="setValue(field.field, $event.detail.value)"
      >
        <view class="picker" :class="{ placeholder: !hasValue(field) }">
          <text class="picker-text">{{ pickerText(field) }}</text>
          <text class="arrow">›</text>
        </view>
      </picker>
 
      <!-- 时间 -->
      <picker
        v-else-if="field.kind === 'time'"
        mode="time"
        :value="pickerValue(field)"
        @change="setValue(field.field, $event.detail.value)"
      >
        <view class="picker" :class="{ placeholder: !hasValue(field) }">
          <text class="picker-text">{{ pickerText(field) }}</text>
          <text class="arrow">›</text>
        </view>
      </picker>
 
      <!-- 评分 -->
      <view v-else-if="field.kind === 'rate'" class="rate">
        <text
          v-for="n in 5"
          :key="n"
          class="star"
          :class="{ active: Number(getValue(field)) >= n }"
          @click="setValue(field.field, n)"
        >
          ★
        </text>
      </view>
 
      <!-- 用户 / 部门 / 字典 / 省市区选择:交给父页面弹出选择面板 -->
      <view
        v-else-if="isSelectLike(field)"
        class="picker"
        :class="{ placeholder: !hasValue(field) }"
        @click="emit('pick', field)"
      >
        <text class="picker-text">{{ pickerText(field) }}</text>
        <text class="arrow">›</text>
      </view>
 
      <!-- 兜底:不支持的组件按文本录入,保证值能填上 -->
      <input
        v-else
        class="input"
        :value="getValue(field) ?? ''"
        :placeholder="field.placeholder"
        @input="setValue(field.field, $event.detail.value)"
      />
 
      <text v-if="field.info" class="field-info">{{ field.info }}</text>
    </view>
 
    <view v-if="!visibleFields().length" class="empty">暂无表单字段</view>
  </view>
</template>
 
<style scoped lang="scss">
  .bpm-form {
    padding: 24rpx 0;
  }
 
  .field {
    width: 100%;
    min-width: 0;
    margin-bottom: 28rpx;
    box-sizing: border-box;
  }
 
  .field-label {
    display: flex;
    align-items: center;
    margin-bottom: 14rpx;
    font-size: 28rpx;
    color: #333;
  }
 
  .required {
    margin-left: 6rpx;
    color: #ff4d4f;
  }
 
  .input,
  .textarea,
  .picker {
    display: block;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    padding: 20rpx 24rpx;
    font-size: 28rpx;
    color: #333;
    background: #f7f8fa;
    border-radius: 12rpx;
    box-sizing: border-box;
  }
 
  .input {
    height: 84rpx;
    line-height: 44rpx;
  }
 
  /* 只读字段:与「可输入」在观感上明确区分(虚框 + 灰字、无输入框底色),
     否则只读字段看起来跟输入框一模一样,容易被误认为「输入框点不动」 */
  .readonly {
    width: 100%;
    padding: 18rpx 24rpx;
    font-size: 28rpx;
    color: #7d8a9c;
    background: transparent;
    border: 1rpx dashed #dde5ef;
    border-radius: 12rpx;
    box-sizing: border-box;
  }
 
  .textarea {
    display: block;
    width: 100%;
    min-height: 160rpx;
  }
 
  .field :deep(input),
  .field :deep(textarea) {
    width: 100%;
    min-width: 0;
    box-sizing: border-box;
  }
 
  .picker {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12rpx;
    color: #333;
  }
 
  .picker-text {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
 
  .picker.placeholder .picker-text {
    color: #b3bcc9;
  }
 
  .arrow {
    flex: none;
    color: #c2cad6;
    font-size: 32rpx;
    line-height: 1;
  }
 
  .rate {
    display: flex;
    gap: 12rpx;
  }
 
  .star {
    font-size: 40rpx;
    color: #ddd;
  }
 
  .star.active {
    color: #ffb400;
  }
 
  .upload {
    padding: 16rpx 24rpx;
    background: #f7f8fa;
    border-radius: 12rpx;
  }
 
  .upload-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10rpx 0;
  }
 
  .upload-name {
    flex: 1;
    font-size: 26rpx;
    color: #333;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
 
  .upload-remove {
    margin-left: 20rpx;
    font-size: 26rpx;
    color: #ff4d4f;
  }
 
  .upload-tip {
    display: block;
    margin-top: 8rpx;
    font-size: 24rpx;
    color: #999;
  }
 
  .field-info {
    display: block;
    margin-top: 8rpx;
    font-size: 24rpx;
    color: #999;
  }
 
  .empty {
    padding: 60rpx 0;
    font-size: 28rpx;
    color: #999;
    text-align: center;
  }
</style>