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