Skip to content

Commit 8c811d2

Browse files
authored
refactor: H5 设备震动 API 调整 (#18250)
1 parent d962619 commit 8c811d2

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed
Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
1-
import Taro from '@tarojs/api'
2-
3-
import { MethodHandler } from '../../utils/handler'
4-
5-
const vibrator = function vibrator (mm: number | number[]) {
6-
try {
7-
return window.navigator.vibrate(mm)
8-
} catch (e) {
9-
console.warn('当前浏览器不支持 vibrate。')
10-
}
11-
}
12-
13-
/**
14-
* 使手机发生较短时间的振动(15 ms)。仅在 iPhone 7 / 7 Plus 以上及 Android 机型生效
15-
*/
16-
export const vibrateShort: typeof Taro.vibrateShort = ({ success, fail, complete } = {}) => {
17-
const handle = new MethodHandler({ name: 'vibrateShort', success, fail, complete })
18-
if (vibrator(15)) {
19-
return handle.success()
20-
} else {
21-
return handle.fail({ errMsg: 'style is not support' })
22-
}
23-
}
24-
25-
/**
26-
* 使手机发生较长时间的振动(400 ms)
27-
*/
28-
export const vibrateLong: typeof Taro.vibrateLong = ({ success, fail, complete } = {}) => {
29-
const handle = new MethodHandler({ name: 'vibrateLong', success, fail, complete })
30-
if (vibrator(400)) {
31-
return handle.success()
32-
} else {
33-
return handle.fail({ errMsg: 'style is not support' })
34-
}
35-
}
1+
import Taro from '@tarojs/api'
2+
3+
import { MethodHandler } from '../../utils/handler'
4+
5+
const vibrator = function vibrator (mm: number | number[]): boolean {
6+
try {
7+
if (typeof window.navigator.vibrate === 'function') {
8+
return window.navigator.vibrate(mm)
9+
}
10+
console.warn('当前浏览器不支持 vibrate。')
11+
return false
12+
} catch (e) {
13+
console.warn('当前浏览器不支持 vibrate。')
14+
return false
15+
}
16+
}
17+
18+
/**
19+
* 使手机发生较短时间的振动(15 ms)。仅在 iPhone 7 / 7 Plus 以上及 Android 机型生效
20+
*/
21+
export const vibrateShort: typeof Taro.vibrateShort = (options: Taro.vibrateShort.Option = {}) => {
22+
const { success, fail, complete } = options
23+
const handle = new MethodHandler({ name: 'vibrateShort', success, fail, complete })
24+
if (vibrator(15)) {
25+
return handle.success()
26+
} else {
27+
return handle.fail({ errMsg: 'style is not support' })
28+
}
29+
}
30+
31+
/**
32+
* 使手机发生较长时间的振动(400 ms)
33+
*/
34+
export const vibrateLong: typeof Taro.vibrateLong = (options: Taro.vibrateLong.Option = {}) => {
35+
const { success, fail, complete } = options
36+
const handle = new MethodHandler({ name: 'vibrateLong', success, fail, complete })
37+
if (vibrator(400)) {
38+
return handle.success()
39+
} else {
40+
return handle.fail({ errMsg: 'style is not support' })
41+
}
42+
}

0 commit comments

Comments
 (0)