diff --git a/page_user/dingshi.vue b/page_user/dingshi.vue index 26684b3..fef7e35 100644 --- a/page_user/dingshi.vue +++ b/page_user/dingshi.vue @@ -4,17 +4,17 @@ id="navbar"> - - - - 07:00 - P{{index + 1}} 5分15秒 - - - - - - + + + + {{ formattedTime(values[0], values[1]) }} + P{{ index + 1 }} {{ formatTime(values[2]) }} + + + + + + @@ -84,7 +84,9 @@ minute:'--', second:'--', hour:'--', - minutekq:'--' + minutekq:'--', + list:{}, + checkedStatus: {} } }, // 分享到好友(会话) @@ -94,7 +96,6 @@ path: '/pages/index/index' } }, - // 分享到朋友圈 onShareTimeline: function() { return { @@ -103,10 +104,37 @@ path: '/pages/index/index' } }, - onLoad() { - + 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); }, methods:{ + formattedTime(minutes, seconds) { + // 将数字转换为字符串并补零 + const formattedMinutes = String(minutes).padStart(2, '0'); + const formattedSeconds = String(seconds).padStart(2, '0'); + // 返回格式化后的时间字符串 + return `${formattedMinutes}:${formattedSeconds}`; + }, + + formatTime(seconds) { + const minutes = Math.floor(seconds / 60); + const remainingSeconds = seconds % 60; + return `${minutes}分${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}秒`; + }, + // 点击定时 btnitem(){ this.timeflag = true diff --git a/page_user/lanya.vue b/page_user/lanya.vue index 20eac89..ea91922 100644 --- a/page_user/lanya.vue +++ b/page_user/lanya.vue @@ -22,14 +22,13 @@ MAC:{{item.name}} - + 添加 - @@ -66,7 +65,8 @@ deviceId:'', name:'', mac:'2952BB7A4EE0', - flags:true + flags:true, + userid:'' } }, // 分享到好友(会话) @@ -86,6 +86,8 @@ } }, onLoad() { + this.getinfo() + let that = this xBlufi.initXBlufi(1); xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent) @@ -106,6 +108,38 @@ }, 2000) }, methods:{ + // 获取用户信息 + getinfo() { + this.$u.get(`/appVerify/profile`).then((res) => { + if (res.code == 200) { + this.userid = res.data.userId + } + }) + }, + // 点击添加添加绑定设备 + btnadd(e){ + let mac = e.name.slice(5, 17) + let data = { + mac:mac, + userId:this.userid + } + this.$u.get(`/app/bandDevice`,data).then((res) => { + if (res.code == 200) { + uni.showToast({ + title: '添加成功', + icon: 'none', + duration: 3000 + }) + }else{ + uni.showToast({ + title: res.msg, + icon: 'none', + duration: 3000 + }) + } + }) + }, + btnss(){ uni.showLoading({ title: '搜索中...' @@ -136,7 +170,6 @@ }, 2000) }, - // 获取附近蓝牙设备列表 funListenDeviceMsgEvent: function(options) { switch (options.type) { diff --git a/pages/index/index.vue b/pages/index/index.vue index b4c20e7..2b04871 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -30,7 +30,7 @@ - + 丁丁浇花器 @@ -62,21 +62,99 @@ - - + + OFF + + NO + - P{{index + 1}} + P1 - + + + + + + + OFF + + + NO + + + P2 + + + + + + + + + OFF + + + NO + + + P3 + + + + + + + + + OFF + + + NO + + + P4 + + + + + + + + + OFF + + + NO + + + P5 + + + + + + + + + OFF + + + NO + + + P6 + + + - + @@ -113,9 +191,9 @@ - 距离下次:8小时 - 下次浇水时长:15分钟 - 上次浇水:10-15 15:22 + 距离下次:{{xctime}} + 下次浇水时长:{{xctimesc}} + 上次浇水:{{sctimejs}} @@ -147,7 +225,7 @@ 选择浇水时长 - + {{minute}}分 : {{second}}秒 @@ -163,7 +241,7 @@ - + @@ -191,7 +269,7 @@ selectedMinute: '1', selectedSecond: '1', sdjsflag:false , - show: false, + showjs: false, params: { year: false, month: false, @@ -207,8 +285,13 @@ deviceId:'', name:'', mac:'2952BB7A4EE0', - jstime:'' - + jstime:'', + ver_data:{}, + showobj:{}, + xctime:'--', + sctimejs:'--', + xctimesc:'--', + xctimesj:'' } }, // 分享到好友(会话) @@ -248,12 +331,55 @@ }, 2000) }, onShow() { - + this.getinfo() }, mounted() { }, methods: { + formattedTime(minutes, seconds) { + // 将数字转换为字符串并补零 + 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 + }) + setTimeout(() => { + xBlufi.notifyStartDiscoverBle({ + 'isStart': false + }) + if (that.devicesList.length > 0) { + xBlufi.notifyConnectBle({ + isStart: true, + deviceId: that.deviceId, + name: that.name + }) + } + }, 2000) + }, + // 获取用户信息 + getinfo() { + this.$u.get(`/appVerify/profile`).then((res) => { + if (res.code == 200) { + this.userobj = res.data + // 根据用户id获取当前用户 + this.$u.get(`/app/getDeviceInfoByUser?userId=${this.userobj.userId}`).then((res) => { + if (res.code == 200) { + + } + }) + } + }) + }, + kq(){ uni.getNetworkType({ success(res) { @@ -298,6 +424,28 @@ } }) }, + hq(){ + uni.getNetworkType({ + success(res) { + if (res.networkType !== 'none') { + uni.getConnectedBluetoothDevices({ + success(res) { + setTimeout(()=> { + xBlufi.notifySendCustomData({ + customData:'11get' + }) + },1500) + }, + fail(err) { + console.error('获取已连接蓝牙设备信息失败:',err) + } + }) + } else { + console.log('手机未连接网络') + } + } + }) + }, // 雨水感应 btnyushui(){ let that = this @@ -414,7 +562,7 @@ // 点击跳转到定时页面 btntime(){ uni.navigateTo({ - url:'/page_user/dingshi' + url:'/page_user/dingshi?list=' + JSON.stringify(this.ver_data) }) }, @@ -438,7 +586,7 @@ success(res) { setTimeout(()=> { xBlufi.notifySendCustomData({ - customData: "11time@" + that.jstime + customData: "11time@" + that.jstime + '@' }) that.sdjsflag = false that.kgflag = true @@ -556,9 +704,9 @@ success(res) { setTimeout(()=> { xBlufi.notifySendCustomData({ - customData: "11unlock" + customData: "11get" }) - },1500) + },3000) }, fail(err) { console.error('获取已连接蓝牙设备信息失败:',err) @@ -582,8 +730,98 @@ break; case xBlufi.XBLUFI_TYPE.TYPE_RECIEVE_CUSTON_DATA: console.log("1收到设备发来的自定义数据结果:", options.data) - this.ver_data = this.parseCustomData(options.data) - break; + const inputString = options.data + const pairs = inputString.split(';') + const showObject = {} + const pSetObjects = {} + pairs.forEach(pair => { + const [key, value] = pair.split(':'); + if (key === 'show') { + showObject.showArray = value.split(',').map(Number); + } else if (key.startsWith('p_set')) { + const numbers = value.split(',').map(Number); + pSetObjects[key] = numbers; + } + }) + this.ver_data = pSetObjects //六个浇水时间段 + this.showobj = showObject //雨滴,锁,等是否开启 + if(this.showobj.showArray[1] == 1){ + this.yschecked = false + }else{ + this.yschecked = true + } + if(this.showobj.showArray[0] == 1){ + this.etchecked = false + }else{ + this.etchecked = true + } + // 计算浇水时间 + const ver_Data = this.ver_data + function secondsToMinutesAndSeconds(seconds) { + const minutes = Math.floor(seconds / 60); + const remainingSeconds = seconds % 60; + return `${minutes}分${remainingSeconds}秒`; + } + // 获取当前时间 + 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; + } + // 辅助函数:计算两个时间之间的差值(以小时和分钟表示) + 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 }; + } + // 遍历 ver_Data 对象 + 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 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]); // 获取并转换浇水时长 + } + } else { + if (!prevTime || (now.getTime() - timeDate.getTime()) < (now.getTime() - prevTime.getTime())) { + prevTimeDiff = timeDifference(now, timeDate); // 注意这里要反过来算 + prevTime = timeDate; + } + } + } + } + // 输出结果 + this.xctime = `${nextTimeDiff.hours}时${nextTimeDiff.minutes}分` + this.xctimesc = nextWaterDuration + let timesc = prevTime.toTimeString().split(' ')[0] + this.sctimejs = timesc.slice(0,-3) + let xcjs = nextTime.toTimeString().split(' ')[0] + this.xctimesj = xcjs.slice(0,-3) + // console.log('距离下次最近的时间差:', nextTimeDiff ? `${nextTimeDiff.hours}时${nextTimeDiff.minutes}分` : '无'); + // console.log('下次时间:', nextTime ? nextTime.toTimeString().split(' ')[0] : '无'); + // console.log('下次浇水时长:', nextWaterDuration || '无'); + // console.log('距离上次最近的时间差:', prevTimeDiff ? `${prevTimeDiff.hours}时${prevTimeDiff.minutes}分` : '无'); + // console.log('上次时间:', prevTime ? prevTime.toTimeString().split(' ')[0] : '无') + + + break case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP: if (options.result) { let uniqueDevicesList = Array.from(new Set(this.devicesList)) @@ -604,27 +842,10 @@ break } }, - - // 从蓝牙拿到数据进行解析 - parseCustomData(data) { - // 将字符串按照 "@" 分割成数组 - const dataArray = data.split('@'); - const voltage = parseFloat(dataArray[0].substring(1)); // 去除前缀 "V",并将字符串转换为浮点数 - const switchState = dataArray[1].substring(1); // 去除前缀 "S" - const current = parseFloat(dataArray[2].substring(1)); // 去除前缀 "A",并将字符串转换为浮点数 - const power = parseFloat(dataArray[4].substring(1)); // 去除前缀 "P",并将字符串转换为浮点数 - const remainingPower = parseFloat(dataArray[5].substring(1)); // 去除前缀 "M",并将字符串转换为浮点数 - const setMode = dataArray[6].substring(1); // 去除前缀 "T" - // 返回解析后的数据对象 - return { - voltage, - switchState, - current, - power, - remainingPower, - setMode - } - }, + + + + } } @@ -969,12 +1190,13 @@ display: flex; justify-content: space-between; margin-top: 34rpx; - align-items: center; + // align-items: center; .weeklist { .kg { font-size: 24rpx; color: #999999; + font-weight: 600; } .weekday {