kaiguan-zfb/page_fenbao/zhuce.vue

580 lines
16 KiB
Vue
Raw Normal View History

2024-07-11 18:02:47 +08:00
<template>
<view class="pages">
2024-07-17 17:59:51 +08:00
<u-navbar title="设备录入" :border-bottom="false" :background="bgc" title-size='38' title-color='#fff'
back-icon-color="#fff" height='50'></u-navbar>
2024-07-11 18:02:47 +08:00
<view class="title">
<text>设备录入列表</text>
2024-09-30 13:47:14 +08:00
<text style="font-size: 28rpx;width: 160rpx;height: 60rpx;border-radius: 20rpx;background-color: #8883F0;text-align: center;color: #fff;line-height: 60rpx;margin-top: 20rpx;" @click="btnkzt">控制台</text>
2024-07-11 18:02:47 +08:00
</view>
2024-09-10 18:34:35 +08:00
<view class="dblist" v-for="(item, index) in devicesLists" :key="index">
2024-09-23 18:01:24 +08:00
<view class="cen" :id="item.deviceId" @click="createBLEConnections(item)">
2024-07-11 18:02:47 +08:00
<view class="name">
2024-09-12 18:02:54 +08:00
<view class="">
2024-09-15 17:39:55 +08:00
状态<text v-if="item.flags == true" style="color: #e10328;">已录入</text> <text v-else
2024-09-12 18:02:54 +08:00
style="color:seagreen;">未录入</text>
</view>
2024-09-23 18:01:24 +08:00
<view class="" style="display: flex;width: 100%;">
SN码:{{item.sn == undefined ? '--' : item.sn}}
2024-09-10 18:34:35 +08:00
</view>
2024-07-11 18:02:47 +08:00
</view>
<view class="mac">
2024-08-12 21:49:12 +08:00
MAC{{item.name.substring(5)}}
2024-07-11 18:02:47 +08:00
</view>
2024-07-22 18:03:27 +08:00
<view class="" style="color: #8883F0;margin-top: 10rpx;margin-bottom: 10rpx;text-align: left;">
蓝牙强度:{{item.RSSI}}
</view>
2024-09-23 18:01:24 +08:00
<!-- <view @click="btnshows(item,index)"
2024-09-10 18:34:35 +08:00
style="color: #8883F0;margin-top: 10rpx;margin-bottom: 10rpx;text-align: left;">
2024-07-22 18:03:27 +08:00
型号选择:{{item.xuanz == undefined ? '--' : item.xuanz}}
2024-09-23 18:01:24 +08:00
</view> -->
2024-07-11 18:02:47 +08:00
</view>
2024-09-23 18:01:24 +08:00
<view class="rt" :id="item.deviceId" @click="createBLEConnections(item)">
<!-- <text style="margin-bottom: 10rpx;" :id="item.deviceId" @click="createBLEConnections(item)">{{item.lj}}</text> -->
<text style="margin-bottom: 10rpx;">选择</text>
<!-- <text @click="scanQRCode(item)" style="margin-bottom: 10rpx;">扫码</text>
<text :id="item.deviceId" @click="createBLEConnection(item)">录入</text> -->
2024-07-11 18:02:47 +08:00
</view>
</view>
2024-11-04 18:03:49 +08:00
<view class="anniu" v-if="seachflag" @click="Search">
2024-07-11 18:02:47 +08:00
<button>重新扫描</button>
</view>
<view class="mask" v-if="titleflag">
<view class="titles">
温馨提示
</view>
2024-07-15 18:00:44 +08:00
<text>未扫描到附近未绑定设备:</text>
2024-07-11 18:02:47 +08:00
<text>1.确保待连接设备在附近且蓝牙处于打开状态</text>
<text>2.确保设备未与其他设备进行蓝牙连接</text>
2024-07-22 18:03:27 +08:00
<text>3.确保设备未被添加过</text>
2024-07-11 18:02:47 +08:00
<button @click="btnyc">好的</button>
</view>
2024-09-10 18:34:35 +08:00
<view v-if="mengcflag" class="mengc"
style="width: 100%;height: 100vh;background-color: #000;opacity: .5;position: fixed;top: 0;left: 0;"></view>
2024-07-22 18:03:27 +08:00
<!-- 选择蓝牙型号 -->
<u-select v-model="shows" :list="arr" @confirm="confirm"></u-select>
2024-07-17 17:59:51 +08:00
</view>
2024-07-11 18:02:47 +08:00
</template>
<script>
const app = getApp();
var xBlufi = require("@/page_components/blufi/xBlufi.js");
export default {
data() {
return {
2024-09-10 18:34:35 +08:00
mengcflag: false,
2024-07-11 18:02:47 +08:00
titleflag: false, //提示隐藏
bgc: {
backgroundColor: "#F7FAFE",
},
devicesList: [
// {name:'110000',
// mac:'11111'}
],
2024-09-05 18:00:53 +08:00
devicesLists: [],
2024-07-11 18:02:47 +08:00
searching: false,
2024-07-17 17:59:51 +08:00
texts: '正在扫描设备...',
2024-07-11 18:02:47 +08:00
btnflag: true,
tishiflag: false,
option: '',
bluthlist: [], //蓝牙数组
status: 'loading',
statusflag: false,
Bluetoothmac: '',
gps: {},
mac: '',
status: false,
2024-07-17 17:59:51 +08:00
storeId: '',
2024-07-11 18:02:47 +08:00
bgc: {
backgroundColor: "#8883f0",
},
2024-07-17 17:59:51 +08:00
lurulist: [],
2024-09-10 18:34:35 +08:00
arr: [],
shows: false,
arrname: '',
valuexh: '',
deviceId: '',
2024-09-12 18:02:54 +08:00
arrs: '',
sn: '输入SN',
snname: '',
qrResult:'',
name:'',
2024-11-04 18:03:49 +08:00
ver_data:null,
seachflag:true
2024-07-11 18:02:47 +08:00
}
},
onLoad(option) {
// console.log(option);
2024-07-22 18:03:27 +08:00
this.getxingh()
2024-07-11 18:02:47 +08:00
this.storeId = option.sn
},
2024-09-10 18:34:35 +08:00
onUnload: function() {
console.log("unload ");
xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
wx.closeBLEConnection({
deviceId: this.deviceId,
})
},
2024-07-17 17:59:51 +08:00
// 分享到好友(会话)
2024-09-10 18:34:35 +08:00
onShareAppMessage: function() {
return {
title: '创想物联',
path: '/pages/shouye/index'
}
},
// 分享到朋友圈
onShareTimeline: function() {
return {
title: '创想物联',
query: '',
path: '/pages/shouye/index'
}
},
2024-07-11 18:02:47 +08:00
mounted() {
2024-08-12 21:49:12 +08:00
// this.videoContext = uni.createVideoContext('myVideo', this)
2024-07-11 18:02:47 +08:00
},
methods: {
2024-09-30 13:47:14 +08:00
btnkzt(){
uni.navigateTo({
url:'/page_user/wifijoint/index'
})
},
2024-09-10 18:34:35 +08:00
// 扫码sn
2024-09-12 18:02:54 +08:00
scanQRCode(item) {
2024-09-10 18:34:35 +08:00
this.snname = item.name
uni.scanCode({
onlyFromCamera: true,
scanType: ['qrCode'],
success: res => {
function getQueryParam(url, paramName) {
let regex = new RegExp(`[?&]${paramName}=([^&]*)`)
let results = regex.exec(url);
return results ? decodeURIComponent(results[1].replace(/\+/g, ' ')) : null
}
let sceneValue = res.result
let decodedValue = decodeURIComponent(sceneValue)
let id = getQueryParam(decodedValue, 's')
2024-09-12 18:02:54 +08:00
this.storeId = id
2024-09-10 18:34:35 +08:00
this.devicesLists.forEach(item => {
if (item.name == this.snname) {
item.sn = id
}
})
},
fail: err => {
console.error('扫描失败:', err)
uni.showToast({
title: '扫描失败',
icon: 'none'
})
}
})
2024-09-12 18:02:54 +08:00
},
2024-09-10 18:34:35 +08:00
getxingh() {
2024-07-22 18:03:27 +08:00
this.$u.get(`/app/model/all`).then(res => {
2024-09-10 18:34:35 +08:00
if (res.code == 200) {
2024-07-22 18:03:27 +08:00
this.arr = res.data
2024-09-10 18:34:35 +08:00
this.arr.forEach(item => {
if (item.hasOwnProperty('modelId')) {
item.value = item.modelId;
}
if (item.hasOwnProperty('modelName')) {
item.label = item.modelName;
}
})
console.log(this.arr, '2121212')
2024-07-22 18:03:27 +08:00
}
})
},
2024-09-10 18:34:35 +08:00
btnshows(item, index) {
2024-07-22 18:03:27 +08:00
this.arrname = item.name
this.shows = true
},
// 选择型号
2024-09-10 18:34:35 +08:00
confirm(e) {
2024-07-22 18:03:27 +08:00
this.devicesLists.forEach(item => {
2024-09-10 18:34:35 +08:00
if (item.name == this.arrname) {
2024-07-22 18:03:27 +08:00
item.xuanz = e[0].label
this.valuexh = e[0].value
}
})
},
2024-07-11 18:02:47 +08:00
btnyc() {
this.titleflag = false
},
funListenDeviceMsgEvent: function(options) {
switch (options.type) {
2024-09-12 18:02:54 +08:00
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_MY_DATA:
let loadPercent = options.data;
let loadText = '文件读取中'
break;
case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA:
this.ver_data = this.parseCustomData(options.data)
console.log("1收到设备发来的自定义数据结果", this.ver_data)
break;
2024-07-11 18:02:47 +08:00
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
if (options.result) {
2024-08-12 21:49:12 +08:00
this.devicesList = options.data
2024-09-12 18:02:54 +08:00
this.devicesList.forEach(device => {
const mac = device.name.substring(5)
if (device.name == this.qrResult) {
this.deviceId = device.deviceId
this.name = device.name
this.mac = device.name.slice(5, 17)
}
})
2024-07-11 18:02:47 +08:00
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
2024-09-12 18:02:54 +08:00
// console.log("连接回调:" + JSON.stringify(options))
2024-07-11 18:02:47 +08:00
if (options.result) {
2024-09-12 18:02:54 +08:00
setTimeout(()=>{
this.bluetoothflag = false
},3000)
2024-09-15 17:39:55 +08:00
{
2024-07-11 18:02:47 +08:00
console.log("连接回调options.data.deviceId" + options.data.deviceId,
2024-09-12 18:02:54 +08:00
"连接回调options.data.name" + options.data.name)
2024-07-11 18:02:47 +08:00
}
2024-09-12 18:02:54 +08:00
} else {
this.shibaiflag = true
this.bluetoothflag = false
2024-09-15 17:39:55 +08:00
2024-07-11 18:02:47 +08:00
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
if (!options.result) {
2024-09-10 18:34:35 +08:00
this.mengcflag = false
uni.hideLoading()
2024-10-29 16:05:02 +08:00
console.log("蓝牙未开启", options)
2024-07-11 18:02:47 +08:00
uni.showToast({
title: '蓝牙未开启,请打开手机蓝牙',
icon: 'none',
duration: 3000
2024-09-15 17:39:55 +08:00
})
2024-07-11 18:02:47 +08:00
} else {
this.searching = true
}
break;
case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
if (options.result) {
let uniqueDevicesList = Array.from(new Set(this.devicesList));
2024-09-10 18:34:35 +08:00
this.devicesLists = uniqueDevicesList.map(device => {
2024-10-29 16:05:02 +08:00
const deviceId = device.name.substring(5)
2024-09-10 18:34:35 +08:00
const newDevice = {
...device,
xuanz: "请选择",
}; // 直接在复制时添加 xuanz 字段
2024-10-29 16:05:02 +08:00
return newDevice
2024-07-17 17:59:51 +08:00
})
2024-08-12 21:49:12 +08:00
let devicesarr = options.data
this.devicesList.forEach(device => {
2024-09-15 17:39:55 +08:00
if (device.name.substring(0, 4) == "CCYK") {
2024-10-29 16:05:02 +08:00
this.devicesList.push(device)
let uniqueDevicesList = Array.from(new Set(this.devicesList))
2024-08-12 21:49:12 +08:00
this.devicesLists = uniqueDevicesList.filter(device => {
2024-10-29 16:05:02 +08:00
const deviceId = device.name.substring(5)
2024-09-10 18:34:35 +08:00
return deviceId
2024-08-12 21:49:12 +08:00
})
}
})
2024-09-10 18:34:35 +08:00
// console.log('蓝牙停止搜索ok')
2024-11-09 18:02:13 +08:00
// this.mengcflag = false
2024-07-17 17:59:51 +08:00
if (this.devicesLists.length == 0) {
2024-07-15 18:00:44 +08:00
this.titleflag = true
2024-09-10 18:34:35 +08:00
} else {
2024-09-12 18:02:54 +08:00
let arr = []
2024-09-23 18:01:24 +08:00
// console.log(this.devicesLists,'101010');
2024-09-10 18:34:35 +08:00
this.devicesLists.forEach(item => {
2024-09-12 18:02:54 +08:00
arr.push(item.name.slice(5))
2024-09-10 18:34:35 +08:00
})
2024-09-12 18:02:54 +08:00
this.arrs = arr.join(',')
2024-09-23 18:01:24 +08:00
this.$u.get(`/app/device/getExistMac/${this.arrs}`).then(res => {
if (res.code == 200 && Array.isArray(res.data)) {
2024-10-29 16:05:02 +08:00
const existingDevices = new Map(res.data.map(item => [item.mac, true]))// 使用Map来存储已找到的MAC
2024-09-23 18:01:24 +08:00
this.devicesLists = this.devicesLists.map(device => {
2024-10-29 16:05:02 +08:00
const trimmedName = device.name.slice(5) // 假设name中包含MAC地址的一部分
const mac = trimmedName // 如果trimmedName直接就是MAC则不需要再处理
device.found = existingDevices.has(mac) // 添加一个found属性来标记是否找到
2024-09-23 18:01:24 +08:00
if (device.found) {
2024-10-29 16:05:02 +08:00
const sn = res.data.find(val => val.mac === mac)?.sn // 从res.data中找到匹配的sn
2024-09-23 18:01:24 +08:00
if (sn) {
2024-10-29 16:05:02 +08:00
device.sn = sn // 设置sn
2024-09-23 18:01:24 +08:00
}
2024-10-29 16:05:02 +08:00
device.flags = true // 设置flags为true因为找到了匹配的MAC
2024-09-23 18:01:24 +08:00
} else {
2024-10-29 16:05:02 +08:00
device.flags = false // 可选:如果你想要明确表示未找到
2024-09-23 18:01:24 +08:00
}
2024-10-29 16:05:02 +08:00
return device // 但map函数仍然需要return来保持结构
})
2024-09-23 18:01:24 +08:00
this.devicesLists.sort((a, b) => {
2024-10-29 16:05:02 +08:00
if (a.found && !b.found) return 1 // b排在a前面即a在后面
if (!a.found && b.found) return -1 // a排在b前面
2024-09-23 18:01:24 +08:00
return 0;
});
}
2024-09-10 18:34:35 +08:00
})
2024-09-15 17:39:55 +08:00
setTimeout(()=>{
uni.hideLoading()
},2000)
2024-07-15 18:00:44 +08:00
}
2024-07-11 18:02:47 +08:00
} else {
//蓝牙停止搜索失败
2024-09-10 18:34:35 +08:00
this.mengcflag = false
2024-09-15 17:39:55 +08:00
setTimeout(()=>{
uni.hideLoading()
},2000)
2024-10-29 16:05:02 +08:00
console.log('蓝牙停止搜索失败')
2024-07-11 18:02:47 +08:00
}
this.searching = false
2024-09-12 18:02:54 +08:00
break
}
},
// 发送开关
createBLEConnections(e) {
2024-09-23 18:01:24 +08:00
let Bluetoothmac = e.name.substring(5)
uni.navigateTo({
url:'/page_fenbao/device/joinDevice?mac='+Bluetoothmac
2024-09-15 17:39:55 +08:00
})
2024-07-17 17:59:51 +08:00
},
2024-09-12 18:02:54 +08:00
parseCustomData(data) {
const dataArray = data.split('@');
const parsedData = {};
const prefixMap = {
'V': 'voltage',
'S': 'switchState',
'A': 'current',
'P': 'power',
'M': 'remainingPower',
'T': 'setMode',
'W': 'temperature'
};
// 遍历数组并解析每个字段
for (let i = 0; i < dataArray.length; i++) {
2024-10-29 16:05:02 +08:00
const field = dataArray[i]
2024-09-12 18:02:54 +08:00
for (const prefix in prefixMap) {
if (field.startsWith(prefix)) {
2024-10-29 16:05:02 +08:00
const value = field.substring(1)
const propertyName = prefixMap[prefix]
parsedData[propertyName] = isNaN(parseFloat(value)) ? value : parseFloat(value)
break
2024-09-12 18:02:54 +08:00
}
}
}
return parsedData;
},
2024-07-11 18:02:47 +08:00
// 点击重新搜索
Search() {
2024-07-15 18:00:44 +08:00
// if (this.status) {
2024-11-09 18:02:13 +08:00
this.seachflag = false
2024-09-10 18:34:35 +08:00
this.mengcflag = true
uni.showLoading({
title: '请稍后...'
})
2024-09-15 17:39:55 +08:00
xBlufi.initXBlufi(1);
xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
2024-07-17 17:59:51 +08:00
xBlufi.notifyStartDiscoverBle({
'isStart': true
2024-09-15 17:39:55 +08:00
})
2024-07-17 17:59:51 +08:00
// 重新搜索清空蓝牙数组
this.bluthlist = []
this.devicesList = []
this.devicesLists = []
2024-09-10 18:34:35 +08:00
this.arrs = []
2024-07-17 17:59:51 +08:00
// 重新搜索
// this.startBluetoothDevicesDiscovery()
this.statusflag = true
this.texts = '正在扫描蓝牙设备...'
setTimeout(() => {
this.statusflag = false
2024-10-29 16:05:02 +08:00
uni.hideLoading()
2024-09-15 17:39:55 +08:00
// if (this.searching) {
2024-07-17 17:59:51 +08:00
xBlufi.notifyStartDiscoverBle({
'isStart': false
});
2024-11-09 18:02:13 +08:00
setTimeout(()=>{
this.seachflag = true
this.mengcflag = false
},2000)
2024-07-17 17:59:51 +08:00
}, 2000)
2024-07-15 18:00:44 +08:00
// }
2024-07-11 18:02:47 +08:00
},
// 点击隐藏没有设备提示
btnhd() {
this.tishiflag = false
}
}
}
</script>
<style lang="scss">
page {
background-color: #F7FAFE !important;
2024-08-12 21:49:12 +08:00
padding-bottom: 300rpx;
box-sizing: border-box;
2024-07-11 18:02:47 +08:00
}
.containers {
width: 100%;
height: 100vh;
position: fixed;
top: 0;
padding-top: 130rpx;
box-sizing: border-box;
left: 0;
z-index: 999 !important;
}
.pages {
2024-07-22 18:03:27 +08:00
padding: 0 32rpx;
2024-07-11 18:02:47 +08:00
box-sizing: border-box;
}
.sm {
color: #77808D;
border-radius: 0rpx 0rpx 0rpx 0rpx;
display: inline-block;
}
.title {
margin-bottom: 84rpx;
2024-07-17 17:59:51 +08:00
margin-top: 50rpx;
2024-09-30 13:47:14 +08:00
display: flex;
justify-content: space-between;
2024-07-11 18:02:47 +08:00
image {
display: inline-block;
width: 48rpx;
height: 48rpx;
vertical-align: bottom;
margin-right: 10rpx;
}
text:nth-child(1) {
font-weight: 400;
font-size: 66rpx;
color: #262B37;
line-height: 88rpx;
text-align: left;
font-style: normal;
text-transform: none;
display: block;
margin-bottom: 48rpx;
}
}
.dblist {
display: flex;
2024-09-10 18:34:35 +08:00
justify-content: space-between;
2024-07-11 18:02:47 +08:00
width: 100%;
margin-top: 34rpx;
background: #FFFFFF;
box-shadow: 0rpx 10rpx 64rpx 0rpx rgba(0, 0, 0, 0.08);
2024-09-10 18:34:35 +08:00
padding: 20rpx 20rpx 24rpx 18rpx;
2024-07-11 18:02:47 +08:00
box-sizing: border-box;
text-align: center;
border-radius: 10rpx;
.lt {
padding-left: 10rpx;
box-sizing: border-box;
2024-07-22 18:03:27 +08:00
margin-right: 18rpx;
2024-07-11 18:02:47 +08:00
image {
2024-07-22 18:03:27 +08:00
width: 120rpx;
height: 200rpx;
2024-07-11 18:02:47 +08:00
}
}
.cen {
2024-07-22 18:03:27 +08:00
width: 365rpx;
2024-07-11 18:02:47 +08:00
padding-left: 10rpx;
box-sizing: border-box;
.name {
font-family: AlibabaPuHuiTi, AlibabaPuHuiTi;
font-weight: 400;
font-size: 32rpx;
color: #262B37;
line-height: 40rpx;
text-align: left;
font-style: normal;
text-transform: none;
margin-top: 10rpx;
}
.mac {
font-family: AlibabaPuHuiTi, AlibabaPuHuiTi;
font-weight: 400;
font-size: 26rpx;
color: #262B37;
line-height: 32rpx;
text-align: left;
font-style: normal;
text-transform: none;
margin-top: 16rpx;
}
}
.rt {
2024-07-22 18:03:27 +08:00
margin-top: 32rpx;
box-sizing: border-box;
2024-07-11 18:02:47 +08:00
text {
2024-09-10 18:34:35 +08:00
display: block;
2024-07-11 18:02:47 +08:00
width: 108rpx;
height: 60rpx;
background: rgba(255, 255, 255, 0);
border: 2rpx solid #8883F0;
filter: blur(0px);
border-radius: 20rpx;
text-align: center;
line-height: 60rpx;
color: #8883F0;
}
}
}
.anniu {
padding: 0 90rpx;
width: 100%;
box-sizing: border-box;
position: fixed;
left: 0;
bottom: 112rpx;
button {
background: #8883F0;
border-radius: 52rpx 52rpx 52rpx 52rpx;
color: #fff;
}
}
.mask {
width: 622rpx;
height: 710rpx;
background: #FFFFFF;
filter: blur(0px);
border-radius: 20rpx;
position: fixed;
top: 475rpx;
left: 50%;
transform: translateX(-50%);
padding-top: 38rpx;
padding-left: 60rpx;
padding-right: 60rpx;
box-sizing: border-box;
.titles {
font-size: 48rpx;
color: #262B37;
line-height: 70rpx;
text-align: center;
margin-bottom: 24rpx;
}
text {
display: block;
font-size: 32rpx;
color: #262B37;
line-height: 56rpx;
text-align: left;
}
button {
margin-top: 46rpx;
width: 266rpx;
height: 96rpx;
background: #8883F0;
border-radius: 52rpx 52rpx 52rpx 52rpx;
color: #fff;
text-align: center;
line-height: 96rpx;
}
}
</style>