ReedawFoodApp/pageTwo/devices/pcL06.vue

423 lines
10 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="weightPages">
<view class="content ">
<view class="status">{{bleTipsText}}</view>
<view class="quan">
<view>
<text class="weight">{{weight?weight:'0.00'}}</text>{{unit}}
</view>
<view class="typeInfo" v-if="typeInfo!=0">{{typeInfo==2?'稳定重量':'实时重量'}}</view>
</view>
<view class="tips">
<text>提示</text>
<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";
export default {
data() {
return {
text: "",
typeInfo: 0,
head: "",
weight: "",
height: "",
unit: "kg",
isHeight: false,
isSave: true, //保存按钮是否显示
deviceId: "",
serviceId: "",
write: "",
notify: "",
isFinished: false, //是否锁定数据
devicesList: [],
IsLing: false, //是否点击清零按钮
IsNum: false, //是否点击锁定按钮
}
},
computed: {
...mapState(["user", "bleValue"]),
userInfo() {
return this.user
},
bleTipsText() {
return this.bleValue.bleTipsText
}
},
onLoad(options) {
let that = this
that.typeInfo = 0
that.height = that.userInfo.height
that.head = that.userInfo.head_data
that.deviceId = options.deviceId
that.$ble.createBLEConnection("FFF0")
that.$ble.onBLEConnectionStateChange()
},
watch: {
'bleValue.isBluetoothTyle'() {
let that = this
if (!that.bleValue.isBluetoothTyle) {
that.$tools.showModal('蓝牙连接已断开,请重新连接后测量')
setTimeout(function() {
uni.switchTab({
url: "/pages/index/index"
})
}, 1000)
}
},
'bleValue.serviceId'() {
let that = this
if (that.bleValue.serviceId) {
that.serviceId = that.bleValue.serviceId
that.getBLEDeviceCharacteristics(that.bleValue.serviceId)
}
},
isFinished: function() {
let that = this
console.log("是否开启弹框", that.weight, that.isFinished, that.typeInfo)
if (!that.isFinished) {
that.isHeight = false
return
}
if (that.isFinished && that.typeInfo == 2) {
setTimeout(function() {
that.isHeight = true
}, 1500)
}
}
},
methods: {
/**
* 获取指定服务的特征值
*/
getBLEDeviceCharacteristics(serviceId) {
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: serviceId,
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
that.writeBLECharacteristicValue("A6020500076A") // 清零
}
}
that.$store.commit("changeBluetoothValue", {
type: 1,
isConnectStatus: 2,
bleTipsText: "蓝牙连接成功,请上秤测量",
})
uni.notifyBLECharacteristicValueChange({
deviceId: that.deviceId,
serviceId: serviceId,
characteristicId: that.notify,
state: true,
})
uni.notifyBLECharacteristicValueChange({
deviceId: that.deviceId,
serviceId: serviceId,
characteristicId: that.write,
state: true,
})
that.isFinished = false
setTimeout(function() {
that.notifyBLECharacteristicValue(serviceId)
}, 800)
},
fail: res => {
console.log('获取特征值失败:', JSON.stringify(res))
}
})
},
/**
* 开启订阅特征值
* read: true, //读,write: true, //写,notify: true, //广播
*/
notifyBLECharacteristicValue(serviceId) {
let that = this;
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: that.deviceId,
serviceId: serviceId,
characteristicId: that.notify,
success(res) {
that.isSave = true
uni.onBLECharacteristicValueChange(function(res) {
let value = that.$tools.ab2hex(res.value, "");
let type = value.substring(4, 6) // 状态
let typeInfo = value.substring(6, 8) //实时、稳定
let weight = parseInt(value.substring(8, 14), 16) //体重
let digit = value.substring(18, 19) // 小数
let unit = value.substring(19, 20) //单位
let weight1 = ""
let weight2 = ""
// 小数
if (digit == "0") {
weight = weight
}
if (digit == "1") {
weight = weight / 10
}
if (digit == '2') {
weight = weight / 100
}
if (digit == "3") {
weight = weight / 1000
}
// 单位
if (unit == "0") {
that.unit = "kg"
}
if (unit == "1") {
that.unit = "斤"
}
if (unit == "2") {
console.log("st:lb", weight)
weight1 = Math.floor(weight / 14)
weight2 = weight - weight1 * 14
weight = weight1 + ":" + weight2.toFixed(2)
that.unit = "st:lb"
}
if (unit == "3") {
that.unit = "lb"
}
if (unit == "4") {
that.unit = "g"
}
// 实时体重
if (typeInfo == "01") {
that.typeInfo = 1
that.weight = weight
that.IsLing = false
that.isHeight = false
that.isFinished = false
}
// 稳定体重
if (typeInfo == "02" && !that.isFinished) {
that.typeInfo = 2
that.weight = weight
if (weight > 0 || toString(weight) != '0:0.00') {
that.isFinished = true
}
}
console.log("状态:", value, type, typeInfo, weight, that.unit, that.isFinished)
});
},
fail(res) {
console.log("测量失败", res.value);
}
});
},
// 清零
handleIsLing() {
let that = this
if (!that.IsLing) {
that.writeBLECharacteristicValue("A6020500076A")
that.IsLing = true
}
},
//锁定
handleIsNum() {
let that = this
that.writeBLECharacteristicValue("A6020400066A")
},
writeBLECharacteristicValue(str) {
var that = this;
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
console.log("buffer", str, buf)
uni.writeBLECharacteristicValue({
deviceId: that.deviceId,
serviceId: that.serviceId,
characteristicId: that.write,
value: buf.buffer,
success: res => {
console.log('下发指令成功', res.errMsg)
},
fail: res => {
console.log("下发指令失败", res);
},
})
},
// 保存测量结果
handleGetMeasure() {
let that = this
if (!that.height) {
this.$tools.msg("请输入身高")
return
}
that.$model.getmeasurefunit({
weight: that.weight + that.unit,
height: that.height,
aud_id: that.userInfo.aud_id,
head_data: that.head ? that.head : 0,
lefthand: 0,
righthand: 0,
leftfoot: 0,
rightfoot: 0,
heart_rate: 0,
}).then(res => {
that.isHeight = false
that.isSave = false
if (res.code == 0) {
that.$store.dispatch('getUserInfo', {
aud_id: that.user.aud_id
})
that.$store.dispatch("getResult", {
aud_id: that.user.aud_id
})
that.$tools.msg("测量成功")
} else {
that.$tools.msg("测量失败")
}
uni.switchTab({
url: "/pages/index/index"
})
})
},
//重新测量
handleBack() {
let that = this
let str = "A6020500076A"
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
uni.writeBLECharacteristicValue({
deviceId: that.deviceId,
serviceId: that.serviceId,
characteristicId: that.write,
value: buf.buffer,
success: res => {
console.log('下发指令成功', res.errMsg)
uni.showToast({
title: '数据重置中',
icon: "none"
})
console.log("重置", that.typeInfo, that.weight)
},
fail: res => {
console.log("下发指令失败", res);
},
})
},
},
}
</script>
<style scoped lang="scss">
.content {
background: #fff;
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.btnClose {
color: #fff;
background: linear-gradient(-90deg, #fccf4f, #fba418 80%) !important;
}
.baocun {
color: #fff;
background: linear-gradient(-90deg, #feaa50, #e96b13 80%) !important;
}
.quan {
width: 460rpx;
height: 460rpx;
border: 8px solid #feaa50;
border-radius: 50%;
margin: 15px auto;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
box-shadow: 0px 0px 25px 10px #e96b13;
animation: pulse 1s infinite;
.weight {
font-size: 50px;
color: #000;
font-weight: bold;
}
.typeInfo {
width: 100%;
text-align: center;
font-size: 16px;
color: #f0ae43;
}
}
@keyframes pulse {
0% {
box-shadow: 0px 0px 25px 10px #e96b13;
}
50% {
box-shadow: 0px 0px 25px 10px #fba418;
}
100% {
box-shadow: 0px 0px 25px 10px #e96b13;
}
}
.status {
width: 70%;
font-size: 16px;
height: 35px;
line-height: 35px;
text-align: center;
border-radius: 15px;
margin: 15px auto;
background-color: #ffdda6;
}
.disabled {
width: 150px;
background-color: #DFDFDF;
border: 1px solid #f7f7f7;
color: #fff;
text-align: center;
padding: 7px;
border-radius: 10px;
margin: 15px auto;
}
.tips {
font-size: 14px !important;
margin-bottom: 20px;
}
</style>