344 lines
14 KiB
Vue
344 lines
14 KiB
Vue
<template>
|
||
<div class="space-y-3">
|
||
<p class="input-hint">{{ t('ds.processChannelHint') }}</p>
|
||
|
||
<div>
|
||
<ProcessPicker
|
||
:model-value="srcProcess"
|
||
:label="t('ds.processPick')"
|
||
:placeholder="t('ds.processPickPlaceholder')"
|
||
:loading="loadingProcesses"
|
||
@update:model-value="setProcess(String($event))"
|
||
/>
|
||
<p class="input-hint">{{ t('ds.processPickHint') }}</p>
|
||
<p v-if="srcProcess && processRow && !processRow.published" class="input-hint text-amber-600 dark:text-amber-400">
|
||
{{ t('ds.processNotPublished') }}
|
||
</p>
|
||
</div>
|
||
|
||
<div v-if="srcProcess" class="grid grid-cols-3 gap-3">
|
||
<div>
|
||
<span class="input-label">{{ t('ds.processStatus') }}</span>
|
||
<Select :model-value="match.status ?? 'completed'" :options="statusOptions" @update:model-value="setStatus($event)" />
|
||
<p class="input-hint">{{ t('ds.processStatusHint') }}</p>
|
||
</div>
|
||
<div>
|
||
<span class="input-label">{{ t('ds.processOrder') }}</span>
|
||
<Select :model-value="match.order ?? 'latest'" :options="orderOptions" @update:model-value="setOrder($event)" />
|
||
<p class="input-hint">{{ t('ds.processOrderHint') }}</p>
|
||
</div>
|
||
<div>
|
||
<span class="input-label">{{ t('ds.processScope') }}</span>
|
||
<Select :model-value="match.scope ?? 'own'" :options="scopeOptions" @update:model-value="setScope($event)" />
|
||
<p class="input-hint">{{ scopeHint }}</p>
|
||
</div>
|
||
<Input
|
||
type="number"
|
||
compact
|
||
:model-value="String(match.days ?? 0)"
|
||
:label="t('ds.processDays')"
|
||
@update:model-value="patch({ days: Number($event) || 0 })"
|
||
/>
|
||
<Input
|
||
v-if="kind === 'search'"
|
||
type="number"
|
||
compact
|
||
:model-value="String(match.limit ?? 20)"
|
||
:label="t('ds.processLimit')"
|
||
@update:model-value="patch({ limit: Number($event) || 20 })"
|
||
/>
|
||
<Input
|
||
v-else
|
||
compact
|
||
:model-value="String(match.person ?? '')"
|
||
:label="t('ds.processPerson')"
|
||
:placeholder="t('ds.processPersonPlaceholder')"
|
||
@update:model-value="patch({ person: String($event) })"
|
||
/>
|
||
</div>
|
||
|
||
<div v-if="srcProcess && isCross" class="rounded-xl border border-amber-200 p-2.5 text-xs dark:border-amber-500/40">
|
||
<p class="font-medium text-amber-700 dark:text-amber-400">{{ t('ds.processCrossTitle') }}</p>
|
||
<p class="mt-1 text-amber-700/90 dark:text-amber-400/90">{{ t('ds.processCrossHint') }}</p>
|
||
<p v-if="!minPermission" class="mt-1 font-medium text-amber-700 dark:text-amber-400">{{ t('ds.processCrossNeedPerm') }}</p>
|
||
</div>
|
||
|
||
<!-- 匹配条件:值用的还是外部取数那一份写法,这里不另教一套语法 -->
|
||
<div v-if="srcProcess" class="border-t border-gray-100 pt-2 dark:border-dark-700">
|
||
<p class="input-label">{{ t('ds.processFilters') }}</p>
|
||
<p class="input-hint">{{ t('ds.processFiltersHint') }}</p>
|
||
<div
|
||
v-for="(item, index) in filters"
|
||
:key="index"
|
||
class="mb-1.5 grid grid-cols-[minmax(0,1.1fr)_minmax(0,.8fr)_minmax(0,1.1fr)_2rem] items-end gap-1.5"
|
||
>
|
||
<Select
|
||
:model-value="item.field"
|
||
:options="fieldOptions"
|
||
:placeholder="t('ds.processFieldCol')"
|
||
compact
|
||
searchable
|
||
@update:model-value="setFilter(index, { field: String($event) })"
|
||
/>
|
||
<Select
|
||
:model-value="item.op || 'eq'"
|
||
:options="opOptions"
|
||
compact
|
||
@update:model-value="setFilter(index, { op: String($event) })"
|
||
/>
|
||
<Input
|
||
v-if="needsValue(item.op)"
|
||
compact
|
||
:model-value="String(item.value ?? '')"
|
||
:placeholder="t('ds.processValuePlaceholder')"
|
||
@update:model-value="setFilter(index, { value: String($event) })"
|
||
/>
|
||
<span v-else class="pb-2 text-xs text-gray-400">{{ t('ds.processValueNone') }}</span>
|
||
<button
|
||
type="button"
|
||
class="btn btn-ghost btn-icon mb-0.5 h-8 w-8 shrink-0 text-gray-400 hover:text-red-600"
|
||
:title="t('ds.processFilterRemove')"
|
||
:aria-label="t('ds.processFilterRemove')"
|
||
@click="removeFilter(index)"
|
||
>
|
||
<Icon name="x" class="h-3.5 w-3.5" />
|
||
</button>
|
||
</div>
|
||
<p v-if="!filters.length" class="input-hint">{{ t('ds.processFiltersEmpty') }}</p>
|
||
<button type="button" class="btn btn-secondary btn-sm w-full" :disabled="filters.length >= MAX_FILTERS" @click="addFilter">
|
||
<Icon name="plus" class="mr-1 h-3.5 w-3.5" />{{ t('ds.processFiltersAdd') }}
|
||
</button>
|
||
<p v-if="droppedFields.length" class="mt-1 input-hint text-amber-600 dark:text-amber-400">
|
||
{{ t('ds.processMissingFields', { fields: droppedFields.join('、') }) }}
|
||
</p>
|
||
<p v-if="filters.length" class="input-hint">{{ t('ds.processScanNote') }}</p>
|
||
</div>
|
||
|
||
<!-- 带出哪些字段:勾一下就把 pick / mapExtra 那两条路径写好,解析那一栏不再让人手打 -->
|
||
<div v-if="srcProcess" class="border-t border-gray-100 pt-2 dark:border-dark-700">
|
||
<div class="flex items-center justify-between">
|
||
<p class="input-label mb-0">{{ t('ds.processFields') }}</p>
|
||
<span v-if="ticked.length" class="badge badge-primary">{{ t('ds.processFieldsOn', { count: ticked.length }) }}</span>
|
||
</div>
|
||
<p class="input-hint">{{ kind === 'search' ? t('ds.processFieldsHintSearch') : t('ds.processFieldsHintFetch') }}</p>
|
||
<p v-if="loadingFields" class="input-hint">{{ t('ds.processFieldsLoading') }}</p>
|
||
<p v-else-if="!fields.length" class="input-hint text-amber-600 dark:text-amber-400">{{ t('ds.processFieldsEmpty') }}</p>
|
||
<div v-else class="max-h-[300px] space-y-1 overflow-y-auto rounded-lg border border-gray-200 p-2 dark:border-dark-600">
|
||
<div v-for="item in fields" :key="item.key" class="flex items-center gap-2 text-xs">
|
||
<input
|
||
type="checkbox"
|
||
class="rounded border-gray-300"
|
||
:checked="ticked.includes(item.key)"
|
||
:aria-label="item.label"
|
||
@change="toggleField(item.key, ($event.target as HTMLInputElement).checked)"
|
||
/>
|
||
<span class="min-w-0 flex-1 truncate">
|
||
{{ item.label }}
|
||
<span class="font-mono text-[11px] text-gray-400">{{ item.key }}</span>
|
||
</span>
|
||
<span v-if="item.formName && !item.default" class="flex-shrink-0 text-[11px] text-gray-400">{{ item.formName }}</span>
|
||
<button
|
||
v-if="kind === 'fetch' && ticked.includes(item.key)"
|
||
type="button"
|
||
class="btn btn-ghost btn-sm flex-shrink-0"
|
||
:class="{ 'btn-primary': mainKey === item.key }"
|
||
:title="t('ds.processMainHint')"
|
||
@click="setMain(item.key)"
|
||
>
|
||
{{ mainKey === item.key ? t('ds.processMainOn') : t('ds.processMain') }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
<p v-if="kind === 'fetch' && ticked.length && !mainKey" class="mt-1 input-hint text-amber-600 dark:text-amber-400">
|
||
{{ t('ds.processMainMissing') }}
|
||
</p>
|
||
<p v-if="kind === 'fetch'" class="input-hint">{{ t('ds.processParseLocked') }}</p>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
/**
|
||
* 数据源的「流程数据」通道:从本平台另一个流程的单据里带数据。
|
||
*
|
||
* 这个面板只写四样东西:``srcProcess``、``srcMatch``、``pick``、``mapExtra``。
|
||
* 后两样由「勾选要带出哪些字段」翻译出来(翻译只在 utils/processSource 那一处),
|
||
* 所以「解析」那一栏对这条通道不再摆输入框——同一份配置两个地方能改,早晚对不上。
|
||
*/
|
||
import { computed, onMounted, ref, watch } from 'vue'
|
||
import { useI18n } from 'vue-i18n'
|
||
import Icon from '@/components/icons/Icon.vue'
|
||
import ProcessPicker from '@/components/oa/design/ProcessPicker.vue'
|
||
import Input from '@/components/common/Input.vue'
|
||
import Select from '@/components/common/Select.vue'
|
||
import { processFields as fetchProcessFields } from '@/api/dataSources'
|
||
import { processCatalog } from '@/utils/processCatalog'
|
||
import {
|
||
MAX_FILTERS,
|
||
PROCESS_OPS,
|
||
mainFieldOfPick,
|
||
missingFilterFields,
|
||
tickedFields,
|
||
usableFilters,
|
||
writePicked
|
||
} from '@/utils/processSource'
|
||
import type { ProcessDataFilter, ProcessDataMatch, ProcessDef, ProcessFieldOption } from '@/types'
|
||
|
||
const props = defineProps<{
|
||
srcProcess: string
|
||
match: ProcessDataMatch
|
||
kind: 'fetch' | 'search'
|
||
pick: string
|
||
mapExtra: Record<string, string>
|
||
/** 跨人带数据必须有调用权限挡着,这里只负责把这句话讲到,配置本体在「可靠性与兜底」那一栏 */
|
||
minPermission: string
|
||
}>()
|
||
const emit = defineEmits<{ (e: 'patch', values: Record<string, unknown>): void }>()
|
||
const { t } = useI18n()
|
||
|
||
const processes = ref<ProcessDef[]>([])
|
||
const loadingProcesses = ref(false)
|
||
const fields = ref<ProcessFieldOption[]>([])
|
||
const loadingFields = ref(false)
|
||
/** 面板自己摊开的条件行:与后端认的那份之间只隔一次 usableFilters。 */
|
||
const filters = ref<ProcessDataFilter[]>([])
|
||
const ticked = ref<string[]>([])
|
||
const mainKey = ref('')
|
||
|
||
const processRow = computed(() => processes.value.find((item) => item.id === props.srcProcess) ?? null)
|
||
const isCross = computed(() => (props.match.scope ?? 'own') === 'all')
|
||
|
||
const statusOptions = computed(() =>
|
||
(['completed', 'running', 'any'] as const).map((item) => ({ value: item, label: t(`ds.processStatus_${item}`) }))
|
||
)
|
||
const orderOptions = computed(() =>
|
||
(['latest', 'earliest'] as const).map((item) => ({ value: item, label: t(`ds.processOrder_${item}`) }))
|
||
)
|
||
const scopeOptions = computed(() =>
|
||
(['own', 'all'] as const).map((item) => ({ value: item, label: t(`ds.processScope_${item}`) }))
|
||
)
|
||
const opOptions = computed(() => PROCESS_OPS.map((item) => ({ value: item, label: t(`ds.processOp_${item}`) })))
|
||
const fieldOptions = computed(() => fields.value.map((item) => ({ value: item.key, label: `${item.label} · ${item.key}` })))
|
||
const scopeHint = computed(() => t(isCross.value ? 'ds.processScopeHintAll' : 'ds.processScopeHintOwn'))
|
||
const droppedFields = computed(() => missingFilterFields({ ...props.match, filters: filters.value }, fields.value))
|
||
|
||
function needsValue(op: string): boolean {
|
||
return !['empty', 'notEmpty'].includes(String(op || 'eq'))
|
||
}
|
||
|
||
/** 把当前勾选与条件写回父级;条件里没填完整的行不进请求,免得存一堆半拉子。 */
|
||
function commit(next: Partial<ProcessDataMatch> = {}, picked: { keys: string[]; main: string } | null = null) {
|
||
const merged: ProcessDataMatch = { ...props.match, ...next }
|
||
const values: Record<string, unknown> = { srcMatch: merged }
|
||
const list = picked ?? { keys: ticked.value, main: mainKey.value }
|
||
if (picked) {
|
||
ticked.value = picked.keys
|
||
mainKey.value = picked.main
|
||
}
|
||
Object.assign(values, props.kind === 'search'
|
||
? { mapExtra: writePicked(list.keys, '').mapExtra }
|
||
: writePicked(list.keys, list.main))
|
||
emit('patch', values)
|
||
}
|
||
|
||
function patch(next: Partial<ProcessDataMatch>) {
|
||
commit(next)
|
||
}
|
||
|
||
// 三档下拉的候选都是常量表,值域由这里替 Select 收一次,不在模板里写 cast
|
||
function setStatus(value: unknown) {
|
||
commit({ status: value as ProcessDataMatch['status'] })
|
||
}
|
||
|
||
function setOrder(value: unknown) {
|
||
commit({ order: value as ProcessDataMatch['order'] })
|
||
}
|
||
|
||
function setScope(value: unknown) {
|
||
commit({ scope: value as ProcessDataMatch['scope'] })
|
||
}
|
||
|
||
function setProcess(id: string) {
|
||
// 换流程等于换一套表单:上一套勾出来的字段路径已经指不到东西了,一次清干净
|
||
ticked.value = []
|
||
mainKey.value = ''
|
||
filters.value = []
|
||
emit('patch', {
|
||
srcProcess: id,
|
||
srcMatch: { ...props.match, filters: [] },
|
||
pick: '',
|
||
mapExtra: {}
|
||
})
|
||
}
|
||
|
||
function addFilter() {
|
||
filters.value = [...filters.value, { field: '', op: 'eq', value: '' }]
|
||
}
|
||
|
||
function removeFilter(index: number) {
|
||
filters.value = filters.value.filter((_, at) => at !== index)
|
||
commit({ filters: usableFilters(filters.value) })
|
||
}
|
||
|
||
function setFilter(index: number, next: Partial<ProcessDataFilter>) {
|
||
filters.value = filters.value.map((item, at) => (at === index ? { ...item, ...next } : item))
|
||
commit({ filters: usableFilters(filters.value) })
|
||
}
|
||
|
||
function toggleField(key: string, on: boolean) {
|
||
const keys = on ? [...ticked.value, key] : ticked.value.filter((item) => item !== key)
|
||
const main = keys.includes(mainKey.value) ? mainKey.value : keys[0] ?? ''
|
||
commit({}, { keys, main })
|
||
}
|
||
|
||
function setMain(key: string) {
|
||
commit({}, { keys: ticked.value, main: key })
|
||
}
|
||
|
||
async function loadFields() {
|
||
if (!props.srcProcess) {
|
||
fields.value = []
|
||
return
|
||
}
|
||
loadingFields.value = true
|
||
try {
|
||
fields.value = (await fetchProcessFields(props.srcProcess)).fields
|
||
} catch {
|
||
fields.value = []
|
||
} finally {
|
||
loadingFields.value = false
|
||
}
|
||
}
|
||
|
||
watch(() => props.srcProcess, loadFields)
|
||
watch(
|
||
() => [props.srcProcess, props.pick, JSON.stringify(props.mapExtra ?? {})],
|
||
() => {
|
||
ticked.value = tickedFields(
|
||
{ code: '', name: '', kind: props.kind, url: '', provider: 'process', pick: props.pick, mapExtra: props.mapExtra },
|
||
fields.value
|
||
)
|
||
mainKey.value = mainFieldOfPick(props.pick)
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
|
||
onMounted(async () => {
|
||
loadingProcesses.value = true
|
||
// 清单本身交给选择器去拉,这边只要拿它判一下选中的流程发没发布
|
||
processes.value = (await processCatalog()).processes
|
||
loadingProcesses.value = false
|
||
loadFields()
|
||
})
|
||
|
||
/** 条件行跟着存下来的配置走:新建时后端那份是空的,编辑时按存过的摊开。 */
|
||
watch(
|
||
() => props.match.filters,
|
||
(raw) => {
|
||
const stored = usableFilters(Array.isArray(raw) ? raw : [])
|
||
if (JSON.stringify(stored) !== JSON.stringify(filters.value)) filters.value = stored
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
</script>
|