mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 14:26:36 +00:00
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 { message, Modal } from 'antd';
|
||||
import get from 'lodash/get';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
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) {
|
||||
let url;
|
||||
|
||||
@ -215,6 +224,9 @@ export const useCustomizeRequestActionProps = () => {
|
||||
const form = useForm();
|
||||
const { fields, getField } = useCollection();
|
||||
const { field, resource } = useBlockRequestContext();
|
||||
const currentRecord = useRecord();
|
||||
const currentUserContext = useCurrentUserContext();
|
||||
const currentUser = currentUserContext?.data?.data;
|
||||
return {
|
||||
async onClick() {
|
||||
const { skipValidator, onSuccess, requestSettings } = actionSchema?.['x-action-settings'] ?? {};
|
||||
@ -234,11 +246,16 @@ export const useCustomizeRequestActionProps = () => {
|
||||
const values = getFormValues(filterByTk, field, form, fieldNames, getField, resource);
|
||||
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({
|
||||
...requestSettings,
|
||||
headers,
|
||||
params,
|
||||
data,
|
||||
...requestBody,
|
||||
});
|
||||
|
||||
if (!onSuccess?.successMessage) {
|
||||
|
@ -520,6 +520,9 @@ SchemaSettings.ActionModalItem = React.memo((props: any) => {
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
}}
|
||||
onKeyDown={(e) => {
|
||||
e.stopPropagation();
|
||||
}}
|
||||
>
|
||||
<Modal
|
||||
width={'50%'}
|
||||
|
@ -3,4 +3,3 @@ export * from './mixin';
|
||||
export * from './mixin/AsyncEmitter';
|
||||
export * from './registry';
|
||||
export * from './uid';
|
||||
|
||||
|
@ -28,6 +28,7 @@ export async function exportXlsx(ctx: Context, next: Next) {
|
||||
fields,
|
||||
appends,
|
||||
except,
|
||||
context: ctx,
|
||||
});
|
||||
const collectionFields = columns.map((col) => collection.fields.get(col.dataIndex[0]));
|
||||
const { rows, ranges } = await render({ columns, fields: collectionFields, data }, ctx);
|
||||
|
Loading…
Reference in New Issue
Block a user