1
lkk
2025-04-16 4f55c0255e5e68b3685d1510062dff6e1e3dbc48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<template>
  <div class="chat-container">
    <h2 class="chat-title">DeepSeek 聊天室</h2>
    <div class="message-box">
      <textarea 
        v-model="inputMessage" 
        placeholder="请输入您的消息..."
        class="chat-input"
        :disabled="isLoading"
      ></textarea>
      <button 
        @click="sendMessage" 
        class="send-button" 
        :disabled="isLoading"
      >
        {{ isLoading ? '发送中...' : '发送消息' }}
      </button>
    </div>
    <div class="response-area">
      <p class="response-label">回复:</p>
      <div class="response-content">{{ reply || '暂无回复' }}</div>
    </div>
    <p v-if="error" class="error-message">{{ error }}</p>
 
    <!-- 遮罩层和加载动画 -->
    <div v-if="isLoading" class="loading-overlay">
      <div class="spinner"></div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'Chat',
  data() {
    return {
      inputMessage: '',
      reply: '',
      error: '',
      isLoading: false // 新增加载状态
    };
  },
  methods: {
    async sendMessage() {
      if (!this.inputMessage.trim()) {
        this.error = '消息不能为空';
        return;
      }
      this.error = '';
      this.reply = '';
      this.isLoading = true; // 开始加载
 
      try {
        const response = await fetch('http://localhost:11434/api/chat', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            model: 'ltkj-jy-ai',
            messages: [
              {
                role: 'system',
                content: this.inputMessage
              }
            ],
            stream: false
          })
        });
 
        if (!response.ok) {
          throw new Error('网络响应错误');
        }
 
        const data = await response.json();
        this.reply = data.message?.content || '收到回复,但格式可能不正确';
      } catch (err) {
        this.error = '请求出错: ' + err.message;
        console.error('Fetch 错误:', err);
      } finally {
        this.isLoading = false; // 结束加载
      }
    }
  }
};
</script>
 
<style scoped>
.chat-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: relative; /* 为遮罩层定位 */
}
 
.chat-title {
  color: #2c3e50;
  text-align: center;
  margin-bottom: 20px;
  font-family: 'Arial', sans-serif;
}
 
.message-box {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
 
.chat-input {
  width: 100%;
  height: 120px;
  padding: 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  resize: none;
  font-size: 14px;
  background: #fff;
  transition: border-color 0.3s;
}
 
.chat-input:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 5px rgba(52, 152, 219, 0.3);
}
 
.chat-input:disabled {
  background: #f0f0f0;
  cursor: not-allowed;
}
 
.send-button {
  padding: 10px 20px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.3s;
  align-self: flex-end;
}
 
.send-button:hover:not(:disabled) {
  background: #2980b9;
}
 
.send-button:disabled {
  background: #95a5a6;
  cursor: not-allowed;
}
 
.response-area {
  margin-top: 20px;
  background: #fff;
  padding: 15px;
  border-radius: 8px;
  border: 1px solid #eee;
}
 
.response-label {
  margin: 0 0 10px 0;
  color: #7f8c8d;
  font-size: 14px;
}
 
.response-content {
  color: #2c3e50;
  line-height: 1.5;
  word-wrap: break-word;
}
 
.error-message {
  color: #e74c3c;
  margin-top: 10px;
  font-size: 14px;
  text-align: center;
}
 
/* 遮罩层样式 */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  z-index: 10;
}
 
/* 加载动画 */
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
 
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>