adultDeviceApp/pages/index/active.vue

415 lines
12 KiB
Vue
Raw Normal View History

2022-05-03 21:35:39 +08:00
<template>
2022-05-27 17:20:31 +08:00
<view class="content" :style="{borderTopColor:appTheme}">
2022-05-13 09:22:34 +08:00
<view class="tips">请在设备开机状态下搜索设备</view>
<view class="item" @click="handleWeight">开始搜索设备</view>
<view class="devices_summary">已发现 {{devices.length}} 个设备</view>
<view>
<scroll-view class="device_list" scroll-y scroll-with-animation v-if="popup">
<view v-for="(item,index) in devices" :key="index" @tap="createBLEConnection(item)" class="device_item">
<view>
<text>{{item.name|| item.localName}}</text>
</view>
<view>mac地址:{{item.macAddr || item.deviceId}}</view>
2022-05-03 21:35:39 +08:00
</view>
2022-05-13 09:22:34 +08:00
</scroll-view>
</view>
<view class="tishi">
<view class="text">
<icon class="t-icon t-icon-tishi"></icon> 设备激活流程说明
</view>
<view class="dv">
<text>1打开手机蓝牙和位置信息</text>
<text>2ios系统需打开设置>应用>微信里的蓝牙权限</text>
<text>3设备亮屏状态下搜索蓝牙</text>
<text>4选择蓝牙进行激活</text>
</view>
2022-05-03 21:35:39 +08:00
</view>
2022-05-27 17:20:31 +08:00
<userLogin :url="'active'" :code='code'></userLogin>
2022-05-13 09:22:34 +08:00
</view>
2022-05-03 21:35:39 +08:00
</template>
<script>
2022-05-13 09:22:34 +08:00
var myTime;
import {
mapState
} from "vuex";
import userLogin from "@/components/userLogin.vue"
export default {
data() {
return {
token: "",
macAddr: "",
code: "",
deviceId: "",
popup: false,
devices: [],
}
},
components: {
userLogin
},
computed: {
...mapState(["user", "isBluetoothTyle", "appTheme"])
},
2022-05-27 17:20:31 +08:00
async onLoad(options) {
2022-05-13 09:22:34 +08:00
let that = this
// 导航栏颜色
uni.setNavigationBarColor({
frontColor: '#ffffff',
backgroundColor: this.appTheme,
})
//
console.log("设备code码", options)
if (options) {
that.code = options.code
}
that.token = uni.getStorageSync('token')
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
2022-05-27 17:20:31 +08:00
await that.login()
2022-05-13 09:22:34 +08:00
},
methods: {
login() {
let that = this
uni.login({
success(res) {
if (res.code) {
if (res.errMsg = "login:ok") {
that.$model.onlogin({
code: res.code,
}).then(res => {
if (res.code == 2) {
uni.clearStorageSync()
uni.removeStorageSync('token')
uni.removeStorageSync('changeUser')
uni.setStorageSync('tenantid', res.data.tenantid)
uni.setStorageSync('sessionid', res.data.sessionid)
uni.setStorageSync('iswxphone', res.data.iswxphone)
2022-05-27 17:20:31 +08:00
that.$store.dispatch("getConfig", {
tenantId: uni.getStorageSync('tenantid')
})
2022-05-13 09:22:34 +08:00
} else {
2022-05-27 17:20:31 +08:00
uni.setStorageSync('userid', res.data.userid)
2022-05-13 09:22:34 +08:00
uni.setStorageSync('sessionid', res.data.sessionid)
uni.setStorageSync('token', res.data.token)
uni.setStorageSync('iswxphone', res.data.iswxphone)
uni.setStorageSync('tenantid', res.data.tenantid)
uni.setStorageSync('refreshtoken', res.data.refreshtoken)
2022-05-27 17:20:31 +08:00
that.$store.dispatch("getConfig", {
tenantId: uni.getStorageSync('tenantid')
})
2022-05-13 09:22:34 +08:00
}
}).catch(e => {})
}
}
}
})
},
handleWeight() {
let that = this
that.devices = []
if (!that.token) {
that.$store.commit("changeUserLogin", true)
return
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
uni.openBluetoothAdapter({
success: e => {
that.$store.commit("changeBluetooth", true);
that.startBluetoothDeviceDiscovery()
},
fail: e => {
console.log("openBluetoothAdapter失败", e)
return that.$tools.getBluetoothAdapter(e)
}
});
},
startBluetoothDeviceDiscovery() {
let that = this;
wx.showLoading({
title: '设备搜索中',
})
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: res => {
2022-06-11 16:21:11 +08:00
wx.hideLoading()
2022-05-13 09:22:34 +08:00
that.onBluetoothDeviceFound();
},
fail: res => {
wx.hideLoading()
console.log("startBluetoothDeviceDiscovery失败", res)
}
});
},
onBluetoothDeviceFound() {
var that = this;
const foundDevices = []
uni.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
if (!device.name && !device.localName) {
return
}
2022-06-11 16:21:11 +08:00
let buff = ""
if (device.name.indexOf("ELK") != -1 && device.advertisServiceUUIDs.length && device
.advertisServiceUUIDs[0].indexOf("F0A0") != -1) {
buff = device.advertisData.slice(0, 8)
} else if (device.name.indexOf('WSD') != -1) {
buff = device.advertisData.slice(3, 9)
} else {
buff = device.advertisData.slice(-6)
2022-05-13 09:22:34 +08:00
}
2022-06-11 16:21:11 +08:00
const foundDevices = that.devices
const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
device.mac = new Uint8Array(buff)
let tempMac = Array.from(device.mac)
tempMac.reverse()
device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
that.deviceId = device.deviceId;
if (idx === -1) {
that.devices.push(device);
} else {
that.devices[idx] = device
2022-05-13 09:22:34 +08:00
}
2022-06-11 16:21:11 +08:00
that.popup = true
// if (device.name.indexOf('AiLink_') != -1) {
// const foundDevices = that.devices
// const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
// let buff = device.advertisData.slice(-6)
// device.mac = new Uint8Array(buff) // 保存广播数据中的mac地址这是由于iOS不直接返回mac地址
// let tempMac = Array.from(device.mac)
// tempMac.reverse()
// device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
// that.deviceId = device.deviceId
// that.macAddr = device.macAddr
// if (idx === -1) {
// that.devices.push(device);
// } else {
// that.devices[idx] = device
// }
// that.popup = true
// return
// }
// if (device.name.indexOf("ELK") !== -1 && device.advertisServiceUUIDs[0].indexOf("F0A0") !== -1) {
// const foundDevices = that.devices
// const idx = that.$tools.inArray(foundDevices, "deviceId", device.deviceId)
// let buffer = device.advertisData.slice(0, 8)
// device.mac = new Uint8Array(buffer)
// let tempMac = Array.from(device.mac)
// tempMac.reverse()
// device.macAddr = that.$tools.ab2hex(tempMac, ':').toUpperCase()
// that.deviceId = device.deviceId;
// that.macAddr = device.macAddr
// console.log("device1", device.macAddr, that.deviceId)
// if (idx === -1) {
// that.devices.push(device);
// } else {
// that.devices[idx] = device
// }
// that.popup = true
// return
// }
2022-05-13 09:22:34 +08:00
})
});
that.handleMyTime()
},
handleMyTime() {
var that = this;
myTime = setTimeout(function() {
if (!that.devices.length) {
clearTimeout(myTime);
wx.hideLoading()
that.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
uni.showModal({
title: '提示',
2022-06-11 16:21:11 +08:00
content: '请确定蓝牙已打开!是否继续?',
2022-05-13 09:22:34 +08:00
cancelText: "取消",
confirmText: "继续",
success(res) {
if (res.confirm) {
that.startBluetoothDeviceDiscovery()
} else {
that.$tools.msg("您已取消操作")
}
}
})
} else {
clearTimeout(myTime);
that.stopBluetoothDevicesDiscovery()
}
}, 30000);
},
/**
* 停止搜索蓝牙设备
*/
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: e => {
console.log("停止搜索蓝牙设备", e)
},
});
},
// 连接蓝牙
createBLEConnection(e) {
let that = this;
that.stopBluetoothDevicesDiscovery()
that.macAddr = e.macAddr
that.getCode()
},
getCode() {
let that = this;
clearTimeout(myTime);
wx.hideLoading()
uni.showModal({
title: '提示',
content: '是否激活该设备?',
cancelText: "取消",
confirmText: "确定",
success: (res) => {
if (res.confirm) {
that.getActive()
} else {
that.$tools.msg("您已取消操作")
}
}
})
},
getActive() {
let that = this
that.$model.getdevactive({
code: that.code,
sn: that.macAddr
}).then(res => {
that.$tools.msg(res.message)
that.closeBluetoothAdapter()
that.closeBLEConnection()
that.devices = []
if (res.code == 0) {
setTimeout(function() {
uni.reLaunch({
2022-06-11 16:21:11 +08:00
url: "/pages/index/index"
2022-05-13 09:22:34 +08:00
})
}, 500)
}
})
},
/**
* 断开蓝牙模块
*/
closeBluetoothAdapter() {
let that = this;
uni.closeBluetoothAdapter({
success: res => {
console.log('蓝牙模块关闭成功');
}
})
},
/**
* 断开蓝牙连接
*/
closeBLEConnection() {
var that = this;
uni.closeBLEConnection({
deviceId: that.deviceId,
success: res => {
console.log('断开蓝牙连接成功');
}
});
},
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
}
2022-05-03 21:35:39 +08:00
</script>
<style scoped lang="scss">
2022-05-13 09:22:34 +08:00
.content {
2022-06-11 16:21:11 +08:00
min-height: calc(100vh - 66px);
2022-05-13 09:22:34 +08:00
padding: 0;
2022-05-27 17:20:31 +08:00
border-top: 66px solid #F9FAFC;
2022-05-13 09:22:34 +08:00
background-color: #fff;
}
2022-05-03 21:35:39 +08:00
2022-05-13 09:22:34 +08:00
.tishi {
width: 100%;
font-size: 14px;
line-height: 25px;
font-weight: bold;
position: absolute;
2022-06-11 16:21:11 +08:00
bottom: 20px;
2022-05-13 09:22:34 +08:00
padding-left: 15px;
2022-05-03 21:35:39 +08:00
2022-05-13 09:22:34 +08:00
.text {
display: flex;
align-items: center;
2022-05-03 21:35:39 +08:00
2022-05-13 09:22:34 +08:00
icon {
margin-right: 5px;
}
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
text {
font-weight: 500;
font-size: 12px;
color: #999;
width: 100%;
display: block;
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
}
2022-05-03 21:35:39 +08:00
2022-05-13 09:22:34 +08:00
.item {
width: 70%;
height: 40px;
line-height: 38px;
text-align: center;
background: #f7f7f7;
border: 1px solid #dfdfdf;
font-weight: bold;
margin: auto;
border-radius: 15px;
margin-top: 15px;
}
2022-05-03 21:35:39 +08:00
2022-05-13 09:22:34 +08:00
.devices_summary {
width: 100%;
height: 40px;
line-height: 40px;
text-align: center;
font-size: 16px;
color: #666;
}
2022-05-03 21:35:39 +08:00
2022-05-13 09:22:34 +08:00
.device_list {
flex: 1;
width: 100%;
height: auto;
2022-06-11 16:21:11 +08:00
max-height: 340px;
2022-05-13 09:22:34 +08:00
margin-top: 0;
margin-bottom: 10px;
2022-06-11 16:21:11 +08:00
position: absolute;
bottom: 160px;
top: 190px;
2022-05-03 21:35:39 +08:00
2022-05-13 09:22:34 +08:00
.device_item {
font-size: 14px;
2022-06-11 16:21:11 +08:00
padding: 7px 10px;
2022-05-13 09:22:34 +08:00
color: #999;
border-bottom: 1px solid #dfdfdf;
2022-05-03 21:35:39 +08:00
2022-05-13 09:22:34 +08:00
text {
display: inline-block;
2022-05-03 21:35:39 +08:00
font-size: 14px;
2022-05-13 09:22:34 +08:00
font-weight: bold;
color: #666;
margin-bottom: 5px;
}
2022-05-03 21:35:39 +08:00
}
2022-05-13 09:22:34 +08:00
}
.tips {
font-size: 14px;
text-align: center;
color: #e83a1e;
background: #f7e4c8;
padding: 5px 0;
margin-top: 15px;
}
2022-05-03 21:35:39 +08:00
</style>