更新记录

1.0.0(2025-11-03) 下载此版本

feat: 初始化


平台兼容性

uni-app(4.76)

Vue2 Vue3 Vue2插件版本 Chrome Safari app-vue app-nvue Android iOS 鸿蒙 鸿蒙插件版本
× 1.0.0 × × × × × × 12 1.0.0
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟
× × × × × × × × × × ×

harmony-flashlight

插件介绍

harmony-flashlight 是一个鸿蒙平台的闪光灯控制插件,可以让你在 uni-app 项目中轻松控制设备闪光灯,实现手电筒功能。

功能特性

  • ✅ 打开闪光灯(手电筒模式)
  • ✅ 关闭闪光灯
  • ✅ 自动检测设备是否支持闪光灯
  • ✅ 完善的错误处理机制
  • ✅ 支持 success/fail/complete 回调

平台支持

  • ✅ HarmonyOS Next

安装

将插件文件夹复制到项目的 uni_modules 目录下。

使用方法

基础示例

<script setup lang="ts">
import { turnOnFlashlight, turnOffFlashlight } from "@/uni_modules/harmony-flashlight"
import { ref } from 'vue'

const isFlashlightOn = ref(false)

// 打开闪光灯
const openFlashlight = () => {
  turnOnFlashlight({
    success: (res) => {
      console.log('打开闪光灯成功', res)
      isFlashlightOn.value = true
      uni.showToast({
        title: '手电筒已打开',
        icon: 'success'
      })
    },
    fail: (err) => {
      console.error('打开闪光灯失败', err)
      uni.showToast({
        title: err.errMsg,
        icon: 'none'
      })
    }
  })
}

// 关闭闪光灯
const closeFlashlight = () => {
  turnOffFlashlight({
    success: (res) => {
      console.log('关闭闪光灯成功', res)
      isFlashlightOn.value = false
      uni.showToast({
        title: '手电筒已关闭',
        icon: 'success'
      })
    },
    fail: (err) => {
      console.error('关闭闪光灯失败', err)
      uni.showToast({
        title: err.errMsg,
        icon: 'none'
      })
    }
  })
}

// 切换闪光灯状态
const toggleFlashlight = () => {
  if (isFlashlightOn.value) {
    closeFlashlight()
  } else {
    openFlashlight()
  }
}
</script>

<template>
  <view class="container">
    <button @click="toggleFlashlight" class="flashlight-btn">
      {{ isFlashlightOn ? '关闭手电筒' : '打开手电筒' }}
    </button>
  </view>
</template>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.flashlight-btn {
  padding: 20px 40px;
  font-size: 18px;
}
</style>

API 说明

turnOnFlashlight(options)

打开闪光灯

参数说明:

参数名 类型 必填 说明
options TurnOnFlashlightOptions 配置选项

TurnOnFlashlightOptions 说明:

参数名 类型 必填 说明
success Function 成功回调函数
fail Function 失败回调函数
complete Function 完成回调函数(无论成功失败都会执行)

success 回调参数:

{
  errMsg: string // 成功消息
}

fail 回调参数:

{
  errCode: number // 错误码
  errMsg: string  // 错误信息
  errSubject: string // 错误主题
}

turnOffFlashlight(options)

关闭闪光灯

参数说明:

参数名 类型 必填 说明
options TurnOffFlashlightOptions 配置选项

TurnOffFlashlightOptions 说明:

参数名 类型 必填 说明
success Function 成功回调函数
fail Function 失败回调函数
complete Function 完成回调函数(无论成功失败都会执行)

错误码

错误码 说明
13001 设备不支持闪光灯
13002 操作不允许
13003 相机服务错误
13004 未知错误

权限说明

插件需要使用相机权限来控制闪光灯,会自动在 module.json5 中配置以下权限:

  • ohos.permission.CAMERA: 相机权限,用于控制闪光灯

首次使用时,系统会提示用户授权。

注意事项

  1. 闪光灯功能依赖设备硬件支持,部分设备可能不支持
  2. 使用前会自动检测设备是否支持闪光灯
  3. 应用退出后闪光灯会自动关闭
  4. 建议在合适的时机关闭闪光灯以节省电量

常见问题

Q: 为什么闪光灯打不开?

A: 可能的原因:

  1. 设备不支持闪光灯功能
  2. 没有获得相机权限
  3. 其他应用正在使用闪光灯
  4. 设备电量过低

Q: 如何判断设备是否支持闪光灯?

A: 可以通过调用 turnOnFlashlight 并在 fail 回调中判断错误码,如果是 13001 则表示设备不支持。

更新日志

1.0.0 (2024-11-03)

  • 首次发布
  • 支持打开/关闭闪光灯
  • 自动检测设备支持情况

隐私、权限声明

1. 本插件需要申请的系统权限列表:

相机权限(用于控制闪光灯)

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

暂无用户评论。