extrafilecommit 适配小程序传函数
This commit is contained in:
parent
a44267d281
commit
1477f417a9
|
|
@ -4,6 +4,8 @@ import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
|
|||
import { ReactComponentProps } from 'oak-frontend-base/lib/types/Page';
|
||||
import { ButtonProps } from 'antd';
|
||||
import { ButtonProps as AmButtonProps } from 'antd-mobile';
|
||||
type AfterCommit = (() => void) | undefined;
|
||||
type BeforeCommit = (() => boolean | undefined | Promise<boolean | undefined>) | undefined;
|
||||
declare const _default: <ED2 extends EntityDict & BaseEntityDict, T2 extends keyof ED2>(props: ReactComponentProps<ED2, T2, true, {
|
||||
action?: string | undefined;
|
||||
size?: ButtonProps['size'] | AmButtonProps['size'];
|
||||
|
|
@ -28,7 +30,11 @@ declare const _default: <ED2 extends EntityDict & BaseEntityDict, T2 extends key
|
|||
style?: (import("react").CSSProperties & Partial<Record<"--text-color" | "--background-color" | "--border-radius" | "--border-width" | "--border-style" | "--border-color", string>>) | undefined;
|
||||
tabIndex?: number | undefined;
|
||||
} & import("react").AriaAttributes) | undefined;
|
||||
afterCommit?: (() => any) | undefined;
|
||||
beforeCommit?: (() => boolean | undefined | Promise<boolean | undefined>) | undefined;
|
||||
afterCommit?: AfterCommit;
|
||||
beforeCommit?: BeforeCommit;
|
||||
fnSetMp?: {
|
||||
afterCommit?: AfterCommit;
|
||||
beforeCommit?: BeforeCommit;
|
||||
} | undefined;
|
||||
}>) => React.ReactElement;
|
||||
export default _default;
|
||||
|
|
|
|||
|
|
@ -25,8 +25,12 @@ export default OakComponent({
|
|||
type: 'primary',
|
||||
executeText: '',
|
||||
buttonProps: {},
|
||||
afterCommit: () => { },
|
||||
beforeCommit: (() => true),
|
||||
afterCommit: undefined,
|
||||
beforeCommit: undefined,
|
||||
fnSet: {
|
||||
afterCommit: undefined,
|
||||
beforeCommit: undefined,
|
||||
}, //小程序传递函数 需要以对象形式传入组件
|
||||
},
|
||||
data: {
|
||||
failureIds: undefined,
|
||||
|
|
@ -103,41 +107,52 @@ export default OakComponent({
|
|||
});
|
||||
if (promises.length > 0) {
|
||||
await Promise.all(promises);
|
||||
if (failureIds.length > 0) {
|
||||
this.setState({
|
||||
failureIds,
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.setState({
|
||||
failureIds: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
return failureIds;
|
||||
},
|
||||
async onSubmit() {
|
||||
async onSubmit(e) {
|
||||
const { oakExecutable } = this.state;
|
||||
const { beforeCommit, afterCommit, action } = this.props;
|
||||
const { beforeCommit, afterCommit, action, fnSet } = this.props;
|
||||
const ids = this.getEfIds();
|
||||
const beforeCommit2 = fnSet?.beforeCommit || beforeCommit;
|
||||
const afterCommit2 = fnSet?.afterCommit || afterCommit;
|
||||
if (oakExecutable) {
|
||||
if (beforeCommit) {
|
||||
const beforeCommitResult = await beforeCommit();
|
||||
if (typeof beforeCommit2 === 'function') {
|
||||
const beforeCommitResult = await beforeCommit2();
|
||||
if (beforeCommitResult === false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
await this.execute(action || undefined);
|
||||
await this.upload(ids);
|
||||
if (afterCommit) {
|
||||
afterCommit();
|
||||
const failureIds = await this.upload(ids);
|
||||
if (failureIds && failureIds.length > 0) {
|
||||
this.setState({
|
||||
failureIds,
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.setState({
|
||||
failureIds: undefined,
|
||||
});
|
||||
if (typeof afterCommit2 === 'function') {
|
||||
afterCommit2();
|
||||
}
|
||||
}
|
||||
else {
|
||||
const { failureIds } = this.state;
|
||||
assert(failureIds && failureIds.length > 0);
|
||||
await this.upload(failureIds);
|
||||
if (afterCommit) {
|
||||
afterCommit();
|
||||
const failureIds2 = await this.upload(failureIds);
|
||||
if (failureIds2 && failureIds2.length > 0) {
|
||||
this.setState({
|
||||
failureIds: failureIds2,
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.setState({
|
||||
failureIds: undefined,
|
||||
});
|
||||
if (typeof afterCommit2 === 'function') {
|
||||
afterCommit2();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
<!-- index.wxml -->
|
||||
<l-button size="long" disabled="{{disabled}}" bind:lintap="onSubmit">
|
||||
{{ executeText || t('common:submit') }}
|
||||
<l-button size="long" disabled="{{oakExecuting}}" bind:lintap="onSubmit">
|
||||
{{ executeText || t('common::submit')}}
|
||||
</l-button>
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import { WebComponentProps } from 'oak-frontend-base';
|
||||
import { ButtonProps } from 'antd';
|
||||
import { WebComponentProps } from 'oak-frontend-base';
|
||||
import { EntityDict } from '../../../oak-app-domain';
|
||||
import { FileState } from '../../../features/extraFile';
|
||||
export default function render(props: WebComponentProps<EntityDict, any, true, {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,15 @@ export default OakComponent({
|
|||
}
|
||||
return proj;
|
||||
},
|
||||
filters: [
|
||||
{
|
||||
filter() {
|
||||
const { entityFilter } = this.props;
|
||||
assert(entityFilter);
|
||||
return entityFilter;
|
||||
},
|
||||
}
|
||||
],
|
||||
properties: {
|
||||
disabled: false,
|
||||
entity: '',
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ export declare class ExtraFile<ED extends EntityDict, Cxt extends BackendRuntime
|
|||
addLocalFile(id: string, file: File | string): void;
|
||||
removeLocalFiles(ids: string[]): void;
|
||||
upload(id: string): Promise<void>;
|
||||
uploadCommit(efPaths: string[], oakFullpath: string): Promise<void>;
|
||||
getUrl(extraFile?: EntityDict['extraFile']['OpSchema'] | EntityDict['extraFile']['Schema'] | null, style?: string): string;
|
||||
getFileState(id: string): {
|
||||
state: FileState;
|
||||
|
|
|
|||
|
|
@ -70,36 +70,7 @@ export class ExtraFile extends Feature {
|
|||
item.state = 'failed';
|
||||
item.percentage = undefined;
|
||||
this.publish();
|
||||
}
|
||||
}
|
||||
async uploadCommit(efPaths, oakFullpath) {
|
||||
assert(false, '方法已经废弃');
|
||||
assert(efPaths && efPaths.length > 0);
|
||||
let ids = [];
|
||||
if (oakFullpath) {
|
||||
ids = efPaths
|
||||
.map((path) => {
|
||||
const path2 = path ? `${oakFullpath}.${path}` : oakFullpath;
|
||||
const data = this.runningTree.getFreshValue(path2);
|
||||
assert(data, `efPath为${path}的路径上取不到extraFile数据,请设置正确的相对路径`);
|
||||
return data.map((ele) => ele.id);
|
||||
})
|
||||
.flat()
|
||||
.filter((ele) => !!ele);
|
||||
}
|
||||
assert(ids.length > 0);
|
||||
const promises = [];
|
||||
ids.forEach((id) => {
|
||||
const fileState = this.getFileState(id);
|
||||
if (fileState) {
|
||||
const { state } = fileState;
|
||||
if (['local', 'failed'].includes(state)) {
|
||||
promises.push(this.upload(id));
|
||||
}
|
||||
}
|
||||
});
|
||||
if (promises.length > 0) {
|
||||
await Promise.all(promises);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
getUrl(extraFile, style) {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ export declare class ExtraFile<ED extends EntityDict, Cxt extends BackendRuntime
|
|||
addLocalFile(id: string, file: File | string): void;
|
||||
removeLocalFiles(ids: string[]): void;
|
||||
upload(id: string): Promise<void>;
|
||||
uploadCommit(efPaths: string[], oakFullpath: string): Promise<void>;
|
||||
getUrl(extraFile?: EntityDict['extraFile']['OpSchema'] | EntityDict['extraFile']['Schema'] | null, style?: string): string;
|
||||
getFileState(id: string): {
|
||||
state: FileState;
|
||||
|
|
|
|||
|
|
@ -73,36 +73,7 @@ class ExtraFile extends oak_frontend_base_1.Feature {
|
|||
item.state = 'failed';
|
||||
item.percentage = undefined;
|
||||
this.publish();
|
||||
}
|
||||
}
|
||||
async uploadCommit(efPaths, oakFullpath) {
|
||||
(0, assert_1.assert)(false, '方法已经废弃');
|
||||
(0, assert_1.assert)(efPaths && efPaths.length > 0);
|
||||
let ids = [];
|
||||
if (oakFullpath) {
|
||||
ids = efPaths
|
||||
.map((path) => {
|
||||
const path2 = path ? `${oakFullpath}.${path}` : oakFullpath;
|
||||
const data = this.runningTree.getFreshValue(path2);
|
||||
(0, assert_1.assert)(data, `efPath为${path}的路径上取不到extraFile数据,请设置正确的相对路径`);
|
||||
return data.map((ele) => ele.id);
|
||||
})
|
||||
.flat()
|
||||
.filter((ele) => !!ele);
|
||||
}
|
||||
(0, assert_1.assert)(ids.length > 0);
|
||||
const promises = [];
|
||||
ids.forEach((id) => {
|
||||
const fileState = this.getFileState(id);
|
||||
if (fileState) {
|
||||
const { state } = fileState;
|
||||
if (['local', 'failed'].includes(state)) {
|
||||
promises.push(this.upload(id));
|
||||
}
|
||||
}
|
||||
});
|
||||
if (promises.length > 0) {
|
||||
await Promise.all(promises);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
getUrl(extraFile, style) {
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@
|
|||
"make:locale": "ts-node ./scripts/buildLocale.ts",
|
||||
"make:domain": "ts-node ./scripts/make.ts",
|
||||
"clean": "rimraf lib/* && rimraf es/*",
|
||||
"copy-js": "copyfiles -u 1 src/**/*.js lib/ & copyfiles -u 1 src/**/*.js es/",
|
||||
"copy-js": "copyfiles -u 1 src/**/*.js es/",
|
||||
"copy-svg": "copyfiles -u 1 src/**/*.svg es/ & copyfiles -u 1 src/**/*.png es/",
|
||||
"copy-less": "copyfiles -u 1 src/**/*.less es/ & copyfiles -u 1 src/**/*.wxss es/ & copyfiles -u 1 src/**/*.css es/",
|
||||
"copy-wxs": "copyfiles -u 1 src/**/*.wxs es/",
|
||||
|
|
|
|||
|
|
@ -6,6 +6,11 @@ import { ReactComponentProps } from 'oak-frontend-base/lib/types/Page';
|
|||
import { ButtonProps } from 'antd';
|
||||
import { ButtonProps as AmButtonProps } from 'antd-mobile';
|
||||
|
||||
type AfterCommit = (() => void) | undefined;
|
||||
type BeforeCommit =
|
||||
| (() => boolean | undefined | Promise<boolean | undefined>)
|
||||
| undefined;
|
||||
|
||||
export default OakComponent({
|
||||
formData({ features }) {
|
||||
const ids: string[] = this.getEfIds();
|
||||
|
|
@ -31,11 +36,17 @@ export default OakComponent({
|
|||
type: 'primary',
|
||||
executeText: '',
|
||||
buttonProps: {},
|
||||
afterCommit: () => {},
|
||||
beforeCommit: (() => true) as () =>
|
||||
| boolean
|
||||
| undefined
|
||||
| Promise<boolean | undefined>,
|
||||
afterCommit: undefined as AfterCommit,
|
||||
beforeCommit: undefined as BeforeCommit,
|
||||
fnSetMp: {
|
||||
afterCommit: undefined,
|
||||
beforeCommit: undefined,
|
||||
} as
|
||||
| {
|
||||
afterCommit?: AfterCommit;
|
||||
beforeCommit?: BeforeCommit;
|
||||
}
|
||||
| undefined, //小程序传递函数 需要以对象形式传入组件
|
||||
},
|
||||
data: {
|
||||
failureIds: undefined as string[] | undefined,
|
||||
|
|
@ -101,7 +112,7 @@ export default OakComponent({
|
|||
}
|
||||
|
||||
const promises: Promise<void>[] = [];
|
||||
const failureIds = [] as string[];
|
||||
const failureIds = [] as string[];
|
||||
ids.forEach((id) => {
|
||||
const fileState = this.features.extraFile.getFileState(id);
|
||||
if (fileState) {
|
||||
|
|
@ -111,8 +122,7 @@ export default OakComponent({
|
|||
(async () => {
|
||||
try {
|
||||
await this.features.extraFile.upload(id);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
failureIds.push(id);
|
||||
}
|
||||
})()
|
||||
|
|
@ -121,44 +131,56 @@ export default OakComponent({
|
|||
}
|
||||
});
|
||||
|
||||
if (promises.length > 0) {
|
||||
if (promises.length > 0) {
|
||||
await Promise.all(promises);
|
||||
if (failureIds.length > 0) {
|
||||
this.setState({
|
||||
failureIds,
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.setState({
|
||||
failureIds: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
return failureIds;
|
||||
},
|
||||
async onSubmit() {
|
||||
async onSubmit(e: any) {
|
||||
const { oakExecutable } = this.state;
|
||||
const { beforeCommit, afterCommit, action } = this.props;
|
||||
const { beforeCommit, afterCommit, action, fnSet } = this.props;
|
||||
const ids = this.getEfIds();
|
||||
|
||||
const beforeCommit2 = fnSet?.beforeCommit || beforeCommit;
|
||||
const afterCommit2 = fnSet?.afterCommit || afterCommit;
|
||||
|
||||
if (oakExecutable) {
|
||||
if (beforeCommit) {
|
||||
const beforeCommitResult = await beforeCommit();
|
||||
if (typeof beforeCommit2 === 'function') {
|
||||
const beforeCommitResult = await beforeCommit2();
|
||||
if (beforeCommitResult === false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await this.execute(action || undefined);
|
||||
await this.upload(ids);
|
||||
if (afterCommit) {
|
||||
afterCommit();
|
||||
const failureIds = await this.upload(ids);
|
||||
if (failureIds && failureIds.length > 0) {
|
||||
this.setState({
|
||||
failureIds,
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.setState({
|
||||
failureIds: undefined,
|
||||
});
|
||||
if (typeof afterCommit2 === 'function') {
|
||||
afterCommit2();
|
||||
}
|
||||
} else {
|
||||
const { failureIds } = this.state;
|
||||
assert(failureIds && failureIds.length > 0);
|
||||
await this.upload(failureIds);
|
||||
if (afterCommit) {
|
||||
afterCommit();
|
||||
const failureIds2 = await this.upload(failureIds);
|
||||
if (failureIds2 && failureIds2.length > 0) {
|
||||
this.setState({
|
||||
failureIds: failureIds2,
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.setState({
|
||||
failureIds: undefined,
|
||||
});
|
||||
if (typeof afterCommit2 === 'function') {
|
||||
afterCommit2();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
@ -176,11 +198,13 @@ export default OakComponent({
|
|||
type?: ButtonProps['type'] | AmButtonProps['type'];
|
||||
executeText?: string;
|
||||
buttonProps?: ButtonProps & AmButtonProps;
|
||||
afterCommit?: () => any;
|
||||
beforeCommit?: () =>
|
||||
| boolean
|
||||
| undefined
|
||||
| Promise<boolean | undefined>;
|
||||
afterCommit?: AfterCommit;
|
||||
beforeCommit?: BeforeCommit;
|
||||
fnSetMp?: {
|
||||
//小程序传递函数 需要以对象形式传入组件
|
||||
afterCommit?: AfterCommit;
|
||||
beforeCommit?: BeforeCommit;
|
||||
};
|
||||
}
|
||||
>
|
||||
) => React.ReactElement;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<!-- index.wxml -->
|
||||
<l-button size="long" disabled="{{disabled}}" bind:lintap="onSubmit">
|
||||
{{ executeText || t('common:submit') }}
|
||||
<l-button size="long" disabled="{{oakExecuting}}" bind:lintap="onSubmit">
|
||||
{{ executeText || t('common::submit')}}
|
||||
</l-button>
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import { WebComponentProps } from 'oak-frontend-base';
|
||||
import React from 'react';
|
||||
import { Button, ButtonProps } from 'antd';
|
||||
import { WebComponentProps } from 'oak-frontend-base';
|
||||
import { EntityDict } from '../../../oak-app-domain';
|
||||
import { FileState } from '../../../features/extraFile';
|
||||
|
||||
|
|
|
|||
|
|
@ -109,44 +109,7 @@ export class ExtraFile<
|
|||
item.state = 'failed';
|
||||
item.percentage = undefined;
|
||||
this.publish();
|
||||
}
|
||||
}
|
||||
|
||||
async uploadCommit(efPaths: string[], oakFullpath: string) {
|
||||
assert(false, '方法已经废弃');
|
||||
assert(efPaths && efPaths.length > 0);
|
||||
let ids = [] as string[];
|
||||
if (oakFullpath) {
|
||||
ids = efPaths
|
||||
.map((path) => {
|
||||
const path2 = path ? `${oakFullpath}.${path}` : oakFullpath;
|
||||
const data = this.runningTree.getFreshValue(path2);
|
||||
assert(
|
||||
data,
|
||||
`efPath为${path}的路径上取不到extraFile数据,请设置正确的相对路径`
|
||||
);
|
||||
return (
|
||||
data as Partial<EntityDict['extraFile']['OpSchema']>[]
|
||||
).map((ele) => ele.id);
|
||||
})
|
||||
.flat()
|
||||
.filter((ele) => !!ele) as string[];
|
||||
}
|
||||
assert(ids.length > 0);
|
||||
|
||||
const promises: Promise<void>[] = [];
|
||||
ids.forEach((id) => {
|
||||
const fileState = this.getFileState(id);
|
||||
if (fileState) {
|
||||
const { state } = fileState;
|
||||
if (['local', 'failed'].includes(state)) {
|
||||
promises.push(this.upload(id));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (promises.length > 0) {
|
||||
await Promise.all(promises);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue