Commit 3118a781 by 姜雷

修改洗浴使用逻辑

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