<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>
|