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?
相关推荐
程序猿追20 分钟前
HarmonyOS 6.0 NEXT:基于 Map Kit 实现一款“手绘路线”骑行导航应用
华为·harmonyos
轻口味2 小时前
HarmonyOS 6.1 全栈实战录 - 08 视讯巅峰:Media Kit 视频缩略图批量提取与 HDR 渲染链路实战
华为·音视频·harmonyos
想你依然心痛2 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与HMAF的“医智助手“——医疗影像AI智能体辅助诊断平台
人工智能·华为·harmonyos
nashane2 小时前
HarmonyOS 6学习:卡片组件圆角白边问题的诊断与修复实战
人工智能·pytorch·深度学习·harmonyos
川石课堂软件测试2 小时前
接口测试常见面试题及答案
python·网络协议·mysql·华为·单元测试·prometheus·harmonyos
痕忆丶3 小时前
openharmony北向开发基础之OpenHarmony签名机制详解
linux·harmonyos
not coder3 小时前
HarmonyOS NEXT 原生OFD阅读库实测:纯ArkTS无依赖,完美适配电子发票与政务公文开发
华为·harmonyos·鸿蒙·ofd·政务
richard_yuu4 小时前
鸿蒙隐私级树洞实战|ArkTS 实现写完即清空、零内存残留、零隐私留存
华为·harmonyos
三声三视4 小时前
Electron 在鸿蒙 PC 上启动慢?我把冷启动从 7 秒压到 1.5 秒的完整记录
electron·harmonyos·桌面应用
leon_teacher4 小时前
HarmonyOS 6 古诗学习宝实战:基于 Preferences 实现错题本自动派生与题级去重系统
学习·华为·harmonyos