import { getDateTimeUnit } from '@jnpf/utils';
|
|
import { useUserStore } from '@vben/stores';
|
|
import dayjs from 'dayjs';
|
import { cloneDeep, lowerFirst, upperFirst } from 'lodash-es';
|
|
import { useFlowState } from '#/hooks/flow/useFlowStatus';
|
import { $t } from '#/locales';
|
|
import { useDateList, useInputList, useSelectList } from './config';
|
|
const { flowStatusList } = useFlowState();
|
|
export const getRealProps = (data, jnpfKey) => {
|
if (Reflect.has(data, 'clearable')) {
|
data.allowClear = data.clearable;
|
delete data.clearable;
|
}
|
if (Reflect.has(data, 'filterable')) {
|
data.showSearch = data.filterable;
|
delete data.filterable;
|
}
|
if (Reflect.has(data, 'props')) {
|
data.fieldNames = data.props;
|
delete data.props;
|
}
|
if (jnpfKey === 'switch') {
|
data.checkedValue = data.activeValue;
|
data.unCheckedValue = data.inactiveValue;
|
delete data.activeValue;
|
delete data.inactiveValue;
|
}
|
if (jnpfKey === 'card') {
|
data.title = data.header;
|
delete data.title;
|
data.hoverable = data.shadow === 'hover';
|
delete data.shadow;
|
}
|
return data;
|
};
|
export const getFormSchemaItem = (data) => {
|
const item = getRealProps(data, data.__config__.jnpfKey);
|
const componentProps = cloneDeep(item);
|
// delete componentProps.__config__;
|
delete componentProps.on;
|
delete componentProps.__vModel__;
|
// if (Reflect.has(item.__config__, 'children')) {
|
// componentProps.children = item.__config__.children;
|
// }
|
|
const rules: any[] = [];
|
const regList = item.__config__.regList;
|
if (regList) {
|
for (const element of regList) {
|
rules.push({
|
pattern: eval(element.pattern),
|
message: element.messageI18nCode ? $t(element.messageI18nCode, element.message) : element.message,
|
trigger: item.__config__.trigger,
|
});
|
}
|
}
|
|
const schema = {
|
field: item.prop,
|
label: item.labelI18nCode ? $t(item.labelI18nCode, item.label) : item.label,
|
defaultValue: item.__config__.defaultValue,
|
component: upperFirst(item.__config__.jnpfKey),
|
componentProps,
|
labelWidth: item.__config__.labelWidth || null,
|
required: !!item.__config__.required,
|
colProps: { span: item.__config__.span || 24 },
|
className: item.__config__.className,
|
value: item.value,
|
sourceType: item.sourceType,
|
rules,
|
show: !item.noShow,
|
__config__: item.__config__,
|
};
|
return schema;
|
};
|
export const getFormSchemas = (list: any[]) => {
|
const schemas: any[] = [];
|
const list1: any[] = [];
|
const loop = (list) => {
|
for (const element of list) {
|
if (!element?.noShow) {
|
const schema = getFormSchemaItem(element);
|
list1.push({ ...element, schema });
|
schemas.push(schema);
|
}
|
}
|
};
|
loop(list);
|
return schemas;
|
};
|
export const getFormConfig = (formConf) => {
|
return {
|
labelWidth: formConf.labelWidth,
|
schemas: getFormSchemas(formConf.fields),
|
layout: formConf.labelPosition === 'top' ? 'vertical' : 'horizontal',
|
labelAlign: formConf.labelPosition === 'right' ? 'right' : 'left',
|
disabled: formConf.disabled,
|
};
|
};
|
export const getSearchFormSchemas = (list: any[], isFlow = false) => {
|
// 获取当前时间
|
const now = dayjs();
|
const schemas: any[] = getFormSchemas(list);
|
for (const e of schemas) {
|
const delList = ['className', 'colProps', 'labelWidth', 'required', 'rules', 'defaultValue'];
|
for (const element of delList) {
|
delete e[element];
|
}
|
setSearchDefaultValue(e, now);
|
if (useInputList.includes(lowerFirst(e.component))) {
|
e.component = 'Input';
|
e.componentProps = { placeholder: $t('common.inputTextPrefix') + e.label, allowClear: true };
|
}
|
if (useDateList.includes(lowerFirst(e.component))) {
|
e.component = 'DateRange';
|
e.componentProps = { format: 'YYYY-MM-DD HH:mm:ss' };
|
}
|
if (e.component === 'Switch') {
|
e.componentProps = {
|
checkedValue: e.componentProps.checkedValue,
|
unCheckedValue: e.componentProps.unCheckedValue,
|
};
|
}
|
if (useSelectList.includes(lowerFirst(e.component))) {
|
e.component = 'Select';
|
e.componentProps = {
|
options: e.componentProps.options,
|
showSearch: true,
|
multiple: !!e.componentProps.searchMultiple,
|
fieldNames: e.componentProps.fieldNames,
|
placeholder: $t('common.chooseTextPrefix') + e.label,
|
};
|
}
|
if (['Calculate', 'InputNumber', 'Rate', 'Slider'].includes(e.component)) {
|
let precision = e.componentProps.precision;
|
if ((!e.componentProps.precision && e.component === 'Calculate') || e.component === 'Slider') precision = 0;
|
if (e.component === 'Rate') precision = e.componentProps.allowHalf ? 1 : 0;
|
if (!e.__config__.isFromParam) e.component = 'NumberRange';
|
e.componentProps = { precision };
|
}
|
if (e.component === 'Cascader') {
|
e.componentProps = {
|
options: e.componentProps.options,
|
showSearch: true,
|
multiple: !!e.componentProps.searchMultiple,
|
fieldNames: e.componentProps.fieldNames,
|
showAllLevels: e.componentProps.showAllLevels,
|
placeholder: $t('common.chooseTextPrefix') + e.label,
|
};
|
}
|
if (e.component === 'TimePicker') {
|
e.component = 'TimeRange';
|
e.componentProps = { format: e.componentProps.format };
|
}
|
if (['DateCalculate', 'DatePicker'].includes(e.component)) {
|
if (!e.__config__.isFromParam) e.component = 'DateRange';
|
e.componentProps = { format: e.componentProps.format || 'yyyy-MM-dd HH:mm:ss' };
|
}
|
if (e.component === 'AreaSelect') {
|
e.componentProps = { placeholder: $t('common.chooseTextPrefix') + e.label, level: e.componentProps.level };
|
}
|
if (e.component === 'TreeSelect') {
|
e.componentProps = {
|
options: e.componentProps.options,
|
showSearch: true,
|
multiple: !!e.componentProps.searchMultiple,
|
fieldNames: e.componentProps.fieldNames,
|
placeholder: $t('common.chooseTextPrefix') + e.label,
|
};
|
}
|
if (['GroupSelect', 'OrganizeSelect', 'PosSelect', 'RoleSelect'].includes(e.component)) {
|
e.componentProps = {
|
selectType: e.componentProps.selectType,
|
ableIds: e.componentProps.ableIds,
|
multiple: !!e.componentProps.searchMultiple,
|
placeholder: $t('common.chooseTextPrefix') + e.label,
|
};
|
}
|
if (e.component === 'UsersSelect') {
|
e.componentProps = { placeholder: $t('common.chooseTextPrefix') + e.label };
|
}
|
if (e.component === 'UserSelect') {
|
e.componentProps = {
|
selectType: e.componentProps.selectType != 'all' && e.componentProps.selectType != 'custom' ? 'all' : e.componentProps.selectType,
|
ableIds: e.componentProps.ableIds,
|
multiple: !!e.componentProps.searchMultiple,
|
placeholder: $t('common.chooseTextPrefix') + e.label,
|
};
|
}
|
if (['CreateUser', 'ModifyUser'].includes(e.component)) {
|
e.component = 'UserSelect';
|
e.componentProps = { placeholder: $t('common.chooseTextPrefix') + e.label };
|
}
|
if (e.component === 'CurrOrganize') {
|
e.component = 'OrganizeSelect';
|
e.componentProps = { multiple: true, placeholder: $t('common.chooseTextPrefix') + e.label };
|
}
|
if (e.component === 'CurrPosition') {
|
e.component = 'PosSelect';
|
e.componentProps = { multiple: true, placeholder: $t('common.chooseTextPrefix') + e.label };
|
}
|
if (e.component === 'AutoComplete') {
|
e.componentProps = {
|
total: e.componentProps.total,
|
interfaceId: e.componentProps.interfaceId,
|
relationField: e.componentProps.relationField,
|
templateJson: e.componentProps.templateJson,
|
placeholder: $t('common.inputTextPrefix') + e.label,
|
};
|
}
|
}
|
if (list.some((o) => o.isKeyword)) {
|
const keywordItem = {
|
field: 'jnpfKeyword',
|
label: $t('common.keyword'),
|
component: 'Input',
|
componentProps: { placeholder: $t('common.leftTreeSearchText'), allowClear: true },
|
value: undefined,
|
show: true,
|
__config__: { jnpfKey: 'input' },
|
};
|
schemas.unshift(keywordItem);
|
}
|
if (isFlow && list.length) {
|
const flowStateItem = {
|
field: 'jnpfFlowState',
|
label: $t('component.table.status'),
|
component: 'Select',
|
componentProps: {
|
placeholder: $t('common.chooseTextPrefix') + $t('component.table.status'),
|
options: flowStatusList,
|
showSearch: true,
|
},
|
value: undefined,
|
show: true,
|
__config__: {
|
jnpfKey: 'Select',
|
},
|
};
|
schemas.push(flowStateItem);
|
}
|
return schemas;
|
};
|
function setSearchDefaultValue(item, now) {
|
if (!['DatePicker', 'OrganizeSelect', 'PosSelect', 'UserSelect'].includes(item.component) || item.sourceType != 4) return;
|
const userStore = useUserStore();
|
const userInfo: any = userStore.getUserInfo || {};
|
const val = item.value;
|
|
if (item.component === 'DatePicker') {
|
const format = item.componentProps.format || 'YYYY-MM-DD HH:mm:ss';
|
const unit = getDateTimeUnit(format);
|
let firstDate = now;
|
const lastTimeStamp = now.startOf(unit).valueOf();
|
if (item.__config__.isFromParam) {
|
item.value = lastTimeStamp;
|
return;
|
}
|
if (val === '@pastThreeDays') {
|
// 前三天
|
firstDate = now.subtract(3, 'day');
|
}
|
// 前一周
|
if (val === '@pastWeek') {
|
firstDate = now.subtract(1, 'week');
|
}
|
// 前一个月
|
if (val === '@pastMonth') {
|
firstDate = now.subtract(1, 'month');
|
}
|
// 前三个月
|
if (val === '@pastThreeMonths') {
|
firstDate = now.subtract(3, 'month');
|
}
|
// 前半年
|
if (val === '@pastSixMonths') {
|
firstDate = now.subtract(6, 'month');
|
}
|
// 前一年
|
if (val === '@pastYear') {
|
firstDate = now.subtract(1, 'year');
|
}
|
const firstTimeStamp = firstDate.startOf(unit).valueOf();
|
item.value = [firstTimeStamp, lastTimeStamp];
|
return;
|
}
|
if (item.component === 'OrganizeSelect' && val === '@currOrganize') {
|
// 当前组织
|
if (!userInfo.organizeIds?.length) {
|
item.value = item.componentProps.searchMultiple ? [] : '';
|
return;
|
}
|
item.value = item.componentProps.searchMultiple ? userInfo.organizeIds : userInfo.organizeId;
|
return;
|
}
|
if (item.component === 'PosSelect' && val === '@currPosition') {
|
// 当前岗位
|
if (!userInfo.positionIds?.length) {
|
item.value = item.componentProps.searchMultiple ? [] : '';
|
return;
|
}
|
item.value = item.componentProps.searchMultiple ? userInfo.positionIds : userInfo.positionId;
|
return;
|
}
|
if (item.component === 'UserSelect' && val === '@currUser') {
|
// 当前用户
|
item.value = item.componentProps.searchMultiple ? [userInfo.userId] : userInfo.userId;
|
}
|
}
|