From 3566bb73e59fc9cd3638f5bbad5dafccaf46b759 Mon Sep 17 00:00:00 2001 From: CJJJune <1011104553@qq.com> Date: Thu, 3 Mar 2022 15:31:10 +0800 Subject: [PATCH 01/14] commit --- .idea/.gitignore | 3 + .idea/compiler.xml | 6 + .idea/gradle.xml | 22 ++ .idea/jarRepositories.xml | 25 +++ .idea/misc.xml | 4 + .idea/vcs.xml | 6 + .../src/main/ets/MainAbility/api/arouter.d.ts | 60 +++++ .../MainAbility/api/interceptorService.d.ts | 109 +++++++++ .../ets/MainAbility/api/logisticsCenter.d.ts | 20 ++ .../MainAbility/api/navigationCallback.d.ts | 39 ++++ .../main/ets/MainAbility/api/postcard.d.ts | 159 +++++++++++++ .../MainAbility/api/pretreatmenService.d.ts | 26 +++ entry/src/main/ets/MainAbility/app.ets | 9 + .../main/ets/MainAbility/arouter/arouter.ets | 76 +++++++ .../arouter/interceptorService.ets | 84 +++++++ .../MainAbility/arouter/logisticsCeter.ets | 19 ++ .../arouter/navigationCallback.ets | 22 ++ .../main/ets/MainAbility/arouter/postcard.ets | 129 +++++++++++ .../arouter/pretreatmenService.ets | 20 ++ .../src/main/ets/MainAbility/pages/index.ets | 211 ++++++++++++++++++ .../main/ets/MainAbility/pages/returnTo.ets | 94 ++++++++ .../main/ets/MainAbility/pages/transit.ets | 118 ++++++++++ 22 files changed, 1261 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/compiler.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 entry/src/main/ets/MainAbility/api/arouter.d.ts create mode 100644 entry/src/main/ets/MainAbility/api/interceptorService.d.ts create mode 100644 entry/src/main/ets/MainAbility/api/logisticsCenter.d.ts create mode 100644 entry/src/main/ets/MainAbility/api/navigationCallback.d.ts create mode 100644 entry/src/main/ets/MainAbility/api/postcard.d.ts create mode 100644 entry/src/main/ets/MainAbility/api/pretreatmenService.d.ts create mode 100644 entry/src/main/ets/MainAbility/app.ets create mode 100644 entry/src/main/ets/MainAbility/arouter/arouter.ets create mode 100644 entry/src/main/ets/MainAbility/arouter/interceptorService.ets create mode 100644 entry/src/main/ets/MainAbility/arouter/logisticsCeter.ets create mode 100644 entry/src/main/ets/MainAbility/arouter/navigationCallback.ets create mode 100644 entry/src/main/ets/MainAbility/arouter/postcard.ets create mode 100644 entry/src/main/ets/MainAbility/arouter/pretreatmenService.ets create mode 100644 entry/src/main/ets/MainAbility/pages/index.ets create mode 100644 entry/src/main/ets/MainAbility/pages/returnTo.ets create mode 100644 entry/src/main/ets/MainAbility/pages/transit.ets diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..bc249b5 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..19e238e --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..58918f5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/api/arouter.d.ts b/entry/src/main/ets/MainAbility/api/arouter.d.ts new file mode 100644 index 0000000..bd763d1 --- /dev/null +++ b/entry/src/main/ets/MainAbility/api/arouter.d.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export declare class Arouter{ + /** + * Route Redirection Path + * @devices phone, tablet, car + * @since 1.0.0 + * @param uri Jump Path + * @returns Postcard + */ + build(uri:string):Postcard; + /** + * Create an Arouter class. + * @devices phone, tablet, car + * @since 1.0.0 + * @returns Arouter class + */ + getInstance():Arouter; + /** + * Import the interface to the postcard and import the postcard globally. + * @devices phone, tablet, car + * @since 1.0.0 + * @param Postcard class, NavigationCallback interface + */ + navigation(postcard:Postcard,callback:NavigationCallback) + /** + * Add Routes to Array + * @devices phone, tablet,car + * @since 1.0.0 + * @returns null + */ + getPostcard(path:string):Postcard + /** + * The system determines that routes is empty and generates a new array + * @devices phone, tablet, car + * @since 1.0.0 + * @param postcard + * @param callback + */ + inject(postcard:Postcard) + /** + * Route redirection + * @devices phone, tablet, car + * @since 1.0.0 + */ + _navigation(postcard:Postcard) +} diff --git a/entry/src/main/ets/MainAbility/api/interceptorService.d.ts b/entry/src/main/ets/MainAbility/api/interceptorService.d.ts new file mode 100644 index 0000000..9fb5b8a --- /dev/null +++ b/entry/src/main/ets/MainAbility/api/interceptorService.d.ts @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /** + * Intercept Interface + */ +export declare interface IInterceptor{ + /** + * Route interception + * @devices phone, tablet, car + * @since 1.0.0 + * @params Postcard class + * InterceptorCallback Interface + */ + process(postcard:Postcard,callback:InterceptorCallback); +} + /** + * Interception Implementation Interface + */ +export declare interface InterceptorCallback{ + /** + * Continue to jump + * @devices phone, tablet, car + * @since 1.0.0 + * @param Postcard + */ + onContinue(postcard:Postcard); + /** + * Terminate Jump + * @devices phone, tablet, car + * @since 1.0.0 + * @param Postcard + */ + onInterrupt(postcard:Postcard); +} + /** + * Implement route redirection. + * @devices phone, tablet, car + * @since 1.0.0 + * @param Postcard + */ +export declare function navigationWithInterceptor(postcard:Postcard):void; + /** + * Registration route interception + * @devices phone, tablet, car + * @since 1.0.0 + * @param IInterceptor + */ +export declare function registerInterceptor(iInterceptor:IInterceptor) +/** + * Disabling Route Interception + * @devices phone, tablet, car + * @since 1.0.0 + */ +export declare function unregisterInterceptor() +class Interceptor{ + /** + * Constructor + * @devices phone, tablet, car + * @since 1.0.0 + * @param iInterceptor + */ + constructor(iInterceptor:IInterceptor){ + /** + * Interceptor callback pause + * @devices phone, tablet, car + * @since 1.0.0 + */ + onInterrupt(postcard:Postcard) + /** + * Interceptor callback continue + * @devices phone, tablet, car + * @since 1.0.0 + */ + onContinue(postcard:Postcard) + } + /** + * Set the interceptor. + * @devices phone, tablet, car + * @since 1.0.0 + * @param iInterceptor + */ + setIIterceptor(iInterceptor:IInterceptor) + /** + * Get the interceptor. + * @devices phone, tablet, car + * @since 1.0.0 + * @returns this iInterceptor + */ + getIInterceptor():IInterceptor + /** + * Get Callback + * @devices phone, tablet, car + * @since 1.0.0 + * @returns this callback + */ + getCallback():InterceptorCallback +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/api/logisticsCenter.d.ts b/entry/src/main/ets/MainAbility/api/logisticsCenter.d.ts new file mode 100644 index 0000000..bd4b9a6 --- /dev/null +++ b/entry/src/main/ets/MainAbility/api/logisticsCenter.d.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Intercept Interface + */ +export declare function clear(){ + +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/api/navigationCallback.d.ts b/entry/src/main/ets/MainAbility/api/navigationCallback.d.ts new file mode 100644 index 0000000..e0450cf --- /dev/null +++ b/entry/src/main/ets/MainAbility/api/navigationCallback.d.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export declare interface NavigationCallback{ + + /** + * Route redirection succeeds. + * @devices phone, tablet, car + * @since 1.0.0 + * @param Postcard + */ + onArrival(Postcard); + /** + * Route interception redirection + * @devices phone, tablet, car + * @since 1.0.0 + * @param Postcard + */ + onInterrupt(Postcard); + /** + * Route callback parameters + * @devices phone, tablet, car + * @since 1.0.0 + * @param Postcard + */ + onActivityResult(data); +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/api/postcard.d.ts b/entry/src/main/ets/MainAbility/api/postcard.d.ts new file mode 100644 index 0000000..b0b0f28 --- /dev/null +++ b/entry/src/main/ets/MainAbility/api/postcard.d.ts @@ -0,0 +1,159 @@ + /* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + /* + * Route Redirection Method class + */ +export declare class Postcard{ + /** + * Set the redirection path. + * @devices phone, tablet, car + * @since 1.0.0 + * @param uri Jump Path + */ + constructor(uri:string); + /** + * Status Interface + * @devices phone, tablet, car + * @since 1.0.0 + * @param Status Interface + */ + setNavigationCallback(callback:NavigationCallback):void; + /** + * Status Interface + * @devices phone, tablet, car + * @since 1.0.0 + * @returns Status Interface + */ + getNavigationCallbcak():NavigationCallback; + /** + * Set the redirection path. + * @devices phone, tablet, car + * @since 1.0.0 + * @param Jump Path + * @return Returns Routing Method + */ + setUri(uri:string):Postcard; + /** + * Route Transfer Parameter + * @devices phone, tablet, car + * @since 1.0.0 + * @param params Parameters to be passed on the next page object + * @return Returns Routing Method + */ + withParams(params:Object):Postcard; + /** + * Green Channel + * @devices phone, tablet, car + * @since 1.0.0 + * @param true Direct jump + * false Jump Interception + */ + setGreenChannel(greenChannel:boolean):void; + /** + * Green Channel + * @devices phone, tablet, car + * @since 1.0.0 + * @returns true Direct jump + * false Jump Interception + */ + getGreenChannel():boolean; + /** + * Route Transfer Parameter + * @devices phone, tablet, car + * @since 1.0.0 + * @return Routing parameters object + */ + getParams():Object; + /** + * Set the redirection path. + * @devices phone, tablet, car + * @since 1.0.0 + * return Route Redirection Path + */ + getUri():string; + /** + * Normal route redirection + * @devices phone, tablet, car + * @since 1.0.0 + */ + navigation():void; + /** + * Route redirection and status callback + * @devices phone, tablet, car + * @since 1.0.0 + * @param callback + */ + navigationWithCallback(callback:NavigationCallback):void; + /** + * Obtaining Tags + *@devices phone, tablet, car + * @since 1.0.0 + * @param tag + */ + getTag():Object; + /** + * Setting Labels + *@devices phone, tablet, car + * @since 1.0.0 + * @param flags + */ + setTage(tag:Object):Postcard; + /** + * Set flag + *@devices phone, tablet, car + * @since 1.0.0 + */ + withFlags(flags:boolean):Postcard; + /** + * Add flag + *@devices phone, tablet, car + * @since 1.0.0 + */ + getFlags():boolean; + /** + * Export String + * @devices phone, tablet, car + * @since 1.0.0 + */ + toString():string; + /** + * @devices phone, tablet, car + * @since 1.0.0 + * @param val + */ + routers() + /** + * Defines and determines whether the value is a non-negative integer. + * @devices phone, tablet, car + * @since 1.0.0 + */ + isIntNum(val) + /** + * Set the pretreatment servince + * @devices phone, tablet, car + * @since 1.0.0 + * @param pretreatmentService + * @returns this.pretreatmentService + */ + setPretreatmentService(pretreatmentService:PretreatmentService):Postcard + /** + * Get the pretreatment servince + * @devices phone, tablet, car + * @since 1.0.0 + * @returns this pretreatmentService + */ + getPretreatmentService() + +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/api/pretreatmenService.d.ts b/entry/src/main/ets/MainAbility/api/pretreatmenService.d.ts new file mode 100644 index 0000000..89cf354 --- /dev/null +++ b/entry/src/main/ets/MainAbility/api/pretreatmenService.d.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Intercept Interface + */ +export declare interface PretreatmentService{ + /** + * Check whether interception can be performed before redirection. + * @devices phone, tablet, car + * @since 1.0.0 + */ +onPretreatment(postcard:Postcard):boolean; + +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/app.ets b/entry/src/main/ets/MainAbility/app.ets new file mode 100644 index 0000000..79ac16a --- /dev/null +++ b/entry/src/main/ets/MainAbility/app.ets @@ -0,0 +1,9 @@ +export default { + + onCreate() { + console.info('Application onCreate') + }, + onDestroy() { + console.info('Application onDestroy') + }, +} diff --git a/entry/src/main/ets/MainAbility/arouter/arouter.ets b/entry/src/main/ets/MainAbility/arouter/arouter.ets new file mode 100644 index 0000000..5d44056 --- /dev/null +++ b/entry/src/main/ets/MainAbility/arouter/arouter.ets @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {Postcard} from './postcard'; +import {navigationWithInterceptor} from "./interceptorService" +import {PretreatmentService} from './pretreatmenService' +import {NavigationCallback} from './navigationCallback' +import router from '@system.router'; + + +export class Arouter{ + build(uri:string):Postcard{ + return new Postcard(uri, null); + + } + + private static sInstance:Arouter + public static getInstance():Arouter{ + if (!this.sInstance) { + this.sInstance=new Arouter() + } + return this.sInstance + } + + getPostcard(path:string):Postcard { + let routes = globalThis.exports.default.routes; + if(routes != null && routes.get(path) != null){ + return routes.get(path); + } + return null; + } + + inject(postcard:Postcard){ + if(globalThis.exports.default.routes == null){ + globalThis.exports.default.routes = new Map(); + } + globalThis.exports.default.routes.set(postcard.getUri(), postcard); + } + + navigation(postcard:Postcard,callback:NavigationCallback) { + postcard.setNavigationCallback(callback); + let pretreatmentService:PretreatmentService = postcard.getPretreatmentService(); + if(pretreatmentService != null && !pretreatmentService.onPretreatment(postcard)){ + return null; + } + navigationWithInterceptor(postcard) + } + + _navigation(postcard:Postcard){ + this.inject(postcard); + if(postcard.getFlags()){ + router.replace({ + uri:postcard.getUri(), + params:postcard.getParams() + }) + } else { + router.push({ + uri:postcard.getUri(), + params:postcard.getParams() + }) + } + } +} + diff --git a/entry/src/main/ets/MainAbility/arouter/interceptorService.ets b/entry/src/main/ets/MainAbility/arouter/interceptorService.ets new file mode 100644 index 0000000..5e73ab8 --- /dev/null +++ b/entry/src/main/ets/MainAbility/arouter/interceptorService.ets @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {Postcard} from './postcard'; +import {Arouter} from "./arouter"; + +export interface IInterceptor{ + process(postcard:Postcard,callback:InterceptorCallback); +} + +export interface InterceptorCallback{ + onContinue(postcard:Postcard); + onInterrupt(postcard:Postcard); +} + + +export function navigationWithInterceptor(postcard:Postcard){ + if(postcard.getGreenChannel()) { + Arouter.getInstance()._navigation(postcard) + } else { + var interceptor = globalThis.exports.default.interceptor; + if(interceptor != null) { + interceptor.getIInterceptor().process(postcard, interceptor.getCallback()) + } else { + Arouter.getInstance()._navigation(postcard) + } + } +} + +export function registerInterceptor(iInterceptor:IInterceptor){ + if(globalThis.exports.default.interceptor == null) { + globalThis.exports.default.interceptor = new Interceptor(iInterceptor) + } else { + globalThis.exports.default.interceptor.setIIterceptor(iInterceptor); + } +} + +export function unregisterInterceptor() { + globalThis.exports.default.interceptor = null +} + +class Interceptor{ + private callback:InterceptorCallback + private iInterceptor:IInterceptor + + constructor(iInterceptor:IInterceptor){ + this.iInterceptor = iInterceptor; + this.callback = { + onInterrupt(postcard:Postcard){ + if(postcard != null && postcard.getNavigationCallbcak() != null){ + postcard.getNavigationCallbcak().onInterrupt(postcard) + } + }, + onContinue(postcard:Postcard){ + Arouter.getInstance()._navigation(postcard) + } + }; + globalThis.exports.default.interceptor = this; + }; + + setIIterceptor(iInterceptor:IInterceptor) { + this.iInterceptor = iInterceptor; + } + + getIInterceptor():IInterceptor{ + return this.iInterceptor + } + + getCallback():InterceptorCallback{ + return this.callback; + } + +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/arouter/logisticsCeter.ets b/entry/src/main/ets/MainAbility/arouter/logisticsCeter.ets new file mode 100644 index 0000000..3d2b362 --- /dev/null +++ b/entry/src/main/ets/MainAbility/arouter/logisticsCeter.ets @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export function clear(){ + globalThis.exports.default.routes = null; + globalThis.exports.default.interceptor = null; +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/arouter/navigationCallback.ets b/entry/src/main/ets/MainAbility/arouter/navigationCallback.ets new file mode 100644 index 0000000..a83d69a --- /dev/null +++ b/entry/src/main/ets/MainAbility/arouter/navigationCallback.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Postcard } from './postcard.ets'; + +export interface NavigationCallback{ + onArrival(postcard:Postcard); + onInterrupt(postcard:Postcard); + onActivityResult(data); +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/arouter/postcard.ets b/entry/src/main/ets/MainAbility/arouter/postcard.ets new file mode 100644 index 0000000..bbfc76c --- /dev/null +++ b/entry/src/main/ets/MainAbility/arouter/postcard.ets @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {NavigationCallback} from './navigationCallback' +import {navigationWithInterceptor} from "./interceptorService" +import {PretreatmentService} from './pretreatmenService'; +import {Arouter} from "./arouter"; + +export class Postcard { + private uri:string + private params:Object + private navigationCallback:NavigationCallback + private pretreatmentService:PretreatmentService + private greenChannel; + private tag:Object; + private flags:boolean; + + constructor(uri:string, params:Object){ + this.uri = uri; + this.params = params; + } + + setPretreatmentService(pretreatmentService:PretreatmentService):Postcard{ + this.pretreatmentService = pretreatmentService; + return this; + } + + getPretreatmentService(){ + return this.pretreatmentService + } + + setNavigationCallback(callback:NavigationCallback) { + this.navigationCallback = callback; + } + + getNavigationCallbcak():NavigationCallback{ + return this.navigationCallback + } + + setUri(uri:string):Postcard{ + this.uri = uri; + return this; + } + + withParams(params:Object):Postcard{ + this.params = params; + return this; + } + + setGreenChannel(greenChannel:boolean):Postcard{ + this.greenChannel = greenChannel; + return this; + } + + getGreenChannel():boolean{ + return this.greenChannel + } + + getParams():Object{ + return this.params; + } + + getUri():string{ + return this.uri; + } + // 正常跳转 + navigation(){ + this.routers() + } + // 跳转回调 + navigationWithCallback(callback:NavigationCallback) { + this.setNavigationCallback(callback); + this.routers() + } + + getTag():Object{ + return this.tag; + } + + setTag(tag:Object):Postcard{ + this.tag = tag; + return this; + } + + withFlags(flags:boolean):Postcard { + this.flags = flags; + return this; + } + + getFlags():boolean { + return this.flags; + } + + toString():string { + return "Postcard{" + + "uri=" + this.uri + + ", tag=" + this.tag + + ", params=" + this.params.toString() + + ", flags=" + this.flags + + ", greenChannel=" + this.greenChannel + + "}\n"; + } + + private routers(){ + Arouter.getInstance().navigation(this, this.getNavigationCallbcak()) + } + + private isIntNum(val){ + var regPos = / ^\d+$/; // 非负整数 + var regNeg = /^\-[1-9][0-9]*$/; // 负整数 + if(regPos.test(val) && regNeg.test(val)){ + return true; + }else{ + return false; + } + } +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/arouter/pretreatmenService.ets b/entry/src/main/ets/MainAbility/arouter/pretreatmenService.ets new file mode 100644 index 0000000..a6f53e8 --- /dev/null +++ b/entry/src/main/ets/MainAbility/arouter/pretreatmenService.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Postcard } from './postcard.ets'; + +export interface PretreatmentService { + onPretreatment(postcard:Postcard):boolean; +} \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/pages/index.ets b/entry/src/main/ets/MainAbility/pages/index.ets new file mode 100644 index 0000000..f3b1fc1 --- /dev/null +++ b/entry/src/main/ets/MainAbility/pages/index.ets @@ -0,0 +1,211 @@ +import {Arouter} from "../arouter/arouter"; +import {Postcard} from '../arouter/postcard'; +import {registerInterceptor, InterceptorCallback,IInterceptor} from "../arouter/interceptorService"; +import {PretreatmentService} from '../arouter/pretreatmenService'; +import {NavigationCallback} from '../arouter/navigationCallback' + +var iInterceptor:IInterceptor = { + process(postcard:Postcard, callback:InterceptorCallback){ + if(postcard.getUri() === 'pages/transit' || postcard.getUri() === 'pages/returnTo'){ + AlertDialog.show( + { + title: '', + message: '被拦截了,点击继续跳转', + primaryButton: { + value: '取消', + action: () => { + callback.onInterrupt(postcard) + } + }, + secondaryButton: { + value: '继续', + action: () => { + callback.onContinue(postcard); + } + }, + cancel: () => { + + } + } + ) + } else { + callback.onContinue(postcard); + } + } +} + +var returnData = { + data:"" +} + + var callback:NavigationCallback = { + onInterrupt(postcard:Postcard){ + + }, + onArrival(postcard:Postcard){ + }, + onActivityResult(data){ + returnData.data = data.index + } +}; + +var service="true" +var pretreatmentService:PretreatmentService = { + onPretreatment(postcard:Postcard):boolean{ + switch(service){ + case "true": + return true + break + case "false": + return false + break + default: + return true + break + } + } +} + + + +@Entry +@Component +struct Index { + // 输入框中的参数 + @State value: string= "Route redirection" + // 回调返回的参数 + @State index: string= "" + @State service:string="true" + // 标题 + @State title: string= "Route redirection" + // 自定义跳转路径 + @State router: string= "pages/transit" + build() { + Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { + // 标题 + Text(this.title) + .fontSize(24) + .fontWeight(FontWeight.Bold) + // 正常无参跳转 + Button("正常跳转") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + Arouter.getInstance() + .build("pages/transit") + .setGreenChannel(true) + .navigation() + }) + + Row(){ + Text("参数:") + .fontSize(18) + Text("text: this.value") + .width(240) + .height(40) + .margin({left: '30px'}) + .border({width: "2",color: "#c2bfbf",radius: "15"}) + + } + Text("将文本框中的内容传至transit页面").fontSize(18) + // 跳转传参 + Button("跳转传参") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + Arouter.getInstance() + .build("pages/transit") + .withParams({index: this.value}) + .setGreenChannel(true) + .navigation() + }) + + Text(this.index) + .fontSize(24) + // 跳转并返回数据参数 + Button("跳转回调") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + Arouter.getInstance() + .build("pages/transit") + .setGreenChannel(true) + .navigationWithCallback(callback) + }) + // 跳转拦截 + Button("跳转拦截") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(() => { + Arouter.getInstance() + .build("pages/transit") + .withParams({index: "Route redirection"}) + .navigationWithCallback(callback) + }) + // 自定义跳转路径 + Row(){ + Text("自定义跳转:") + .fontSize(18) + Text("text: this.route") + .width(240) + .height(40) + .margin({left:'30px'}) + .border({width: "2",color: "#c2bfbf",radius: "15"}) + } + Text("pages/transit或pages/returnTo") + .fontSize(20) + + Button("自定义跳转") + .width("460px") + .height("200px") + .backgroundColor("#bb5959") + .margin(10) + .onClick(()=>{ + Arouter.getInstance() + .build(this.router) + .withParams({index: "Route redirection"}) + .setPretreatmentService(pretreatmentService) + .navigationWithCallback(callback) + }) + Row(){ + Text("预处理:") + .fontSize(18) + Text("text:this.service") + .width(240) + .height(40) + .margin({left: '30px'}) + .border({width: "2",color: "#c2bfbf",radius: "15"}) + } + Text("false不跳转,true继续跳转,其他默认为true,只对自定义跳转有效") + .fontSize(16).fontColor("red").margin({top:"10px"}) + .width("70%") + + } + .width('100%') + .height('100%') + } + // 页面显示时 + onPageShow(){ + this.index = returnData.data + } + + aboutToAppear(){ + registerInterceptor(iInterceptor) + } + + pageTransition() { + PageTransitionEnter({ duration: 600, curve: Curve.Linear }) + .slide(SlideEffect.Right) + PageTransitionExit({ duration: 600, curve: Curve.Linear }) + .slide(SlideEffect.Left) + } + +} diff --git a/entry/src/main/ets/MainAbility/pages/returnTo.ets b/entry/src/main/ets/MainAbility/pages/returnTo.ets new file mode 100644 index 0000000..9175693 --- /dev/null +++ b/entry/src/main/ets/MainAbility/pages/returnTo.ets @@ -0,0 +1,94 @@ +import router from '@system.router'; +import {registerInterceptor, IInterceptor, InterceptorCallback} from "../arouter/interceptorService"; +import {Postcard} from '../arouter/postcard'; +import {Arouter} from "../arouter/arouter"; + +var iInterceptor:IInterceptor = { + process(postcard:Postcard, callback:InterceptorCallback){ + if(postcard.getUri() === 'pages/transit' || postcard.getUri() === 'pages/returnTo'){ + AlertDialog.show( + { + title: '', + message: '被拦截了,点击继续跳转', + primaryButton: { + value: '取消', + action: () => { + callback.onInterrupt(postcard) + } + }, + secondaryButton: { + value: '继续', + action: () => { + callback.onContinue(postcard); + } + }, + cancel: () => { + console.info('Closed callbacks') + } + } + ) + } else { + callback.onContinue(postcard); + } + } +} + +@Entry +@Component +struct returnTo { + build() { + Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { + Text('returnTo') + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button("返回index") + .onClick(()=>{ + router.back({uri: "pages/index"}) + }) + Button("返回transit") + .margin({top: 10}) + .onClick(()=>{ + + router.push({uri: "pages/transit"}) + }) + Button("点击打开拦截器") + .margin({top: 10}) + .onClick(()=>{ + registerInterceptor(iInterceptor) + AlertDialog.show({ + message: '拦截器已打开', + confirm: { + value: '知道啦', + action: () => { + + } + }, + cancel: () => { + + } + }) + }) + } + + .width('100%') + .height('100%') + } + + onPageShow(){ + if (postcard == null) { + postcard = Arouter.getInstance().getPostcard(router.getState().path + router.getState().name); + if(postcard != null && postcard.getNavigationCallbcak() != null){ + postcard.getNavigationCallbcak().onArrival(postcard); + } + return postcard; + } + } + + pageTransition() { + PageTransitionEnter({ duration: 600, curve: Curve.Linear }) + .slide(SlideEffect.Right) + PageTransitionExit({ duration: 600, curve: Curve.Linear }) + .slide(SlideEffect.Left) + } +} +var postcard:Postcard \ No newline at end of file diff --git a/entry/src/main/ets/MainAbility/pages/transit.ets b/entry/src/main/ets/MainAbility/pages/transit.ets new file mode 100644 index 0000000..ac4b896 --- /dev/null +++ b/entry/src/main/ets/MainAbility/pages/transit.ets @@ -0,0 +1,118 @@ +import router from '@system.router'; +import {Arouter} from "../arouter/arouter"; +import {Postcard} from '../arouter/postcard'; +import {unregisterInterceptor} from "../arouter/interceptorService" +var callback = { + onInterrupt(postcard:Postcard){ + + }, + onArrival(postcard:Postcard){ + + }, + onActivityResult(data){ + + } +}; +@Entry +@Component +struct Transit { + // index页面传来的参数 + @State value: string= "Back to previous page" + + build() { + Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center}) { + Column(){ + Text(`index页面传来的参数:`) + .fontSize(24) + .width("100%") + .height(34) + + Divider().color("#e1d7d7").strokeWidth(2).width(300) + // index页面传来的参数 + Text(router.getParams()? router.getParams().index? router.getParams().index: "空": "空") + .fontColor("#7e7a7a") + .fontSize(24) + }.alignItems(HorizontalAlign.Start) + .width(300) + .height(200) + .border({width: "4px",color: "#c5bfbf",radius: 10}) + .margin("20px") + + Divider().color("#888585").strokeWidth(2) + + Text("将输入框中的内容回调至index页面") + .fontSize(28) + + Row(){ + Text("参数:").fontSize(18) + // 将输入框中的内容回调至index页面 + Text("text: this.value") + .width(240) + .height(40) + .margin({left:'30px'}) + .border({width: "2",color: "#c2bfbf",radius: "15"}) + } + Text("* 此按钮点击跳转回调才会有效果").fontSize(16).fontColor("red") + Button("返回上个页面并回调参数") + .width("460px") + .height("200px") + .margin({bottom:22}) + .onClick(() => { + if(postcard != null && postcard.getNavigationCallbcak() != null){ + postcard.getNavigationCallbcak() + .onActivityResult({index: this.value}) + } + router.back(); + }) + + Button("返回上个页面") + .width("460px") + .height("200px") + .margin(22) + .onClick(() => { + router.back(); + }) + + Button("关闭页面拦截") + .onClick(()=>{ + // 关闭页面拦截 + unregisterInterceptor() + AlertDialog.show({ + message: '拦截器已关闭', + confirm: { + value: '知道啦', + action: () => { + + } + }, + cancel: () => { + + } + }) + }) + Button("returnTo").onClick(() => { + Arouter.getInstance() + .build("pages/returnTo") + .navigationWithCallback(callback) + }) + } + .width('100%') + .height('100%') + } + onPageShow(){ + if (postcard == null) { + postcard = Arouter.getInstance().getPostcard(router.getState().path + router.getState().name); + if(postcard != null && postcard.getNavigationCallbcak() != null){ + postcard.getNavigationCallbcak().onArrival(postcard); + } + } + } + + pageTransition() { + PageTransitionEnter({ duration: 600, curve: Curve.Linear }) + .slide(SlideEffect.Right) + PageTransitionExit({ duration: 600, curve: Curve.Linear }) + .slide(SlideEffect.Left) + } +} +var postcard; -- Gitee From 49c34d1cafb722049e5f166c1711e98f1f136c4f Mon Sep 17 00:00:00 2001 From: lyfopenharmony <2907279807@qq.com> Date: Thu, 3 Mar 2022 16:25:21 +0800 Subject: [PATCH 02/14] commit --- .idea/gradle.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/.idea/gradle.xml b/.idea/gradle.xml index bc249b5..9bdd85f 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,5 +1,6 @@ +