新增redirectTo

This commit is contained in:
Wang Kejun 2022-07-04 18:54:19 +08:00
parent 03678862d5
commit 5ce89336a1
4 changed files with 117 additions and 40 deletions

View File

@ -117,7 +117,7 @@ export function makeCommonComponentMethods<
>['formData'] >['formData']
): Omit< ): Omit<
OakCommonComponentMethods<ED, T>, OakCommonComponentMethods<ED, T>,
'navigateTo' | 'navigateBack' | 'resolveInput' 'navigateTo' | 'navigateBack' | 'resolveInput' | 'redirectTo'
> & > &
ComponentThisType<ED, T, FormedData, IsList, TData, TProperty, TMethod> { ComponentThisType<ED, T, FormedData, IsList, TData, TProperty, TMethod> {
return { return {
@ -260,8 +260,14 @@ export function makeCommonComponentMethods<
} }
}, },
toggleNode(nodeData: Record<string, any>, checked: boolean, path?: string) { toggleNode(
const fullpath = path ? `${this.state.oakFullpath}.${path}` : this.state.oakFullpath; nodeData: Record<string, any>,
checked: boolean,
path?: string
) {
const fullpath = path
? `${this.state.oakFullpath}.${path}`
: this.state.oakFullpath;
features.runningTree.toggleNode(fullpath, nodeData, checked); features.runningTree.toggleNode(fullpath, nodeData, checked);
}, },

View File

@ -49,7 +49,7 @@ function makeCommonComponentMethods<
): OakCommonComponentMethods<ED, T> & ): OakCommonComponentMethods<ED, T> &
ComponentThisType<ED, T, FormedData, IsList, TData, TProperty, TMethod> { ComponentThisType<ED, T, FormedData, IsList, TData, TProperty, TMethod> {
return { return {
resolveInput: (input: WechatMiniprogram.CustomEvent, keys) => { resolveInput(input: WechatMiniprogram.CustomEvent, keys) {
const { currentTarget, detail } = input; const { currentTarget, detail } = input;
const { dataset } = currentTarget; const { dataset } = currentTarget;
const { value } = detail; const { value } = detail;
@ -66,19 +66,19 @@ function makeCommonComponentMethods<
} }
return result; return result;
}, },
navigateBack: (option) => { navigateBack(option) {
return new Promise( return new Promise((resolve, reject) => {
(resolve, reject) => { wx.navigateBack(
wx.navigateBack(assign({}, option, { assign({}, option, {
success() { success() {
resolve(undefined); resolve(undefined);
}, },
fail(err: any) { fail(err: any) {
reject(err); reject(err);
} },
})); })
} );
); });
}, },
navigateTo(options) { navigateTo(options) {
const { url, events, fail, complete, success, ...rest } = options; const { url, events, fail, complete, success, ...rest } = options;
@ -97,20 +97,52 @@ function makeCommonComponentMethods<
assign(options, { assign(options, {
url: url2, url: url2,
}); });
return new Promise( return new Promise((resolve, reject) => {
(resolve, reject) => { wx.navigateTo(
wx.navigateTo(assign({}, options, { assign({}, options, {
success(res: any) { success(res: any) {
success && success (res); success && success(res);
resolve(undefined); resolve(undefined);
}, },
fail(err: any) { fail(err: any) {
fail && fail(err); fail && fail(err);
reject(err); reject(err);
} },
})) })
} );
); });
},
redirectTo(options) {
const { url, events, fail, complete, success, ...rest } = options;
let url2 = url.includes('?')
? url.concat(`&oakFrom=${this.state.oakFullpath}`)
: url.concat(`?oakFrom=${this.state.oakFullpath}`);
for (const param in rest) {
const param2 = param as unknown as keyof typeof rest;
url2 += `&${param}=${
typeof rest[param2] === 'string'
? rest[param2]
: JSON.stringify(rest[param2])
}`;
}
assign(options, {
url: url2,
});
return new Promise((resolve, reject) => {
wx.redirectTo(
assign({}, options, {
success(res: any) {
success && success(res);
resolve(undefined);
},
fail(err: any) {
fail && fail(err);
reject(err);
},
})
);
});
}, },
...makeCommon(features, exceptionRouterDict, formData), ...makeCommon(features, exceptionRouterDict, formData),

View File

@ -57,7 +57,7 @@ function makeCommonComponentMethods<
): OakCommonComponentMethods<ED, T> & ): OakCommonComponentMethods<ED, T> &
ComponentThisType<ED, T, FormedData, IsList, TData, TProperty, TMethod> { ComponentThisType<ED, T, FormedData, IsList, TData, TProperty, TMethod> {
return { return {
resolveInput: (input: React.BaseSyntheticEvent, keys) => { resolveInput(input: React.BaseSyntheticEvent, keys) {
const { currentTarget, target } = input; const { currentTarget, target } = input;
const { value, dataset } = target; const { value, dataset } = target;
const result = { const result = {
@ -73,9 +73,20 @@ function makeCommonComponentMethods<
} }
return result; return result;
}, },
navigateBack: (option) => wx.navigateBack(option), navigateBack(option) {
const { delta } = option || {};
return new Promise((resolve, reject) => {
try {
this.props.navigateBack(delta || -1);
resolve(undefined);
} catch (err) {
reject(err);
}
});
},
navigateTo(options) { navigateTo(options) {
const { url, events, fail, complete, success, ...rest } = options; const { url, events, fail, complete, success, state, ...rest } =
options;
let url2 = url.includes('?') let url2 = url.includes('?')
? url.concat(`&oakFrom=${this.state.oakFullpath}`) ? url.concat(`&oakFrom=${this.state.oakFullpath}`)
: url.concat(`?oakFrom=${this.state.oakFullpath}`); : url.concat(`?oakFrom=${this.state.oakFullpath}`);
@ -88,10 +99,24 @@ function makeCommonComponentMethods<
: JSON.stringify(rest[param2]) : JSON.stringify(rest[param2])
}`; }`;
} }
assign(options, { return this.props.navigate(url2, { replace: false, state });
url: url2, },
}); redirectTo(options) {
return wx.navigateTo(options); const { url, events, fail, complete, success, state, ...rest } =
options;
let url2 = url.includes('?')
? url.concat(`&oakFrom=${this.state.oakFullpath}`)
: url.concat(`?oakFrom=${this.state.oakFullpath}`);
for (const param in rest) {
const param2 = param as unknown as keyof typeof rest;
url2 += `&${param}=${
typeof rest[param2] === 'string'
? rest[param2]
: JSON.stringify(rest[param2])
}`;
}
return this.props.navigate(url2, { replace: true, state });
}, },
...makeCommon(features, exceptionRouterDict, formData), ...makeCommon(features, exceptionRouterDict, formData),
@ -241,11 +266,8 @@ export function createPage<
}); });
} }
if (methods) { if (methods) {
const { const { onPullDownRefresh, onReachBottom, ...restMethods } =
onPullDownRefresh, methods;
onReachBottom,
...restMethods
} = methods;
for (const m in restMethods) { for (const m in restMethods) {
assign(this, { assign(this, {
[m]: restMethods[m as keyof typeof restMethods]!.bind( [m]: restMethods[m as keyof typeof restMethods]!.bind(
@ -294,14 +316,15 @@ export function createPage<
this.isReachBottom = isCurrentReachBottom; this.isReachBottom = isCurrentReachBottom;
} }
componentDidMount() { async componentDidMount() {
methods?.onLoad && methods.onLoad.call(this, this.props); await onLoad.call(this, this.props);
methods?.onReady && methods.onReady.call(this); methods?.onLoad && await methods.onLoad.call(this, this.props);
lifetimes?.ready && lifetimes.ready.call(this); methods?.onReady && await methods.onReady.call(this);
pageLifetimes?.show && pageLifetimes.show.call(this); lifetimes?.ready && await lifetimes.ready.call(this);
pageLifetimes?.show && await pageLifetimes.show.call(this);
} }
componentWillUnmount() { async componentWillUnmount() {
hiddenMethods.unsubscribe.call(this); hiddenMethods.unsubscribe.call(this);
methods?.onUnload && methods.onUnload.call(this); methods?.onUnload && methods.onUnload.call(this);
lifetimes?.detached && lifetimes.detached.call(this); lifetimes?.detached && lifetimes.detached.call(this);
@ -446,12 +469,26 @@ export function createComponent<
features = features; features = features;
isReachBottom = false; isReachBottom = false;
componentDidMount() { async componentDidMount() {
const { oakPath, oakParent } = this.props;
if (oakParent && oakPath) {
const oakFullpath = `${oakParent}.${oakPath}`;
this.setState(
{
oakFullpath,
oakEntity: entity as any,
},
() => {
commonMethods.reRender.call(this);
}
);
}
hiddenMethods.subscribe.call(this);
lifetimes?.ready && lifetimes.ready.call(this); lifetimes?.ready && lifetimes.ready.call(this);
pageLifetimes?.show && pageLifetimes.show.call(this); pageLifetimes?.show && pageLifetimes.show.call(this);
} }
componentWillUnmount() { async componentWillUnmount() {
hiddenMethods.unsubscribe.call(this); hiddenMethods.unsubscribe.call(this);
lifetimes?.detached && lifetimes.detached.call(this); lifetimes?.detached && lifetimes.detached.call(this);
} }

View File

@ -212,6 +212,7 @@ export type OakCommonComponentMethods<ED extends EntityDict, T extends keyof ED>
[k in K]?: any; [k in K]?: any;
}; };
reRender: (extra?: Record<string, any>) => Promise<void>; reRender: (extra?: Record<string, any>) => Promise<void>;
redirectTo: <T2 extends keyof ED>(options: Parameters<typeof wx.redirectTo>[0] & OakNavigateToParameters<ED, T2>) => Promise<void>
navigateTo: <T2 extends keyof ED>(options: Parameters<typeof wx.navigateTo>[0] & OakNavigateToParameters<ED, T2>) => Promise<void>; navigateTo: <T2 extends keyof ED>(options: Parameters<typeof wx.navigateTo>[0] & OakNavigateToParameters<ED, T2>) => Promise<void>;
navigateBack: (option?: { delta: number }) => Promise<void>; navigateBack: (option?: { delta: number }) => Promise<void>;
resetUpdateData: () => void; resetUpdateData: () => void;
@ -287,6 +288,7 @@ export type OakPageData<ED extends EntityDict, T extends keyof ED> = {
oakLegalActions: ED[T]['Action'][]; oakLegalActions: ED[T]['Action'][];
oakLoading: boolean; oakLoading: boolean;
oakLoadingMore: boolean; oakLoadingMore: boolean;
oakEntity: string;
}; };
export type OakComponentData<ED extends EntityDict, T extends keyof ED> = { export type OakComponentData<ED extends EntityDict, T extends keyof ED> = {