405 lines
8.8 KiB
Vue
405 lines
8.8 KiB
Vue
<template>
|
|
<view class="weightPages">
|
|
<view class="table">
|
|
<view class="text">
|
|
<image src="/static/zhong.png"></image>
|
|
<text @click="openBluetoothAdapter">{{bleTipsText}}</text>
|
|
</view>
|
|
</view>
|
|
<view class="name">{{name}}</view>
|
|
<!-- 蓝牙称重 -->
|
|
<view class="weight-wrap">
|
|
<view class="weight" @click="inputDialogToggle">
|
|
<text class="val">{{weight == '' ? '0.0':weight}}</text>
|
|
<text class="unit">{{unit}}</text>
|
|
</view>
|
|
<view class="weight">
|
|
<text class="val">{{kcal?kcal:0}}</text>
|
|
<text class="unit">kcal</text>
|
|
</view>
|
|
</view>
|
|
<view class="groupbtn">
|
|
<view class="btn danwei">
|
|
<view class="lan border-bottom">
|
|
<view class="right">
|
|
<picker mode="selector" :range="unitList" range-key="name" @change="changleUnits"
|
|
:value="unitListIndex">
|
|
<view class="uni-input">
|
|
单位
|
|
</view>
|
|
</picker>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
<view class="btn" @click="handleDetailSub" :style="{'width':!stopblue?'20%':'45%'}">保存</view>
|
|
<view class="btn" @click="handleDetailNext" v-if="!stopblue">下一味</view>
|
|
<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>
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<script>
|
|
import {
|
|
mapState
|
|
} from "vuex";
|
|
export default {
|
|
data() {
|
|
return {
|
|
kcal: "",
|
|
// weight: "",
|
|
weightALL: "",
|
|
bleTipsText: "",
|
|
unit: 'g',
|
|
weight0: 0,
|
|
stopblue: false,
|
|
devicesList: [],
|
|
unitList: [{
|
|
name: "克",
|
|
id: '00',
|
|
unit: "g"
|
|
}, {
|
|
name: "盎司",
|
|
id: "08",
|
|
unit: "oz"
|
|
}],
|
|
unitListIndex: 0,
|
|
units: ['kg', '斤', 'st:lb', 'lb', 'g', 'ml', 'Waterml',
|
|
'milkml', 'oz', 'floz', 'lboz'
|
|
]
|
|
}
|
|
},
|
|
props: {
|
|
weightKcal: {
|
|
type: Number,
|
|
default: 0 //当前测量食物每g含的kcal
|
|
},
|
|
isLast: {
|
|
type: Boolean,
|
|
default: false
|
|
},
|
|
name: {
|
|
type: String,
|
|
default: ''
|
|
}
|
|
},
|
|
computed: {
|
|
...mapState(["user", "isBluetoothTyle", "bleValue"]),
|
|
weight() {
|
|
let kcal = (Number(this.weightKcal) * this.bleValue.countWeight).toFixed(2)
|
|
this.unit = this.unitConversion(this.bleValue.unit)
|
|
this.kcal = this.convertToGrams(kcal, this.bleValue.unit).toFixed(2)
|
|
return this.bleValue.countWeight
|
|
},
|
|
isConnection() {
|
|
this.bleTipsText = this.bleValue.bleTipsText
|
|
return this.bleValue.isConnectStatus
|
|
},
|
|
},
|
|
mounted() {
|
|
let that = this
|
|
uni.onBluetoothAdapterStateChange(function(res) {
|
|
that.$store.commit("changeBluetooth", res.available);
|
|
})
|
|
},
|
|
watch: {
|
|
isBluetoothTyle: function() {
|
|
let that = this
|
|
if (!that.isBluetoothTyle) {
|
|
that.handleBack()
|
|
}
|
|
},
|
|
isLast: function() {
|
|
let that = this
|
|
that.stopblue = that.isLast
|
|
}
|
|
},
|
|
methods: {
|
|
// 初始化蓝牙
|
|
openBluetoothAdapter() {
|
|
let that = this
|
|
if (that.isConnection == 0) return
|
|
that.kcal = ""
|
|
that.$store.commit('changeBluetoothValue', {
|
|
deviceId: "",
|
|
serviceId: "",
|
|
notify: '',
|
|
write: '',
|
|
unit: "g",
|
|
countWeight: "",
|
|
bleTipsText: "蓝牙搜索中",
|
|
isConnectStatus: 0,
|
|
})
|
|
that.$ble.openBluetoothAdapter()
|
|
},
|
|
changleUnits(e) {
|
|
let that = this
|
|
let name = that.unitList[e.detail.value].name
|
|
console.log("单位切换", name, that.unit)
|
|
if (that.unit != name) {
|
|
that.handletoggleUnit(name == '盎司' ? 0x08 : 0x04)
|
|
}
|
|
that.unitListIndex = [e.detail.value]
|
|
that.$store.commit('changeBluetoothValue', {
|
|
unit: that.unitList[e.detail.value].unit
|
|
})
|
|
|
|
},
|
|
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)
|
|
},
|
|
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);
|
|
},
|
|
})
|
|
},
|
|
// 保存测量结果
|
|
handlesub() {
|
|
let that = this
|
|
console.log("测量保存", that.weight, that.unit, that.kcal)
|
|
if (Number(that.weight) > 0) {
|
|
that.$emit("handleBle", that.weight, that.unit, that.kcal)
|
|
} else {
|
|
that.$tools.msg("数据异常,请清零后重新测量!")
|
|
}
|
|
|
|
},
|
|
convertToGrams(value, fromUnit) {
|
|
const conversionFactors = {
|
|
'lb': 453.59, // 1磅 = 453.59237克
|
|
'oz': 28.35, // 1盎司 = 28.349523125克
|
|
'kg': 1000, // 1公斤 = 1000克
|
|
'g': 1
|
|
};
|
|
|
|
if (!conversionFactors.hasOwnProperty(fromUnit)) {
|
|
return ''
|
|
}
|
|
|
|
return value * conversionFactors[fromUnit];
|
|
},
|
|
// 备料完成
|
|
handleDetailSub() {
|
|
let that = this
|
|
if (Number(that.weight) > 0) {
|
|
that.$emit("handleDetailSub", that.weight, that.unit, that.kcal)
|
|
} else {
|
|
that.$tools.msg("数据异常,请重新测量!")
|
|
}
|
|
},
|
|
//备料下一个
|
|
handleDetailNext() {
|
|
let that = this
|
|
if (Number(that.weight) > 0) {
|
|
that.$emit("handleDetailNext", that.weight, that.unit, that.kcal)
|
|
} else {
|
|
that.$tools.msg("数据异常,请清零后重新测量!")
|
|
}
|
|
},
|
|
unitConversion(unit) {
|
|
if (unit == 'kcal') {
|
|
return '千卡'
|
|
} else if (unit == 'g') {
|
|
return '克'
|
|
} else if (unit == 'lb') {
|
|
return '磅'
|
|
} else if (unit == 'oz') {
|
|
return '盎司'
|
|
}
|
|
return unit
|
|
},
|
|
handleBack() {
|
|
let that = this
|
|
that.$store.commit("changeBluetoothValue", {
|
|
bleTipsText: "连接失败,点击重新连接",
|
|
isConnectStatus: 1
|
|
})
|
|
that.$ble.stopBluetoothDevicesDiscovery() //取消蓝牙搜索
|
|
that.$ble.closeBLEConnection(that.bleValue.deviceId)
|
|
that.$ble.closeBluetoothAdapter()
|
|
},
|
|
confirm(value) {
|
|
console.log("手动输入", value)
|
|
this.$store.commit("changeBluetoothValue", {
|
|
countWeight: value,
|
|
unit: this.unitList[this.unitListIndex].unit
|
|
})
|
|
this.$refs.popup.close()
|
|
},
|
|
close() {
|
|
this.$refs.popup.close()
|
|
},
|
|
inputDialogToggle() {
|
|
this.$refs.popup.open()
|
|
},
|
|
},
|
|
}
|
|
</script>
|
|
|
|
<style scoped lang="scss">
|
|
.table {
|
|
width: 100%;
|
|
font-size: 14px;
|
|
align-items: center;
|
|
padding: 5px 0;
|
|
border-radius: 5px;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
|
|
.text {
|
|
color: #8284f0;
|
|
display: flex;
|
|
|
|
image {
|
|
width: 22px;
|
|
height: 22px;
|
|
margin-right: 5px;
|
|
}
|
|
}
|
|
}
|
|
|
|
.weightPages {
|
|
position: absolute;
|
|
left: 15px;
|
|
right: 15px;
|
|
bottom: 30rpx;
|
|
top: 15px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: space-between;
|
|
|
|
.weight {
|
|
background: #fff;
|
|
color: #666;
|
|
font-size: 16px;
|
|
flex-wrap: wrap;
|
|
text-align: center;
|
|
|
|
view {
|
|
width: 60%;
|
|
height: 100rpx;
|
|
display: flex;
|
|
margin-left: 25%;
|
|
align-items: flex-end;
|
|
margin-bottom: 30rpx;
|
|
|
|
text {
|
|
width: 80px;
|
|
display: inline-block;
|
|
border-bottom: 1px solid #dfdfdf;
|
|
margin: 0 20rpx;
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
color: #f0ae43;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
.tips {
|
|
font-size: 24rpx;
|
|
text-align: center;
|
|
}
|
|
|
|
.groupbtn {
|
|
margin-top: 0;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
|
|
.btn {
|
|
width: 20%;
|
|
color: $maincolor;
|
|
text-align: center;
|
|
height: 40px;
|
|
line-height: 40px;
|
|
border-radius: 10px;
|
|
border: 1px solid $maincolor;
|
|
background: #fff;
|
|
margin: 0;
|
|
|
|
view {
|
|
border: none !important;
|
|
width: auto !important;
|
|
height: 40px !important;
|
|
line-height: 40px !important;
|
|
}
|
|
}
|
|
}
|
|
|
|
.weight-wrap {
|
|
display: flex;
|
|
justify-content: space-around;
|
|
align-items: center;
|
|
color: #666;
|
|
font-size: 16px;
|
|
text-align: center;
|
|
height: 60px;
|
|
border-radius: 10px;
|
|
|
|
.weight {
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
width: 48%;
|
|
padding: 30rpx 0;
|
|
border-radius: 20rpx;
|
|
background-color: #F8F8F8;
|
|
|
|
.val {
|
|
font-size: 54rpx;
|
|
color: #F0AE43;
|
|
margin: 0 !important;
|
|
}
|
|
|
|
.unit {
|
|
padding: 10rpx;
|
|
margin-left: 20rpx;
|
|
font-size: 28rpx;
|
|
color: #fff;
|
|
border-radius: 8rpx;
|
|
background-color: #F0AE43;
|
|
}
|
|
}
|
|
}
|
|
|
|
.name {
|
|
width: 100%;
|
|
text-align: center;
|
|
font-size: 16px;
|
|
font-weight: bold;
|
|
}
|
|
}
|
|
</style> |