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