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://platformapi 或 weixin://wap/pay?。
相关推荐
李游Leo11 小时前
HarmonyOS 7 + Spatial Recon Kit-Core File Kit:3DGS 重建产物的原子发布与中断恢复【鸿蒙心迹】
3d·华为·harmonyos
HwJack2013 小时前
【共创稿事节】HarmonyOS 7空间排布原则:视锥、舒适区与可达性
microsoft·华为·harmonyos
李游Leo13 小时前
HarmonyOS 7 Core Vision Kit + Image Kit:超分批处理的 PixelMap 预算、失败降级与结果原子替换【鸿蒙心迹】
harmonyos
李游Leo14 小时前
HarmonyOS 7 AbilityAccessCtrl + ArkUI:权限弹窗重入治理与提审证据链【鸿蒙心迹】
华为·harmonyos
李游Leo14 小时前
HarmonyOS 7 Spatial Recon Kit + Preferences:重建会话中断恢复与脏任务回收【鸿蒙心迹】
华为·harmonyos
李游Leo15 小时前
《HarmonyOS 7 ArkGraphics 3D 空间设计开发实战》03:glTF模型加载、实例复用与资源生命周期【鸿蒙心迹】
3d·harmonyos
李游Leo16 小时前
HarmonyOS 7 + ArkTS-HUKS:精准碰一碰载荷的签名校验与重放窗口【鸿蒙心迹】
华为·harmonyos
花花_116 小时前
HarmonyOS 7 状态手记 01|页面状态别乱放
华为·harmonyos
李游Leo17 小时前
HarmonyOS 7 + Hvigor-Localization Kit:多语言资源占位符签名与回退链路预检【鸿蒙心迹】
harmonyos
李游Leo17 小时前
HarmonyOS 7 AVSession + Float Window:闪控窗播放控制权的会话移交与重复指令去重【鸿蒙心迹】
华为·harmonyos