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])
相关推荐
珠海西格电力3 小时前
零碳园区的能源结构优化需要哪些技术支持?
大数据·人工智能·物联网·架构·能源
J_liaty5 小时前
OpenFeign微服务实战指南
微服务·云原生·架构·openfeign
Easonmax6 小时前
基础入门 React Native 鸿蒙跨平台开发:实现面包屑导航
react native·react.js·harmonyos
Easonmax6 小时前
基础入门 React Native 鸿蒙跨平台开发:冒泡排序动画可视化
react native·react.js·harmonyos
鸣弦artha8 小时前
Flutter框架跨平台鸿蒙开发——GridView数据绑定实战
flutter·华为·harmonyos
爬山算法10 小时前
Hibernate(65)如何在微服务架构中使用Hibernate?
微服务·架构·hibernate
zhujian8263711 小时前
三十、【鸿蒙 NEXT】实现吸顶效果
harmonyos·鸿蒙·next·吸顶·吸顶效果·nestedscroll
码农三叔11 小时前
(6-1)手部、足部与末端执行器设计:仿生手设计
人工智能·架构·机器人·人形机器人
●VON11 小时前
Flutter for OpenHarmony:基于可选描述字段与上下文感知渲染的 TodoList 任务详情子系统实现
学习·flutter·架构·交互·von
无穷小亮11 小时前
Flutter框架跨平台鸿蒙开发——育儿知识APP的开发流程
flutter·华为·harmonyos·鸿蒙