# 代理方法
小程序中部分业务是抽象定义的,这些抽象的业务通过接口的形式暴露给了外部,外部可以自行实现具体的业务逻辑。
所有代理类均是在调用 FinAppClient.init
方法返回出来 FinAppClient
实例的 proxyHandlerManager
属性上,示例代码中的 client
即为 FinAppClient
的实例
函数签名如果是需要返回 boolean 类型表示是否需要阻止默认行为,如果需要阻止就返回
true
,否则则返回false
函数里的参数
appId
和apiServer
均为触发的小程序的信息,可以根据这两个数据来对不同的小程序来执行不同的逻辑
# 1. CapsuleHandler
胶囊按钮的代理类
class CapsuleHandler {
// 更多按钮的点击事件
onMoreButtonClick?: (appId: string, apiServer: string) => boolean
// 关闭按钮的点击事件
onCloseButtonClick?: (appId: string, apiServer: string) => boolean
}
示例代码
client.proxyHandlerManager.capsuleHandler.onMoreButtonClick = (appId: string, apiServer: string) => {
// do something
return false // 不阻止默认行为
}
# 2. MoreMenuHandler
更多菜单的代理类
class MoreMenuHandler {
/**
* @param type 菜单按钮的唯一标识
* @param currentPath 小程序当前的页面路径
* @returns
*
*/
onMoreMenuItemClick?: (appId: string, apiServer: string, type: string, currentPath: string) => boolean
/**
* @returns 自定义更多菜单的按钮
*/
getMoreMenuItems: () => IMoreMenuItem[]
}
IMoreMenuItem
属性 | 类型 | 是否必填 | 描述 |
---|---|---|---|
label | String | 是 | 小程序 id |
icon | ResourceStr | 是 | 菜单按钮的图片资源 |
disableIcon | ResourceStr | 否 | 菜单按钮禁用的图片资源 |
darkIcon | ResourceStr | 否 | 暗黑模式菜单按钮的图片资源 |
disableDarkIcon | ResourceStr | 否 | 暗黑模式菜单按钮禁用的图片资源 |
menuItemId | String | 是 | 菜单按钮的唯一标识,即 onMoreMenuItemClick 参数的 type |
hover | Boolean | 否 | 是否开启 hover 效果 |
disable | Boolean | 否 | 是否禁用 |
示例代码
client.proxyHandlerManager.moreMenuHandler.onMoreMenuItemClick = (appId: string, apiServer: string, type: string, currentPath: string) => {
if(type === 'custom'){
// do something
return true // 阻止默认行为
}
return false
}
client.proxyHandlerManager.moreMenuHandler.getMoreMenuItems = () => {
return [
{
label: '自定义 Btn',
icon: $r('app.media.app_icon'),
menuItemId: 'custom'
}
]
}
# 3. PrivacyHandler
隐私协议的代理类
class PrivacyHandler {
/**
* @param scope 当前请求的权限,如果为空则是在关于页面显示的隐私协议
* @returns IFinApplet.IPrivacy
*/
getPrivacyInfo?: (appId: string, apiServer: string, scope?: string) => IFinApplet.IPrivacy
}
IFinApplet.IPrivacy
属性 | 类型 | 是否必填 | 描述 |
---|---|---|---|
title | String | 是 | 自定义隐私授权弹窗标题 |
copyWriting | String | 是 | 自定义隐私授权弹窗文案,内容包含 自定义隐私协议文档名称 |
docName | String | 是 | 自定义隐私协议文档名称 |
docUrl | String | 是 | 自定义隐私协议文档链接 |
示例代码
client.proxyHandlerManager.privacyHandler.getPrivacyInfo = (appId: string, apiServer: string, scope?: string) => {
return {
title: '自定义授权弹窗标题',
docName: '自定义隐私协议文档名称',
docUrl: 'https://www.finclip.com',
copyWriting: '自定义授权弹窗文案,隐私协议文档名称为《自定义隐私协议文档名称》'
}
}
# 4. ScopeHandler
自定义权限的代理类
class ScopeHandler {
/**
* @param api 当初小程序触发的 API 名称
* @param context 当前 Ability 的 context * @param scopes 权限列表,包括内置权限和自定义权限
* @param callback API 的回调函数,如果用户通过权限调用 callback 传 true,后续则会走自定义 API 的逻辑,否则则直接走 fail 逻辑
* @returns
*/
onCustomApiInvoke?: (appId: string, apiServer: string, api: string, context: common.UIAbilityContext, scopes: IFinApplet.IScope[], callback: (res: boolean) => void) => void
// 自定义弹窗,实现方式可以参考示例代码
customView?: () => void
// 注册自定义权限
getCustomScopes: (appId: string, apiServer: string) => IFinApplet.IScope[]
}
IFinApplet. IScope
属性 | 类型 | 是否必填 | 描述 |
---|---|---|---|
scope | String | 是 | 权限标识位 |
title | String | 是 | 权限标题 |
desc | String | 是 | 权限描述 |
scopeStatus | EPermissionState | 是 | 权限状态 |
scopeName | String | 是 | 设置页描述 |
EPermissionState
属性 | 值 | 描述 |
---|---|---|
UNSET | 0 | 未设置 |
DISALLOW | 1 | 不允许 |
ALLOW_WHEN_USING | 2 | 使用小程序时 |
ALLOW | 3 | 允许 |
示例代码
client.proxyHandlerManager.scopeHandler.getCustomScopes = (appId: string, apiServer: string) => {
return [
{
scope: 'idCard',
scopeName: '身份证',
title: '获取身份证信息',
desc: "获取您的身份证信息用于XXXXXX",
scopeStatus: 0
}
]
}
@Builder
function customView() {
CustomViewComponent()
}
@Component
struct CustomViewComponent {
@State show: boolean = false
private context = getContext(this) as common.UIAbilityContext
aboutToAppear() {
this.context.eventHub.on('showCustomToast', () => {
this.show = true
})
}
handlePermission(res: boolean) {
this.context.eventHub.emit('onShowCustomToast', res)
this.show = false
}
@Builder
toast() {
Stack({ alignContent: Alignment.Bottom }) {
Row() {
} .height('100%')
.width('100%')
.backgroundColor('rgba(0,0,0,0.5)')
Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceBetween }) {
Row() {
Button('拒绝').onClick(() => {
this.handlePermission(false)
})
Button('同意').onClick(() => {
this.handlePermission(true)
})
}
.width('100%')
}
.padding({ top: 24, left: 18, right: 18, bottom: 24 })
.width('100%')
.height(241)
.backgroundColor('#F5F6F6')
.clip(true)
.borderRadius({ topLeft: 16, topRight: 16 })
.zIndex(10)
}
}
build() {
if (this.show) {
this.toast()
}
}}
client.proxyHandlerManager.scopeHandler.customView = customView
const onCustomApiInvoke = (appId: string, apiServer: string, api: String, context: common.UIAbilityContext, scopes: IFinApplet.IScope[], callback: (res: boolean) => void) => {
const scope = scopes.find(i => i.scope === 'idCard')
if (scope?.scopeStatus === 0) {
context.eventHub.emit('showCustomToast', api)
context.eventHub.on('onShowCustomToast', (res: boolean) => {
scope.scopeStatus = res ? 3 : 1
callback(res)
context.eventHub.off('onShowCustomToast')
})
} else {
callback(true)
}
}
client.proxyHandlerManager.scopeHandler.onCustomApiInvoke = onCustomApiInvoke