bike-ali/pages_admin/worke/bind_mac.vue

745 lines
15 KiB
Vue
Raw Normal View History

2024-06-22 18:06:23 +08:00
<template>
<view class="page">
<u-navbar title="添加设备" :border-bottom="false" :background="bgc" title-color='#262B37' title-size='38'
height='50'></u-navbar>
2024-08-01 10:03:54 +08:00
<view class="iptbox" @click="backpage()">
<input type="text" class="ips" v-model="mac" placeholder=" " style="margin-left: 32rpx;" disabled
placeholder-class="my-placeholder" />
<view class="iconfont icon-xiangyou1 ">
2024-06-22 18:06:23 +08:00
</view>
2024-08-01 10:03:54 +08:00
</view>
2024-06-22 18:06:23 +08:00
<view class="iptbox">
<view class="qrcode" @click="qrcode()">
2024-12-17 10:20:53 +08:00
<image src="https://api.ccttiot.com/smartmeter/img/static/uy7BNwAMIKwvstqFnRhs" mode="aspectFit">
</image>
2024-06-22 18:06:23 +08:00
</view>
<input type="text" class="ips" v-model="sn" placeholder="请扫描设备上的二维码" style="margin-left: 32rpx;"
placeholder-class="my-placeholder" />
2024-08-01 10:03:54 +08:00
2024-07-29 18:04:42 +08:00
</view>
2024-12-17 10:20:53 +08:00
<view class="iptbox" @click="show = true">
2024-08-01 10:03:54 +08:00
<view class="" v-if="hardwareInfo.version">
2024-12-17 10:20:53 +08:00
版本{{ hardwareInfo.version }} 版本id:{{ hardwareInfo.id }}
2024-08-01 10:03:54 +08:00
</view>
<view class="" v-else>
请点击选择版本
2024-07-29 18:04:42 +08:00
</view>
</view>
2024-08-01 10:03:54 +08:00
<view class="tips" v-if="hardwareInfo.version">
2024-12-17 10:20:53 +08:00
版本介绍{{ hardwareInfo.instructions }}
2024-06-22 18:06:23 +08:00
</view>
2024-08-01 10:03:54 +08:00
<u-select v-model="show" :list="list" title='选择运营区' @confirm="confirm"></u-select>
2024-12-17 10:20:53 +08:00
2024-09-18 18:01:41 +08:00
<view class="imgs" style="margin-top: 330rpx;">
2024-12-17 10:20:53 +08:00
<image src="https://lxnapi.ccttiot.com/bike/img/static/u9wFJhLZGm7rboSDySV4" mode="aspectFit"
@click="qrcode()"></image>
2024-09-18 18:01:41 +08:00
</view>
2024-06-22 18:06:23 +08:00
<view class="btn" @click="bind()">
确认添加
</view>
2024-08-01 10:03:54 +08:00
<view class="bot_btn">
<view class="btn1" @click="Binddevice()">
2024-06-22 18:06:23 +08:00
蓝牙链接
</view>
2024-08-01 10:03:54 +08:00
<view class="btn1" @click="ring()" v-if="carstause">
2024-06-22 18:06:23 +08:00
响铃寻车
</view>
2024-08-01 10:03:54 +08:00
<view class="btn1" @click="open()" v-if="carstause">
2024-06-22 18:06:23 +08:00
蓝牙开锁
</view>
2024-08-01 10:03:54 +08:00
<view class="btn1" @click="close()" v-if="carstause">
2024-06-22 18:06:23 +08:00
蓝牙关锁
</view>
2024-08-01 10:03:54 +08:00
<view class="btn1" @click="reboot()" v-if="carstause">
2024-06-22 18:06:23 +08:00
设备重启
</view>
</view>
2024-09-09 18:02:17 +08:00
<u-mask :show="showbind" :z-index='100' />
<view class="tip_box" v-if="showbind">
<view class="top" v-if="showbind">
<view class="tip">
操作提示
</view>
2024-12-17 10:20:53 +08:00
2024-09-09 18:02:17 +08:00
<view class="ipt_box" style="justify-content: center;">
<view class="text" style="width: 80%;text-align: center;">
该mac号已有绑定sn码是否修改为当前sn码
</view>
2024-12-17 10:20:53 +08:00
2024-09-09 18:02:17 +08:00
</view>
2024-12-17 10:20:53 +08:00
2024-09-09 18:02:17 +08:00
</view>
<view class="bots">
2024-12-17 10:20:53 +08:00
<view class="bot_left" @click="showbind = false">
2024-09-09 18:02:17 +08:00
取消
</view>
<view class="bot_right" @click="changeBind()">
确定
</view>
</view>
</view>
2024-06-22 18:06:23 +08:00
<!-- <view class="bot">
<view class="btn">
退款
</view>
</view> -->
</view>
</template>
<script>
2024-12-17 14:17:42 +08:00
import xBlufi from '@/utils/blufi/xBlufi.js'
2024-12-17 10:20:53 +08:00
export default {
data() {
return {
2024-12-17 14:17:42 +08:00
// 蓝牙相关数据
devices: [],
isSearching: false,
isConnecting: false,
bluetoothStatus: '未初始化',
selectedDevice: null,
hasConnectedDevice: false,
serviceId: '000000FF-0000-1000-8000-00805F9B34FB',
characteristicId: '0000FF01-0000-1000-8000-00805F9B34FB',
// 原有的业务数据
2024-12-17 10:20:53 +08:00
mac: '',
sn: '',
2024-12-17 14:17:42 +08:00
carstause: false,
2024-12-17 10:20:53 +08:00
show: false,
list: [],
hardwareInfo: {},
showbind: false,
2024-12-17 14:17:42 +08:00
bgc: {
backgroundColor: "#F7FAFE",
}
2024-12-17 10:20:53 +08:00
}
},
2024-12-17 14:17:42 +08:00
2024-12-17 10:20:53 +08:00
onLoad(e) {
this.mac = e.mac
2024-12-17 14:17:42 +08:00
this.initBluetooth()
this.getlist()
// 添加自动连接
if (this.mac) {
setTimeout(() => {
this.Binddevice()
}, 1000) // 延迟1秒执行确保蓝牙初始化完成
}
2024-12-17 10:20:53 +08:00
// 添加蓝牙状态监听
my.onBluetoothAdapterStateChange((res) => {
console.log('蓝牙适配器状态:', res);
if (!res.available) {
2024-12-17 14:17:42 +08:00
this.isSearching = false;
2024-12-17 10:20:53 +08:00
uni.showToast({
title: '蓝牙已关闭',
icon: 'none'
});
2024-06-22 18:06:23 +08:00
}
2024-12-17 10:20:53 +08:00
});
2024-12-17 14:17:42 +08:00
my.onBLEConnectionStateChanged((res) => {
console.log('蓝牙连接状态变化:', res);
if (!res.connected) {
this.carstause = false;
this.hasConnectedDevice = false;
} else {
this.carstause = true;
this.hasConnectedDevice = true;
}
2024-12-17 10:20:53 +08:00
});
},
2024-12-17 14:17:42 +08:00
onUnload() {
this.cleanupBluetooth()
2024-12-17 10:20:53 +08:00
},
2024-12-17 14:17:42 +08:00
onHide() {
this.cleanupBluetooth()
2024-12-17 10:20:53 +08:00
},
2024-12-17 14:17:42 +08:00
methods: {
initBluetooth() {
xBlufi.initXBlufi(xBlufi.XMQTT_SYSTEM.Alis);
xBlufi.listenDeviceMsgEvent(true, (res) => {
console.log('设备消息:', res);
switch (res.type) {
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
if (res.result && res.data) {
console.log("搜索到设备:", res.data);
const bbleDevices = res.data.filter(device => {
const name = device.name || device.deviceName || '';
return name.startsWith('BBLE');
});
this.devices = bbleDevices;
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
if (res.result) {
this.carstause = true;
this.hasConnectedDevice = true;
uni.showToast({
title: '连接成功',
icon: 'success',
duration: 1000
});
} else {
this.carstause = false;
this.hasConnectedDevice = false;
uni.showToast({
title: '连接断开',
icon: 'none',
duration: 1000
});
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_SEND_CUSTOM_DATA_RESULT:
console.log('发送自定义数据结果:', res);
if (res.result) {
uni.showToast({
title: '命令发送成功',
icon: 'success',
duration: 1000
});
} else {
uni.showToast({
title: '命令发送失败',
icon: 'none',
duration: 1000
});
}
break;
}
});
},
2024-12-17 10:20:53 +08:00
2024-12-17 14:17:42 +08:00
// 蓝牙连接方法
Binddevice() {
uni.showLoading({
title: '连接中..'
});
2024-12-17 10:20:53 +08:00
2024-12-17 14:17:42 +08:00
this.devices = [];
this.isSearching = true;
2024-12-17 10:20:53 +08:00
2024-12-17 14:17:42 +08:00
xBlufi.notifyStartDiscoverBle({
'isStart': true
});
2024-12-17 10:20:53 +08:00
2024-12-17 14:17:42 +08:00
setTimeout(() => {
xBlufi.notifyStartDiscoverBle({
'isStart': false
});
setTimeout(() => {
uni.hideLoading();
if (this.devices.length == 0) {
uni.showToast({
title: '暂无发现对应设备,请靠近设备',
icon: 'none'
});
} else {
const matchedDevice = this.devices.find(device => {
const macFromName = device.name.substring(device.name.length - 12);
return macFromName === this.mac;
});
if (matchedDevice) {
console.log('找到匹配设备:', matchedDevice);
this.selectedDevice = matchedDevice;
// 连接设备
xBlufi.notifyConnectBle({
connect: true,
deviceId: matchedDevice.deviceId
});
// 初始化设备
xBlufi.notifyInitBleEsp32({
deviceId: matchedDevice.deviceId
});
} else {
console.log('未找到匹配设备');
uni.showToast({
title: '未找到对应设备',
icon: 'none'
});
}
}
}, 200);
}, 2000);
},
// 响铃寻车
ring() {
if (!this.hasConnectedDevice || !this.selectedDevice) {
uni.showToast({
title: '请先连接设备',
icon: 'none'
});
return;
}
my.getBLEDeviceServices({
deviceId: this.selectedDevice.deviceId,
success: (res) => {
console.log('设备的所有服务:', res.services);
xBlufi.notifySendCustomData({
customData: "11play1@"
});
},
fail: (error) => {
console.error('获取服务列表失败:', error);
uni.showToast({
title: '获取服务失败',
icon: 'none'
});
2024-12-17 10:20:53 +08:00
}
2024-12-17 14:17:42 +08:00
});
},
// 蓝牙开锁
open() {
if (!this.hasConnectedDevice || !this.selectedDevice) {
uni.showToast({
title: '请先连接设备',
icon: 'none'
});
return;
2024-12-17 10:20:53 +08:00
}
2024-12-17 14:17:42 +08:00
xBlufi.notifySendCustomData({
customData: "11open"
});
2024-06-22 18:06:23 +08:00
},
2024-12-17 14:17:42 +08:00
// 蓝牙关锁
close() {
if (!this.hasConnectedDevice || !this.selectedDevice) {
uni.showToast({
title: '请先连接设备',
icon: 'none'
});
return;
}
xBlufi.notifySendCustomData({
customData: "11close"
});
},
// 设备重启
reboot() {
if (!this.hasConnectedDevice || !this.selectedDevice) {
uni.showToast({
title: '请先连接设备',
icon: 'none'
});
return;
}
xBlufi.notifySendCustomData({
customData: "11reboot"
});
},
// 获取硬件版本列表
2024-12-17 10:20:53 +08:00
getlist() {
this.$u.get('/app/hardwareVersion/list').then((res) => {
if (res.code === 200) {
this.Versionlist = res.data
const hardwareVersionId = uni.getStorageSync('hardwareVersionId');
if (hardwareVersionId) {
this.hardwareVersionId = hardwareVersionId
2024-12-17 14:17:42 +08:00
const matchedVersion = this.Versionlist.find(version => version.id === hardwareVersionId);
2024-12-17 10:20:53 +08:00
if (matchedVersion) {
this.hardwareInfo = matchedVersion;
}
}
this.list = res.data.map(item => ({
value: item.id,
label: item.version
}));
} else {
uni.showToast({
title: res.msg,
icon: 'none',
duration: 2000
});
}
})
2024-07-29 18:04:42 +08:00
},
2024-12-17 10:20:53 +08:00
2024-12-17 14:17:42 +08:00
// 版本选择确认
confirm(e) {
uni.setStorageSync('hardwareVersionId', e[0].value);
this.hardwareVersionId = e[0].value
if (this.hardwareVersionId) {
const matchedVersion = this.Versionlist.find(version => version.id === this.hardwareVersionId);
if (matchedVersion) {
this.hardwareInfo = matchedVersion;
}
}
},
// 绑定设备
bind() {
if (!this.sn || this.sn.length !== 7 || !/^\d+$/.test(this.sn)) {
uni.showToast({
title: '请输入正确的SN码',
icon: 'none'
});
return;
}
this.$u.post(`/appVerify/band?sn=${this.sn}&mac=${this.mac}&hardwareVersionId=${this.hardwareVersionId}`).then((res) => {
if (res.code === 200) {
2024-12-17 10:20:53 +08:00
uni.showToast({
2024-12-17 14:17:42 +08:00
title: '绑定成功',
2024-12-17 10:20:53 +08:00
icon: 'none',
duration: 2000
});
2024-12-17 14:17:42 +08:00
setTimeout(() => {
uni.navigateTo({
url: '/pages_admin/controlDevice?sn=' + this.sn
})
}, 2000)
} else {
if (res.msg === '该MAC号已经存在') {
this.showbind = true;
} else {
uni.showToast({
title: res.msg,
icon: 'none',
duration: 2000
});
}
2024-12-17 10:20:53 +08:00
}
})
2024-06-22 18:06:23 +08:00
},
2024-12-17 14:17:42 +08:00
// 修改绑定
2024-12-17 10:20:53 +08:00
changeBind() {
2024-12-17 14:17:42 +08:00
this.$u.post(`/appVerify/updateSn?sn=${this.sn}&mac=${this.mac}&hardwareVersionId=${this.hardwareVersionId}`).then((res) => {
2024-12-17 10:20:53 +08:00
if (res.code === 200) {
uni.showToast({
title: '绑定成功',
icon: 'none',
duration: 2000
});
setTimeout(() => {
uni.navigateTo({
url: '/pages_admin/controlDevice?sn=' + this.sn
})
}, 2000)
} else {
uni.showToast({
title: res.msg,
icon: 'none',
duration: 2000
});
}
})
2024-06-22 18:06:23 +08:00
},
2024-08-01 10:03:54 +08:00
2024-12-17 14:17:42 +08:00
// 扫描二维码
2024-12-17 10:20:53 +08:00
qrcode() {
uni.scanCode({
onlyFromCamera: true,
scanType: ['qrCode'],
success: res => {
let sn = null;
let queryParams = res.result.split('?')[1];
if (queryParams) {
let params = queryParams.split('&');
params.forEach(param => {
let [key, value] = param.split('=');
if (key === 'sn') {
sn = value;
}
2024-06-22 18:06:23 +08:00
});
}
2024-08-01 10:03:54 +08:00
2024-12-17 14:17:42 +08:00
this.sn = sn;
if (!this.sn) {
uni.showToast({
title: '无效的二维码',
icon: 'none'
});
2024-12-17 10:20:53 +08:00
}
},
fail: err => {
console.error('扫描失败:', err);
uni.showToast({
title: '扫描失败',
icon: 'none'
2024-06-22 18:06:23 +08:00
});
2024-12-17 10:20:53 +08:00
}
});
},
2024-08-01 10:03:54 +08:00
2024-12-17 14:17:42 +08:00
// 返回上一页
backpage() {
uni.navigateBack({
delta: 1
2024-12-17 10:20:53 +08:00
});
},
2024-08-01 10:03:54 +08:00
2024-12-17 14:17:42 +08:00
// 生命周期钩子和清理方法
2024-08-01 10:03:54 +08:00
2024-12-17 14:17:42 +08:00
// 清理蓝牙相关资源
cleanupBluetooth() {
xBlufi.listenDeviceMsgEvent(false);
2024-12-17 10:20:53 +08:00
xBlufi.notifyStartDiscoverBle({
'isStart': false
});
2024-08-01 10:03:54 +08:00
2024-12-17 14:17:42 +08:00
if (this.hasConnectedDevice && this.selectedDevice) {
xBlufi.notifyConnectBle({
connect: false,
deviceId: this.selectedDevice.deviceId
2024-06-22 18:06:23 +08:00
});
2024-12-17 10:20:53 +08:00
}
2024-12-17 14:17:42 +08:00
}
2024-06-22 18:06:23 +08:00
}
2024-12-17 10:20:53 +08:00
}
2024-06-22 18:06:23 +08:00
</script>
<style lang="scss">
2024-12-17 10:20:53 +08:00
page {
overflow-x: hidden;
background-color: #F3F3F3;
}
.page {
padding-bottom: 250rpx;
width: 750rpx;
.tip_box {
position: fixed;
left: 72rpx;
top: 628rpx;
width: 610rpx;
// height: 282rpx;
background: #FFFFFF;
border-radius: 30rpx 30rpx 30rpx 30rpx;
z-index: 110;
padding-bottom: 100rpx;
.top {
padding: 52rpx 38rpx 42rpx 36rpx;
.ipt_box {
margin-top: 22rpx;
2024-09-09 18:02:17 +08:00
display: flex;
flex-wrap: nowrap;
2024-12-17 10:20:53 +08:00
align-items: center;
.text {
width: 350rpx;
font-weight: 400;
2024-09-09 18:02:17 +08:00
font-size: 32rpx;
color: #3D3D3D;
}
2024-12-17 10:20:53 +08:00
.ipt {
padding: 10rpx 18rpx;
2024-09-09 18:02:17 +08:00
display: flex;
align-items: center;
2024-12-17 10:20:53 +08:00
justify-content: space-between;
margin-left: 26rpx;
width: 420rpx;
height: 64rpx;
border-radius: 0rpx 0rpx 0rpx 0rpx;
border: 2rpx solid #979797;
.input {
width: 80%;
}
2024-09-09 18:02:17 +08:00
}
}
2024-12-17 10:20:53 +08:00
.tip {
width: 100%;
text-align: center;
font-weight: 700;
font-size: 32rpx;
color: #3D3D3D;
2024-09-09 18:02:17 +08:00
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.txt {
margin-top: 32rpx;
width: 100%;
text-align: center;
font-weight: 500;
font-size: 32rpx;
color: #3D3D3D;
}
2024-07-29 18:04:42 +08:00
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.bots {
position: absolute;
width: 610rpx;
// border-top: 2rpx solid #D8D8D8;
2024-06-22 18:06:23 +08:00
display: flex;
flex-wrap: nowrap;
2024-12-17 10:20:53 +08:00
// height: 100%;
bottom: -20rpx;
.bot_left {
border-radius: 0rpx 0rpx 0rpx 30rpx;
width: 50%;
height: 86rpx;
display: flex;
align-items: center;
justify-content: center;
font-weight: 500;
font-size: 32rpx;
color: #3D3D3D;
background: #EEEEEE;
2024-06-22 18:06:23 +08:00
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.bot_right {
border-radius: 0rpx 0rpx 30rpx 0rpx;
width: 50%;
height: 86rpx;
background: #4C97E7;
display: flex;
align-items: center;
justify-content: center;
color: #FFFFFF;
// border-left: 2rpx solid #D8D8D8;
font-weight: 500;
font-size: 32rpx;
// color: #4C97E7;
2024-08-01 10:03:54 +08:00
2024-06-22 18:06:23 +08:00
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
}
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.imgs {
margin-top: 50rpx;
width: 100%;
display: flex;
justify-content: center;
image {
width: 400rpx;
height: 400rpx;
2024-06-22 18:06:23 +08:00
}
2024-12-17 10:20:53 +08:00
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.btn {
margin: 160rpx auto;
margin-top: 30rpx;
// position: fixed;
display: flex;
align-items: center;
justify-content: center;
// bottom: 100rpx;
// left: 46rpx;
width: 658rpx;
height: 88rpx;
background: #27c2fd;
border-radius: 20rpx 20rpx 20rpx 20rpx;
font-weight: 500;
font-size: 32rpx;
color: #FFFFFF;
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.tips {
margin: 28rpx auto 0;
width: 658rpx;
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.iptbox {
display: flex;
align-items: center;
flex-wrap: nowrap;
padding: 22rpx;
margin: 28rpx auto 0;
width: 658rpx;
height: 88rpx;
background: #FFFFFF;
box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0, 0, 0, 0.15);
border-radius: 20rpx 20rpx 20rpx 20rpx;
.qrcode {
padding-right: 20rpx;
border-right: 2rpx solid #D8D8D8;
image {
width: 54rpx;
height: 54rpx;
2024-06-22 18:06:23 +08:00
}
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.ips {
width: 630rpx;
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
image {
width: 18rpx;
height: 32rpx;
}
2024-08-01 10:03:54 +08:00
2024-12-17 10:20:53 +08:00
.my-placeholder {
font-weight: 400;
font-size: 32rpx;
color: #808080;
}
2024-06-22 18:06:23 +08:00
}
2024-12-17 10:20:53 +08:00
.bot_btn {
position: fixed;
bottom: 0;
display: flex;
flex-wrap: wrap;
padding: 40rpx 18rpx;
width: 750rpx;
// height: 330rpx;
background: #fff;
// background: linear-gradient( 180deg, #FFFFFF 0%, rgba(255,255,255,0) 100%);
box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
border-radius: 0rpx 0rpx 0rpx 0rpx;
.btn:nth-child(4n) {
margin-right: 0;
}
.btn1 {
margin-top: 10rpx;
margin-right: 12rpx;
display: flex;
align-items: center;
justify-content: center;
width: 164rpx;
height: 66rpx;
background: #E2F2FF;
border-radius: 0rpx 0rpx 0rpx 0rpx;
border: 2rpx solid #4C97E7;
font-weight: 400;
font-size: 28rpx;
color: #3D3D3D;
}
}
}
2024-06-22 18:06:23 +08:00
</style>