84 行
2.0 KiB
Plaintext
84 行
2.0 KiB
Plaintext
<template>
|
|
<!-- #ifdef APP -->
|
|
<scroll-view style="flex: 1;padding: 6px;">
|
|
<!-- #endif -->
|
|
<text class="uni-h2">振动</text>
|
|
<text class="notice">如下情况会导致无法振动:设备电量不足、静音模式、关闭了系统触感</text>
|
|
<text class="uni-h3">短振动 uni.vibrateShort</text>
|
|
<text class="margin-v">短振动支持 `light`、`medium`、`heavy` 三种力度类型。</text>
|
|
<button class="margin-v" @tap="callShort('light')">轻振动</button>
|
|
<button class="margin-v" @tap="callShort('medium')">中振动</button>
|
|
<button class="margin-v" @tap="callShort('heavy')">强振动</button>
|
|
|
|
<text class="uni-h3">长振动 uni.vibrateLong</text>
|
|
<button @tap="callLong">触发长振动</button>
|
|
|
|
<text class="uni-h4">最近日志:</text>
|
|
<text class="log-text">{{ statusText }}</text>
|
|
<!-- #ifdef APP -->
|
|
</scroll-view>
|
|
<!-- #endif -->
|
|
</template>
|
|
|
|
<script setup lang="uts">
|
|
|
|
type ShortType = 'heavy' | 'medium' | 'light'
|
|
|
|
const statusText = ref('等待触发振动')
|
|
|
|
function updateStatus(message : string) : void {
|
|
statusText.value = message
|
|
}
|
|
|
|
function getShortTypeLabel(type : ShortType) : string {
|
|
switch (type) {
|
|
case 'light':
|
|
return '轻振动'
|
|
case 'medium':
|
|
return '中振动'
|
|
default:
|
|
return '强振动'
|
|
}
|
|
}
|
|
|
|
function callShort(type : ShortType) : void {
|
|
uni.vibrateShort({
|
|
type,
|
|
success: () => {
|
|
updateStatus(`${getShortTypeLabel(type)}调用成功`)
|
|
},
|
|
fail: (err) => {
|
|
updateStatus(`短振动调用失败:${err.errMsg}`)
|
|
}
|
|
})
|
|
}
|
|
|
|
function callLong() : void {
|
|
uni.vibrateLong({
|
|
success: () => {
|
|
updateStatus('长振动调用成功')
|
|
},
|
|
fail: (err) => {
|
|
updateStatus(`长振动调用失败:${err.errMsg}`)
|
|
}
|
|
})
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
.margin-v {
|
|
margin: 5px 0;
|
|
}
|
|
.log-text {
|
|
padding: 10px;
|
|
font-size: 14px;
|
|
border: 1px solid #ccc;
|
|
}
|
|
.notice {
|
|
color: #550000;
|
|
font-size: 14px;
|
|
font-style: italic;
|
|
}
|
|
|
|
</style>
|