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?
相关推荐
我是小a39 分钟前
「财务管家」智能记账应用:HarmonyOS NEXT 页面布局设计与实现详解
华为·harmonyos·鸿蒙·鸿蒙系统
SuperHeroWu71 小时前
HarmonyOS7特性解读
harmonyos·特性·智能化·多窗形态·场景协同·空间化
心中有国也有家3 小时前
使用 DevEco Studio 配置 Flutter 鸿蒙签名
学习·flutter·华为·harmonyos
如此风景4 小时前
鸿蒙开发状态管理 V2 全部装饰器详解速查表
harmonyos
爱吃大芒果4 小时前
鸿蒙 ArkTS 网络工程进阶:@ohos.net.http 模块下的长连接、超时与防内存泄漏实战
华为·harmonyos
FrameNotWork4 小时前
HarmonyOS 6.0 沉浸式状态栏与安全区域适配实战
华为·harmonyos
绝世番茄5 小时前
鸿蒙原生ArkTS布局方式之Stack+Opacity叠加淡入淡出深度解析
华为·harmonyos·鸿蒙
心中有国也有家5 小时前
鸿蒙Flutter开发环境从零搭建教程(Windows/macOS双平台·避坑版)
学习·flutter·华为·harmonyos
国服第二切图仔5 小时前
HarmonyOS APP《画伴梦工厂》开发第43篇-多模块架构设计——模块拆分与依赖管理
华为·harmonyos
HarmonyOS_SDK6 小时前
一次开发,多端高效协同:解码HarmonyOS SDK窗口的响应式设计哲学
harmonyos