用springboot和vue写一个在线咨询的代码
时间: 2023-05-18 18:03:03 浏览: 38
非常感谢您的提问!以下是一个简单的在线咨询系统的代码示例,使用Spring Boot和Vue.js编写:
Spring Boot后端代码:
```java
@RestController
@RequestMapping("/api")
public class ChatController {
@PostMapping("/chat")
public String chat(@RequestBody String message) {
// 在这里编写与ChatGPT交互的代码
return "ChatGPT的回答";
}
}
```
Vue.js前端代码:
```html
<template>
<div>
<div v-for="message in messages" :key="message.id">
<p v-if="message.type === 'user'">{{ message.text }}</p>
<p v-if="message.type === 'bot'">{{ message.text }}</p>
</div>
<input v-model="inputText" @keyup.enter="sendMessage" />
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
messages: [],
inputText: '',
};
},
methods: {
async sendMessage() {
const message = {
id: Date.now(),
type: 'user',
text: this.inputText,
};
this.messages.push(message);
this.inputText = '';
try {
const response = await axios.post('/api/chat', message.text);
const botMessage = {
id: Date.now(),
type: 'bot',
text: response.data,
};
this.messages.push(botMessage);
} catch (error) {
console.error(error);
}
},
},
};
</script>
```
请注意,这只是一个简单的示例,您需要根据您的需求进行修改和扩展。