HarmonyOS基本工具封装——BasicLibrary的基本使用(一)

前言

简介

鸿蒙基本库封装,提升鸿蒙开发效率

安装

ohpm install @peakmain/library

基本使用

一、权限框架使用
  1. 创建request对象
ini 复制代码
request: PermissionUtils = new PermissionUtils()
  1. 检查是否有权限 方法如下:
kotlin 复制代码
this.request.checkPermissions(权限数组)

示例如下

csharp 复制代码
  async checkPermission() {
    let result = await this.request.checkPermissions(['ohos.permission.LOCATION', "ohos.permission.APPROXIMATELY_LOCATION"])
    if (result) {
      promptAction.showToast({ message: "已授予位置权限" })
    }
    return result
  }
  1. 请求权限
kotlin 复制代码
this.request.requestPermission(权限数组)

示例如下

kotlin 复制代码
 result = await this.request.requestPermission(['ohos.permission.LOCATION', "ohos.permission.APPROXIMATELY_LOCATION"])
 if (result) {
   this.sLocation = true
   promptAction.showToast({ message: "已授予位置权限" })
 } else {
   this.sLocation = false
   promptAction.showToast({ message: "已拒绝位置权限" })
}
二、版本管理框架使用
  1. 获取版本名,如1.0.0
javascript 复制代码
 AppManager.getAppVersionNameSync().then((result) => {
    console.log("获取到版本名:"+result)
 }
  1. 获取版本号,如:1000000
javascript 复制代码
 AppManager.getAppVersionCode().then((result) => {
    console.log("获取到版本号:"+result)
 }
三、日期工具类
  1. 两个日期相差天数
arduino 复制代码
DateUtils.getDaysDifference("2023.12.1", "2023.12.2")
  1. 日期转换工具,2024.04.17->2024年04月17日
arduino 复制代码
DateUtils.dateFormat("2024.04.17")
  1. 转换时间:①、当天的时间:返回 时间:分钟;②、今年的时间:返回 月日 ③、否则返回:年月日
scss 复制代码
DateUtils.transTime(1713419434879)//返回13:50
四、常用工具类
  1. Base64解密转成字符串
csharp 复制代码
 let params="param=eyJjYWxsSWQiOiJwYWdlL2p1bXBUb1doZXJlIiwiZGF0YSI6eyJwYWdlIjoid2VidmlldyIsImRhdGEiOnsidXJsIjoiaHR0cHM6Ly9oNW1hbGwueWFkdW8uY29tL3BhZ2VzL2FjdGl2ZVBhZ2UvYWN0aXZlUGFnZT9wcm9tb3RlQ29kZT1tdG9qc2Emc291cmNlQ29kZT1oNTAxIn19fQ=="
 jsonString = await Utils.decodeToString(params.split("=")[1])
相关推荐
xingyuzhisuan1 天前
从x86到Arm:GPU服务器CPU架构多元化趋势深度解读
服务器·arm开发·架构·gpu算力
信也科技布道师1 天前
把7个页面变成1段对话:AI如何重构借款流程
前端·人工智能·重构·架构·交互·用户体验
想你依然心痛1 天前
HarmonyOS 5.0智慧交通开发实战:构建分布式车载智能座舱与手机无缝互联系统
分布式·智能手机·harmonyos·智慧交通·智能座舱
麒麟ZHAO1 天前
鸿蒙flutter第三方库适配 - 动态表单
flutter·华为·harmonyos
墨雪遗痕1 天前
工程架构认知(三):从传统Web系统到AI大模型驱动系统
前端·人工智能·架构
Warren2Lynch1 天前
无缝知识发布:开发者指南——将 Visual Paradigm OpenDocs 与企业 WordPress 集成
人工智能·架构·uml
无忧智库1 天前
[特殊字符] 数字孪生与万物智联:解构智慧水利水务的全栈技术架构与场景化落地(PPT)
架构
见山是山-见水是水1 天前
鸿蒙flutter第三方库适配 - 页面转场应用
flutter·华为·harmonyos
ZOOOOOOU1 天前
智慧社区云对讲门禁系统架构设计:中优云联免布线、全免费核心功能技术解析
数据库·人工智能·架构·边缘计算
Dontla1 天前
异步知识库索引管线:与在线问答链路解耦架构介绍(离线构建,在线查询)分层索引、Elasticsearch
elasticsearch·架构