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

754 阅读1分钟

前言

简介

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

安装

ohpm install @peakmain/library

基本使用

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

示例如下

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

示例如下

 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
 AppManager.getAppVersionNameSync().then((result) => {
    console.log("获取到版本名:"+result)
 }
  1. 获取版本号,如:1000000
 AppManager.getAppVersionCode().then((result) => {
    console.log("获取到版本号:"+result)
 }
三、日期工具类
  1. 两个日期相差天数
DateUtils.getDaysDifference("2023.12.1", "2023.12.2")
  1. 日期转换工具,2024.04.17->2024年04月17日
DateUtils.dateFormat("2024.04.17")
  1. 转换时间:①、当天的时间:返回 时间:分钟;②、今年的时间:返回 月日 ③、否则返回:年月日
DateUtils.transTime(1713419434879)//返回13:50
四、常用工具类
  1. Base64解密转成字符串
 let params="param=eyJjYWxsSWQiOiJwYWdlL2p1bXBUb1doZXJlIiwiZGF0YSI6eyJwYWdlIjoid2VidmlldyIsImRhdGEiOnsidXJsIjoiaHR0cHM6Ly9oNW1hbGwueWFkdW8uY29tL3BhZ2VzL2FjdGl2ZVBhZ2UvYWN0aXZlUGFnZT9wcm9tb3RlQ29kZT1tdG9qc2Emc291cmNlQ29kZT1oNTAxIn19fQ=="
 jsonString = await Utils.decodeToString(params.split("=")[1])