feat:蓝牙绑定和在线客服
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
:class="`pair-check-list__status--${item.status}`"
|
||||
>
|
||||
<text v-if="item.status === 'done'" class="pair-check-list__check">✓</text>
|
||||
<text v-else-if="item.status === 'failed'" class="pair-check-list__check">×</text>
|
||||
</view>
|
||||
<text class="pair-check-list__label">{{ item.title }}</text>
|
||||
<view
|
||||
@@ -82,6 +83,10 @@ defineProps<{
|
||||
background: #e8eaed;
|
||||
}
|
||||
|
||||
.pair-check-list__status--failed {
|
||||
background: #e54d4d;
|
||||
}
|
||||
|
||||
.pair-check-list__check {
|
||||
font-size: 22rpx;
|
||||
color: #fff;
|
||||
@@ -113,6 +118,10 @@ defineProps<{
|
||||
background: #f5f6f8;
|
||||
}
|
||||
|
||||
.pair-check-list__tag--failed {
|
||||
background: #fff5f5;
|
||||
}
|
||||
|
||||
.pair-check-list__tag-text {
|
||||
font-size: 24rpx;
|
||||
font-weight: 500;
|
||||
@@ -129,4 +138,8 @@ defineProps<{
|
||||
.pair-check-list__tag--pending .pair-check-list__tag-text {
|
||||
color: #98a2b3;
|
||||
}
|
||||
|
||||
.pair-check-list__tag--failed .pair-check-list__tag-text {
|
||||
color: #e54d4d;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -5,24 +5,43 @@
|
||||
<text class="pair-device-summary__name">{{ device.name }}</text>
|
||||
<text class="pair-device-summary__serial">设备编号:{{ device.serialMasked }}</text>
|
||||
</view>
|
||||
<view class="pair-device-summary__status">
|
||||
<view
|
||||
class="pair-device-summary__status"
|
||||
:class="`pair-device-summary__status--${connectStatus}`"
|
||||
>
|
||||
<view class="pair-device-summary__status-dot" />
|
||||
<text class="pair-device-summary__status-text">连接中</text>
|
||||
<text class="pair-device-summary__status-text">{{ statusText }}</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { staticAssets } from '@/utils/static-assets'
|
||||
import type { PairDeviceInfo } from '../../pair.types'
|
||||
import type { PairConnectStatus, PairDeviceInfo } from '../../pair.types'
|
||||
|
||||
withDefaults(
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
device: PairDeviceInfo
|
||||
connectStatus?: PairConnectStatus
|
||||
thumbSrc?: string
|
||||
}>(),
|
||||
{ thumbSrc: staticAssets.logo },
|
||||
{
|
||||
connectStatus: 'connecting',
|
||||
thumbSrc: staticAssets.logo,
|
||||
},
|
||||
)
|
||||
|
||||
const statusText = computed(() => {
|
||||
switch (props.connectStatus) {
|
||||
case 'success':
|
||||
return '已连接'
|
||||
case 'failed':
|
||||
return '未连接'
|
||||
default:
|
||||
return '连接中'
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
@@ -69,22 +88,55 @@ withDefaults(
|
||||
align-items: center;
|
||||
gap: 8rpx;
|
||||
padding: 10rpx 16rpx;
|
||||
background: #effbf4;
|
||||
border-radius: 999rpx;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--connecting {
|
||||
background: #fff4e8;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--success {
|
||||
background: #effbf4;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--failed {
|
||||
background: #fff5f5;
|
||||
}
|
||||
|
||||
.pair-device-summary__status-dot {
|
||||
width: 12rpx;
|
||||
height: 12rpx;
|
||||
border-radius: 50%;
|
||||
border: 3rpx solid $color-primary;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--connecting .pair-device-summary__status-dot {
|
||||
border: 3rpx solid #e67a2e;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--success .pair-device-summary__status-dot {
|
||||
background: $color-primary;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--failed .pair-device-summary__status-dot {
|
||||
background: #e54d4d;
|
||||
}
|
||||
|
||||
.pair-device-summary__status-text {
|
||||
font-size: 24rpx;
|
||||
color: $color-primary;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--connecting .pair-device-summary__status-text {
|
||||
color: #e67a2e;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--success .pair-device-summary__status-text {
|
||||
color: $color-primary;
|
||||
}
|
||||
|
||||
.pair-device-summary__status--failed .pair-device-summary__status-text {
|
||||
color: #e54d4d;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -6,13 +6,15 @@
|
||||
<bind-steps :steps="BIND_STEPS" :current="currentStep" />
|
||||
<pair-hero />
|
||||
<view class="pair-page__status">
|
||||
<text class="pair-page__title">配对中...</text>
|
||||
<text class="pair-page__desc">正在与设备建立安全连接,请保持手机靠近</text>
|
||||
<text class="pair-page__title">{{ pageTitle }}</text>
|
||||
<text class="pair-page__desc">{{ pageDesc }}</text>
|
||||
</view>
|
||||
|
||||
<view class="pair-page__progress-block">
|
||||
<view v-if="connectStatus !== 'failed'" class="pair-page__progress-block">
|
||||
<view class="pair-page__progress-head">
|
||||
<text class="pair-page__progress-label">正在同步设备配置...</text>
|
||||
<text class="pair-page__progress-label">
|
||||
{{ connectStatus === 'success' ? '蓝牙连接成功' : '正在建立蓝牙连接...' }}
|
||||
</text>
|
||||
<text class="pair-page__progress-percent">{{ progress }}%</text>
|
||||
</view>
|
||||
<view class="pair-page__progress-track">
|
||||
@@ -21,45 +23,101 @@
|
||||
:style="{ width: `${progress}%` }"
|
||||
/>
|
||||
</view>
|
||||
<text class="pair-page__progress-tip">
|
||||
{{ progress >= 100 ? '配置同步完成' : `预计还需约 ${etaSeconds} 秒,请勿离开当前页面` }}
|
||||
</text>
|
||||
<text class="pair-page__progress-tip">{{ progressTip }}</text>
|
||||
</view>
|
||||
|
||||
<view v-else class="pair-page__error-block">
|
||||
<text class="pair-page__error-text">{{ errorMessage }}</text>
|
||||
</view>
|
||||
|
||||
<pair-check-list :items="checkItems" />
|
||||
<pair-device-summary :device="device" />
|
||||
<pair-device-summary :device="device" :connect-status="connectStatus" />
|
||||
</view>
|
||||
</scroll-view>
|
||||
<view class="pair-page__footer">
|
||||
<view class="pair-page__cancel" @tap="onCancelPair">
|
||||
<text class="pair-page__cancel-text">取消配对</text>
|
||||
<view
|
||||
v-if="connectStatus === 'failed'"
|
||||
class="pair-page__retry"
|
||||
@tap="onRetryPair"
|
||||
>
|
||||
<text class="pair-page__retry-text">重新连接</text>
|
||||
</view>
|
||||
<view class="pair-page__cancel" @tap="onFooterCancel">
|
||||
<text class="pair-page__cancel-text">
|
||||
{{ connectStatus === 'failed' ? '返回' : '取消配对' }}
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { onLoad } from '@dcloudio/uni-app'
|
||||
import BindSteps from '../components/bind-steps/index.vue'
|
||||
import { BIND_STEPS } from '../use-add-device'
|
||||
import { readBindDeviceContext } from '../add-device.types'
|
||||
import PairCheckList from './components/pair-check-list/index.vue'
|
||||
import PairDeviceSummary from './components/pair-device-summary/index.vue'
|
||||
import PairHero from './components/pair-hero/index.vue'
|
||||
import { readBindDeviceContext } from '../add-device.types'
|
||||
import { usePairDevice } from './use-pair-device'
|
||||
|
||||
const { currentStep, device, progress, etaSeconds, checkItems, onCancelPair } =
|
||||
usePairDevice()
|
||||
const {
|
||||
currentStep,
|
||||
device,
|
||||
connectStatus,
|
||||
progress,
|
||||
errorMessage,
|
||||
checkItems,
|
||||
applyDevice,
|
||||
startPairing,
|
||||
onCancelPair,
|
||||
onRetryPair,
|
||||
} = usePairDevice()
|
||||
|
||||
const pageTitle = computed(() => {
|
||||
if (connectStatus.value === 'failed') return '连接失败'
|
||||
if (connectStatus.value === 'success') return '连接成功'
|
||||
return '配对中...'
|
||||
})
|
||||
|
||||
const pageDesc = computed(() => {
|
||||
if (connectStatus.value === 'failed') {
|
||||
return '请确认设备已开机、手机蓝牙已开启,并靠近设备后重试'
|
||||
}
|
||||
if (connectStatus.value === 'success') {
|
||||
return '蓝牙连接已建立,即将进入下一步'
|
||||
}
|
||||
return '正在与设备建立安全连接,请保持手机靠近'
|
||||
})
|
||||
|
||||
const progressTip = computed(() => {
|
||||
if (connectStatus.value === 'success') return '连接完成,正在跳转...'
|
||||
if (progress.value >= 70) return '正在连接设备,请稍候...'
|
||||
if (progress.value >= 30) return '正在搜索附近设备...'
|
||||
return '正在初始化蓝牙,请勿离开当前页面'
|
||||
})
|
||||
|
||||
function onFooterCancel() {
|
||||
if (connectStatus.value === 'failed') {
|
||||
uni.navigateBack()
|
||||
return
|
||||
}
|
||||
onCancelPair()
|
||||
}
|
||||
|
||||
onLoad((query) => {
|
||||
const ctx = readBindDeviceContext(query ?? {})
|
||||
if (ctx.name) device.value.name = ctx.name
|
||||
if (ctx.serialMasked) device.value.serialMasked = ctx.serialMasked
|
||||
if (ctx.deviceNo) device.value.deviceNo = ctx.deviceNo
|
||||
if (ctx.productId != null) device.value.productId = ctx.productId
|
||||
if (ctx.model) device.value.model = ctx.model
|
||||
if (ctx.imei) device.value.imei = ctx.imei
|
||||
if (ctx.mac) device.value.mac = ctx.mac
|
||||
applyDevice({
|
||||
name: ctx.name ?? '',
|
||||
serialMasked: ctx.serialMasked ?? '',
|
||||
deviceNo: ctx.deviceNo ?? '',
|
||||
productId: ctx.productId,
|
||||
model: ctx.model,
|
||||
imei: ctx.imei,
|
||||
mac: ctx.mac,
|
||||
})
|
||||
void startPairing()
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -87,6 +87,36 @@
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.pair-page__error-block {
|
||||
margin: 0 32rpx 24rpx;
|
||||
padding: 24rpx;
|
||||
background: #fff5f5;
|
||||
border-radius: 20rpx;
|
||||
border: 1rpx solid #ffd6d6;
|
||||
}
|
||||
|
||||
.pair-page__error-text {
|
||||
font-size: 26rpx;
|
||||
color: #e54d4d;
|
||||
line-height: 1.65;
|
||||
}
|
||||
|
||||
.pair-page__retry {
|
||||
margin-bottom: 16rpx;
|
||||
height: 88rpx;
|
||||
border-radius: 44rpx;
|
||||
background: $color-primary;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.pair-page__retry-text {
|
||||
font-size: 30rpx;
|
||||
font-weight: 600;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.pair-page__footer {
|
||||
padding: 16rpx 32rpx calc(24rpx + env(safe-area-inset-bottom));
|
||||
background: $color-bg-page;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export type PairCheckStatus = 'done' | 'syncing' | 'pending'
|
||||
export type PairCheckStatus = 'done' | 'syncing' | 'pending' | 'failed'
|
||||
|
||||
export type PairConnectStatus = 'connecting' | 'success' | 'failed'
|
||||
|
||||
export interface PairCheckItem {
|
||||
id: string
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { onUnmounted, ref, watch } from 'vue'
|
||||
import { onUnmounted, ref } from 'vue'
|
||||
import {
|
||||
ensureBleConnection,
|
||||
ensureBluetoothReady,
|
||||
getBluetoothErrorMessage,
|
||||
isBluetoothApiSupported,
|
||||
scanForBleDeviceByMac,
|
||||
} from '@/utils/bluetooth'
|
||||
import { queryZ2BleState } from '@/utils/z2-ble'
|
||||
import { buildBindDeviceQuery } from '../add-device.types'
|
||||
import type { PairCheckItem, PairDeviceInfo } from './pair.types'
|
||||
import type { PairCheckItem, PairConnectStatus, PairDeviceInfo } from './pair.types'
|
||||
|
||||
const DEFAULT_DEVICE: PairDeviceInfo = {
|
||||
name: '',
|
||||
@@ -8,61 +16,136 @@ const DEFAULT_DEVICE: PairDeviceInfo = {
|
||||
deviceNo: '',
|
||||
}
|
||||
|
||||
export function usePairDevice(initial?: Partial<PairDeviceInfo>) {
|
||||
const currentStep = 3
|
||||
const device = ref<PairDeviceInfo>({
|
||||
...DEFAULT_DEVICE,
|
||||
...initial,
|
||||
})
|
||||
const SCAN_TIMEOUT_MS = 15000
|
||||
|
||||
const progress = ref(65)
|
||||
const etaSeconds = ref(15)
|
||||
export function usePairDevice() {
|
||||
const currentStep = 3
|
||||
const device = ref<PairDeviceInfo>({ ...DEFAULT_DEVICE })
|
||||
const connectStatus = ref<PairConnectStatus>('connecting')
|
||||
const progress = ref(0)
|
||||
const errorMessage = ref('')
|
||||
const checkItems = ref<PairCheckItem[]>([
|
||||
{ id: 'signal', title: '蓝牙信号强度', status: 'done', tag: '良好' },
|
||||
{ id: 'credential', title: '设备凭证校验', status: 'done', tag: '通过' },
|
||||
{ id: 'network', title: '网络配置下发', status: 'syncing', tag: '同步中' },
|
||||
{ id: 'signal', title: '蓝牙信号强度', status: 'pending', tag: '待检测' },
|
||||
{ id: 'credential', title: '设备凭证校验', status: 'pending', tag: '待校验' },
|
||||
{ id: 'network', title: '蓝牙连接建立', status: 'pending', tag: '待连接' },
|
||||
])
|
||||
|
||||
let etaTick = 0
|
||||
const tick = setInterval(() => {
|
||||
if (progress.value < 100) {
|
||||
progress.value = Math.min(100, progress.value + 1)
|
||||
}
|
||||
etaTick += 1
|
||||
if (etaTick % 2 === 0 && etaSeconds.value > 0 && progress.value < 100) {
|
||||
etaSeconds.value = Math.max(0, etaSeconds.value - 1)
|
||||
}
|
||||
if (progress.value >= 100) {
|
||||
checkItems.value = checkItems.value.map((item) =>
|
||||
item.id === 'network'
|
||||
? { ...item, status: 'done', tag: '完成' }
|
||||
: item,
|
||||
)
|
||||
}
|
||||
}, 500)
|
||||
let pairing = false
|
||||
let bleDeviceId: string | undefined
|
||||
|
||||
let navigated = false
|
||||
function updateCheck(id: string, status: PairCheckItem['status'], tag: string) {
|
||||
checkItems.value = checkItems.value.map((item) =>
|
||||
item.id === id ? { ...item, status, tag } : item,
|
||||
)
|
||||
}
|
||||
|
||||
watch(progress, (value) => {
|
||||
if (value < 100 || navigated) return
|
||||
navigated = true
|
||||
const d = device.value
|
||||
const q = buildBindDeviceQuery({
|
||||
deviceNo: d.deviceNo,
|
||||
name: d.name,
|
||||
serialMasked: d.serialMasked,
|
||||
productId: d.productId,
|
||||
model: d.model,
|
||||
imei: d.imei,
|
||||
mac: d.mac,
|
||||
function markFailedFromStep(stepId: string, message: string) {
|
||||
const order = ['signal', 'credential', 'network']
|
||||
const failedIndex = order.indexOf(stepId)
|
||||
checkItems.value = checkItems.value.map((item) => {
|
||||
const idx = order.indexOf(item.id)
|
||||
if (idx < failedIndex && item.status === 'done') return item
|
||||
if (item.id === stepId) return { ...item, status: 'failed', tag: '失败' }
|
||||
if (idx > failedIndex) return { ...item, status: 'pending', tag: '未开始' }
|
||||
return item
|
||||
})
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({ url: `/pages/add-device/complete/index?${q}` })
|
||||
}, 600)
|
||||
})
|
||||
connectStatus.value = 'failed'
|
||||
errorMessage.value = message
|
||||
progress.value = Math.max(progress.value, 30)
|
||||
}
|
||||
|
||||
async function startPairing() {
|
||||
if (pairing) return
|
||||
pairing = true
|
||||
connectStatus.value = 'connecting'
|
||||
errorMessage.value = ''
|
||||
progress.value = 5
|
||||
|
||||
checkItems.value = [
|
||||
{ id: 'signal', title: '蓝牙信号强度', status: 'syncing', tag: '检测中' },
|
||||
{ id: 'credential', title: '设备凭证校验', status: 'pending', tag: '待校验' },
|
||||
{ id: 'network', title: '蓝牙连接建立', status: 'pending', tag: '待连接' },
|
||||
]
|
||||
|
||||
try {
|
||||
if (!isBluetoothApiSupported()) {
|
||||
throw new Error('当前环境不支持蓝牙,请使用 App 或微信小程序')
|
||||
}
|
||||
|
||||
await ensureBluetoothReady()
|
||||
updateCheck('signal', 'done', '良好')
|
||||
progress.value = 30
|
||||
|
||||
const mac = device.value.mac?.trim()
|
||||
const name = device.value.name?.trim()
|
||||
if (!mac && !name) {
|
||||
throw new Error('缺少设备 MAC 或名称,无法建立蓝牙连接')
|
||||
}
|
||||
|
||||
updateCheck('credential', 'syncing', '扫描中')
|
||||
progress.value = 45
|
||||
|
||||
const found = await scanForBleDeviceByMac(mac ?? '', {
|
||||
timeoutMs: SCAN_TIMEOUT_MS,
|
||||
name,
|
||||
})
|
||||
if (!found) {
|
||||
markFailedFromStep('credential', '未找到设备,请确认已开机并靠近手机')
|
||||
return
|
||||
}
|
||||
|
||||
updateCheck('credential', 'done', '通过')
|
||||
progress.value = 70
|
||||
updateCheck('network', 'syncing', '连接中')
|
||||
|
||||
await ensureBleConnection(found.deviceId)
|
||||
bleDeviceId = found.deviceId
|
||||
|
||||
try {
|
||||
await queryZ2BleState(found.deviceId)
|
||||
} catch {
|
||||
// 连接成功即可继续
|
||||
}
|
||||
|
||||
updateCheck('network', 'done', '已连接')
|
||||
progress.value = 100
|
||||
connectStatus.value = 'success'
|
||||
|
||||
const d = device.value
|
||||
const q = buildBindDeviceQuery({
|
||||
deviceNo: d.deviceNo,
|
||||
name: d.name,
|
||||
serialMasked: d.serialMasked,
|
||||
productId: d.productId,
|
||||
model: d.model,
|
||||
imei: d.imei,
|
||||
mac: d.mac,
|
||||
bluetoothConnected: '1',
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({ url: `/pages/add-device/complete/index?${q}` })
|
||||
}, 500)
|
||||
} catch (err) {
|
||||
const message = getBluetoothErrorMessage(err)
|
||||
const failedStep = progress.value < 30
|
||||
? 'signal'
|
||||
: progress.value < 70
|
||||
? 'credential'
|
||||
: 'network'
|
||||
markFailedFromStep(failedStep, message)
|
||||
uni.showToast({ title: message, icon: 'none' })
|
||||
} finally {
|
||||
pairing = false
|
||||
}
|
||||
}
|
||||
|
||||
function applyDevice(partial?: Partial<PairDeviceInfo>) {
|
||||
device.value = { ...device.value, ...partial }
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
clearInterval(tick)
|
||||
bleDeviceId = undefined
|
||||
})
|
||||
|
||||
function onCancelPair() {
|
||||
@@ -77,12 +160,20 @@ export function usePairDevice(initial?: Partial<PairDeviceInfo>) {
|
||||
})
|
||||
}
|
||||
|
||||
function onRetryPair() {
|
||||
void startPairing()
|
||||
}
|
||||
|
||||
return {
|
||||
currentStep,
|
||||
device,
|
||||
connectStatus,
|
||||
progress,
|
||||
etaSeconds,
|
||||
errorMessage,
|
||||
checkItems,
|
||||
applyDevice,
|
||||
startPairing,
|
||||
onCancelPair,
|
||||
onRetryPair,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user