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?
相关推荐
慧海灵舟1 小时前
鸿蒙南向开发教程 Day 6:事件标志组(Event Flags)
华为·harmonyos
慧海灵舟2 小时前
鸿蒙南向开发教程 Day 5:延时与系统节拍
华为·harmonyos
2501_919749032 小时前
鸿蒙 Flutter 实战:saver_gallery 5.1.0 适配 3.27-ohos 全流程
flutter·华为·harmonyos
前端不太难3 小时前
鸿蒙游戏需要 GameEngine 吗?
游戏·状态模式·harmonyos
●VON3 小时前
鸿蒙Flutter实战:异步回调mounted检查安全实践
flutter·华为·harmonyos·鸿蒙
特立独行的猫a3 小时前
鸿蒙 PC 移植记:将微软的 `edit` 轻量级终端编辑器带到 OpenHarmony
microsoft·rust·编辑器·harmonyos·鸿蒙pc·edit
●VON3 小时前
鸿蒙Flutter实战:MethodChannel桥接获取OHOS文件目录
flutter·华为·harmonyos·鸿蒙
HwJack203 小时前
HarmonyOS APP开发中Native 层崩溃信号全集与生存指南
华为·harmonyos
李二。4 小时前
鸿蒙原生ArkTS-太空探索新闻AI
人工智能·华为·harmonyos
慧海灵舟4 小时前
鸿蒙南向开发教程 Day 7:互斥锁(Mutex)
华为·harmonyos