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?
相关推荐
tyqtyq228 小时前
旅行打包清单 App — HarmonyOS AI 应用开发技术博客
人工智能·学习·华为·生活·harmonyos
HONG````8 小时前
Lazy Import 动态加载:减少 HarmonyOS 应用冷启动时间
harmonyos
条tiao条12 小时前
Navigation页面跳转教学博客
ui·华为·harmonyos·鸿蒙·页面跳转
ZZZMMM.zip12 小时前
竞品分析框架-基于鸿蒙的AI竞品分析应用开发实践
人工智能·华为·harmonyos·鸿蒙系统
ldsweet13 小时前
《HarmonyOS技术精讲-Basic Services Kit》账号管理:应用内账号创建与管理
华为·harmonyos
shaodong112315 小时前
HarmonyOS新特性-沉浸光感在叠叠消小游戏中的落地实践
harmonyos·鸿蒙
xd18557855515 小时前
歌词创作工坊-基于鸿蒙的AI歌词创作应用开发实践
人工智能·华为·harmonyos·鸿蒙
子非鱼942716 小时前
07-Flutter 鸿蒙实战 07:故事集列表与缓存设计
学习·flutter·缓存·华为·harmonyos
程序员黑豆16 小时前
从零开始:创建第一个鸿蒙应用程序
前端·harmonyos
千逐6818 小时前
HarmonyOS 实战 | 弹窗/提示框怎么做——Alert、Dialog、Toast 详解
华为·harmonyos·鸿蒙