diff --git a/common/http.interceptor.js b/common/http.interceptor.js index f5b14ff..a0130b6 100644 --- a/common/http.interceptor.js +++ b/common/http.interceptor.js @@ -21,7 +21,7 @@ const install = (Vue, vm) => { // }); Vue.prototype.$u.http.setConfig({ baseUrl: 'https://yxd.ccttiot.com/prod-api', - // baseUrl: 'https://znb.ccttiot.com', + // baseUrl: 'http://192.168.2.56:8081', loadingText: '努力加载中~', loadingTime: 800, // 设置自定义头部content-type diff --git a/components/blufi/xBlufi-wx-impl.js b/components/blufi/xBlufi-wx-impl.js index ace484b..2c19b72 100644 --- a/components/blufi/xBlufi-wx-impl.js +++ b/components/blufi/xBlufi-wx-impl.js @@ -118,7 +118,7 @@ function string2buffer(str) { } af += s; var n1 = parseInt('110' + af.substring(0, 5), 2); - var n2 = parseInt('110' + af.substring(5), 2); + var n2 = parseInt('110' + af.slice(-12), 2); if (n1 > 127) n1 -= 256; if (n2 > 127) n2 -= 256; bytes.push(n1); @@ -1099,6 +1099,7 @@ function init() { }; mDeviceEvent.notifyDeviceMsgEvent(obj); }); + console.log("初始化!!!!!!!!!!!!") mDeviceEvent.listenStartDiscoverBle(true, function (options) { if (options.isStart) { //第一步检查蓝牙适配器是否可用 @@ -1109,12 +1110,12 @@ function init() { uni.closeBluetoothAdapter({ complete: function (res) { - // console.log("这里蓝牙数据") - uni.openBluetoothAdapter({ + console.log("这里蓝牙数据") + wx.openBluetoothAdapter({ success: function (res) { uni.getBluetoothAdapterState({ success: function (res) { - // console.log("这里蓝牙数据222") + console.log("这里蓝牙数据222") { let devicesList = []; let countsTimes = 0; @@ -1146,7 +1147,8 @@ function init() { // && devices.devices[0].name != 'SMART_R2XS' // ) if (devices.devices[0].name.indexOf("WATER") != -1){ - // console.log("跳过",devices.devices[0].name) + // if ( devices.devices[0].name.indexOf("gjkg") != -1 ){ + // console.log("跳过",devices.devices[0].name) // isnotexist = false; isnotexist = true; } @@ -1170,6 +1172,13 @@ function init() { if (isnotexist) { devicesList.push(devices.devices[0]); + console.log("devicesList",devicesList); + // let obj = { + // 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS, + // 'result': true, + // 'data': devicesList + // }; + // mDeviceEvent.notifyDeviceMsgEvent(obj); } } else if (devices[0]) { if (devices[0].advertisData) { @@ -1186,6 +1195,13 @@ function init() { if (isnotexist) { devicesList.push(devices[0]); + console.log("devicesList",devicesList); + // let obj = { + // 'type': mDeviceEvent.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS, + // 'result': true, + // 'data': devicesList + // }; + // mDeviceEvent.notifyDeviceMsgEvent(obj); } } diff --git a/components/blufi/xBlufi.js b/components/blufi/xBlufi.js index 2079f80..e37b58b 100644 --- a/components/blufi/xBlufi.js +++ b/components/blufi/xBlufi.js @@ -50,6 +50,7 @@ function initXBlufi(type) { break; case XMQTT_SYSTEM.WeChat: + console.log("初始化@@@@@@@!!!!!!!!!!!!") if(once==0) { once = 1; @@ -175,8 +176,8 @@ function listenSendRouterSsidAndPassword(isSetListener, funtion) { function notifySendCustomData(options) { - // console.log("设备数据",options) - mOnFire.fire(OnFireEvent.EVENT_NOFITY_SEND_CUSTON_DATA, options); + console.log("设备数据",options) + mOnFire.fire(OnFireEvent.EVENT_NOFITY_SEND_CUSTON_DATA, options); } /** * 发送自定义数据 diff --git a/page_user/dingshi.vue b/page_user/dingshi.vue index 753cf7d..cb30d51 100644 --- a/page_user/dingshi.vue +++ b/page_user/dingshi.vue @@ -52,7 +52,7 @@ - 设备未连接 + 没有更多定时浇水啦... @@ -69,7 +69,6 @@ flag:false, checked:false, timeflag:false, - show: false, params: { year: false, @@ -119,47 +118,33 @@ } }, onLoad(option) { - // this.list = JSON.parse(option.list) - // for (let key in this.list) { - // if (this.list.hasOwnProperty(key)) { - // // 获取当前数组 - // let arr = this.list[key]; - // // 判断数组的最后一个元素,并进行替换 - // if (arr[arr.length - 1] === 0) { - // arr[arr.length - 1] = false; - // } else if (arr[arr.length - 1] === 1) { - // arr[arr.length - 1] = true; - // } - // } - // } - // console.log(this.list); + xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent) + if(option.list){ //判断有无数据 有数据直接拿 无数据则发送命令获取数据 + this.list = JSON.parse(option.list) + for (let key in this.list) { + if (this.list.hasOwnProperty(key)) { + // 获取当前数组 + let arr = this.list[key]; + // 判断数组的最后一个元素,并进行替换 + if (arr[arr.length - 1] === 0) { + arr[arr.length - 1] = false; + } else if (arr[arr.length - 1] === 1) { + arr[arr.length - 1] = true; + } + } + } + console.log(this.list) + }else{ + xBlufi.notifySendCustomData({ + customData: "11get" + }) + } }, onShow() { - xBlufi.initXBlufi(1) - xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent) - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(()=> { - xBlufi.notifySendCustomData({ - customData: "11get" - }) - },1000) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:',err) - } - }) - } else { - console.log('手机未连接网络') - } - } - }) + }, methods:{ - // 点击开关 + // 点击开关是否开启 btnchange(key,index,values){ this.kgflag = true this.btnitem(key,index,values) @@ -181,30 +166,15 @@ } } let cucun = '11num' + this.deviceindex + '@' + this.devicehour + '@' + this.deviceminute + '@' + this.devicemiao + '@' + this.deviceflag + '@' - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - xBlufi.notifySendCustomData({ - customData: cucun - }) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:',err) - } - }) - } else { - console.log('手机未连接网络') - } - } - }) + xBlufi.notifySendCustomData({ + customData: cucun + }) setTimeout(()=>{ this.kgflag = false },1000) }, - // 点击定时 + // 点击设置定时时间 btnitem(key,index,values){ this.deviceindex = index this.timeflag = true @@ -242,7 +212,7 @@ return `${minutes}分${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}秒`; }, - // 取消选择定时 + // 取消选择设置定时时间 btnqx(){ this.timeflag = false this.hour = '--' @@ -250,7 +220,7 @@ this.minute = '--' this.second = '--' }, - // 确定选择定时 + // 确定选择设置定时时间 btnqd(){ this.timeflag = false this.hour = '--' @@ -258,29 +228,14 @@ this.minute = '--' this.second = '--' let cucun = '11num' + this.deviceindex + '@' + this.devicehour + '@' + this.deviceminute + '@' + this.devicemiao + '@' + this.deviceflag + '@' - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - xBlufi.notifySendCustomData({ - customData: cucun - }) - setTimeout(()=> { - xBlufi.notifySendCustomData({ - customData: "11get" - }) - },1000) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:',err) - } - }) - } else { - console.log('手机未连接网络') - } - } - }) + xBlufi.notifySendCustomData({ + customData: cucun + }) + setTimeout(()=> { + xBlufi.notifySendCustomData({ + customData: "11get" + }) + },1000) }, // 定时浇水时间 confirm(e){ @@ -294,23 +249,14 @@ this.minute = e.minute this.second = e.second this.devicemiao = Number(this.minute) * 60 + Number(this.second) - console.log(this.devicemiao); + console.log(this.devicemiao) }, funListenDeviceMsgEvent: function(options) { switch (options.type) { case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED: if (!options.result) { - // uni.showModal({ - // title: '很抱歉提醒你!', - // content: '小程序与设备异常断开', - // showCancel: false, - // //是否显示取消按钮 - // success: function(res) { - // uni.hideLoading() - // uni.removeStorage({key:'device_key'}) - // } - // }) + } break; case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA: @@ -321,11 +267,11 @@ const showObject = {} const pSetObjects = {} pairs.forEach(pair => { - const [key, value] = pair.split(':'); + const [key, value] = pair.split(':') if (key === 'show') { - showObject.showArray = value.split(',').map(Number); + showObject.showArray = value.split(',').map(Number) } else if (key.startsWith('p_set')) { - const numbers = value.split(',').map(Number); + const numbers = value.split(',').map(Number) pSetObjects[key] = numbers } }) @@ -333,51 +279,51 @@ const ver_Data = this.list function secondsToMinutesAndSeconds(seconds) { - const minutes = Math.floor(seconds / 60); - const remainingSeconds = seconds % 60; - return `${minutes}分${remainingSeconds}秒`; + const minutes = Math.floor(seconds / 60) + const remainingSeconds = seconds % 60 + return `${minutes}分${remainingSeconds}秒` } // 获取当前时间 - const now = new Date(); + const now = new Date() // 辅助函数:将时间数组转换为 Date 对象 function timeArrayToDate(timeArray) { - const [hour, minute, second] = timeArray.slice(0, 3); - const date = new Date(); - date.setHours(hour, minute, second, 0); // 毫秒设为0 - return date; + const [hour, minute, second] = timeArray.slice(0, 3) + const date = new Date() + date.setHours(hour, minute, second, 0) // 毫秒设为0 + return date } // 辅助函数:计算两个时间之间的差值(以小时和分钟表示) function timeDifference(date1, date2) { - const diffMs = Math.abs(date1 - date2); - const diffSeconds = Math.floor(diffMs / 1000); - const diffMinutes = Math.floor(diffSeconds / 60); - const diffHours = Math.floor(diffMinutes / 60); - const remainingMinutes = diffMinutes % 60; - return { hours: diffHours, minutes: remainingMinutes }; + const diffMs = Math.abs(date1 - date2) + const diffSeconds = Math.floor(diffMs / 1000) + const diffMinutes = Math.floor(diffSeconds / 60) + const diffHours = Math.floor(diffMinutes / 60) + const remainingMinutes = diffMinutes % 60 + return { hours: diffHours, minutes: remainingMinutes } } // 遍历 ver_Data 对象 - let nextTimeDiff = null; - let nextTime = null; - let prevTimeDiff = null; - let prevTime = null; - let nextWaterDuration = null; // 用于存储下次浇水时长 + let nextTimeDiff = null + let nextTime = null + let prevTimeDiff = null + let prevTime = null + let nextWaterDuration = null // 用于存储下次浇水时长 for (const key in ver_Data) { if (ver_Data.hasOwnProperty(key) && ver_Data[key][3] === 1) { - const timeArray = ver_Data[key]; - const timeDate = timeArrayToDate(timeArray); + const timeArray = ver_Data[key] + const timeDate = timeArrayToDate(timeArray) // 计算与当前时间的差值 - const diff = timeDifference(timeDate, now); + const diff = timeDifference(timeDate, now) // 判断是下一次时间还是上一次时间 if (timeDate > now) { if (!nextTime || diff.hours * 60 + diff.minutes < nextTimeDiff.hours * 60 + nextTimeDiff.minutes) { - nextTimeDiff = diff; - nextTime = timeDate; - nextWaterDuration = secondsToMinutesAndSeconds(timeArray[2]); // 获取并转换浇水时长 + nextTimeDiff = diff + nextTime = timeDate + nextWaterDuration = secondsToMinutesAndSeconds(timeArray[2]) // 获取并转换浇水时长 } } else { if (!prevTime || (now.getTime() - timeDate.getTime()) < (now.getTime() - prevTime.getTime())) { - prevTimeDiff = timeDifference(now, timeDate); // 注意这里要反过来算 - prevTime = timeDate; + prevTimeDiff = timeDifference(now, timeDate) // 注意这里要反过来算 + prevTime = timeDate } } } @@ -387,20 +333,18 @@ for (let key in this.list) { if (this.list.hasOwnProperty(key)) { // 获取当前数组 - let arr = this.list[key]; + let arr = this.list[key] // 判断数组的最后一个元素,并进行替换 if (arr[arr.length - 1] === 0) { - arr[arr.length - 1] = false; + arr[arr.length - 1] = false } else if (arr[arr.length - 1] === 1) { - arr[arr.length - 1] = true; + arr[arr.length - 1] = true } } } break } }, - - } } diff --git a/page_user/lanya.vue b/page_user/lanya.vue index 96eaa86..26877cb 100644 --- a/page_user/lanya.vue +++ b/page_user/lanya.vue @@ -87,25 +87,7 @@ }, onLoad() { this.getinfo() - - let that = this - xBlufi.initXBlufi(1); - xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent) - xBlufi.notifyStartDiscoverBle({ - 'isStart': true - }) - setTimeout(() => { - xBlufi.notifyStartDiscoverBle({ - 'isStart': false - }) - if (that.devicesList.length > 0) { - // xBlufi.notifyConnectBle({ - // isStart: true, - // deviceId: that.deviceId, - // name: that.name - // }) - } - }, 5000) + this.btnss() }, methods:{ // 获取用户信息 @@ -123,7 +105,7 @@ // 点击添加添加绑定设备 btnadd(e){ console.log(e,'0101'); - let mac = e.slice(5, 17) + let mac = e.slice(-12) let data = { mac:mac, userId:this.userid @@ -154,7 +136,6 @@ }) this.flag = false let that = this - xBlufi.initXBlufi(1); xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent) xBlufi.notifyStartDiscoverBle({ 'isStart': true @@ -165,11 +146,6 @@ }) if (that.devicesList.length > 0) { that.flags = true - // xBlufi.notifyConnectBle({ - // isStart: true, - // deviceId: that.deviceId, - // name: that.name - // }) }else{ that.flags = false } @@ -201,7 +177,7 @@ if (options.result) { let devicesarr = options.data this.devicesList = devicesarr - console.log(devicesarr,'111'); + // console.log(devicesarr,'111'); // devicesarr.forEach(device => { // const mac = device.name.substring(4); // if (device.name.slice(5, 17) == this.mac) { diff --git a/page_user/photolist.vue b/page_user/photolist.vue index bdfa216..aef7cbf 100644 --- a/page_user/photolist.vue +++ b/page_user/photolist.vue @@ -61,7 +61,7 @@ btnxq(plantId){ if(plantId == null || plantId == ''){ uni.showToast({ - title: '无该非植物信息', + title: '暂无非植物信息', icon: 'none', duration: 2000 }) diff --git a/page_user/photoxq.vue b/page_user/photoxq.vue index 2dab4eb..d777d74 100644 --- a/page_user/photoxq.vue +++ b/page_user/photoxq.vue @@ -209,7 +209,6 @@ .title { margin-top: 23rpx; font-size: 80rpx; - // font-weight: 400; letter-spacing: 0rpx; line-height: 88rpx; color: rgba(80, 86, 90, 1); @@ -233,13 +232,10 @@ .cont_left { display: flex; flex-wrap: wrap; - // margin-right: 40rpx; - // align-items: baseline; width: 50%; .cont_left_tit { font-size: 28rpx; - // font-weight: 400; letter-spacing: 0.56rpx; line-height: 36rpx; color: #3D3D3D; @@ -249,7 +245,6 @@ margin-top: 10rpx; width: 294rpx; font-size: 32rpx; - // font-weight: 600; letter-spacing: 0.64rpx; line-height: 40rpx; color: #50565A; @@ -289,7 +284,6 @@ } .txt { - // margin-top: 24rpx; width: 100%; margin-top: 12rpx; font-size: 32rpx; diff --git a/page_user/upload.vue b/page_user/upload.vue index f8ded50..c4e6169 100644 --- a/page_user/upload.vue +++ b/page_user/upload.vue @@ -17,11 +17,6 @@ MAC {{user.mac == undefined ? '--' : user.mac}} - 息屏 {{xptxt}} @@ -139,8 +134,10 @@ duration: 2000 }) setTimeout(() => { - uni.navigateBack() - }, 2000) + uni.reLaunch({ + url:'/pages/index/index' + }) + }, 1000) } else { uni.showToast({ title: res.msg, diff --git a/pages/index/index.vue b/pages/index/index.vue index 34dbf44..b216386 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -17,9 +17,9 @@ 绑定 - + @@ -30,28 +30,37 @@ - + - - {{user.deviceName == undefined ? '--' : user.deviceName}} + + {{user.deviceName == undefined ? '--' : user.deviceName}} + + 点击连接 + + + 连接中 + + + 已连接 + - + 型号:{{user.model == undefined ? '--' : user.model}} - MAC:{{user.mac == undefined ? '--' : user.mac}} + MAC:{{user.mac == undefined ? '--' : user.mac}} @@ -74,15 +83,17 @@ OFF - NO + ON P1 + + {{ver_data.p_set0[0] == undefined ? '' : ver_data.p_set0[0] + ':'}}{{ver_data.p_set0[1] == undefined ? '' : ver_data.p_set0[1]}} + - + @@ -91,14 +102,16 @@ OFF - NO + ON P2 + + {{ver_data.p_set1[0] == undefined ? '' : ver_data.p_set1[0] + ':'}}{{ver_data.p_set1[1] == undefined ? '' : ver_data.p_set1[1]}} + - @@ -108,14 +121,16 @@ OFF - NO + ON P3 + + {{ver_data.p_set2[0] == undefined ? '' : ver_data.p_set2[0] + ':'}}{{ver_data.p_set2[1] == undefined ? '' : ver_data.p_set2[1]}} + - @@ -125,14 +140,16 @@ OFF - NO + ON P4 + + {{ver_data.p_set3[0] == undefined ? '' : ver_data.p_set3[0] + ':'}}{{ver_data.p_set3[1] == undefined ? '' : ver_data.p_set3[1]}} + - @@ -142,14 +159,16 @@ OFF - NO + ON P5 + + {{ver_data.p_set4[0] == undefined ? '' : ver_data.p_set4[0] + ':'}}{{ver_data.p_set4[1] == undefined ? '' : ver_data.p_set4[1]}} + - @@ -159,14 +178,16 @@ OFF - NO + ON P6 + + {{ver_data.p_set5[0] == undefined ? '' : ver_data.p_set5[0] + ':'}}{{ver_data.p_set5[1] == undefined ? '' : ver_data.p_set5[1]}} + - @@ -233,6 +254,7 @@ {{item.deviceName}} 型号:{{item.model}} + MAC:{{item.mac}} @@ -306,7 +328,9 @@ mac: '', jstime: '', ver_data: {}, + vardataflag:1, showobj: {}, + jstimeobj:{}, xctime: '--', sctimejs: '--', xctimesc: '--', @@ -320,7 +344,9 @@ lastChar:'', timer:'', bjflag:false, - userobj:{} + userobj:{}, + devicesarr:[], + intervalId: null, } }, // 分享到好友(会话) @@ -340,9 +366,6 @@ } }, onLoad() { - - }, - onShow() { wx.openBluetoothAdapter({ success: function (res) { console.log('蓝牙适配器初始化成功'); @@ -355,25 +378,20 @@ duration:2000 }) } + }) + xBlufi.initXBlufi(1) + }, + onShow() { + let that = this + xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent) + xBlufi.notifyStartDiscoverBle({ + 'isStart': true }) - // uni.getLocation({ - // type: 'wgs84', - // success: (res) => { - - // }, - // fail: (err) => { - // console.error('获取位置失败:', err); - // uni.showToast({ - // title: '获取位置失败', - // icon: 'none', - // duration:2000 - // }) - // } - // }) // 请求个人信息 this.getinfo() - // 一进来每分钟发一次上传信息命令 - this.getshuju() + }, + mounted() { + this.getshuju(); // 页面加载时启动定时器 }, methods: { // 点击进行设备录入 @@ -395,65 +413,8 @@ const formattedMinutes = String(minutes).padStart(2, '0'); const formattedSeconds = String(seconds).padStart(2, '0'); // 返回格式化后的时间字符串 - return `${formattedMinutes}:${formattedSeconds}`; - }, - // 点击连接 - btnlj() { - let that = this - xBlufi.initXBlufi(1) - xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent) - xBlufi.notifyStartDiscoverBle({ - 'isStart': true - }) - this.getchao() - setTimeout(() => { - xBlufi.notifyStartDiscoverBle({ - 'isStart': false - }) - if (that.devicesList.length > 0) { - xBlufi.notifyConnectBle({ - isStart: true, - deviceId: that.deviceId, - name: that.name - }) - } - }, 5000) - }, - // 连接超时提示 - getchao(){ - uni.showLoading({ - title: '浇花器连接中...' - }) - this.getshebxq() - setTimeout(()=>{ - uni.hideLoading() - if(this.datalist == ''){ - let that = this - uni.showModal({ - title: '提示', - content: '未连接成功,是否重新连接,重新连接请确保设备在附近5米范围内', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.getinfo() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } - }) - }else{ - uni.showToast({ - title: '设备连接成功', - icon: 'success', - duration:2000 - }) - } - },15000) + console.log(`${formattedMinutes}:${formattedSeconds}`,'111111111'); + return `${formattedMinutes}:${formattedSeconds}` }, // 静默登录 jmlogin() { @@ -500,221 +461,178 @@ } }) }, + // 获取设备列表 + getlist(){ + this.$u.get(`/app/getDeviceInfoByUser?userId=${this.userobj.userId}`).then((res) => { + if (res.code == 200) { + if(res.data.length > 0 || res.data != ''){ + this.bjflag = false + this.devicelist = res.data + this.mac = res.data[0].mac + this.deviceId = res.data[0].mac.match(/.{2}/g).reverse().join(':') + this.user = res.data[0] + this.shebid = res.data[0].deviceId + let that = this + }else{ + this.bjflag = true + } + } + }) + }, + // 点击连接蓝牙函数 + btnlj() { + console.log("进入111蓝牙连接") + if (this.vardataflag != 3) { + this.vardataflag = 2 + let that = this + if (that.deviceId != '') { + let systemInfo = uni.getSystemInfoSync() + if (systemInfo.platform == 'android') { + console.log("进入这里是IOS") + xBlufi.notifyStartDiscoverBle({ + 'isStart': false + }) + console.log("进入蓝牙连接", that.deviceId) + console.log("进入that.name", that.name) + xBlufi.notifyConnectBle({ + isStart: true, + deviceId: that.deviceId, + name: that.name + }) + } else if (systemInfo.platform == 'ios') { + // 当前设备是 iOS + console.log("进入这里是IOS") + that.findDevice() //使用递归函数不断去匹配 + } + } + } + }, + // ios递归函数匹配 + findDevice() { + // 使用 find 方法查找匹配的设备 + const matchedDevice = this.devicesarr.find(device => { + console.log(device.name, this.mac, '111') + console.log("IOSMAC", device) + return device.name.slice(-12) == this.mac.slice(-12) + }) + if (matchedDevice) { + // 找到匹配的设备 + xBlufi.notifyStartDiscoverBle({ + 'isStart': false + }) + xBlufi.notifyConnectBle({ + isStart: true, + deviceId: matchedDevice.deviceId, + name: matchedDevice.name + }) + this.deviceId = matchedDevice.deviceId + this.name = matchedDevice.name + console.log(this.deviceId, '11221') + } else { + console.log("未找到匹配的设备,继续查找...") + setTimeout(this.findDevice.bind(this), 1000) // 使用 bind 保持 this 上下文 + } + }, + // 雨水感应开启 btnyushui() { this.one = false this.ones = true - if(this.datalist == ''){ - let that = this - uni.showModal({ - title: '提示', - content: '设备未连接,是否进行设备连接?', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.btnlj() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } + if(this.vardataflag != 3){ + uni.showToast({ + title: '设备未连接', + icon: 'none', + duration:2000 }) }else{ uni.showLoading({ title: '开启中...' }) - let that = this - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(() => { - that.yschecked = false - xBlufi.notifySendCustomData({ - customData: '11yudi' - }) - uni.hideLoading() - }, 1500) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:', err) - } - }) - } else { - console.log('手机未连接网络') - } - } - }) + setTimeout(() => { + this.yschecked = false + xBlufi.notifySendCustomData({ + customData: '11yudi' + }) + uni.hideLoading() + }, 1500) } }, // 雨水感应关闭 btnyushuis() { this.one = false this.ones = true - if(this.datalist == ''){ - let that = this - uni.showModal({ - title: '提示', - content: '设备未连接,是否进行设备连接?', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.btnlj() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } + if(this.vardataflag != 3){ + uni.showToast({ + title: '设备未连接', + icon: 'none', + duration:2000 }) }else{ uni.showLoading({ title: '关闭中...' }) - let that = this - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(() => { - that.yschecked = true - xBlufi.notifySendCustomData({ - customData: '11unyudi' - }) - uni.hideLoading() - }, 1500) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:', err) - } - }) - } else { - console.log('手机未连接网络') - } - } - }) + setTimeout(() => { + this.yschecked = true + xBlufi.notifySendCustomData({ + customData: '11unyudi' + }) + uni.hideLoading() + }, 1500) } }, // 点击儿童锁开启 btnertong() { this.two = false this.twos = true - if(this.datalist == ''){ - let that = this - uni.showModal({ - title: '提示', - content: '设备未连接,是否进行设备连接?', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.btnlj() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } - }) + if(this.vardataflag != 3){ + uni.showToast({ + title: '设备未连接', + icon: 'none', + duration:2000 + }) }else{ uni.showLoading({ title: '开启中...' }) - let that = this - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(() => { - that.etchecked = false - xBlufi.notifySendCustomData({ - customData: '11lock' - }) - uni.hideLoading() - }, 1500) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:', err) - } - }) - } else { - console.log('手机未连接网络') - } - } - }) + setTimeout(() => { + this.etchecked = false + xBlufi.notifySendCustomData({ + customData: '11lock' + }) + uni.hideLoading() + }, 1500) } }, // 点击儿童锁关闭 btnertongs() { this.two = false this.twos = true - if(this.datalist == ''){ - let that = this - uni.showModal({ - title: '提示', - content: '设备未连接,是否进行设备连接?', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.btnlj() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } - }) + if(this.vardataflag != 3){ + uni.showToast({ + title: '设备未连接', + icon: 'none', + duration:2000 + }) }else{ uni.showLoading({ title: '关闭中...' }) - let that = this - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(() => { - that.etchecked = true - xBlufi.notifySendCustomData({ - customData: '11unlock' - }) - uni.hideLoading() - }, 1500) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:', err) - } - }) - } else { - console.log('手机未连接网络') - } - } - }) + setTimeout(() => { + this.etchecked = true + xBlufi.notifySendCustomData({ + customData: '11unlock' + }) + uni.hideLoading() + }, 1500) } }, // 点击添加设备 btnaddsb() { //跳转后清空数据 重新进行连接 - this.datalist = '' - this.mac = '' + this.vardataflag = 1 this.ver_data = {} + this.datalist = '' uni.navigateTo({ url: '/page_user/lanya' }) @@ -729,9 +647,17 @@ // 点击跳转到定时页面 btntime() { - uni.navigateTo({ - url: '/page_user/dingshi?list=' + JSON.stringify(this.ver_data) - }) + if(this.vardataflag != 3){ + uni.showToast({ + title: '蓝牙未连接', + icon: 'none', + duration: 2000 + }) + }else{ + uni.navigateTo({ + url: '/page_user/dingshi?list=' + JSON.stringify(this.ver_data) + }) + } }, @@ -751,129 +677,73 @@ uni.showLoading({ title: '开启中...' }) - let that = this - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(() => { - xBlufi.notifySendCustomData({ - customData: "11time@" + that.jstime + '@' - }) - uni.hideLoading() - that.startTimer(that.jstime) - that.sdjsflag = false - that.kgflag = true - }, 1500) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:', err) - } - }) - } else { - console.log('手机未连接网络') - } - } - }) + setTimeout(() => { + xBlufi.notifySendCustomData({ + customData: "11time@" + this.jstime + '@' + }) + uni.hideLoading() + this.startTimer(this.jstime) + this.sdjsflag = false + this.kgflag = true + }, 1500) } }, // 手动浇水定时器 startTimer(totalSeconds) { - let remainingSeconds = totalSeconds; // 正确初始化 remainingSeconds - const sdminutes = ''; // 如果您打算在组件中使用这些变量,最好在这里声明它们 - const sdseconds = ''; + let remainingSeconds = totalSeconds + const sdminutes = '' + const sdseconds = '' this.timer = setInterval(() => { // 计算分钟和秒 - const minutes = Math.floor(remainingSeconds / 60).toString(); - const secs = (remainingSeconds % 60).toString(); + const minutes = Math.floor(remainingSeconds / 60).toString() + const secs = (remainingSeconds % 60).toString() // 格式化秒和分钟,确保是两位数 - this.sdseconds = secs.padStart(2, '0'); - this.sdminutes = (minutes > 0 ? minutes : '').padStart(2, '0'); // 如果分钟为0,则不显示分钟 + this.sdseconds = secs.padStart(2, '0') + this.sdminutes = (minutes > 0 ? minutes : '').padStart(2, '0') // 如果分钟为0,则不显示分钟 // 更新剩余秒数 - remainingSeconds--; + remainingSeconds-- // 如果剩余秒数为0,则停止定时器 if (remainingSeconds <= 0) { - clearInterval(this.timer); - this.btnkq(); // 确保这个函数是存在的,并且您想要在这里调用它 - this.sdminutes = ''; // 清除分钟显示(可选,根据您的需求) - this.sdseconds = ''; // 清除秒显示(可选,根据您的需求) + clearInterval(this.timer) + this.btnkq() + this.sdminutes = '' + this.sdseconds = '' } - }, 1000); + }, 1000) }, // 关闭手动浇水 btnkq() { - if(this.datalist == ''){ - let that = this - uni.showModal({ - title: '提示', - content: '设备未连接,是否进行设备连接?', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.btnlj() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } + if(this.vardataflag != 3){ + uni.showToast({ + title: '设备未连接', + icon: 'none', + duration:2000 }) }else{ uni.showLoading({ title: '关闭中...' }) - let that = this - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(() => { - xBlufi.notifySendCustomData({ - customData: "11close" - }) - // that.startTimer(0) - clearInterval(that.timer) - that.sdminutes = '' - that.sdseconds = '' - that.kgflag = false - uni.hideLoading() - }, 1500) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:', err) - } - }) - } - } - }) + setTimeout(() => { + xBlufi.notifySendCustomData({ + customData: "11close" + }) + clearInterval(this.timer) + this.sdminutes = '' + this.sdseconds = '' + this.kgflag = false + uni.hideLoading() + }, 1500) } }, // 开启手动浇水 btngb() { - if(this.datalist == ''){ - uni.showModal({ - title: '提示', - content: '设备未连接,是否进行设备连接?', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.btnlj() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } + if(this.vardataflag != 3){ + uni.showToast({ + title: '设备未连接', + icon: 'none', + duration:2000 }) }else{ this.sdjsflag = true @@ -898,6 +768,11 @@ wx.closeBLEConnection({ deviceId: this.mac, }) + xBlufi.notifyConnectBle({ + isStart: false, + deviceId: this.deviceId, + name: this.name + }) uni.showToast({ title: res.msg, icon: 'success', @@ -909,25 +784,11 @@ this.name = '' this.deviceId = '' this.xuanzeflag = false - let that = this - xBlufi.initXBlufi(1) - xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent) - xBlufi.notifyStartDiscoverBle({ - 'isStart': true - }) - setTimeout(() => { - xBlufi.notifyStartDiscoverBle({ - 'isStart': false - }) - if (that.devicesList.length > 0) { - xBlufi.notifyConnectBle({ - isStart: true, - deviceId: that.deviceId, - name: that.name - }) - } - }, 5000) - that.getchao() + this.vardataflag = 1 + this.sdseconds = '' + this.sdminutes = '' + this.kgflag = false + this.getshebxq() }else{ uni.showToast({ title: res.msg, @@ -942,68 +803,12 @@ this.$u.get(`/app/getDeviceInfo/${this.shebid}`).then(res => { if(res.code == 200){ this.mac = res.data.mac + this.deviceId = res.data.mac.match(/.{2}/g).reverse().join(':') this.user = res.data - uni.showLoading({ - title: '浇花器连接中...' - }) } }) }, - // 一开始提示是否连接 - getlianjie(){ - if(this.datalist == ''){ - let that = this - uni.showModal({ - title: '提示', - content: '设备未连接,是否进行设备连接?', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.btnlj() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } - }) - } - }, - // 获取设备列表 - getlist(){ - this.$u.get(`/app/getDeviceInfoByUser?userId=${this.userobj.userId}`).then((res) => { - if (res.code == 200) { - if(res.data.length > 0 || res.data != ''){ - this.bjflag = false - this.devicelist = res.data - this.mac = res.data[0].mac - this.user = res.data[0] - this.shebid = res.data[0].deviceId - let that = this - wx.openBluetoothAdapter({ - success: function (res) { - console.log('蓝牙适配器初始化成功'); - that.getlianjie() - }, - fail: function (err) { - console.error('蓝牙适配器初始化失败,可能是因为没有权限', err); - uni.showToast({ - title: '蓝牙适配器初始化失败,可能是因为没有权限', - icon: 'none', - duration:2000 - }) - } - }) - }else{ - this.bjflag = true - } - } - }) - }, // 点击切换设备 btnksxz() { if (this.xuanzeflag == true) { @@ -1014,107 +819,49 @@ }, //跳转到浇水日志页 btnjs() { - uni.navigateTo({ - url: '/page_user/jiaoshui' + uni.showToast({ + title: '此功能暂未开通', + icon: 'none', + duration:2000 }) + // uni.navigateTo({ + // url: '/page_user/jiaoshui' + // }) }, // 获取附近蓝牙设备列表 funListenDeviceMsgEvent: function(options) { switch (options.type) { case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED: if (!options.result) { - this.datalist = '' - this.ver_data = '' - + this.vardataflag = 1 } break; case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS: if (options.result) { - let devicesarr = options.data - // console.log(devicesarr,'111') - devicesarr.forEach(device => { - const mac = device.name.substring(4) - if (device.name.slice(5, 17) == this.mac) { - this.deviceId = device.deviceId - this.name = device.name - this.devicesList.push(device) - let uniqueDevicesList = Array.from(new Set(this.devicesList)) - this.devicesList = uniqueDevicesList; - } - }) + this.devicesarr = options.data } break; case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED: console.log("连接回调:" + JSON.stringify(options)) - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(() => { - xBlufi.notifySendCustomData({ - customData: "11get" - }) - }, 3000) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:', err) - } - }) - } - } - }) - uni.setStorageSync('device_key', 'true') - if (options.result) { - { - xBlufi.notifyInitBleEsp32({ - deviceId: this.deviceId - }) - this.name = this.name - } - } else { - + if (options.result == true){ + this.datalist = '1' + this.vardataflag = 3 + xBlufi.notifyInitBleEsp32({ + deviceId: this.deviceId + }) + }else{ + this.vardataflag = 1 } - break; + break case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA: console.log("1收到设备发来的自定义数据结果:", options.data,options.data.length) this.datalist = options.data.slice(0, -1) + ";" + this.vardataflag = 3 this.getchuli() break - case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP: - if (options.result) { - let uniqueDevicesList = Array.from(new Set(this.devicesList)) - let filteredDevices = uniqueDevicesList.filter(device => device.name.substring(0, 5) === - "WATER") - // 将过滤后的数组重新赋值给 this.devicesList - this.devicesList = filteredDevices - } - break; case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START: if (!options.result) { - uni.hideLoading() - // uni.showToast({ - // title: '蓝牙未开启', - // icon: 'none', - // duration: 3000 - // }) - uni.showModal({ - title: '提示', - content: '请打开蓝牙和位置信息', - success: function (res) { - if (res.confirm) { - uni.hideLoading() - that.btnlj() - } else if (res.cancel) { - uni.hideLoading() - uni.showToast({ - title: '已取消连接', - icon: 'none', - duration:2000 - }) - } - } - }) + console.log('蓝牙未开启') return } break @@ -1122,32 +869,15 @@ }, // 每隔一分钟发送一次获取数据 getshuju() { - let that = this - const intervalId = setInterval(() => { - uni.getNetworkType({ - success(res) { - if (res.networkType !== 'none') { - uni.getConnectedBluetoothDevices({ - success(res) { - setTimeout(() => { - xBlufi.notifySendCustomData({ - customData: "11get" - }) - console.log(that.ver_data,'000'); - if(that.ver_data != ''){ - that.getchuli() - } - }, 1000) - }, - fail(err) { - console.error('获取已连接蓝牙设备信息失败:', err) - } - }) - } - } - }) - console.log("定时器每分钟执行一次") - }, 60000) + const that = this; + that.intervalId = setInterval(() => { + if (that.vardataflag == 3) { + xBlufi.notifySendCustomData({ + customData: "11get" + }); + } + console.log("定时器一段时间执行一次"); + }, 10000); }, // 处理从设备接收数据 @@ -1166,6 +896,7 @@ } }) this.ver_data = pSetObjects //六个浇水时间段 + this.jstimeobj = pSetObjects console.log(inputString,this.ver_data, '002002002') this.showobj = showObject //雨滴,锁,等是否开启 if (this.showobj.showArray[1] == 1) { @@ -1532,7 +1263,7 @@ } .devicebox { width: 100%; - height: 290rpx; + max-height: 320rpx; background: #FFFFFF; box-shadow: 0rpx 0rpx 38rpx 0rpx rgba(128, 128, 128, 0.3); filter: blur(0px); @@ -1544,8 +1275,8 @@ display: flex; justify-content: space-between; margin-top: 34rpx; - // align-items: center; .weeklist { + text-align: center; .kg { font-size: 24rpx; color: #999999; @@ -1620,6 +1351,16 @@ font-weight: 600; display: flex; align-items: center; + .lj{ + color: red; + margin-left: 10rpx; + font-weight: 400; + font-size: 28rpx; + border: 1px solid red; + padding: 4rpx; + box-sizing: border-box; + border-radius: 10rpx; + } text { display: inline-block; width: 14rpx; diff --git a/pages/myorder/index.vue b/pages/myorder/index.vue index 8bf314e..5260e04 100644 --- a/pages/myorder/index.vue +++ b/pages/myorder/index.vue @@ -331,7 +331,6 @@ uni.showLoading({ title: '请稍后...' }) - xBlufi.initXBlufi(1); xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent); xBlufi.notifyStartDiscoverBle({ 'isStart': true diff --git a/pages/myorder/returned/index.vue b/pages/myorder/returned/index.vue index b0f90ab..c4b993e 100644 --- a/pages/myorder/returned/index.vue +++ b/pages/myorder/returned/index.vue @@ -364,7 +364,6 @@ uni.showLoading({ title: '连接中..' }) - xBlufi.initXBlufi(1); xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent); xBlufi.notifyStartDiscoverBle({ 'isStart': true