mirror of
https://github.com/nocobase/nocobase.git
synced 2026-09-24 16:02:20 +08:00
Merge branch 'feat/2.0-data-vi' into 2.0
This commit is contained in:
+22
-7
@@ -118,6 +118,20 @@ export class ChartBlockModel extends DataBlockModel<ChartBlockModelStructure> {
|
||||
return fields;
|
||||
}
|
||||
|
||||
// 检查当前资源与查询模式是否匹配,不匹配则重新初始化
|
||||
checkResource(query: any): void {
|
||||
const mode = query?.mode || 'builder';
|
||||
if (mode === 'sql') {
|
||||
if (!(this.resource instanceof SQLResource)) {
|
||||
this.initResource('sql');
|
||||
}
|
||||
} else {
|
||||
if (!(this.resource instanceof ChartResource)) {
|
||||
this.initResource('builder');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 公共方法:运行数据查询 + 刷新资源 + 更新结果面板
|
||||
async runQueryAndUpdateResult(query: any) {
|
||||
const uid = this.uid;
|
||||
@@ -146,21 +160,18 @@ export class ChartBlockModel extends DataBlockModel<ChartBlockModelStructure> {
|
||||
}
|
||||
|
||||
async setParamsAndPreview(params) {
|
||||
console.log('----setParamsAndPreview', params);
|
||||
// 暂存预览前的 stepParams,用于取消预览时回滚
|
||||
if (!this._previousStepParams) {
|
||||
this._previousStepParams = _.cloneDeep(this.stepParams);
|
||||
}
|
||||
this.setStepParams('chartSettings', 'configure', params);
|
||||
console.log('---latest setParams', this.getStepParams('chartSettings', 'configure'));
|
||||
// 这里不再直接执行 runQueryAndUpdateResult,避免重复执行
|
||||
// rerender -> applyAutoFlows -> handler -> runQueryAndUpdateResult
|
||||
this.rerender();
|
||||
// rerender -> applyAutoFlows -> handler -> runQueryAndUpdateResult
|
||||
}
|
||||
|
||||
// 取消预览,回滚stepParams
|
||||
async cancelPreview() {
|
||||
console.log('----cancelPreview', this._previousStepParams);
|
||||
if (this._previousStepParams) {
|
||||
this.setStepParams('chartSettings', 'configure', this._previousStepParams);
|
||||
this._previousStepParams = null;
|
||||
@@ -178,8 +189,14 @@ const PreviewButton = ({ style }) => {
|
||||
type="default"
|
||||
style={style}
|
||||
onClick={async () => {
|
||||
await form.submit();
|
||||
const formValues = ctx.getStepFormValues('chartSettings', 'configure');
|
||||
if (formValues?.query?.mode === 'builder') {
|
||||
await form.submit();
|
||||
}
|
||||
ctx.model.checkResource(formValues.query); // 保证 resource 正确
|
||||
if (formValues?.query?.mode === 'sql') {
|
||||
ctx.model.resource.setDebug(true); // 开启 debug 模式,sql 查询不要走 runById
|
||||
}
|
||||
ctx.model.setParamsAndPreview(formValues || {});
|
||||
}}
|
||||
>
|
||||
@@ -237,8 +254,6 @@ ChartBlockModel.registerFlow({
|
||||
},
|
||||
async handler(ctx, params) {
|
||||
const { query, chart } = params;
|
||||
console.log('----setting flow handler params', query, chart);
|
||||
|
||||
if (!query || !chart) {
|
||||
return;
|
||||
}
|
||||
|
||||
+11
-4
@@ -55,14 +55,21 @@ export const QueryPanel: React.FC = observer(() => {
|
||||
const handleRun = async () => {
|
||||
try {
|
||||
setRunning(true);
|
||||
// 先提交表单,触发校验
|
||||
await form.submit();
|
||||
|
||||
// 统一通过 Model 的公共方法执行查询与结果更新
|
||||
const query = form.values?.query;
|
||||
|
||||
// builder 模式需要先提交表单做校验;sql 模式不需要
|
||||
if (query?.mode === 'builder') {
|
||||
await form.submit();
|
||||
}
|
||||
|
||||
ctx.model.checkResource(query); // 保证 resource 正确
|
||||
if (query?.mode === 'sql') {
|
||||
(ctx.model.resource as SQLResource).setDebug(true); // 开启 debug 模式,sql 查询不要走 runById
|
||||
}
|
||||
|
||||
await ctx.model.runQueryAndUpdateResult(query);
|
||||
|
||||
// 运行查询完,直接展开结果面板
|
||||
setShowResult(true);
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
|
||||
+19
-4
@@ -25,7 +25,6 @@ export class ChartResource<TData = any> extends BaseRecordResource<TData> {
|
||||
|
||||
// 整体数据查询参数,内部 QueryBuilder 调用
|
||||
setQueryParams(query: Record<string, any>) {
|
||||
console.log('---ChartResource setQueryParams', query);
|
||||
const { success, message } = this.validateQuery(query);
|
||||
if (!success) {
|
||||
// 这里过程性校验 不强制报错,只做提示
|
||||
@@ -132,10 +131,26 @@ export class ChartResource<TData = any> extends BaseRecordResource<TData> {
|
||||
// 查询数据
|
||||
async run() {
|
||||
const data = this.request.data || {};
|
||||
if (!data.collection || !data.measures?.length) {
|
||||
throw new Error('collection and measures are required');
|
||||
// 尝试从已有字段推断模式;但若无法推断则直接跳过,避免切换时抛错
|
||||
const mode: 'sql' | 'builder' | undefined = data.mode ?? (data.sql ? 'sql' : undefined);
|
||||
|
||||
if (!mode) {
|
||||
// 未配置模式时,认为尚未完成查询参数设置:不抛错、不请求 API,返回现有数据
|
||||
return { data: this.getData(), meta: this.getMeta?.() };
|
||||
}
|
||||
// 请求数据 api.post('chart:query')
|
||||
|
||||
if (mode === 'sql') {
|
||||
if (!data.sql) {
|
||||
throw new Error('sql is required');
|
||||
}
|
||||
} else {
|
||||
// builder 模式
|
||||
if (!data.collection || !data.measures?.length) {
|
||||
throw new Error('collection and measures are required');
|
||||
}
|
||||
}
|
||||
|
||||
// 请求数据 api.post('charts:query')
|
||||
return await this.runAction<TData, any>('query', this.getRefreshRequestOptions());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user