ReedawFoodApp/pageTwo/devices/G02.vue

257 lines
6.4 KiB
Vue
Raw Normal View History

2025-05-30 11:14:38 +08:00
<template>
<view class="weightPages">
<view class="content ">
2026-03-23 16:23:40 +08:00
<view class="status">{{bleTipsText}}</view>
2025-05-30 11:14:38 +08:00
<view class="text">{{text}}</view>
<view class="image">
<image src="/pageTwo/static/HC.png" class="image3"></image>
</view>
<view class="tips">
<view>提示</view>
<text>1.请确定设备已开机</text>
<text>2.请确定手机蓝牙及位置信息已打开</text>
</view>
</view>
<!-- 手动记录 -->
<view class="wrapper" v-if="isHeight">
<view class="bg"></view>
<view class="Blue">
<view class="h4">测量结果提示</view>
<view class="Blue-box">
本次测量身高为<text>{{height}}{{unit}}</text>
</view>
<view class="Blue-box">
上次测量体重为<input v-model="weight" type="digit" placeholder="请输入体重" />kg
</view>
<view class="Blue-btn Blue-close" @click="handleHeight">重新测量</view>
<view class="Blue-btn" @click="handleGetMeasure">保存测量结果</view>
</view>
</view>
</view>
</template>
<script>
import {
mapState
} from "vuex";
let myTime;
export default {
data() {
return {
text: "",
weight: "",
height: "",
height2: "",
deviceId: "",
macAddr: "",
write: "",
notify: "",
unit: "cm",
isHeight: false,
isConnection: 0,
isdevice: false,
2026-03-27 10:04:26 +08:00
Unload: false,
2025-05-30 11:14:38 +08:00
}
},
computed: {
2026-03-27 10:04:26 +08:00
...mapState(["user", "bleValue", "isBluetoothTyle"]),
2025-05-30 11:14:38 +08:00
userInfo() {
return this.user
2026-03-23 16:23:40 +08:00
},
bleTipsText() {
return this.bleValue.bleTipsText
2025-05-30 11:14:38 +08:00
}
},
onLoad(options) {
let that = this
that.weight = that.userInfo.weight
that.deviceId = options.deviceId
2026-03-25 17:27:29 +08:00
},
onUnload: function() {
let that = this
2026-03-30 15:02:56 +08:00
that.$ble.stopBluetoothDevicesDiscovery();
2026-03-25 17:27:29 +08:00
that.$ble.closeBLEConnection()
that.$ble.closeBluetoothAdapter()
2026-03-27 10:04:26 +08:00
console.log("页面返回onUnload")
2025-05-30 11:14:38 +08:00
},
watch: {
2026-03-27 10:04:26 +08:00
isBluetoothTyle: function() {
let that = this
if (!that.isBluetoothTyle) {
that.$tools.showModal('蓝牙连接已断开,请重新连接后测量')
}
},
2026-03-25 17:27:29 +08:00
bleValue: {
handler(newVal, oldVal) {
let that = this
if (newVal.serviceId) {
that.deviceId = newVal.deviceId
that.serviceId = newVal.serviceId
that.getBLEDeviceCharacteristics(newVal.serviceId)
}
},
deep: true,
immediate: true
2025-05-30 11:14:38 +08:00
},
},
methods: {
/**
* 获取指定服务的特征值
*/
2026-03-23 09:50:49 +08:00
getBLEDeviceCharacteristics(serviceId) {
2025-05-30 11:14:38 +08:00
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
2026-03-23 09:50:49 +08:00
serviceId: serviceId,
2025-05-30 11:14:38 +08:00
success: res => {
// * 读read: true, //,写write: true, //,通知notify: true
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i];
if (item.uuid.indexOf('0000FFF1') != -1) {
that.notify = item.uuid
} else if (item.uuid.indexOf('0000FFF2') != -1) {
that.write = item.uuid
}
}
2026-03-23 16:23:40 +08:00
that.$store.commit("changeBluetoothValue", {
type: 1,
isConnectStatus: 2,
2026-03-25 17:27:29 +08:00
bleTipsText: "蓝牙连接成功,请开始测量",
2026-03-23 16:23:40 +08:00
})
2025-05-30 11:14:38 +08:00
uni.notifyBLECharacteristicValueChange({
deviceId: that.deviceId,
2026-03-23 09:50:49 +08:00
serviceId: serviceId,
2025-05-30 11:14:38 +08:00
characteristicId: that.notify,
state: true,
})
uni.notifyBLECharacteristicValueChange({
deviceId: that.deviceId,
2026-03-23 09:50:49 +08:00
serviceId: serviceId,
2025-05-30 11:14:38 +08:00
characteristicId: that.write,
state: true,
})
2026-03-23 09:50:49 +08:00
that.notifyBLECharacteristicValue(serviceId)
2025-05-30 11:14:38 +08:00
},
fail: res => {
console.log('获取特征值失败:', JSON.stringify(res))
}
})
},
// 接收蓝牙数据
2026-03-23 09:50:49 +08:00
notifyBLECharacteristicValue(serviceId) {
2025-05-30 11:14:38 +08:00
let that = this;
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: that.deviceId,
2026-03-23 09:50:49 +08:00
serviceId: serviceId,
2025-05-30 11:14:38 +08:00
characteristicId: that.notify,
success(res) {
uni.onBLECharacteristicValueChange(function(res) {
let value = that.$tools.ab2hex(res.value, "");
let data = parseInt(value.substring(7, 10), 16)
let unit = parseInt(value.substring(10, 12))
let digit = parseInt(value.substring(12, 14))
if (digit == "1") {
data = data / 10
}
if (digit == "2") {
data = data / 100
}
if (unit == "0") {
that.unit = "cm"
}
if (unit == "1") {
that.unit = "inch"
}
if (unit == "2") {
that.unit = "ft"
}
if (Number(data) < 20) {
that.text = "操作错误,请重新测量"
} else {
if (unit == "2") {
let data1 = data / 12
let data2 = Number(data1 - Math.floor(data1)) * 12
that.height2 = data
that.height = Math.floor(data1) + "'" + data2.toFixed(1)
} else {
that.height2 = data
that.height = data
}
that.text = "您的身高是:" + that.height + that.unit
that.isHeight = true
}
console.log("G02", value, unit, data, that.height, that.height2)
})
},
fail(res) {
console.log("测量失败", res.value);
}
})
},
// 保存测量结果
handleGetMeasure() {
let that = this
let height = 0
if (!that.weight) {
that.$tools.msg("请输入体重")
return
}
if (that.unit == 'ft') {
height = Number(that.height2 * 2.54).toFixed(2)
} else {
height = that.height2
}
that.$model.getmeasurefunit({
adc: 0,
2026-03-25 17:27:29 +08:00
weight: that.weight + 'kg',
2025-05-30 11:14:38 +08:00
height: height,
2026-03-23 16:23:40 +08:00
aud_id: that.user.aud_id,
2025-05-30 11:14:38 +08:00
}).then(res => {
that.isHeight = false
2026-03-23 09:50:49 +08:00
console.log("报告", res)
2025-05-30 11:14:38 +08:00
if (res.code == 0) {
2026-03-23 16:23:40 +08:00
that.$store.dispatch('getUserInfo', {
aud_id: that.user.aud_id
})
2026-03-25 17:27:29 +08:00
that.$store.dispatch("getResult", {
aud_id: that.user.aud_id
})
2025-05-30 11:14:38 +08:00
that.$tools.msg("测量成功")
} else {
that.$tools.msg("测量失败")
}
2026-03-27 10:04:26 +08:00
setTimeout(function() {
2026-03-25 17:27:29 +08:00
uni.switchTab({
url: "/pages/index/index"
})
2026-03-27 10:04:26 +08:00
}, 500)
2025-05-30 11:14:38 +08:00
})
},
handleHeight() {
let that = this
that.text = ""
2026-03-23 09:50:49 +08:00
that.height = ""
2025-05-30 11:14:38 +08:00
that.isHeight = false
},
},
}
</script>
<style scoped lang="scss">
.image3 {
width: 200px !important;
height: 340px !important;
}
.status {
width: 70%;
font-size: 16px;
height: 35px;
line-height: 35px;
text-align: center;
border-radius: 15px;
margin: 15px auto;
background-color: #ffdda6;
}
</style>