1# 拉起金融类应用(startAbilityByType) 2 3本章节介绍如何拉起金融类应用扩展面板。 4 5## 金融类应用扩展面板参数说明 6 7startAbilityByType接口中type字段为finance,对应的wantParam参数: 8 9| 参数名 | 类型 | 必填 | 说明 | 10| -------------------- | ------------------------------------------------------------ | -------- | -------- | 11| sceneType | number | 否 | 意图场景,表明本次请求对应的操作意图。1:转账汇款 2:信用卡还款。默认为1 | 12| bankCardNo | string | 否 | 银行卡卡号 | 13 14## 拉起方开发步骤 151. 导入相关模块。 16 ```ts 17 import { common } from '@kit.AbilityKit'; 18 ``` 192. 构造接口参数并调用startAbilityByType接口。 20 21 ```ts 22 @Entry 23 @Component 24 struct Index { 25 @State hideAbility: string = 'hideAbility' 26 27 build() { 28 Row() { 29 Column() { 30 Text(this.hideAbility) 31 .fontSize(30) 32 .fontWeight(FontWeight.Bold) 33 .onClick(() => { 34 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 35 let wantParam: Record<string, Object> = { 36 'sceneType': 1, 37 "bankCardNo": '123456789' 38 }; 39 let abilityStartCallback: common.AbilityStartCallback = { 40 onError: (code: number, name: string, message: string) => { 41 console.log(`onError code ${code} name: ${name} message: ${message}`); 42 }, 43 onResult: (result) => { 44 console.log(`onResult result: ${JSON.stringify(result)}`); 45 } 46 } 47 48 context.startAbilityByType("finance", wantParam, abilityStartCallback, 49 (err) => { 50 if (err) { 51 console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); 52 } else { 53 console.log(`success`); 54 } 55 }); 56 }); 57 } 58 .width('100%') 59 } 60 .height('100%') 61 } 62 } 63 ``` 64 效果示例图: 65 66  67 68## 目标方开发步骤 69 701. 在module.json5中配置[uris](../quick-start/module-configuration-file.md#skills标签),步骤如下: 71 1. 设置linkFeature属性以声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用,取值范围如下: 72 73 | 取值 | 含义 | 74 | -------------- | ---------------------------- | 75 | Transfer | 声明应用支持转账汇款功能 | 76 | CreditCardRepayment | 声明应用支持信用卡还款功能 | 77 2. 设置scheme、host、port、path/pathStartWith属性,与Want中URI相匹配,以便区分不同功能。 78 79 ```json 80 { 81 "abilities": [ 82 { 83 "skills": [ 84 { 85 "uris": [ 86 { 87 "scheme": "finance", // 这里仅示意,应用需确保这里声明的的uri能被外部正常拉起 88 "host": "transfer", 89 "path": "", 90 "linkFeature": "Transfer" // 声明应用支持转账汇款功能 91 }, 92 { 93 "scheme": "finance", // 这里仅示意,应用需确保这里声明的的uri能被外部正常拉起 94 "host": "credit_card_repayment", 95 "path": "", 96 "linkFeature": "CreditCardRepayment" // 声明应用支持信用卡还款功能 97 } 98 ] 99 } 100 ] 101 } 102 ] 103 } 104 ``` 105 1062. 解析面板传过来的参数并做对应处理。 107 108 ```ts 109 UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void 110 ``` 111 112 在参数**want.uri**中会携带目标方配置的linkFeature对应的uri; 113 114 在参数**want.parameters**中会携带Caller方传入的参数,如下表所示: 115 116 | 参数名 | 类型 | 必填 | 说明 | 117 | -------------------- | ------------------------------------------------------------ | -------- | -------- | 118 | bankCardNo | string | 否 | 银行卡卡号 | 119 120 应用可根据[linkFeature](../quick-start/module-configuration-file.md#skills标签)中定义的特性功能,比如转账汇款和信用卡还款,结合接收到的uri开发不同的样式页面。 121 122**完整示例:** 123 124```ts 125import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 126import { hilog } from '@kit.PerformanceAnalysisKit'; 127import { window } from '@kit.ArkUI'; 128 129const TAG = 'EntryAbility' 130 131export default class EntryAbility extends UIAbility { 132 windowStage: window.WindowStage | null = null; 133 134 uri?: string; 135 bankCardNo?: string; 136 137 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 138 hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`); 139 super.onCreate(want, launchParam); 140 this.parseWant(want); 141 } 142 143 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { 144 hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`); 145 super.onNewWant(want, launchParam); 146 this.parseWant(want); 147 if (!this.windowStage) { 148 hilog.error(0x0000, TAG, 'windowStage is null'); 149 this.context.terminateSelf(); 150 return; 151 } 152 this.loadPage(this.windowStage); 153 } 154 155 private parseWant(want: Want): void { 156 this.uri = want.uri as string | undefined; 157 this.bankCardNo = want.parameters?.bankCardNo as string | undefined; 158 } 159 160 private loadPage(windowStage: window.WindowStage): void { 161 hilog.info(0x0000, TAG, `loadPage, uri=${this.uri}`); 162 if (this.uri === 'finance://transfer') { 163 // 构建转账场景参数 164 const storage: LocalStorage = new LocalStorage({ 165 "bankCardNo": this.bankCardNo 166 } as Record<string, Object>); 167 // 拉起转账页面 168 windowStage.loadContent('pages/TransferPage', storage) 169 } else if (this.uri === 'finance://credit_card_repayment') { 170 // 构建信用卡还款场景参数 171 const storage: LocalStorage = new LocalStorage({ 172 "bankCardNo": this.bankCardNo 173 } as Record<string, Object>); 174 // 拉起信用卡还款页面 175 windowStage.loadContent('pages/CreditCardRepaymentPage', storage) 176 } else { 177 // 默认拉起首页 178 windowStage.loadContent('pages/Index', (err) => { 179 if (err.code) { 180 hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s', 181 JSON.stringify(err) ?? ''); 182 return; 183 } 184 hilog.info(0x0000, TAG, 'Succeeded in loading the content.'); 185 }); 186 } 187 } 188 189 onDestroy(): void { 190 hilog.info(0x0000, TAG, `onDestroy`); 191 } 192 193 onWindowStageCreate(windowStage: window.WindowStage): void { 194 hilog.info(0x0000, TAG, `onWindowStageCreate`); 195 this.windowStage = windowStage; 196 this.loadPage(this.windowStage); 197 } 198 199 onWindowStageDestroy(): void { 200 hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy'); 201 } 202 203 onForeground(): void { 204 hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground'); 205 } 206 207 onBackground(): void { 208 hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground'); 209 } 210} 211```