小程序初始提交
This commit is contained in:
82
cool-unix/uni_modules/cool-vibrate/package.json
Normal file
82
cool-unix/uni_modules/cool-vibrate/package.json
Normal file
@@ -0,0 +1,82 @@
|
||||
{
|
||||
"id": "cool-vibrate",
|
||||
"displayName": "cool-vibrate",
|
||||
"version": "1.0.1",
|
||||
"description": "cool-vibrate",
|
||||
"keywords": [
|
||||
"cool-vibrate"
|
||||
],
|
||||
"repository": "",
|
||||
"engines": {
|
||||
"HBuilderX": "^3.9.0"
|
||||
},
|
||||
"dcloudext": {
|
||||
"type": "uts",
|
||||
"sale": {
|
||||
"regular": {
|
||||
"price": "0.00"
|
||||
},
|
||||
"sourcecode": {
|
||||
"price": "0.00"
|
||||
}
|
||||
},
|
||||
"contact": {
|
||||
"qq": ""
|
||||
},
|
||||
"declaration": {
|
||||
"ads": "",
|
||||
"data": "",
|
||||
"permissions": ""
|
||||
},
|
||||
"npmurl": ""
|
||||
},
|
||||
"uni_modules": {
|
||||
"dependencies": [],
|
||||
"encrypt": [],
|
||||
"platforms": {
|
||||
"cloud": {
|
||||
"tcb": "u",
|
||||
"aliyun": "u",
|
||||
"alipay": "u"
|
||||
},
|
||||
"client": {
|
||||
"Vue": {
|
||||
"vue2": "u",
|
||||
"vue3": "u"
|
||||
},
|
||||
"App": {
|
||||
"app-android": "u",
|
||||
"app-ios": "u"
|
||||
},
|
||||
"H5-mobile": {
|
||||
"Safari": "u",
|
||||
"Android Browser": "u",
|
||||
"微信浏览器(Android)": "u",
|
||||
"QQ浏览器(Android)": "u"
|
||||
},
|
||||
"H5-pc": {
|
||||
"Chrome": "u",
|
||||
"IE": "u",
|
||||
"Edge": "u",
|
||||
"Firefox": "u",
|
||||
"Safari": "u"
|
||||
},
|
||||
"小程序": {
|
||||
"微信": "u",
|
||||
"阿里": "u",
|
||||
"百度": "u",
|
||||
"字节跳动": "u",
|
||||
"QQ": "u",
|
||||
"钉钉": "u",
|
||||
"快手": "u",
|
||||
"飞书": "u",
|
||||
"京东": "u"
|
||||
},
|
||||
"快应用": {
|
||||
"华为": "u",
|
||||
"联盟": "u"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
17
cool-unix/uni_modules/cool-vibrate/readme.md
Normal file
17
cool-unix/uni_modules/cool-vibrate/readme.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# cool-vibrate
|
||||
|
||||
### 兼容性
|
||||
|
||||
| IOS | Andriod | WEB | 小程序 |
|
||||
| ---- | ------- | ---- | ------ |
|
||||
| 支持 | 支持 | 支持 | 支持 |
|
||||
|
||||
### 开发文档
|
||||
|
||||
调用:
|
||||
|
||||
```ts
|
||||
import { vibrate } from "@/uni_modules/cool-vibrate";
|
||||
|
||||
vibrate(1);
|
||||
```
|
||||
@@ -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>
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"minSdkVersion": "19",
|
||||
"targetSdkVersion": "34"
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Vibrate } from "../interface.uts";
|
||||
import { VibrateNative } from "./vibrate.ets";
|
||||
|
||||
export function vibrate(duration: number) {
|
||||
VibrateNative.vibrate(duration);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"module": {
|
||||
"requestPermissions": [
|
||||
{
|
||||
"name": "ohos.permission.VIBRATE",
|
||||
"usedScene": {
|
||||
"when": "inuse"
|
||||
},
|
||||
"reason": "$string:permission_VIBRATE_reason"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "permission_VIBRATE_reason",
|
||||
"value": "需要振动权限来提供更好的触觉反馈体验"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"deploymentTarget": "9"
|
||||
}
|
||||
42
cool-unix/uni_modules/cool-vibrate/utssdk/app-ios/index.uts
Normal file
42
cool-unix/uni_modules/cool-vibrate/utssdk/app-ios/index.uts
Normal 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);
|
||||
}
|
||||
}
|
||||
1
cool-unix/uni_modules/cool-vibrate/utssdk/interface.uts
Normal file
1
cool-unix/uni_modules/cool-vibrate/utssdk/interface.uts
Normal file
@@ -0,0 +1 @@
|
||||
export type Vibrate = (duration: number) => void;
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* 震动
|
||||
* @param {number} duration 震动时间单位ms,ios微信失效
|
||||
*/
|
||||
export function vibrate(duration: number) {
|
||||
wx.vibrateShort({
|
||||
type: "medium",
|
||||
success() {},
|
||||
fail(error) {
|
||||
console.error("微信:震动失败");
|
||||
}
|
||||
});
|
||||
}
|
||||
11
cool-unix/uni_modules/cool-vibrate/utssdk/web/index.uts
Normal file
11
cool-unix/uni_modules/cool-vibrate/utssdk/web/index.uts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user