fix(custom-request): support string/json templates (#514)
* fix: request api support dynamic custom * fix: scope undefined will not work * fix: use formily compile * fix: user may be null * fix: export add context
This commit is contained in:
parent
8eec4ac4d1
commit
95c750db15
@ -1,5 +1,7 @@
|
|||||||
|
import { Schema as SchemaCompiler } from '@formily/json-schema';
|
||||||
import { useField, useFieldSchema, useForm } from '@formily/react';
|
import { useField, useFieldSchema, useForm } from '@formily/react';
|
||||||
import { message, Modal } from 'antd';
|
import { message, Modal } from 'antd';
|
||||||
|
import get from 'lodash/get';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
import { useAPIClient } from '../../api-client';
|
import { useAPIClient } from '../../api-client';
|
||||||
@ -20,6 +22,13 @@ export const usePickActionProps = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function renderTemplate(str: string, data: any) {
|
||||||
|
const re = /\{\{\s*((\w+\.?)+)\s*\}\}/g;
|
||||||
|
return str.replace(re, function (_, key) {
|
||||||
|
return get(data, key) || '';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function isURL(string) {
|
function isURL(string) {
|
||||||
let url;
|
let url;
|
||||||
|
|
||||||
@ -215,6 +224,9 @@ export const useCustomizeRequestActionProps = () => {
|
|||||||
const form = useForm();
|
const form = useForm();
|
||||||
const { fields, getField } = useCollection();
|
const { fields, getField } = useCollection();
|
||||||
const { field, resource } = useBlockRequestContext();
|
const { field, resource } = useBlockRequestContext();
|
||||||
|
const currentRecord = useRecord();
|
||||||
|
const currentUserContext = useCurrentUserContext();
|
||||||
|
const currentUser = currentUserContext?.data?.data;
|
||||||
return {
|
return {
|
||||||
async onClick() {
|
async onClick() {
|
||||||
const { skipValidator, onSuccess, requestSettings } = actionSchema?.['x-action-settings'] ?? {};
|
const { skipValidator, onSuccess, requestSettings } = actionSchema?.['x-action-settings'] ?? {};
|
||||||
@ -234,11 +246,16 @@ export const useCustomizeRequestActionProps = () => {
|
|||||||
const values = getFormValues(filterByTk, field, form, fieldNames, getField, resource);
|
const values = getFormValues(filterByTk, field, form, fieldNames, getField, resource);
|
||||||
Object.assign(data, values);
|
Object.assign(data, values);
|
||||||
}
|
}
|
||||||
|
const requestBody = {
|
||||||
|
url: renderTemplate(requestSettings['url'], { currentRecord, currentUser }),
|
||||||
|
method: requestSettings['method'],
|
||||||
|
headers: SchemaCompiler.compile(headers, { currentRecord, currentUser }),
|
||||||
|
params: SchemaCompiler.compile(params, { currentRecord, currentUser }),
|
||||||
|
data: SchemaCompiler.compile(data, { currentRecord, currentUser }),
|
||||||
|
};
|
||||||
|
|
||||||
await apiClient.request({
|
await apiClient.request({
|
||||||
...requestSettings,
|
...requestBody,
|
||||||
headers,
|
|
||||||
params,
|
|
||||||
data,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!onSuccess?.successMessage) {
|
if (!onSuccess?.successMessage) {
|
||||||
|
@ -520,6 +520,9 @@ SchemaSettings.ActionModalItem = React.memo((props: any) => {
|
|||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
}}
|
}}
|
||||||
|
onKeyDown={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<Modal
|
<Modal
|
||||||
width={'50%'}
|
width={'50%'}
|
||||||
|
@ -3,4 +3,3 @@ export * from './mixin';
|
|||||||
export * from './mixin/AsyncEmitter';
|
export * from './mixin/AsyncEmitter';
|
||||||
export * from './registry';
|
export * from './registry';
|
||||||
export * from './uid';
|
export * from './uid';
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ export async function exportXlsx(ctx: Context, next: Next) {
|
|||||||
fields,
|
fields,
|
||||||
appends,
|
appends,
|
||||||
except,
|
except,
|
||||||
|
context: ctx,
|
||||||
});
|
});
|
||||||
const collectionFields = columns.map((col) => collection.fields.get(col.dataIndex[0]));
|
const collectionFields = columns.map((col) => collection.fields.get(col.dataIndex[0]));
|
||||||
const { rows, ranges } = await render({ columns, fields: collectionFields, data }, ctx);
|
const { rows, ranges } = await render({ columns, fields: collectionFields, data }, ctx);
|
||||||
|
Loading…
Reference in New Issue
Block a user