diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index 8a27151068bdc8103cd96d6c96b4e11185d12faa..d7fe87e233655fedda9ba2e1849c2be86c6ce2a1 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -15,6 +15,7 @@ export * from './protocols/device/makePhoneCall' export * from './protocols/device/setClipboardData' export * from './protocols/device/accelerometer' export * from './protocols/device/compass' +export * from './protocols/device/vibrate' export * from './protocols/storage/storage' diff --git a/packages/uni-api/src/protocols/device/vibrate.ts b/packages/uni-api/src/protocols/device/vibrate.ts new file mode 100644 index 0000000000000000000000000000000000000000..d747aaa7caf6ad68110ba6b7fb9d5675d17819b1 --- /dev/null +++ b/packages/uni-api/src/protocols/device/vibrate.ts @@ -0,0 +1,5 @@ +export const API_VIBRATE_SHORT = 'vibrateShort' +export type API_TYPE_VIBRATE_SHORT = typeof uni.vibrateShort + +export const API_VIBRATE_LONG = 'vibrateLong' +export type API_TYPE_VIBRATE_LONG = typeof uni.vibrateLong diff --git a/packages/uni-h5/src/service/api/device/vibrate.ts b/packages/uni-h5/src/service/api/device/vibrate.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b6456b030162de34a677c47c54834c95197ac40 --- /dev/null +++ b/packages/uni-h5/src/service/api/device/vibrate.ts @@ -0,0 +1,33 @@ +import { + API_VIBRATE_SHORT, + API_VIBRATE_LONG, + defineAsyncApi, +} from '@dcloudio/uni-api' +import { + API_TYPE_VIBRATE_SHORT, + API_TYPE_VIBRATE_LONG, +} from '@dcloudio/uni-api' + +const _isSupport = !!window.navigator.vibrate + +export const vibrateShort = defineAsyncApi( + API_VIBRATE_SHORT, + (args, { resolve, reject }) => { + if (_isSupport && window.navigator.vibrate(15)) { + resolve() + } else { + reject('vibrateLong:fail') + } + } +) + +export const vibrateLong = defineAsyncApi( + API_VIBRATE_LONG, + (args, { resolve, reject }) => { + if (_isSupport && window.navigator.vibrate(400)) { + resolve() + } else { + reject('vibrateLong:fail') + } + } +) diff --git a/packages/uni-h5/src/service/api/index.ts b/packages/uni-h5/src/service/api/index.ts index 9c7ff3e9b637fcf001f5002380a03d3391269cb6..1023decac161b8cec85510c5e75aeefcbf2dcf25 100644 --- a/packages/uni-h5/src/service/api/index.ts +++ b/packages/uni-h5/src/service/api/index.ts @@ -6,6 +6,7 @@ export * from './device/getSystemInfoSync' export * from './device/network' export * from './device/accelerometer' export * from './device/compass' +export * from './device/vibrate' export * from './storage/storage'