mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
fix: custom request role list (#4074)
* fix: custom request role list * fix: custom request body users * fix: support external data sources * fix: record data --------- Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
parent
27962ab283
commit
c9171a7117
@ -23,6 +23,7 @@ export type RemoteSelectProps<P = any> = SelectProps<P, any> & {
|
|||||||
mapOptions?: (data: any) => RemoteSelectProps['fieldNames'];
|
mapOptions?: (data: any) => RemoteSelectProps['fieldNames'];
|
||||||
targetField?: any;
|
targetField?: any;
|
||||||
service: ResourceActionOptions<P>;
|
service: ResourceActionOptions<P>;
|
||||||
|
dataSource?: string;
|
||||||
CustomDropdownRender?: (v: any) => any;
|
CustomDropdownRender?: (v: any) => any;
|
||||||
optionFilter?: (option: any) => boolean;
|
optionFilter?: (option: any) => boolean;
|
||||||
};
|
};
|
||||||
@ -41,10 +42,11 @@ const InternalRemoteSelect = connect(
|
|||||||
targetField: _targetField,
|
targetField: _targetField,
|
||||||
CustomDropdownRender,
|
CustomDropdownRender,
|
||||||
optionFilter,
|
optionFilter,
|
||||||
|
dataSource: propsDataSource,
|
||||||
...others
|
...others
|
||||||
} = props;
|
} = props;
|
||||||
const dataSource = useDataSourceKey();
|
const dataSource = useDataSourceKey();
|
||||||
const headers = useDataSourceHeaders(dataSource);
|
const headers = useDataSourceHeaders(propsDataSource || dataSource);
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const firstRun = useRef(false);
|
const firstRun = useRef(false);
|
||||||
const fieldSchema = useFieldSchema();
|
const fieldSchema = useFieldSchema();
|
||||||
|
@ -17,6 +17,10 @@ export class DataSourceManager {
|
|||||||
this.middlewares = [];
|
this.middlewares = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get(dataSourceKey: string) {
|
||||||
|
return this.dataSources.get(dataSourceKey);
|
||||||
|
}
|
||||||
|
|
||||||
async add(dataSource: DataSource, options: any = {}) {
|
async add(dataSource: DataSource, options: any = {}) {
|
||||||
await dataSource.load(options);
|
await dataSource.load(options);
|
||||||
this.dataSources.set(dataSource.name, dataSource);
|
this.dataSources.set(dataSource.name, dataSource);
|
||||||
|
@ -6,6 +6,7 @@ import {
|
|||||||
SchemaSettingsActionModalItem,
|
SchemaSettingsActionModalItem,
|
||||||
actionSettingsItems,
|
actionSettingsItems,
|
||||||
useCollection_deprecated,
|
useCollection_deprecated,
|
||||||
|
useDataSourceKey,
|
||||||
useRequest,
|
useRequest,
|
||||||
} from '@nocobase/client';
|
} from '@nocobase/client';
|
||||||
import { App } from 'antd';
|
import { App } from 'antd';
|
||||||
@ -19,6 +20,7 @@ import { CustomRequestACLSchema, CustomRequestConfigurationFieldsSchema } from '
|
|||||||
export function CustomRequestSettingsItem() {
|
export function CustomRequestSettingsItem() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { name } = useCollection_deprecated();
|
const { name } = useCollection_deprecated();
|
||||||
|
const dataSourceKey = useDataSourceKey();
|
||||||
const fieldSchema = useFieldSchema();
|
const fieldSchema = useFieldSchema();
|
||||||
const customRequestsResource = useCustomRequestsResource();
|
const customRequestsResource = useCustomRequestsResource();
|
||||||
const { message } = App.useApp();
|
const { message } = App.useApp();
|
||||||
@ -45,6 +47,7 @@ export function CustomRequestSettingsItem() {
|
|||||||
options: {
|
options: {
|
||||||
...requestSettings,
|
...requestSettings,
|
||||||
collectionName: name,
|
collectionName: name,
|
||||||
|
dataSourceKey,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
filterKeys: ['key'],
|
filterKeys: ['key'],
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
import { useCollection_deprecated, useCollectionFilterOptions, useCompile } from '@nocobase/client';
|
import {
|
||||||
|
DEFAULT_DATA_SOURCE_KEY,
|
||||||
|
useCollection_deprecated,
|
||||||
|
useCollectionFilterOptions,
|
||||||
|
useCompile,
|
||||||
|
} from '@nocobase/client';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useTranslation } from '../locale';
|
import { useTranslation } from '../locale';
|
||||||
|
|
||||||
@ -6,7 +11,7 @@ export const useCustomRequestVariableOptions = () => {
|
|||||||
const collection = useCollection_deprecated();
|
const collection = useCollection_deprecated();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const fieldsOptions = useCollectionFilterOptions(collection);
|
const fieldsOptions = useCollectionFilterOptions(collection);
|
||||||
const userFieldOptions = useCollectionFilterOptions('users');
|
const userFieldOptions = useCollectionFilterOptions('users', DEFAULT_DATA_SOURCE_KEY);
|
||||||
const compile = useCompile();
|
const compile = useCompile();
|
||||||
|
|
||||||
const [fields, userFields] = useMemo(() => {
|
const [fields, userFields] = useMemo(() => {
|
||||||
|
@ -1,15 +1,7 @@
|
|||||||
import { useField, useFieldSchema, useForm } from '@formily/react';
|
import { useField, useFieldSchema, useForm } from '@formily/react';
|
||||||
import {
|
import { useAPIClient, useActionContext, useCompile, useDataSourceKey, useRecord } from '@nocobase/client';
|
||||||
TableFieldResource,
|
|
||||||
useAPIClient,
|
|
||||||
useActionContext,
|
|
||||||
useBlockRequestContext,
|
|
||||||
useCollection_deprecated,
|
|
||||||
useCompile,
|
|
||||||
useRecord,
|
|
||||||
} from '@nocobase/client';
|
|
||||||
import { App } from 'antd';
|
|
||||||
import { isURL } from '@nocobase/utils/client';
|
import { isURL } from '@nocobase/utils/client';
|
||||||
|
import { App } from 'antd';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
export const useCustomizeRequestActionProps = () => {
|
export const useCustomizeRequestActionProps = () => {
|
||||||
@ -18,13 +10,13 @@ export const useCustomizeRequestActionProps = () => {
|
|||||||
const actionSchema = useFieldSchema();
|
const actionSchema = useFieldSchema();
|
||||||
const compile = useCompile();
|
const compile = useCompile();
|
||||||
const form = useForm();
|
const form = useForm();
|
||||||
const { getPrimaryKey } = useCollection_deprecated();
|
// const { getPrimaryKey } = useCollection_deprecated();
|
||||||
const { resource, __parent, service } = useBlockRequestContext();
|
|
||||||
const record = useRecord();
|
const record = useRecord();
|
||||||
const fieldSchema = useFieldSchema();
|
const fieldSchema = useFieldSchema();
|
||||||
const actionField = useField();
|
const actionField = useField();
|
||||||
const { setVisible } = useActionContext();
|
const { setVisible } = useActionContext();
|
||||||
const { modal, message } = App.useApp();
|
const { modal, message } = App.useApp();
|
||||||
|
const dataSourceKey = useDataSourceKey();
|
||||||
return {
|
return {
|
||||||
async onClick(e?, callBack?) {
|
async onClick(e?, callBack?) {
|
||||||
const { skipValidator, onSuccess } = actionSchema?.['x-action-settings'] ?? {};
|
const { skipValidator, onSuccess } = actionSchema?.['x-action-settings'] ?? {};
|
||||||
@ -33,7 +25,7 @@ export const useCustomizeRequestActionProps = () => {
|
|||||||
await form.submit();
|
await form.submit();
|
||||||
}
|
}
|
||||||
|
|
||||||
let formValues = {};
|
let formValues = { ...record };
|
||||||
if (xAction === 'customize:form:request') {
|
if (xAction === 'customize:form:request') {
|
||||||
formValues = form.values;
|
formValues = form.values;
|
||||||
}
|
}
|
||||||
@ -46,16 +38,14 @@ export const useCustomizeRequestActionProps = () => {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: {
|
data: {
|
||||||
currentRecord: {
|
currentRecord: {
|
||||||
id: record[getPrimaryKey()],
|
// id: record[getPrimaryKey()],
|
||||||
appends: service.params[0]?.appends,
|
// appends: result.params[0]?.appends,
|
||||||
|
dataSourceKey,
|
||||||
data: formValues,
|
data: formValues,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
actionField.data.loading = false;
|
actionField.data.loading = false;
|
||||||
if (!(resource instanceof TableFieldResource)) {
|
|
||||||
__parent?.service?.refresh?.();
|
|
||||||
}
|
|
||||||
// service?.refresh?.();
|
// service?.refresh?.();
|
||||||
if (callBack) {
|
if (callBack) {
|
||||||
callBack?.();
|
callBack?.();
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { DEFAULT_DATA_SOURCE_KEY } from '@nocobase/client';
|
||||||
import { generateNTemplate } from '../locale';
|
import { generateNTemplate } from '../locale';
|
||||||
|
|
||||||
export const CustomRequestACLSchema = {
|
export const CustomRequestACLSchema = {
|
||||||
@ -14,6 +15,7 @@ export const CustomRequestACLSchema = {
|
|||||||
'x-component-props': {
|
'x-component-props': {
|
||||||
multiple: true,
|
multiple: true,
|
||||||
objectValue: true,
|
objectValue: true,
|
||||||
|
dataSource: DEFAULT_DATA_SOURCE_KEY,
|
||||||
service: {
|
service: {
|
||||||
resource: 'roles',
|
resource: 'roles',
|
||||||
},
|
},
|
||||||
|
@ -2,6 +2,7 @@ import { Context, Next } from '@nocobase/actions';
|
|||||||
import { parse } from '@nocobase/utils';
|
import { parse } from '@nocobase/utils';
|
||||||
|
|
||||||
import { appendArrayColumn } from '@nocobase/evaluators';
|
import { appendArrayColumn } from '@nocobase/evaluators';
|
||||||
|
import Application from '@nocobase/server';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import CustomRequestPlugin from '../plugin';
|
import CustomRequestPlugin from '../plugin';
|
||||||
|
|
||||||
@ -46,7 +47,8 @@ const getCurrentUserAppends = (str: string, user) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export async function send(this: CustomRequestPlugin, ctx: Context, next: Next) {
|
export async function send(this: CustomRequestPlugin, ctx: Context, next: Next) {
|
||||||
const { filterByTk, resourceName, values = {} } = ctx.action.params;
|
const resourceName = ctx.action.resourceName;
|
||||||
|
const { filterByTk, values = {} } = ctx.action.params;
|
||||||
const {
|
const {
|
||||||
currentRecord = {
|
currentRecord = {
|
||||||
id: 0,
|
id: 0,
|
||||||
@ -70,7 +72,6 @@ export async function send(this: CustomRequestPlugin, ctx: Context, next: Next)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const repo = ctx.db.getRepository(resourceName);
|
const repo = ctx.db.getRepository(resourceName);
|
||||||
const requestConfig = await repo.findOne({
|
const requestConfig = await repo.findOne({
|
||||||
filter: {
|
filter: {
|
||||||
@ -84,13 +85,23 @@ export async function send(this: CustomRequestPlugin, ctx: Context, next: Next)
|
|||||||
|
|
||||||
ctx.withoutDataWrapping = true;
|
ctx.withoutDataWrapping = true;
|
||||||
|
|
||||||
const { collectionName, url, headers = [], params = [], data = {}, ...options } = requestConfig.options || {};
|
const {
|
||||||
|
dataSourceKey,
|
||||||
|
collectionName,
|
||||||
|
url,
|
||||||
|
headers = [],
|
||||||
|
params = [],
|
||||||
|
data = {},
|
||||||
|
...options
|
||||||
|
} = requestConfig.options || {};
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return ctx.throw(400, ctx.t('Please configure the request settings first', { ns: 'custom-request' }));
|
return ctx.throw(400, ctx.t('Please configure the request settings first', { ns: 'custom-request' }));
|
||||||
}
|
}
|
||||||
let currentRecordValues = {};
|
let currentRecordValues = {};
|
||||||
if (collectionName && typeof currentRecord.id !== 'undefined') {
|
if (collectionName && typeof currentRecord.id !== 'undefined') {
|
||||||
const recordRepo = ctx.db.getRepository(collectionName);
|
const app = ctx.app as Application;
|
||||||
|
const dataSource = app.dataSourceManager.get(dataSourceKey || currentRecord.dataSourceKey || 'main');
|
||||||
|
const recordRepo = dataSource.collectionManager.getRepository(collectionName);
|
||||||
currentRecordValues =
|
currentRecordValues =
|
||||||
(
|
(
|
||||||
await recordRepo.findOne({
|
await recordRepo.findOne({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user