小程序初始提交

This commit is contained in:
jdc
2025-11-13 10:36:23 +08:00
parent f26b4f9a2f
commit 5db3b180eb
447 changed files with 83351 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="io.dcloud.uni_modules.xVibrateS">
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>

View File

@@ -0,0 +1,4 @@
{
"minSdkVersion": "19",
"targetSdkVersion": "34"
}

View File

@@ -0,0 +1,37 @@
import Vibrator from "android.os.Vibrator";
import VibratorManager from "android.os.VibratorManager";
import VibrationEffect from "android.os.VibrationEffect";
import Context from "android.content.Context";
import Build from "android.os.Build";
/**
* 震动
* @param {number} duration 震动时间单位ms
*/
export function vibrate(duration: number) {
try {
const context = UTSAndroid.getAppContext() as Context;
let vb: Vibrator | null = null;
// Android 12 (API 31) 及以上使用 VibratorManager
if (Build.VERSION.SDK_INT >= 31) {
const vibratorManager = context.getSystemService(
Context.VIBRATOR_MANAGER_SERVICE
) as VibratorManager;
vb = vibratorManager.getDefaultVibrator();
}
if (vb != null && vb.hasVibrator()) {
// Android 8.0 (API 26) 及以上使用 VibrationEffect
if (Build.VERSION.SDK_INT >= 26) {
const effect = VibrationEffect.createOneShot(
duration.toLong(),
VibrationEffect.DEFAULT_AMPLITUDE
);
vb.vibrate(effect);
}
}
} catch (e) {
console.error("震动失败:", e);
}
}

View File

@@ -0,0 +1,6 @@
import { Vibrate } from "../interface.uts";
import { VibrateNative } from "./vibrate.ets";
export function vibrate(duration: number) {
VibrateNative.vibrate(duration);
}

View File

@@ -0,0 +1,13 @@
{
"module": {
"requestPermissions": [
{
"name": "ohos.permission.VIBRATE",
"usedScene": {
"when": "inuse"
},
"reason": "$string:permission_VIBRATE_reason"
}
]
}
}

View File

@@ -0,0 +1,8 @@
{
"string": [
{
"name": "permission_VIBRATE_reason",
"value": "需要振动权限来提供更好的触觉反馈体验"
}
]
}

View File

@@ -0,0 +1,43 @@
import { vibrator } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';
/**
* 原生振动控制类
* 用于控制设备振动,提供触觉反馈
*/
export class VibrateNative {
/**
* 使设备振动指定时长
* @param duration 振动持续时间(毫秒)
*/
static vibrate(duration: number): void {
try {
// 调用系统振动API
vibrator.startVibration(
{
type: 'time', // 振动类型为时间模式
duration: duration // 振动持续时间
},
{
id: 0, // 振动任务ID
usage: 'alarm' // 振动场景类型,用于系统开关管控
},
(error: BusinessError) => {
// 错误处理回调
if (error) {
console.error(
`振动启动失败: 错误码 ${error.code}, 错误信息 ${error.message}`
);
return;
}
}
);
} catch (err) {
// 捕获意外错误
const error: BusinessError = err as BusinessError;
console.error(
`发生意外错误: 错误码 ${error.code}, 错误信息 ${error.message}`
);
}
}
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyAccessedAPITypes</key>
<array/>
<key>NSPrivacyCollectedDataTypes</key>
<array/>
<key>NSPrivacyTracking</key>
<false/>
<key>NSPrivacyTrackingDomains</key>
<array/>
</dict>
</plist>

View File

@@ -0,0 +1,3 @@
{
"deploymentTarget": "9"
}

View File

@@ -0,0 +1,42 @@
import * as AudioToolbox from "AudioToolbox";
import * as UIKit from "UIKit";
import { Thread } from "Foundation";
/**
* 触发设备震动
* @param duration 震动持续时间,单位:毫秒(ms)仅在iOS 13.0+有效
*/
export function vibrate(duration: number) {
// 参数验证确保duration为正数
if (duration < 0) {
duration = 0;
}
// 检查iOS版本决定使用哪种震动方式
if (UTSiOS.available("iOS 13.0, *")) {
// 创建中等强度的触觉反馈生成器
const generator = new UIKit.UIImpactFeedbackGenerator(
(style = UIKit.UIImpactFeedbackGenerator.FeedbackStyle.medium)
);
// 准备生成器,提高首次触发的响应速度
generator.prepare();
// 记录开始时间
const startTime = new Date().getTime();
const endTime = startTime + duration;
// 循环产生震动效果,直到达到指定时长
while (new Date().getTime() < endTime) {
// 触发触觉反馈强度为0.5(中等强度)
generator.impactOccurred((intensity = 0.5));
// 暂停100毫秒避免过于频繁的震动
Thread.sleep((forTimeInterval = 0.1));
}
} else {
// iOS 13.0以下版本使用AudioToolbox播放系统震动音效
// 注意此方式无法控制震动时长duration参数将被忽略
AudioToolbox.AudioServicesPlayAlertSound(AudioToolbox.kSystemSoundID_Vibrate);
}
}

View File

@@ -0,0 +1 @@
export type Vibrate = (duration: number) => void;

View File

@@ -0,0 +1,13 @@
/**
* 震动
* @param {number} duration 震动时间单位ms,ios微信失效
*/
export function vibrate(duration: number) {
wx.vibrateShort({
type: "medium",
success() {},
fail(error) {
console.error("微信:震动失败");
}
});
}

View File

@@ -0,0 +1,11 @@
/**
* 震动
* @param {number} duration 震动时间单位ms,ios微信失效
*/
export function vibrate(duration: number) {
try {
navigator.vibrate(duration);
} catch (error) {
console.error("WEB震动失败:", error);
}
}