kitchendDevice/components/bluetooth_food.vue

449 lines
9.8 KiB
Vue
Raw Normal View History

2025-07-28 16:57:16 +08:00
<template>
2025-11-08 16:50:26 +08:00
<view class="weightPages">
2025-11-25 14:21:22 +08:00
<view class="table">
<view class="text">
<image src="/static/zhong.png"></image>
2025-11-26 17:32:18 +08:00
<text @click="openBluetoothAdapter">{{bleTipsText}}</text>
2025-11-25 14:21:22 +08:00
</view>
2026-01-27 16:10:01 +08:00
<view class="duan" @click="handleBack" v-if="isShow&&isConnection == 2">
2025-11-25 14:21:22 +08:00
断开连接
</view>
</view>
<!-- -->
<view class="weight-wrap">
<!-- 蓝牙称重 -->
<view class="weight" @click="inputDialogToggle">
2025-11-29 10:02:33 +08:00
<text class="val">{{weight == '' ? '0.0':weight}}</text>
2025-12-03 15:34:39 +08:00
<text class="unit">{{unitConversion(unit)}}</text>
2025-11-25 14:21:22 +08:00
</view>
</view>
<!-- -->
<view class="groupbtn">
<view class="btn danwei">
<view class="lan border-bottom">
<view class="right">
2026-01-27 16:10:01 +08:00
<picker mode="selector" :range="unitA" range-key="name" @change="changleUnits"
2025-11-25 14:21:22 +08:00
:value="unitListIndex">
<view class="uni-input">
单位
</view>
</picker>
</view>
</view>
2025-11-08 16:50:26 +08:00
</view>
2025-12-03 15:34:39 +08:00
<view class="btn addbtn size14" @click="handlesub" v-if="btnType==2">保存</view>
<view class="btn addbtn" @click="handleAddFood" v-if="btnType==1">+</view>
2025-11-25 14:21:22 +08:00
<view class="btn qingling" @click="handleqingling">清零</view>
</view>
<!-- -->
<view>
<uni-popup ref="popup" type="dialog">
<uni-popup-dialog mode="input" title="重量" placeholder="请输入食物重量" @close="close"
@confirm="confirm"></uni-popup-dialog>
</uni-popup>
2025-11-08 16:50:26 +08:00
</view>
</view>
2025-07-28 16:57:16 +08:00
</template>
<script>
2025-11-08 16:50:26 +08:00
import {
mapState
} from "vuex";
let myTime
let nextCnt = 0
export default {
data() {
return {
kcal: 0,
unit: '',
2025-11-25 14:21:22 +08:00
bleTipsText: "",
inputDialog: false,
2026-01-27 16:10:01 +08:00
unitA: [],
2025-11-25 14:21:22 +08:00
unitListIndex: 0,
2025-11-08 16:50:26 +08:00
}
},
props: {
weightKcal: {
type: Number,
default: 0 //当前测量食物每100g含的kcal
},
2025-11-25 14:21:22 +08:00
btnType: {
type: Number,
default: 1 //1添加食材2保存测量
2026-01-27 16:10:01 +08:00
}
2025-11-08 16:50:26 +08:00
},
computed: {
2025-11-25 14:21:22 +08:00
...mapState(["bleValue", "isBluetoothTyle", "countFoodInfo"]),
weight() {
2026-01-27 16:10:01 +08:00
let weightNew = this.convertToGrams(this.bleValue.countWeight, this.bleValue.unit)
this.kcal = (Number(this.weightKcal) / 100 * weightNew).toFixed(1)
2025-12-03 15:34:39 +08:00
this.unit = this.bleValue.unit
2025-11-25 14:21:22 +08:00
return this.bleValue.countWeight
2025-11-26 17:32:18 +08:00
},
isConnection() {
this.bleTipsText = this.bleValue.bleTipsText
return this.bleValue.isConnectStatus
},
2025-11-29 10:02:33 +08:00
isShow() {
2025-11-26 17:32:18 +08:00
return this.bleValue.serviceId != '' ? true : false
2026-01-27 16:10:01 +08:00
},
2025-11-08 16:50:26 +08:00
},
mounted() {
let that = this
uni.onBluetoothAdapterStateChange(function(res) {
that.$store.commit("changeBluetooth", res.available);
})
},
destroyed() {
2025-11-25 14:21:22 +08:00
// this.isConnection = 1
// this.closeBLEConnection()
// this.closeBluetoothAdapter()
2025-11-08 16:50:26 +08:00
},
watch: {
isBluetoothTyle: function() {
let that = this
if (!that.isBluetoothTyle) {
that.handleBack()
}
},
2026-01-27 16:10:01 +08:00
bleValue: {
handler(newVal, oldVal) {
this.unitA = newVal.unitList
2026-01-29 10:51:54 +08:00
this.unitListIndex = newVal.unitList.findIndex(ite => ite.unit == newVal.unit)
2026-01-27 16:10:01 +08:00
},
deep: true,
immediate: true
},
2025-11-08 16:50:26 +08:00
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
let that = this
2026-01-27 16:10:01 +08:00
if (that.isConnection == 2) return
2025-11-08 16:50:26 +08:00
that.kcal = ""
2025-11-26 17:32:18 +08:00
that.$store.commit('changeBluetoothValue', {
deviceId: "",
serviceId: "",
notify: '',
write: '',
unit: "g",
2026-01-27 16:10:01 +08:00
type: 1,
unitList: that.$json.unitMinus,
2026-03-06 14:08:59 +08:00
countWeight: 0,
oldCountWeight: 0,
2025-11-26 17:32:18 +08:00
bleTipsText: "蓝牙搜索中",
isConnectStatus: 0,
2025-11-08 16:50:26 +08:00
})
2025-11-26 17:32:18 +08:00
that.$ble.openBluetoothAdapter()
2025-11-08 16:50:26 +08:00
},
2025-11-25 14:21:22 +08:00
changleUnits(e) {
let that = this
2026-01-27 16:10:01 +08:00
let name = that.unitA[e.detail.value].name
let val = that.unitA[e.detail.value].unit
2025-11-26 17:32:18 +08:00
if (that.isShow && that.unit != name) {
2026-01-27 16:10:01 +08:00
that.handletoggleUnit(that.unitConversion2(val))
}
if (that.bleValue.serviceId == '') {
that.unitListIndex = [e.detail.value]
that.$store.commit('changeBluetoothValue', {
unit: that.unitA[e.detail.value].unit,
})
}
},
convertToGrams(value, fromUnit) {
const conversionFactors = {
'lb': 453.59237, // 1磅 = 453.59237克
'oz': 28.349523125, // 1盎司 = 28.349523125克
'kg': 1000, // 1公斤 = 1000克
'g': 1,
'ml': 1,
'斤': 500,
'Waterml': 1,
'milkml': 1.03
// "stlb": "floz": "lboz":
};
if (!conversionFactors.hasOwnProperty(fromUnit)) {
return ''
2025-11-25 14:21:22 +08:00
}
2026-01-27 16:10:01 +08:00
return value * conversionFactors[fromUnit];
2025-11-25 14:21:22 +08:00
},
2026-01-27 16:10:01 +08:00
unitConversion2(unit) {
if (unit == 'kg') {
return 0x00
} else if (unit == '斤') {
return 0x01
} else if (unit == 'st:lb') {
return 0x02
} else if (unit == 'lb') {
return 0x03
} else if (unit == 'g') {
return 0x04
} else if (unit == 'ml') {
return 0x05
} else if (unit == 'Waterml') {
return 0x06
} else if (unit == 'milkml') {
return 0x07
} else if (unit == 'oz') {
return 0x08
} else if (unit == 'floz') {
return 0x09
} else if (unit == 'lboz') {
return 0x0A
}
return unit
},
// 下发单位
2025-11-25 14:21:22 +08:00
handletoggleUnit(unit) {
let that = this
let checksum = 0;
const bytes = [0xC5, 0x03, 0x05, 0x11]
bytes[4] = unit
for (let i = 0; i < bytes.length; i++) {
checksum ^= bytes[i];
}
bytes[5] = checksum
that.sendData(new Uint8Array(bytes).buffer)
},
2026-01-27 16:10:01 +08:00
// 清零
2025-11-25 14:21:22 +08:00
handleqingling() {
let that = this
let str = "C503071100D0"
let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
return parseInt(h, 16)
}))
that.sendData(buf.buffer)
},
sendData(buffer) {
let that = this
uni.writeBLECharacteristicValue({
deviceId: that.bleValue.deviceId,
serviceId: that.bleValue.serviceId,
characteristicId: that.bleValue.write,
value: buffer,
success: res => {
console.log('下发指令成功', res.errMsg)
},
fail: res => {
console.log("下发指令失败", res);
},
})
},
2025-11-08 16:50:26 +08:00
// 保存测量结果
handlesub() {
let that = this
2025-11-29 10:02:33 +08:00
console.log("测量保存", that.weight, that.unit, that.kcal)
2025-11-08 16:50:26 +08:00
if (Number(that.weight) > 0) {
2025-11-26 17:32:18 +08:00
that.$emit("handleBle", that.weight, that.unit, that.kcal)
2025-11-08 16:50:26 +08:00
} else {
that.$tools.msg("数据异常,请清零后重新测量!")
}
},
handleBack() {
let that = this
2025-11-26 17:32:18 +08:00
that.$store.commit("changeBluetoothValue", {
bleTipsText: "连接失败,点击重新连接",
2026-01-27 16:10:01 +08:00
unitList: that.$json.unitMinus,
2026-01-29 10:51:54 +08:00
isConnectStatus: 1,
2025-11-08 16:50:26 +08:00
})
2025-11-26 17:32:18 +08:00
that.$ble.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
that.$ble.closeBLEConnection(that.bleValue.deviceId)
that.$ble.closeBluetoothAdapter()
2025-11-08 16:50:26 +08:00
},
2025-11-26 17:32:18 +08:00
2025-11-08 16:50:26 +08:00
unitConversion(unit) {
2026-01-27 16:10:01 +08:00
if (unit == 'kg') {
return '千克'
} else if (unit == '斤') {
return '斤'
} else if (unit == 'stlb') {
return 'stlb'
2026-01-29 10:51:54 +08:00
} else if (unit == 'lb') {
2026-01-27 16:10:01 +08:00
return '磅'
2025-11-08 16:50:26 +08:00
} else if (unit == 'g') {
return '克'
2026-01-27 16:10:01 +08:00
} else if (unit == 'ml') {
return '毫升'
} else if (unit == 'Waterml') {
return 'Waterml'
} else if (unit == 'milkml') {
return 'milkml'
2025-11-08 16:50:26 +08:00
} else if (unit == 'oz') {
return '盎司'
2026-01-27 16:10:01 +08:00
} else if (unit == 'floz') {
return 'floz'
} else if (unit == 'lboz') {
return 'lboz'
2025-11-08 16:50:26 +08:00
}
return unit
2025-11-25 14:21:22 +08:00
},
// 添加食物
handleAddFood() {
uni.navigateTo({
url: "/pageTwo/count/search?name=早餐&time=" + this.countFoodInfo.date
})
},
inputDialogToggle() {
this.$refs.popup.open()
},
2026-01-29 10:51:54 +08:00
// 手动输入
2025-11-25 14:21:22 +08:00
confirm(value) {
console.log(value)
2026-01-27 16:10:01 +08:00
let that = this
2025-11-25 14:21:22 +08:00
this.$store.commit("changeBluetoothValue", {
countWeight: value,
2026-01-27 16:10:01 +08:00
unit: this.unitA[this.unitListIndex].unit
2025-11-25 14:21:22 +08:00
})
this.$refs.popup.close()
},
close() {
this.$refs.popup.close()
},
2025-11-08 16:50:26 +08:00
},
}
2025-07-28 16:57:16 +08:00
</script>
<style scoped lang="scss">
2025-11-25 14:21:22 +08:00
image {
width: 22px;
height: 22px;
}
.more {
padding: 6rpx 10rpx;
border-radius: 12rpx;
color: #fff;
background-color: #f0ae43;
}
2025-11-08 16:50:26 +08:00
.weightPages {
display: flex;
flex-wrap: wrap;
flex-direction: column;
position: relative;
background: #fff;
2025-11-25 14:21:22 +08:00
border-radius: 20rpx;
2025-11-08 16:50:26 +08:00
justify-content: space-around;
.weight-wrap {
display: flex;
2025-11-25 14:21:22 +08:00
justify-content: center;
2025-11-08 16:50:26 +08:00
align-items: center;
background: #fff;
color: #666;
font-size: 16px;
text-align: center;
2025-11-25 14:21:22 +08:00
height: 60px;
margin: 10px 0;
2025-11-08 16:50:26 +08:00
.weight,
.kcal {
display: flex;
justify-content: center;
align-items: center;
width: 70%;
padding: 30rpx 0;
border-radius: 20rpx;
background-color: #F8F8F8;
}
.weight {
.val {
2025-12-04 15:02:07 +08:00
font-size: 54rpx;
2025-11-08 16:50:26 +08:00
color: #F0AE43;
margin: 0 !important;
}
.unit {
padding: 10rpx;
margin-left: 30rpx;
font-size: 28rpx;
color: #fff;
border-radius: 8rpx;
background-color: #F0AE43;
}
}
}
.tips {
2025-11-25 14:21:22 +08:00
font-size: 24rpx;
2025-11-08 16:50:26 +08:00
text-align: center;
}
.groupbtn {
2025-11-25 14:21:22 +08:00
margin-top: 15px;
display: flex;
align-items: center;
justify-content: space-between;
2025-11-08 16:50:26 +08:00
.btn {
2025-11-25 14:21:22 +08:00
width: 30%;
color: $maincolor;
text-align: center;
height: 40px;
line-height: 40px;
border-radius: 10px;
border: 1px solid $maincolor;
background: #fff;
margin: 0;
}
.addbtn {
width: 30%;
color: #fff;
font-size: 45px;
line-height: 38px;
background: $maincolor;
2025-11-08 16:50:26 +08:00
}
}
.table {
width: 100%;
font-size: 14px;
2025-11-25 14:21:22 +08:00
align-items: center;
2025-11-08 16:50:26 +08:00
padding: 5px 0;
border-radius: 5px;
2025-11-25 14:21:22 +08:00
display: flex;
justify-content: space-between;
.text {
color: #8284f0;
display: flex;
}
image {
width: 22px;
height: 22px;
margin-right: 5px;
}
2025-11-08 16:50:26 +08:00
}
.image {
2025-11-25 14:21:22 +08:00
width: 1120rpx;
height: 1120rpx;
2025-11-08 16:50:26 +08:00
margin: auto;
image {
width: 100%;
height: 100%;
}
}
2025-11-25 14:21:22 +08:00
.duan {
width: fit-content;
background: linear-gradient(-90deg, #d4f5c4, #a7d5e4 80%, );
border-radius: 5px;
text-align: center;
padding: 3px 10px;
font-size: 12px;
}
2025-11-08 16:50:26 +08:00
.tips {
margin-top: 30rpx;
2025-11-25 14:21:22 +08:00
margin-left: 30rpx;
2025-11-08 16:50:26 +08:00
display: flex;
color: #999;
}
}
2025-07-28 16:57:16 +08:00
</style>