|
|
|
@ -27,7 +27,7 @@ |
|
|
|
|
<image src="@/static/test/icon-voice.png" mode="widthFix" class="left-icon" |
|
|
|
|
@click="messageType='voice'"> |
|
|
|
|
</image> |
|
|
|
|
<input type="text" v-model="content" class="input" @confirm="send" /> |
|
|
|
|
<input type="text" v-model="content" class="input" @confirm="sendMsg" /> |
|
|
|
|
</block> |
|
|
|
|
<block v-if="messageType === 'voice'"> |
|
|
|
|
<image src="@/static/test/icon-text.png" mode="widthFix" class="left-icon" @click="messageType='text'"> |
|
|
|
@ -60,8 +60,9 @@ |
|
|
|
|
//发送类型 |
|
|
|
|
messageType: 'voice', // text 发送文本;voice 发送语音 |
|
|
|
|
recordStart: false, |
|
|
|
|
identity: "", |
|
|
|
|
//websocket |
|
|
|
|
headers: {}, |
|
|
|
|
Token:null , |
|
|
|
|
userInfo: {}, |
|
|
|
|
wsInfo: { |
|
|
|
|
ws: null, // ws对象 |
|
|
|
@ -78,21 +79,16 @@ |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
onReady() { |
|
|
|
|
const userInfo = uni.getStorageSync("userInfo"); |
|
|
|
|
|
|
|
|
|
onShow() { |
|
|
|
|
const userInfo = uni.getStorageSync("userInfo"); |
|
|
|
|
this.userInfo = { |
|
|
|
|
"message": "", |
|
|
|
|
"dialogId": "", |
|
|
|
|
"identity": userInfo.openid, |
|
|
|
|
"identity": "", |
|
|
|
|
"userId": userInfo.userId |
|
|
|
|
}; |
|
|
|
|
const tok = userInfo.accessToken; |
|
|
|
|
this.headers = { |
|
|
|
|
"content-type": 'application/json', |
|
|
|
|
'Authorization': 'Bearer ' + '3e803f812b5c4bba8a0d6864d011cf28' |
|
|
|
|
}; |
|
|
|
|
}, |
|
|
|
|
onShow() { |
|
|
|
|
this.Token = userInfo.accessToken; |
|
|
|
|
this.connectWs() |
|
|
|
|
}, |
|
|
|
|
methods: { |
|
|
|
@ -101,7 +97,7 @@ |
|
|
|
|
const $this = this |
|
|
|
|
this.wsInfo.ws = null; |
|
|
|
|
this.wsInfo.ws = uni.connectSocket({ |
|
|
|
|
url: 'wss://192.168.130.157:48080/infra/ws?token=3e803f812b5c4bba8a0d6864d011cf28', |
|
|
|
|
url: 'wss://eluyou.ailuquan.cn/prod-api/infra/ws?token='+this.Token, |
|
|
|
|
success() { |
|
|
|
|
$this.wsInfo.alive = true; |
|
|
|
|
console.log("ws连接成功!"); |
|
|
|
@ -111,51 +107,36 @@ |
|
|
|
|
console.log("ws连接失败!"); |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
console.log("ws信息:", this.wsInfo.ws); |
|
|
|
|
// ws打开 |
|
|
|
|
this.wsInfo.ws.onOpen((res) => { |
|
|
|
|
$this.wsInfo.alive = true; |
|
|
|
|
// 开启心跳 |
|
|
|
|
$this.heartBeat(); |
|
|
|
|
console.log("ws开启成功!"); |
|
|
|
|
console.log("ws开启成功!",$this.wsInfo.alive); |
|
|
|
|
}); |
|
|
|
|
// ws消息 |
|
|
|
|
this.wsInfo.ws.onMessage((res) => { |
|
|
|
|
console.log("===", res) |
|
|
|
|
console.log("ws接收消息:", res); |
|
|
|
|
$this.heartBeat(); |
|
|
|
|
// 处理消息 |
|
|
|
|
let data = JSON.parse(res.data); |
|
|
|
|
$this.handlerMessage($this.userInfo); |
|
|
|
|
$this.handlerMessage(data); |
|
|
|
|
console.log("ws接收消息:", data); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// console.log("ws信息:", this.wsInfo.ws); |
|
|
|
|
// // ws打开 |
|
|
|
|
// this.wsInfo.ws.onOpen((res) => { |
|
|
|
|
// this.wsInfo.alive = true; |
|
|
|
|
// // 开启心跳 |
|
|
|
|
// this.heartBeat(); |
|
|
|
|
// console.log("ws开启成功!"); |
|
|
|
|
// }); |
|
|
|
|
// // ws消息 |
|
|
|
|
// this.wsInfo.ws.onMessage((res) => { |
|
|
|
|
// this.heartBeat(); |
|
|
|
|
// // 处理消息 |
|
|
|
|
// let data = JSON.parse(res.data); |
|
|
|
|
// this.handlerMessage(data); |
|
|
|
|
// console.log("ws接收消息:", data); |
|
|
|
|
// }); |
|
|
|
|
// // ws关闭 |
|
|
|
|
// this.wsInfo.ws.onClose((res) => { |
|
|
|
|
// this.wsInfo.alive = false; |
|
|
|
|
// this.reConnect(); |
|
|
|
|
// console.log("ws连接关闭:", res); |
|
|
|
|
// }); |
|
|
|
|
// // ws错误 |
|
|
|
|
// this.wsInfo.ws.onError((err) => { |
|
|
|
|
// this.wsInfo.alive = false; |
|
|
|
|
// this.reConnect(); |
|
|
|
|
// console.log("ws连接错误:", res); |
|
|
|
|
// }); |
|
|
|
|
// ws关闭 |
|
|
|
|
this.wsInfo.ws.onClose((res) => { |
|
|
|
|
$this.wsInfo.alive = false; |
|
|
|
|
$this.reConnect(); |
|
|
|
|
console.log("ws连接关闭:", res); |
|
|
|
|
}); |
|
|
|
|
// ws错误 |
|
|
|
|
this.wsInfo.ws.onError((err) => { |
|
|
|
|
$this.wsInfo.alive = false; |
|
|
|
|
$this.reConnect(); |
|
|
|
|
console.log("ws连接错误:", res); |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
// 心跳检测 |
|
|
|
|
heartBeat() { |
|
|
|
@ -163,11 +144,11 @@ |
|
|
|
|
clearTimeout(this.wsInfo.clientTimer); |
|
|
|
|
clearTimeout(this.wsInfo.serverTimer); |
|
|
|
|
this.wsInfo.clientTimer = setTimeout(() => { |
|
|
|
|
if (this.wsInfo.ws) { |
|
|
|
|
if ($this.wsInfo.ws) { |
|
|
|
|
let pong = { |
|
|
|
|
type: "ping", |
|
|
|
|
}; |
|
|
|
|
this.wsInfo.ws.send({ |
|
|
|
|
$this.wsInfo.ws.send({ |
|
|
|
|
data: JSON.stringify(pong), |
|
|
|
|
fail() { |
|
|
|
|
$this.wsInfo.serverTimer = setTimeout(() => { |
|
|
|
@ -176,7 +157,7 @@ |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}, this.wsInfo.clientTime); |
|
|
|
|
}, $this.wsInfo.clientTime); |
|
|
|
|
}, |
|
|
|
|
//发送消息 |
|
|
|
|
handlerMessage(data) { |
|
|
|
@ -204,23 +185,34 @@ |
|
|
|
|
this.wsInfo.ws.close(); |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 发送消息 |
|
|
|
|
send() { |
|
|
|
|
sendMsg() { |
|
|
|
|
const $this = this |
|
|
|
|
this.list.push({ |
|
|
|
|
content: this.content, |
|
|
|
|
userType: 'self', |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
if (this.userInfo.identity == "") { |
|
|
|
|
this.userInfo.identity = this.generateRandomString(8); |
|
|
|
|
} |
|
|
|
|
this.userInfo.message = this.content |
|
|
|
|
|
|
|
|
|
this.$Request.post(this.$config.aiSendMsg, $this.userInfo, "json", null, false, true) |
|
|
|
|
.then(res => { |
|
|
|
|
console.log("---", res) |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.content = '' |
|
|
|
|
this.scrollToBottom() |
|
|
|
|
// 模拟对方回复 |
|
|
|
|
setTimeout(() => { |
|
|
|
|
this.list.push({ |
|
|
|
|
content: '好的', |
|
|
|
|
userType: 'friend', |
|
|
|
|
}) |
|
|
|
|
this.scrollToBottom() |
|
|
|
|
}, 1500) |
|
|
|
|
// // 模拟对方回复 |
|
|
|
|
// setTimeout(() => { |
|
|
|
|
// this.list.push({ |
|
|
|
|
// content: '好的', |
|
|
|
|
// userType: 'friend', |
|
|
|
|
// }) |
|
|
|
|
// this.scrollToBottom() |
|
|
|
|
// }, 1500) |
|
|
|
|
}, |
|
|
|
|
//屏幕滚动 |
|
|
|
|
scrollToBottom() { |
|
|
|
@ -361,7 +353,18 @@ |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
//获取当前对话 key |
|
|
|
|
generateRandomString(length) { |
|
|
|
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
|
|
|
let result = ''; |
|
|
|
|
for (let i = 0; i < length; i++) { |
|
|
|
|
const randomIndex = Math.floor(Math.random() * characters.length); |
|
|
|
|
result += characters.charAt(randomIndex); |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
</script> |
|
|
|
|