feat(oa): register tripKit in FormCreate designer
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
0101e46ed9
commit
8294361817
8 files changed
+381
-2
No files matched your search
@@ -99,7 +99,8 @@ const SKIP_RULE_TYPES = new Set([
|
||||
'leaveKit',
|
||||
'confirmationKit',
|
||||
'overtimeKit',
|
||||
'resignationKit'
|
||||
'resignationKit',
|
||||
'tripKit'
|
||||
])
|
||||
|
||||
export function resolveStamp(status: string | undefined | null): {
|
||||
@@ -212,6 +213,35 @@ function formatFieldValue(value: unknown, rule: any): string {
|
||||
return formatScalar(value)
|
||||
}
|
||||
|
||||
function formatTripLegForPrint(leg: Record<string, unknown>, index: number): string {
|
||||
const transport = String(leg.transport || '').trim() || '-'
|
||||
const tripType = String(leg.tripType || '').trim()
|
||||
const fromCity = String(leg.fromCity || '').trim() || '-'
|
||||
const toCity = String(leg.toCity || '').trim() || '-'
|
||||
const startDate = String(leg.startDate || '').trim()
|
||||
const startHalf = String(leg.startHalf || '').trim()
|
||||
const endDate = String(leg.endDate || '').trim()
|
||||
const endHalf = String(leg.endHalf || '').trim()
|
||||
const duration = leg.duration
|
||||
const durationText =
|
||||
duration == null || duration === '' ? '-' : `${Number(duration)}天`
|
||||
const route = `${fromCity} → ${toCity}`
|
||||
const period =
|
||||
startDate && endDate
|
||||
? `${startDate}${startHalf ? ` ${startHalf}` : ''} ~ ${endDate}${endHalf ? ` ${endHalf}` : ''}`
|
||||
: '-'
|
||||
const typePart = tripType ? ` · ${tripType}` : ''
|
||||
return `行程${index + 1}:${transport}${typePart} · ${route} · ${period} · ${durationText}`
|
||||
}
|
||||
|
||||
function formatTripItineraryForPrint(value: unknown): string {
|
||||
if (!Array.isArray(value) || !value.length) return '-'
|
||||
const lines = value
|
||||
.filter((leg) => leg && typeof leg === 'object')
|
||||
.map((leg, idx) => formatTripLegForPrint(leg as Record<string, unknown>, idx))
|
||||
return lines.length ? lines.join('\n') : '-'
|
||||
}
|
||||
|
||||
async function walkRules(
|
||||
rules: any[],
|
||||
formData: Record<string, any>,
|
||||
@@ -292,6 +322,11 @@ async function walkRules(
|
||||
continue
|
||||
}
|
||||
|
||||
if (type === 'tripItinerary') {
|
||||
out.push({ label, text: formatTripItineraryForPrint(value) })
|
||||
continue
|
||||
}
|
||||
|
||||
if (UPLOAD_TYPES.has(type) || String(rule.name || '').toLowerCase().includes('upload')) {
|
||||
const urls = extractUrls(value)
|
||||
out.push({ label, urls: urls.length ? urls : [], text: urls.length ? undefined : '-' })
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import FcDesigner from '@form-create/designer'
|
||||
import OaTripKit from '@/components/oa-approval/OaTripKit.vue'
|
||||
import { tripKitDragRule } from '@/utils/oa-trip-kit-rule'
|
||||
|
||||
const FLAG = '__oaTripKitConfigured'
|
||||
|
||||
export function setupOaTripKit(formCreate?: { component?: (name: string, c: unknown) => void }) {
|
||||
if ((FcDesigner as any)[FLAG]) return
|
||||
|
||||
if (typeof FcDesigner.addMenu === 'function') {
|
||||
try {
|
||||
FcDesigner.addMenu({ name: 'kit', title: '套件', list: [] })
|
||||
} catch {
|
||||
/* kit menu may already exist */
|
||||
}
|
||||
}
|
||||
|
||||
FcDesigner.component('tripKit', OaTripKit)
|
||||
if (typeof formCreate?.component === 'function') {
|
||||
formCreate.component('tripKit', OaTripKit)
|
||||
}
|
||||
if (typeof FcDesigner.addDragRule === 'function') {
|
||||
FcDesigner.addDragRule(tripKitDragRule)
|
||||
}
|
||||
;(FcDesigner as any)[FLAG] = true
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
/** FormCreate Designer DragRule:出差套件 */
|
||||
|
||||
import { emptyTripLeg } from '@/utils/oa-trip-days'
|
||||
|
||||
let _uidSeq = 0
|
||||
function uniqueId() {
|
||||
const num = 370 + ++_uidSeq
|
||||
return (
|
||||
'F' +
|
||||
Math.random().toString(36).slice(3, 6) +
|
||||
Number(`${Date.now()}`).toString(36) +
|
||||
num.toString(36) +
|
||||
't'
|
||||
)
|
||||
}
|
||||
|
||||
export const DEFAULT_TRANSPORT_OPTIONS = [
|
||||
{ label: '飞机', value: '飞机' },
|
||||
{ label: '火车', value: '火车' },
|
||||
{ label: '汽车', value: '汽车' },
|
||||
{ label: '其他', value: '其他' }
|
||||
]
|
||||
|
||||
export const DEFAULT_TRIP_TYPE_OPTIONS = [
|
||||
{ label: '单程', value: '单程' },
|
||||
{ label: '往返', value: '往返' }
|
||||
]
|
||||
|
||||
function buildTripKitChildren() {
|
||||
const tripReasonField = uniqueId()
|
||||
const tripListField = uniqueId()
|
||||
const tripDaysField = uniqueId()
|
||||
const tripRemarkField = uniqueId()
|
||||
const tripCompanionsField = uniqueId()
|
||||
const tripExternalField = uniqueId()
|
||||
return {
|
||||
tripReasonField,
|
||||
tripListField,
|
||||
tripDaysField,
|
||||
tripRemarkField,
|
||||
tripCompanionsField,
|
||||
tripExternalField,
|
||||
children: [
|
||||
{
|
||||
type: 'input',
|
||||
field: tripReasonField,
|
||||
title: '出差事由',
|
||||
info: '',
|
||||
$required: true,
|
||||
props: {
|
||||
type: 'textarea',
|
||||
_fc_kit_field: 'tripReason',
|
||||
rows: 3,
|
||||
placeholder: '请输入出差事由'
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'tripItinerary',
|
||||
field: tripListField,
|
||||
title: '行程',
|
||||
info: '',
|
||||
$required: true,
|
||||
value: [emptyTripLeg()],
|
||||
props: {
|
||||
_fc_kit_field: 'tripList',
|
||||
transportOptions: DEFAULT_TRANSPORT_OPTIONS.map((o) => ({ ...o })),
|
||||
tripTypeOptions: DEFAULT_TRIP_TYPE_OPTIONS.map((o) => ({ ...o }))
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'inputNumber',
|
||||
field: tripDaysField,
|
||||
title: '出差天数',
|
||||
info: '',
|
||||
$required: true,
|
||||
props: {
|
||||
_fc_kit_field: 'tripDays',
|
||||
precision: 1,
|
||||
step: 0.5,
|
||||
min: 0,
|
||||
controlsPosition: 'right',
|
||||
align: 'left'
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
field: tripRemarkField,
|
||||
title: '出差备注',
|
||||
info: '',
|
||||
$required: false,
|
||||
props: {
|
||||
type: 'textarea',
|
||||
_fc_kit_field: 'tripRemark',
|
||||
rows: 2,
|
||||
placeholder: '请输入出差备注'
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'person',
|
||||
field: tripCompanionsField,
|
||||
title: '同行人',
|
||||
info: '',
|
||||
$required: false,
|
||||
props: {
|
||||
_fc_kit_field: 'tripCompanions',
|
||||
multiple: true
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
field: tripExternalField,
|
||||
title: '外部人员',
|
||||
info: '',
|
||||
$required: false,
|
||||
props: {
|
||||
_fc_kit_field: 'tripExternal',
|
||||
placeholder: '请输入外部人员'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/** FormCreate Designer DragRule:出差套件 */
|
||||
export const tripKitDragRule = {
|
||||
menu: 'kit' as const,
|
||||
icon: 'icon-date',
|
||||
label: '出差',
|
||||
name: 'tripKit',
|
||||
input: false,
|
||||
drag: true,
|
||||
dragBtn: true,
|
||||
mask: false,
|
||||
inside: true,
|
||||
rule() {
|
||||
const built = buildTripKitChildren()
|
||||
return {
|
||||
type: 'tripKit',
|
||||
props: {
|
||||
tripReasonField: built.tripReasonField,
|
||||
tripListField: built.tripListField,
|
||||
tripDaysField: built.tripDaysField,
|
||||
tripRemarkField: built.tripRemarkField,
|
||||
tripCompanionsField: built.tripCompanionsField,
|
||||
tripExternalField: built.tripExternalField
|
||||
},
|
||||
children: built.children
|
||||
}
|
||||
},
|
||||
props() {
|
||||
return []
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
import { calcTripLegDays, sumTripLegDays } from '@/utils/oa-trip-days'
|
||||
|
||||
export const TRIP_MANUAL_PREFIX = '__manual_'
|
||||
|
||||
export type TripKitRef = {
|
||||
listField: string
|
||||
daysField: string
|
||||
}
|
||||
|
||||
function asRules(schema: unknown): Record<string, unknown>[] {
|
||||
if (Array.isArray(schema)) return schema.filter((r) => r && typeof r === 'object') as any
|
||||
if (schema && typeof schema === 'object') {
|
||||
const s = schema as any
|
||||
const rules = s.rule || s.rules || s.fields
|
||||
if (Array.isArray(rules)) return rules.filter((r: any) => r && typeof r === 'object')
|
||||
if (s.type || s.field) return [s]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
function walk(rules: Record<string, unknown>[], out: Record<string, unknown>[]) {
|
||||
for (const r of rules) {
|
||||
out.push(r)
|
||||
const children = r.children
|
||||
if (Array.isArray(children)) {
|
||||
walk(
|
||||
children.filter((c) => c && typeof c === 'object') as Record<string, unknown>[],
|
||||
out
|
||||
)
|
||||
}
|
||||
const props = r.props as Record<string, unknown> | undefined
|
||||
if (props && Array.isArray(props.rule)) {
|
||||
walk(
|
||||
props.rule.filter((c) => c && typeof c === 'object') as Record<string, unknown>[],
|
||||
out
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function resolveTripKitFields(kit: Record<string, unknown>): TripKitRef | null {
|
||||
const props = (kit.props && typeof kit.props === 'object' ? kit.props : {}) as Record<
|
||||
string,
|
||||
any
|
||||
>
|
||||
let listField = props.tripListField as string | undefined
|
||||
let daysField = props.tripDaysField as string | undefined
|
||||
|
||||
if (listField && daysField) {
|
||||
return { listField: String(listField), daysField: String(daysField) }
|
||||
}
|
||||
|
||||
const children = (
|
||||
Array.isArray(kit.children)
|
||||
? kit.children
|
||||
: Array.isArray(props.rule)
|
||||
? props.rule
|
||||
: []
|
||||
) as Record<string, any>[]
|
||||
|
||||
for (const c of children) {
|
||||
if (!c?.field) continue
|
||||
const kitKey = String(c.props?._fc_kit_field || '')
|
||||
if (kitKey === 'tripList') listField = String(c.field)
|
||||
else if (kitKey === 'tripDays') daysField = String(c.field)
|
||||
}
|
||||
|
||||
if (!listField || !daysField) return null
|
||||
return { listField, daysField }
|
||||
}
|
||||
|
||||
export function listTripKits(schema: unknown): TripKitRef[] {
|
||||
const flat: Record<string, unknown>[] = []
|
||||
walk(asRules(schema), flat)
|
||||
const out: TripKitRef[] = []
|
||||
for (const r of flat) {
|
||||
if (String(r.type || '') !== 'tripKit') continue
|
||||
const ref = resolveTripKitFields(r)
|
||||
if (ref) out.push(ref)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** 未手改时重算行程时长与出差天数;返回是否有变更 */
|
||||
export function applyTripKitDaysToFormData(
|
||||
schema: unknown,
|
||||
formData: Record<string, unknown>
|
||||
): { data: Record<string, unknown>; changed: boolean } {
|
||||
const data = { ...formData }
|
||||
let changed = false
|
||||
|
||||
for (const kit of listTripKits(schema)) {
|
||||
const legsRaw = data[kit.listField]
|
||||
if (!Array.isArray(legsRaw)) continue
|
||||
|
||||
const prevLegsJson = JSON.stringify(data[kit.listField])
|
||||
const legs: Record<string, unknown>[] = []
|
||||
|
||||
for (const leg of legsRaw) {
|
||||
if (!leg || typeof leg !== 'object') {
|
||||
legs.push(leg as Record<string, unknown>)
|
||||
continue
|
||||
}
|
||||
const row = { ...(leg as Record<string, unknown>) }
|
||||
if (!row.durationManual) {
|
||||
const days = calcTripLegDays(
|
||||
String(row.startDate || ''),
|
||||
String(row.startHalf || ''),
|
||||
String(row.endDate || ''),
|
||||
String(row.endHalf || '')
|
||||
)
|
||||
if (days != null && row.duration !== days) {
|
||||
row.duration = days
|
||||
}
|
||||
}
|
||||
legs.push(row)
|
||||
}
|
||||
|
||||
data[kit.listField] = legs
|
||||
if (JSON.stringify(legs) !== prevLegsJson) {
|
||||
changed = true
|
||||
}
|
||||
|
||||
const manualKey = TRIP_MANUAL_PREFIX + kit.daysField
|
||||
if (data[manualKey]) continue
|
||||
|
||||
const sum = sumTripLegDays(legs)
|
||||
if (data[kit.daysField] !== sum) {
|
||||
data[kit.daysField] = sum
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
|
||||
return { data, changed }
|
||||
}
|
||||
Reference in new issue
Block a user