From 2db6e465934008f09e7b88ff7a535675d8f11280 Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Thu, 29 May 2025 14:09:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/userInfo/userInfo.js | 423 +++++++++++++++++++++++++++++++++-- pages/userInfo/userInfo.wxml | 169 ++++++++------ pages/userInfo/userInfo.wxss | 285 ++++++++++++----------- 3 files changed, 663 insertions(+), 214 deletions(-) diff --git a/pages/userInfo/userInfo.js b/pages/userInfo/userInfo.js index d138447..af8bcf4 100644 --- a/pages/userInfo/userInfo.js +++ b/pages/userInfo/userInfo.js @@ -1,27 +1,424 @@ +const app = getApp(); +import $ from "../../utils/request"; + Page({ data: { + BLUE_STATE: {}, + device: null, + connectState: "", + userId: "", + realname: "", + sex: { + index: "", + data: [{ + id: 1, + name: '男' + }, { + id: 2, + name: '女' + }] + }, + birthday: { + label: "1980年01月01日", + value: "1980-01-01" + }, + height: { + index: 50, + data: Array.from({ length: 200 - 120 + 1 }, (_, i) => i + 120) + }, + weight: { + index: [60, 0], + data: [Array.from({ length: 120 + 1 }, (_, i) => i + 10), Array.from({ length: 10 }, (_, i) => i)] + }, }, + // 存储回调函数的引用,以便在 detached 时取消监听 + _connectStateWatcher: null, + onLoad(options) { + this.checkBluetoothPermissionAndInit(); + + app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => { + console.log("searchDevice ===》devicesModel", res); + app.globalData.ppScale.device.mac = res.deviceMac; + app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => { + console.log('connectedDevice ===》deviceConnect', res); + + app.globalData.ppScale.plugin.ScaleAction.startDataProgress(true); + app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol(); + + app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => { + console.log("connectedDevice ===》codeUpdateMTU", res); + + + app.globalData.ppScale.device.name = this.data.device.name; + app.globalData.ppScale.device.connection = this.data.device; + this.selectComponent('#replaceNetwork1Component').getWiFiList(); + }); + }); + }); + + console.log(`[Component] connectState 获取: ${app.globalData.ppScale.device.connectState}`); + this.setData({ + BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE, + connectState: app.globalData.ppScale.device.connectState + }) + this._connectStateWatcher = (newValue, oldValue) => { + console.log(`[Component] connectState 变化: ${oldValue} -> ${newValue}`); + this.setData({ + connectState: newValue || '' + }); + }; + app.watch('ppScale.device.connectState', this._connectStateWatcher); + + let userInfo = wx.getStorageSync("userInfo") || null; + if(userInfo) { + let [year, month, day] = userInfo.birthday.split("-"); + + this.setData({ + userId: userInfo.userId, + realname: userInfo.realname, + ["sex.index"]: this.data.sex.data.findIndex(item => {return item.id === userInfo.sex}), + birthday: { + label: year + "年" + month + "月" + day + "日", + value: userInfo.birthday + }, + ["height.index"]: this.data.height.data.findIndex(item => {return item === userInfo.height}), + }) + + if(userInfo.weight) { + const integerPart = Math.floor(userInfo.weight); + const decimalPart = Math.round((userInfo.weight - integerPart) * 10); + const integerIndex = this.data.weight.data[0].indexOf(integerPart); + const decimalIndex = this.data.weight.data[1].indexOf(decimalPart); + this.setData({ + ["weight.index"]: [integerIndex !== -1 ? integerIndex : null, decimalIndex !== -1 ? decimalIndex : null], + }) + } + } }, - onShow() { + // 姓名输入 + realnameInput(e) { + let realname = e.detail.value.replace(/\s+/g, ''); + this.setData({ + realname: realname + }) + }, + // 性别选择 + sexChange(e) { + let sex = e.detail.value; + console.log(sex); + this.setData({ + ['sex.index']: e.detail.value + }) + if(this.data.height.index === null) { + this.setData({ + ['height.index']: sex == 0 ? 50 : 40 + }) + } + if(this.data.weight.index === null) { + this.setData({ + ['weight.index']: sex == 0 ? 20 : 10 + }) + } + }, + + // 生日选择 + birthdayChange(e) { + let v = e.detail.value; + let [year, month, day] = v.split("-"); + this.setData({ + birthday: { + label: year + "年" + month + "月" + day + "日", + value: v + } + }) + }, + + // 身高选择 + heightChange(e) { + this.setData({ + ['height.index']: e.detail.value + }) + }, + + // 体重选择 + weightChange(e) { + console.log(e.detail.value) + this.setData({ + ['weight.index']: e.detail.value + }) + }, + + setUserInfo() { + if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS) { + let realname = this.data.realname; + let sex = this.data.sex; + let birthday = this.data.birthday; + let height = this.data.height; + let weight = this.data.weight; + if(!realname) { + wx.showToast({ + icon: "none", + title: "姓名不能为空" + }) + return false; + } + if(sex.index === "") { + wx.showToast({ + icon: "none", + title: "性别不能为空" + }) + return false; + } + if(!birthday.value) { + wx.showToast({ + icon: "none", + title: "生日不能为空" + }) + return false; + } + if(height.index === "") { + wx.showToast({ + icon: "none", + title: "身高不能为空" + }) + return false; + } + if(weight.index[0] === null && weight.index[1] === null) { + console.log(weight) + wx.showToast({ + icon: "none", + title: "体重不能为空" + }) + return false; + } + let sn = app.globalData.ppScale.device.mac.replace(/:/g, ''); + $.ajax("weighingScale/edit/user", { + sn: sn, + realname: realname, + sex: sex.data[sex.index].id, + birthday: birthday.value, + height: height.data[height.index], + weight: weight.data[0][weight.index[0]] + '.' + weight.data[1][weight.index[1]] + }, "POST", true, "保存中...").then(res => { + $.ajax("weighingScale/select/user", { + sn: sn + }, "GET", true, "更新用户信息中...").then(res_ => { + wx.setStorageSync("userInfo", res_.result); + wx.showToast({ + icon: "none", + title: res.message + }) + app.globalData.ppScale.activeProtocol.dataSyncUserInfo({ + userID: res_.result.id, + userName: res_.result.realname, + memberID: "", + age: this.getAge(res_.result.birthday), // + gender: res_.result.sex, // + height: res_.result.height, // + isAthleteMode: 0, + currentWeight: res_.result.weight, // + deviceHeaderIndex: 0, + targetWeight: "", + idealWeight: "", + recentData: [], + }, (res) => { + if(res == 0) { + this.triggerEvent('deviceEvent', { + status: true + }); + } else { + console.log("dataSyncUserInfo", res) + wx.showToast({ + title: "用户信息下发失败,请重试。", + icon: "none" + }) + } + }) + }) + }) + } else { + wx.showToast({ + title: "设备连接失败,请重试。", + icon: "none" + }) + } + }, + + // 根据生日获取年龄 + getAge(birthDateString) { + const birthDate = new Date(birthDateString); + const today = new Date(); + + let age = today.getFullYear() - birthDate.getFullYear(); + const monthDiff = today.getMonth() - birthDate.getMonth(); + const dayDiff = today.getDate() - birthDate.getDate(); + + // 如果当前月份小于出生月份,或者同月但当前日期小于出生日期,年龄需要减 1 + if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) { + age--; + } + + return age; + }, + + checkBluetoothPermissionAndInit() { + this.setData({ + initText: "正在检查蓝牙权限..." + }); + wx.getSetting({ + success: (res) => { + if (res.authSetting['scope.bluetooth']) { + this.openBluetoothAdapter(); + } else { + wx.authorize({ + scope: 'scope.bluetooth', + success: () => { + this.openBluetoothAdapter(); + }, + fail: (err) => { + this.setData({ + initText: "蓝牙权限被拒绝。" + }); + wx.showModal({ + title: '提示', + content: '您拒绝了蓝牙权限,将无法连接蓝牙秤。是否前往设置开启?', + confirmText: '去开启', + cancelText: '不开启', + success: (modalRes) => { + if (modalRes.confirm) { + wx.openSetting({ + success: (settingRes) => { + console.log('openSetting success', settingRes); + }, + fail: (settingErr) => { + wx.navigateBack({ + delta: 1 + }); + } + }); + } else { + wx.navigateBack({ + delta: 1 + }); + } + } + }); + } + }); + } + }, + fail: (err) => { + this.setData({ + initText: "获取权限设置失败。" + }); + wx.showToast({ + title: '获取权限设置失败', + icon: 'none' + }); + wx.navigateBack({ + delta: 1 + }); + } + }); + }, + + openBluetoothAdapter() { + this.setData({ + initText: "正在初始化蓝牙..." + }); + wx.openBluetoothAdapter({ + success: () => { + this.setData({ + initText: "蓝牙初始化成功" + }); + this.getDeviceSettingList(); + }, + fail: (err) => { + this.setData({ + initText: "蓝牙初始化失败。" + }); + if (err.errCode === 10001) { + wx.showModal({ + title: "提示", + content: "请确保手机蓝牙已开启。", + showCancel: false, + success: () => { + wx.navigateBack({ + delta: 1 + }); + } + }); + } else if (err.errMsg && err.errMsg.includes('permission denied')) { + wx.showModal({ + title: '提示', + content: '蓝牙权限仍被拒绝,无法使用蓝牙功能。', + showCancel: false, + success: () => { + wx.navigateBack({ + delta: 1 + }); + } + }); + } else { + wx.showToast({ + title: `蓝牙适配器打开失败: ${err.errMsg || err.errCode}`, + icon: 'none', + success: () => { + wx.navigateBack({ + delta: 1 + }); + } + }); + } + }, + }); + }, + + // 获取自己已经配对上的设备 + getDeviceSettingList() { + let params = {}; + $.ajax("weighingScale/list/device", params, "GET", true, "获取设备列表").then((res) => { + this.setData({ + initText: "设备搜索中..." + }) + app.globalData.ppScale.device.list = []; + let seviceList = app.globalData.ppScale.device.setting.find((item) => { + return item.deviceName === res.result[0].type + }); + app.globalData.ppScale.plugin.Blue.setDeviceSetting([seviceList]); + app.globalData.ppScale.plugin.Blue.start(res.result[0].type, false); + app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => { + console.log("searchDevice ===> devicesList", res_); + + let fIndex = res_.findIndex(item => item.deviceId === res.result[0].deviceId); + if (fIndex >= 0) { + app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); + this.setData({ + device: res_[fIndex] + }) + app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[fIndex]); + } + }); + }) }, - /** - * 生命周期函数--监听页面隐藏 - */ - onHide() { + // 点击设备重连 + connectedDevice() { + let device = this.data.device; + if(device) { + app.globalData.ppScale.plugin.Blue.createBLEConnection(device); + } + }, - }, - - /** - * 生命周期函数--监听页面卸载 - */ - onUnload() { - - } + onUnload() { + if (this._connectStateWatcher) { + app.unwatch('ppScale.device.connectState', this._connectStateWatcher); + } + } }) \ No newline at end of file diff --git a/pages/userInfo/userInfo.wxml b/pages/userInfo/userInfo.wxml index 7199abb..daf008e 100644 --- a/pages/userInfo/userInfo.wxml +++ b/pages/userInfo/userInfo.wxml @@ -1,71 +1,102 @@ - - - - - 姓名 - - - - - - 性别 - - - {{sex.index === null ? '请选择性别' : sex.data[sex.index].name}} - - - - - 生日 - - - {{birthday.value ? birthday.label : '请选择生日'}} - - - - - - 手机号 - - - {{phone}} - - - - - - - - - 身高 - - - {{height.index === null ? '请选择身高' : height.data[height.index] + ' cm'}} - - - - - 体重 - - - {{weight.index === null ? '请选择体重' : weight.data[weight.index] + ' kg'}} - - - - - -确定 \ No newline at end of file + \ No newline at end of file diff --git a/pages/userInfo/userInfo.wxss b/pages/userInfo/userInfo.wxss index 2d80170..6959e29 100644 --- a/pages/userInfo/userInfo.wxss +++ b/pages/userInfo/userInfo.wxss @@ -1,145 +1,166 @@ -page { - padding: 30rpx 30rpx 0; -} - -.device { - width: 100%; - display: flex; - flex-wrap: nowrap; - align-items: center; - padding: 35rpx 40rpx; - border-radius: 24rpx; - box-sizing: border-box; - margin-bottom: 24rpx; - background-color: #FFFFFF; - justify-content: space-between; -} - -.device>view:nth-of-type(1)>view:nth-of-type(1) { - color: #252535; - height: 32rpx; - font-weight: bold; - font-size: 32rpx; - line-height: 32rpx; - margin-bottom: 22rpx; -} - -.device>view:nth-of-type(1)>view:nth-of-type(2) { - color: #808080; - height: 26rpx; - font-size: 26rpx; - line-height: 26rpx; -} - -.device>view:nth-of-type(2) { - height: 28rpx; - display: flex; - flex-wrap: nowrap; - align-items: center; -} - -.device>view:nth-of-type(2)>view:nth-of-type(1) { - width: 14rpx; - height: 14rpx; - border-radius: 50%; -} - -.device>view:nth-of-type(2)>view:nth-of-type(2) { - height: 28rpx; - font-weight: bold; - font-size: 28rpx; - line-height: 28rpx; - padding-left: 8rpx; -} - -.form { - width: 100%; - padding: 0 30rpx; - border-radius: 16rpx; - box-sizing: border-box; - background-color: #FFFFFF; - margin-bottom: 80rpx; -} - -.form>view { - width: 100%; - height: 120rpx; - padding: 0 10rpx; - display: flex; - flex-wrap: nowrap; - box-sizing: border-box; - border-bottom: 1rpx solid #EAECF1; -} - -.form>view>view:first-of-type { - color: #252535; - height: 120rpx; - font-size: 32rpx; - font-weight: bold; - line-height: 120rpx; -} - -.form>view>view:last-of-type { - flex: 1; - width: 0; - color: #B6B6B6; - font-size: 28rpx; - height: 120rpx; - line-height: 120rpx; - text-align: right; -} - -.form>view>view:last-of-type.arrowAfter { - padding-right: 30rpx; -} - -.form>view>view:last-of-type input { - color: #252535; - width: 100%; - height: 120rpx; - line-height: 120rpx; - text-align: right; +input { + width: 100%; + height: 58rpx; + font-size: 28rpx; + line-height: 58rpx; } .placeholderClass { - color: #B6B6B6; - font-size: 28rpx; + color: #B6B6B6; + height: 58rpx; + font-size: 28rpx; + line-height: 58rpx; } -.form>view { - border-bottom: 0; +.valueClass { + color: #77849E; + height: 58rpx; + font-size: 28rpx; + font-weight: bold; + line-height: 58rpx; } +.userInfo { + width: 100%; + height: 100%; + display: flex; + flex-flow: column; + padding: 24rpx 30rpx; + box-sizing: border-box; +} + +.header { + width: 100%; + display: flex; + flex-wrap: nowrap; + align-items: center; + padding: 30rpx 40rpx; + box-sizing: border-box; + border-radius: 16rpx; + margin-bottom: 24rpx; + background-color: #FFFFFF; +} + +.header>view:nth-of-type(1) { + flex: 1; +} + +.header>view:nth-of-type(1)>view:nth-of-type(1) { + color: #252535; + height: 32rpx; + font-weight: bold; + font-size: 32rpx; + line-height: 32rpx; + margin-bottom: 16rpx; +} + +.header>view:nth-of-type(1)>view:nth-of-type(2) { + color: #808080; + height: 26rpx; + font-size: 26rpx; + line-height: 26rpx; +} + +.header>view:nth-of-type(2) { + height: 28rpx; + display: flex; + flex-wrap: nowrap; + align-items: center; +} + +.header>view:nth-of-type(2)>view:nth-of-type(1) { + width: 12rpx; + height: 12rpx; + border-radius: 50%; + margin-right: 14rpx; +} + +.header>view:nth-of-type(2)>view:nth-of-type(2) { + height: 28rpx; + font-weight: bold; + font-size: 28rpx; + line-height: 28rpx; +} + +.section { + width: 100%; + flex: 1; + overflow: hidden; + padding: 0 30rpx; + box-sizing: border-box; + background-color: #FFFFFF; +} + +.from { + width: 100%; + margin-bottom: 66rpx; +} + +.from>view { + width: 100%; + padding: 48rpx 10rpx 0; + box-sizing: border-box; + border-bottom: 1rpx solid #EAECF1; +} + +.from>view>view:nth-of-type(1) { + color: #252535; + height: 32rpx; + font-size: 32rpx; + font-weight: bold; + line-height: 32rpx; + margin-bottom: 18rpx; +} + +.from>view>view:nth-of-type(2) { + width: 100%; + height: 58rpx; + display: flex; + flex-wrap: nowrap; + padding-right: 26rpx; + box-sizing: border-box; +} + +.from>view>view:nth-of-type(2)>view:nth-of-type(1) { + flex: 1; + width: 0; +} + +.from>view>view:nth-of-type(2)>view:nth-of-type(2) { + color: #77849E; + height: 58rpx; + font-size: 28rpx; + font-weight: bold; + line-height: 58rpx; +} + + .btn { - color: #FFFFFF; - width: 580rpx; - height: 88rpx; - font-size: 32rpx; - font-weight: bold; - line-height: 88rpx; - text-align: center; - border-radius: 44rpx; - margin: 0 auto 50rpx; - background-color: #1385FA; + width: 100%; + padding: 0 26rpx; + box-sizing: border-box; } -.tips { - color: #77849E; - width: 100%; - font-size: 26rpx; - line-height: 40rpx; - padding: 0 30rpx; - box-sizing: border-box; +.btn>view { + color: #FFFFFF; + width: 100%; + height: 88rpx; + font-size: 32rpx; + font-weight: bold; + text-align: center; + line-height: 88rpx; + border-radius: 44rpx; + margin-bottom: 40rpx; + background-color: #1385FA; } -.delBtn { - color: #1385FA; - width: 580rpx; - height: 88rpx; - font-size: 32rpx; - font-weight: bold; - line-height: 88rpx; - text-align: center; - border-radius: 44rpx; - margin: 0 auto; +.describe { + color: #77849E; + width: 100%; + font-size: 24rpx; + line-height: 40rpx; +} + +.describe text { + color: #F24439; } \ No newline at end of file