|
@@ -1,22 +1,23 @@
|
|
|
<template>
|
|
|
<div>
|
|
|
- <van-sticky :offset-top="0" v-if="dialogueLst.length">
|
|
|
+ <van-sticky :offset-top="0" v-if="robotList.length">
|
|
|
<van-dropdown-menu>
|
|
|
<van-dropdown-item
|
|
|
@change="change"
|
|
|
- v-model="robot"
|
|
|
+ v-model="deviceId"
|
|
|
:options="robotList"
|
|
|
/>
|
|
|
</van-dropdown-menu>
|
|
|
</van-sticky>
|
|
|
+ <van-cell title="党建机器人" size="large" value="" />
|
|
|
<div id="robot" class="app">
|
|
|
<div class="van-hairline--surround msg" ref="scroll">
|
|
|
<div class="" ref="msgScroll">
|
|
|
<div
|
|
|
class="msgBg"
|
|
|
- v-for="item in dialogueLst"
|
|
|
+ v-for="item in dialogueList"
|
|
|
:style="'text-align: ' + (item.type === 'local' ? 'right' : 'left')"
|
|
|
- :key="item.text"
|
|
|
+ :key="item.reply"
|
|
|
>
|
|
|
<div
|
|
|
style="display: inline-block;text-align: left;"
|
|
@@ -33,32 +34,36 @@
|
|
|
</van-button>
|
|
|
</div>
|
|
|
</div>
|
|
|
- <van-cell title="控制机器人" value="" />
|
|
|
+ <van-cell title="遥控器" value="" />
|
|
|
<van-row>
|
|
|
<van-col span="12">
|
|
|
<div class="control">
|
|
|
<van-button
|
|
|
type="default"
|
|
|
- @keydown="() => toOperation('head', 'up')"
|
|
|
+ @touchstart="() => toOperation('head', 'up')"
|
|
|
+ @touchend="() => (touch = false)"
|
|
|
>
|
|
|
上
|
|
|
</van-button>
|
|
|
<div class="conBtnGroup">
|
|
|
<van-button
|
|
|
type="default"
|
|
|
- @keydown="() => toOperation('head', 'left')"
|
|
|
+ @touchstart="() => toOperation('head', 'left')"
|
|
|
+ @touchend="() => (touch = false)"
|
|
|
>
|
|
|
左
|
|
|
</van-button>
|
|
|
<van-button
|
|
|
type="default"
|
|
|
- @keydown="() => toOperation('head', 'down')"
|
|
|
+ @touchstart="() => toOperation('head', 'down')"
|
|
|
+ @touchend="() => (touch = false)"
|
|
|
>
|
|
|
下
|
|
|
</van-button>
|
|
|
<van-button
|
|
|
type="default"
|
|
|
- @keydown="() => toOperation('head', 'right')"
|
|
|
+ @touchstart="() => toOperation('head', 'right')"
|
|
|
+ @touchend="() => (touch = false)"
|
|
|
>
|
|
|
右
|
|
|
</van-button>
|
|
@@ -70,26 +75,30 @@
|
|
|
<div class="control">
|
|
|
<van-button
|
|
|
type="default"
|
|
|
- @keydown="() => toOperation('wheel', 'forward')"
|
|
|
+ @touchstart="() => toOperation('wheel', 'forward')"
|
|
|
+ @touchend="() => (touch = false)"
|
|
|
>
|
|
|
前
|
|
|
</van-button>
|
|
|
<div class="conBtnGroup">
|
|
|
<van-button
|
|
|
type="default"
|
|
|
- @keydown="() => toOperation('wheel', 'left')"
|
|
|
+ @touchstart="() => toOperation('wheel', 'left')"
|
|
|
+ @touchend="() => (touch = false)"
|
|
|
>
|
|
|
左
|
|
|
</van-button>
|
|
|
<van-button
|
|
|
type="default"
|
|
|
- @keydown="() => toOperation('wheel', 'backward')"
|
|
|
+ @touchstart="() => toOperation('wheel', 'backward')"
|
|
|
+ @touchend="() => (touch = false)"
|
|
|
>
|
|
|
后
|
|
|
</van-button>
|
|
|
<van-button
|
|
|
type="default"
|
|
|
- @keydown="() => toOperation('wheel', 'right')"
|
|
|
+ @touchstart="() => toOperation('wheel', 'right')"
|
|
|
+ @touchend="() => (touch = false)"
|
|
|
>
|
|
|
右
|
|
|
</van-button>
|
|
@@ -129,48 +138,86 @@ export default {
|
|
|
name: "app",
|
|
|
robotToken: "",
|
|
|
scoket: undefined,
|
|
|
- deviceId: "",
|
|
|
status: true,
|
|
|
+ timeout: undefined,
|
|
|
data: function() {
|
|
|
return {
|
|
|
robotList: [],
|
|
|
- dialogueLst: [],
|
|
|
- robot: 0,
|
|
|
+ dialogueList: [],
|
|
|
+ remoteControl: [
|
|
|
+ {
|
|
|
+ typeL: "head",
|
|
|
+ btn: {
|
|
|
+ up: "上",
|
|
|
+ left: "左",
|
|
|
+ down: "下",
|
|
|
+ right: "右"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ typeL: "wheel",
|
|
|
+ btn: {
|
|
|
+ forward: "前",
|
|
|
+ left: "左",
|
|
|
+ backward: "后",
|
|
|
+ right: "右"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ touch: false,
|
|
|
+ deviceId: "",
|
|
|
message: ""
|
|
|
};
|
|
|
},
|
|
|
methods: {
|
|
|
+ creatID() {
|
|
|
+ const T =
|
|
|
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
+ let id = "";
|
|
|
+ for (let i = 0; i < 8; i++) {
|
|
|
+ id += T[Math.floor(Math.random() * 62)];
|
|
|
+ }
|
|
|
+ return id;
|
|
|
+ },
|
|
|
tomsg() {
|
|
|
if (!this.status) return Toast("链接已断开!");
|
|
|
if (!this.message) return;
|
|
|
let dialogue = {
|
|
|
type: "local",
|
|
|
- text: this.message
|
|
|
+ reply: this.message
|
|
|
};
|
|
|
- let dialogueLst = JSON.parse(JSON.stringify(this.dialogueLst));
|
|
|
- dialogueLst.push(dialogue);
|
|
|
- this.dialogueLst = dialogueLst;
|
|
|
- this.scoket &&
|
|
|
- this.scoket.send({
|
|
|
- id: this.deviceId,
|
|
|
- dialog: {
|
|
|
- reply: this.message
|
|
|
- }
|
|
|
- });
|
|
|
+ console.log("发送------>", dialogue);
|
|
|
+ let dialogueList = JSON.parse(JSON.stringify(this.dialogueList));
|
|
|
+ dialogueList.push(dialogue);
|
|
|
+ this.dialogueList = dialogueList;
|
|
|
+ let p = {
|
|
|
+ id: this.creatID(),
|
|
|
+ dialog: {
|
|
|
+ reply: this.message
|
|
|
+ }
|
|
|
+ };
|
|
|
+ this.scoket && this.scoket.send(JSON.stringify(p));
|
|
|
+ this.message = "";
|
|
|
},
|
|
|
toOperation(type, opera) {
|
|
|
if (!this.status) return Toast("链接已断开!");
|
|
|
- let p = {};
|
|
|
- p[type] = opera;
|
|
|
- this.scoket &&
|
|
|
- this.scoket.send({
|
|
|
- id: this.deviceId,
|
|
|
- controller: p
|
|
|
- });
|
|
|
+ if (this.timeout) clearInterval(this.timeout);
|
|
|
+ this.touch = true;
|
|
|
+ this.timeout = setInterval(() => {
|
|
|
+ if (!this.touch) clearInterval(this.timeout);
|
|
|
+ let p = {
|
|
|
+ id: this.creatID(),
|
|
|
+ controller: {}
|
|
|
+ };
|
|
|
+ p.controller[type] = opera;
|
|
|
+ this.scoket && this.scoket.send(JSON.stringify(p));
|
|
|
+ }, Config.robotMoveTimeout);
|
|
|
},
|
|
|
createRobotLink() {
|
|
|
if (!window.WebSocket) return Toast("该版本暂时不支持");
|
|
|
- this.scoket && this.scoket.close() && (this.scoket = undefined);
|
|
|
+ let _this = this;
|
|
|
+ this.scoket && this.scoket.onclose() && (this.scoket = undefined);
|
|
|
+ console.log("开始链接");
|
|
|
this.scoket = new WebSocket(
|
|
|
Config.robotBaseWss +
|
|
|
"/ws/app/event?clientId=" +
|
|
@@ -181,30 +228,34 @@ export default {
|
|
|
this.robotToken
|
|
|
);
|
|
|
this.scoket.onmessage = function(event) {
|
|
|
- var data = event.data;
|
|
|
- console.log(data);
|
|
|
+ var data = JSON.parse(event.data || "{}");
|
|
|
// 处理数据
|
|
|
let dialogue = {
|
|
|
type: "",
|
|
|
text: data.reply
|
|
|
};
|
|
|
- let dialogueLst = JSON.parse(JSON.stringify(this.dialogueLst));
|
|
|
- dialogueLst.push(dialogue);
|
|
|
- this.dialogueLst = dialogueLst;
|
|
|
+ console.log("接收------>", data);
|
|
|
+ let dialogueList = JSON.parse(JSON.stringify(this.dialogueList));
|
|
|
+ dialogueList.push(dialogue);
|
|
|
+ this.dialogueList = dialogueList;
|
|
|
};
|
|
|
this.scoket.onopen = function() {
|
|
|
- this.status = true;
|
|
|
+ _this.status = true;
|
|
|
+ console.log("链接成功");
|
|
|
};
|
|
|
this.scoket.onerror = function() {
|
|
|
- this.status = false;
|
|
|
+ _this.status = false;
|
|
|
+ console.log("链接错误");
|
|
|
Toast("链接已断开!");
|
|
|
};
|
|
|
this.scoket.onclose = function() {
|
|
|
- this.status = false;
|
|
|
+ _this.status = false;
|
|
|
+ console.log("链接断开");
|
|
|
Toast("链接已断开!");
|
|
|
};
|
|
|
},
|
|
|
change(e) {
|
|
|
+ if (!e) return;
|
|
|
this.deviceId = e;
|
|
|
this.createRobotLink();
|
|
|
}
|
|
@@ -225,6 +276,7 @@ export default {
|
|
|
value: v.deviceId
|
|
|
};
|
|
|
});
|
|
|
+ this.change(this.robotList[0] ? this.robotList[0].value : "");
|
|
|
});
|
|
|
});
|
|
|
},
|
|
@@ -265,7 +317,6 @@ export default {
|
|
|
.msgBg {
|
|
|
padding: 5px 3px;
|
|
|
> div {
|
|
|
- width: 70%;
|
|
|
padding: 5px;
|
|
|
color: #fff;
|
|
|
border-radius: 5px;
|