HarmonyOS基本工具封装——拦截H5微信、支付宝支付和自定义H5全局拦截

前言

简介

鸿蒙基本库封装,提升鸿蒙开发效率

安装

dart 复制代码
ohpm install @peakmain/library

ToastManager升级,不再需要传入UIContext

设置全局上下文

如果不传UIContext,一定要在EntryAbility.ets调用

kotlin 复制代码
InitUtils.getInstance(this.context)//初始化全局上下文
示例代码
typescript 复制代码
//默认情况
new ToastManager()
  .showNormalMessage("正常提示")
//成功情况
new ToastManager()
  .showSuccessMessage("成功提示")
//错误提示
new ToastManager()
  .showErrorMessage("错误提示")

拦截H5微信、支付宝支付

  • 在entry模块的module.json5文件中添加配置具体标签路径如下:module-> querySchemes
json 复制代码
    "querySchemes": [
      "alipays",
      "weixin"
    ],

2. 自定义H5全局拦截

在 EntryAbility.ets 目录下,通过设置 urlIntercept 实现自定义拦截逻辑

typescript 复制代码
  InitUtils.getInstance(this.context)
      .urlIntercept( (url: string) => {
        if (!bundleManager.canOpenLink(url)) {
          //new ToastManager(this.getUIContext()).showErrorMessage("未安装目标应用")
          return HandleResult.CONSUMED
        }
        const openLinkOptions: OpenLinkOptions = {
          appLinkingOnly: false,
        };
        (getContext(this) as common.UIAbilityContext).openLink(url, openLinkOptions).then(() => {
        }).catch((err: Error) => {

        })
        return HandleResult.CONSUMED
      },'alipays://platformapi',"weixin://wap/pay?")

urlIntercept 方法参数说明

参数 类型 描述
handleUrl (url: string) => HandleResult 拦截 URL 关键词后执行的方法,默认返回 HandleResult.NOT_CONSUME 表示不处理。
searchSting Array<string> 拦截的 URL 关键字,例如 alipays://platformapiweixin://wap/pay?
相关推荐
贾伟康10 小时前
【天体运行模拟|12】HarmonyOS ArkTS 本地数据文件实战:让离线资源读取失败可见可恢复
数据恢复·harmonyos·arkts·preferences·arkdata
~远在太平洋~11 小时前
05-鸿蒙 faultlog 崩溃日志分析
华为·harmonyos
Magic-ZYJ11 小时前
HarmonyOS 文件选择与读写:DocumentViewPicker、URI、沙箱目录一次搞清
深度学习·华为·harmonyos
2501_9197490312 小时前
华为鸿蒙免费提醒APP—小羊提醒
华为·harmonyos·鸿蒙
OH_TPC12 小时前
HarmonyOS APP开发---“图迹“旅行相册App,需要用到这个库
华为·harmonyos·鸿蒙
lilian23312 小时前
HarmonyOS 7 新特性(二十四)|ModularObjectExtensionAbility 与 Taihe IPC
华为·harmonyos
tsqtsqtsq030913 小时前
鸿蒙系统应用市场更新功能详解与开发适配指南
服务器·harmonyos
Georgewu13 小时前
HarmonyOS Dev Assistant (HarmonyOS开发助手)如何打通元服务开发全流程
harmonyos
2501_9197490314 小时前
华为鸿蒙免费计算油耗APP—小羊油耗
华为·harmonyos·鸿蒙
less_1213814 小时前
HarmonyOS WPS Open SDK:OpenFileRequest 构造参数与 sendRequest 打开链路
华为·sdk·harmonyos·wps·鸿蒙开发·文档编辑