Commit 3118a781 by 姜雷

修改洗浴使用逻辑

parent 3a091731
......@@ -86,6 +86,10 @@ class Pay extends Component {
};
this.checkBluetoothAndWs();
}
componentWillUnmount() {
// this.closeBluetooth();
// this.closeDeviceSocket();
}
connectDeviceSocket() {
Taro.connectSocket({ url: SOCKET_URL }).then(task => {
......@@ -103,11 +107,11 @@ class Pay extends Component {
});
task.onMessage(res => {
const msg: string = ab2str(res.data);
console.log('socket onMessage: ', msg);
console.log('获取socket消息: ', msg);
if (msg === '[0]') {
console.log('结束蓝牙以及socket: ', msg);
this.closeDeviceSocket();
this.closeBluetoothConnection();
// this.closeDeviceSocket();
// this.closeBluetoothConnection();
} else if (msg === '[]') {
} else {
if (msg.length > 100) {
......@@ -206,11 +210,13 @@ class Pay extends Component {
}
checkIsShower(): boolean {
const { device } = this.props;
return device.serviceId === 2;
return device.serviceId === 5;
}
checkBluetoothAndWs() {
if (this.checkIsShower()) {
console.log('打开蓝牙和WebSocket');
this.openBluetooth();
this.connectDeviceSocket();
}
}
......@@ -218,9 +224,14 @@ class Pay extends Component {
Taro.openBluetoothAdapter()
.then(res => {
console.log(res);
this.setState({
bluetoothState: true,
});
this.setState(
{
bluetoothState: true,
},
() => {
this.startConnectDevice();
},
);
})
.catch(err => {
console.error(err);
......@@ -271,6 +282,8 @@ class Pay extends Component {
}
startDevicesDiscovery() {
console.log('in startDevicesDiscovery', this.checkBluetoothState());
if (this.checkBluetoothState()) {
return Taro.startBluetoothDevicesDiscovery()
.then(() => {
......@@ -314,7 +327,7 @@ class Pay extends Component {
console.error('startBluetoothDevicesDiscovery: ', err);
// if (err.msg === BlueToothError.DeviceNotFound) {
Taro.stopBluetoothDevicesDiscovery();
this.sendStartShower();
// this.sendStartShower();
throw err;
// } else {
// return this.warnBluetoothOpen();
......@@ -362,7 +375,7 @@ class Pay extends Component {
console.log('获取socket消息: ', datastr);
if (datastr === '<>') {
console.log('结束蓝牙以及socket: ', datastr);
this.closeDeviceSocket();
// this.closeDeviceSocket();
this.closeBluetoothConnection();
} else {
if (
......@@ -382,14 +395,12 @@ class Pay extends Component {
});
}
} else if (datastr.substring(0, 1) === '{') {
console.log('接受头部数据: ', datastr);
abStr = datastr;
} else if (
abStr.length &&
datastr.substring(datastr.length - 1, datastr.length) === '}'
) {
abStr += datastr;
console.log('接受尾部数据后: ', abStr);
console.log('<---发送完整数据: ', abStr);
if (socketTask) {
socketTask.send({
......@@ -410,7 +421,7 @@ class Pay extends Component {
});
Taro.onBLEConnectionStateChange(res => {
const { showerState } = this.state;
// const { showerState } = this.state;
console.log(
`device ${res.deviceId} state has changed, connected: ${
res.connected
......@@ -427,9 +438,9 @@ class Pay extends Component {
this.setState({
deviceDone: false,
});
if (showerState) {
this.reConnectDeviceBluetooth();
}
// if (showerState) {
this.reConnectDeviceBluetooth(); // 只要断了就重连
// }
reject('蓝牙未成功连接');
}
});
......@@ -521,8 +532,8 @@ class Pay extends Component {
let msg = ab2str(res.value);
console.log('获取蓝牙设备消息: ', msg);
if (msg === '<>') {
this.closeBluetoothConnection();
this.closeDeviceSocket();
// this.closeBluetoothConnection();
// this.closeDeviceSocket();
} else {
if (
msg.substring(0, 1) === '{' &&
......@@ -540,14 +551,12 @@ class Pay extends Component {
},
});
} else if (msg.substring(0, 1) === '{') {
console.log('接受头部数据: ', msg);
abStr = msg;
} else if (
abStr.length &&
msg.substring(msg.length - 1, msg.length) === '}'
) {
abStr += msg;
console.log('接受尾部数据后: ', abStr);
console.log('<---发送给socket完整数据: ', abStr);
socketTask &&
......@@ -597,13 +606,7 @@ class Pay extends Component {
});
}
startUseShower(reConnect: boolean) {
if (!reConnect) {
Taro.showLoading({
title: '开启中',
mask: true,
});
}
startConnectDevice() {
const { sockedDone } = this.state;
if (!sockedDone) {
this.connectDeviceSocket();
......@@ -639,11 +642,11 @@ class Pay extends Component {
.then(res => this.openNotify(res))
.then(() => {
this.sendDeviceCode();
if (!reConnect) {
setTimeout(() => {
this.sendStartShower();
}, 2000);
}
// if (!reConnect) {
// setTimeout(() => {
// this.sendStartShower();
// }, 2000);
// }
})
.catch(err => {
console.error(err);
......@@ -653,6 +656,31 @@ class Pay extends Component {
sendStartShower() {
console.log('开启洗浴');
// const { code } = this.props.device;
// const { userinfo } = this.props;
// startShowerEquipment({
// deviceCode: code,
// customerId: userinfo.customerId.toString(),
// customerName: userinfo.customerName,
// customerPhone: userinfo.customerPhone,
// })
// .then(res => {
// console.log(res);
// Taro.showToast({
// title: '开启成功!',
// });
// this.setState({
// showerState: true,
// });
// })
// .catch(err => {
// console.error(err);
// Taro.showToast({
// title: err.msg || '开启失败!',
// icon: 'none',
// });
// this.closeBluetoothConnection();
// });
}
selectPayConfig(id: number) {
......@@ -666,8 +694,17 @@ class Pay extends Component {
device,
userinfo: { customerId },
} = this.props;
const { payId } = this.state;
const { payId, deviceDone, sockedDone } = this.state;
if (payId) {
if (this.checkIsShower()) {
console.log(deviceDone, sockedDone);
if (!deviceDone || !sockedDone) {
return Taro.showModal({
title: '提示',
content: '设备连接中...请稍后再试',
});
}
}
getPayOrder({
equipmentNum: device.originCode,
equipmentPos: device.position,
......@@ -729,14 +766,26 @@ class Pay extends Component {
}
gobackHandle() {
if (this.checkIsShower()) {
} else {
setTimeout(() => {
Taro.navigateTo({
url: '/pages/index/index?', // 需要问号不然回到首页可以取到业务参数
});
}, 2000);
}
// if (this.checkIsShower()) {
// // this.sendStartShower();
// Taro.showModal({
// title: '提示',
// content: '开启洗浴设备',
// success: res => {
// if (res.confirm) {
// console.log('用户点击确定');
// } else if (res.cancel) {
// console.log('用户点击取消');
// }
// },
// });
// } else {
setTimeout(() => {
Taro.navigateTo({
url: '/pages/index/index?', // 需要问号不然回到首页可以取到业务参数
});
}, 2000);
// }
}
render() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment