chat.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view class="chat-container">
  3. <view class="chat-header">
  4. <text class="chat-title">{{ chatName }}</text>
  5. </view>
  6. <scroll-view class="chat-messages" scroll-y :scroll-into-view="scrollInto">
  7. <view
  8. class="message-item"
  9. :class="{ mine: msg.isMine }"
  10. v-for="(msg, index) in messages"
  11. :key="index"
  12. :id="'msg-' + index"
  13. >
  14. <view class="message-time">{{ msg.time }}</view>
  15. <view class="message-bubble">
  16. {{ msg.content }}
  17. </view>
  18. </view>
  19. </scroll-view>
  20. <view class="chat-input">
  21. <input
  22. class="input-field"
  23. v-model="inputText"
  24. placeholder="输入消息..."
  25. @confirm="sendMessage"
  26. />
  27. <button class="send-btn" @click="sendMessage">发送</button>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import { getGuideMessages } from '../../utils/api.js'
  33. import config from '@/config.js'
  34. export default {
  35. data() {
  36. return {
  37. conversationId: '',
  38. chatName: '',
  39. messages: [],
  40. inputText: '',
  41. scrollInto: ''
  42. }
  43. },
  44. onLoad(options) {
  45. this.conversationId = options.conversationId || ''
  46. this.chatName = options.name || '聊天'
  47. this.loadMessages()
  48. },
  49. methods: {
  50. async loadMessages() {
  51. try {
  52. const res = await uni.request({
  53. url: `${config.API_BASE_URL}/api/guide/messages/${this.conversationId}/reply`,
  54. method: 'GET',
  55. header: { Authorization: `Bearer ${uni.getStorageSync('token')}` }
  56. })
  57. if (res.data && res.data.data) {
  58. this.messages = res.data.data
  59. }
  60. } catch (e) {
  61. console.error(e)
  62. this.messages = [
  63. { content: '老师,孩子今天的任务完成了', time: '2024-01-15 10:20', isMine: false },
  64. { content: '好的,我看一下', time: '2024-01-15 10:25', isMine: true },
  65. { content: '谢谢老师', time: '2024-01-15 10:30', isMine: false }
  66. ]
  67. }
  68. this.$nextTick(() => {
  69. this.scrollToBottom()
  70. })
  71. },
  72. async sendMessage() {
  73. if (!this.inputText.trim()) return
  74. const newMessage = {
  75. content: this.inputText,
  76. time: new Date().toLocaleString(),
  77. isMine: true
  78. }
  79. this.messages.push(newMessage)
  80. this.inputText = ''
  81. this.$nextTick(() => {
  82. this.scrollToBottom()
  83. })
  84. try {
  85. await uni.request({
  86. url: `${config.API_BASE_URL}/api/guide/messages/list`,
  87. method: 'POST',
  88. header: { Authorization: `Bearer ${uni.getStorageSync('token')}` },
  89. data: {
  90. conversationId: this.conversationId,
  91. content: newMessage.content
  92. }
  93. })
  94. } catch (e) {
  95. console.error(e)
  96. }
  97. },
  98. scrollToBottom() {
  99. const len = this.messages.length
  100. if (len > 0) {
  101. this.scrollInto = 'msg-' + (len - 1)
  102. }
  103. }
  104. }
  105. }
  106. </script>
  107. <style scoped>
  108. .chat-container {
  109. display: flex;
  110. flex-direction: column;
  111. height: 100vh;
  112. background: #f5f5f5;
  113. }
  114. .chat-header {
  115. background: #fff;
  116. padding: 30rpx;
  117. text-align: center;
  118. border-bottom: 1rpx solid #eee;
  119. }
  120. .chat-title {
  121. font-size: 32rpx;
  122. font-weight: bold;
  123. color: #333;
  124. }
  125. .chat-messages {
  126. flex: 1;
  127. padding: 30rpx;
  128. overflow-y: auto;
  129. }
  130. .message-item {
  131. display: flex;
  132. flex-direction: column;
  133. margin-bottom: 30rpx;
  134. }
  135. .message-item.mine {
  136. align-items: flex-end;
  137. }
  138. .message-time {
  139. font-size: 22rpx;
  140. color: #999;
  141. margin-bottom: 10rpx;
  142. }
  143. .message-bubble {
  144. max-width: 70%;
  145. padding: 20rpx 30rpx;
  146. background: #fff;
  147. border-radius: 16rpx;
  148. font-size: 28rpx;
  149. color: #333;
  150. word-wrap: break-word;
  151. }
  152. .message-item.mine .message-bubble {
  153. background: #667eea;
  154. color: #fff;
  155. }
  156. .chat-input {
  157. display: flex;
  158. align-items: center;
  159. padding: 20rpx 30rpx;
  160. background: #fff;
  161. border-top: 1rpx solid #eee;
  162. }
  163. .input-field {
  164. flex: 1;
  165. padding: 20rpx 30rpx;
  166. background: #f5f5f5;
  167. border-radius: 40rpx;
  168. font-size: 28rpx;
  169. }
  170. .send-btn {
  171. margin-left: 20rpx;
  172. padding: 20rpx 40rpx;
  173. background: #667eea;
  174. color: #fff;
  175. border-radius: 40rpx;
  176. font-size: 28rpx;
  177. }
  178. </style>