chat.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view class="chat-container" :style="{ '--font-scale': fontScale }">
  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 { formatDateTime } from '../../utils/format.js'
  34. import config from '@/config.js'
  35. import fontScaleMixin from '@/mixins/font-scale.js'
  36. export default {
  37. mixins: [fontScaleMixin],
  38. data() {
  39. return {
  40. conversationId: '',
  41. chatName: '',
  42. messages: [],
  43. inputText: '',
  44. scrollInto: ''
  45. }
  46. },
  47. onLoad(options) {
  48. this.conversationId = options.conversationId || ''
  49. this.chatName = options.name || '聊天'
  50. this.loadMessages()
  51. },
  52. methods: {
  53. async loadMessages() {
  54. try {
  55. const res = await getGuideMessages()
  56. if (res && res.code === 200 && res.data && res.data.records) {
  57. this.messages = res.data.records.map((m) => {
  58. var t = m.repliedAt || m.createdAt || ''
  59. return {
  60. content: m.replyContent || m.content,
  61. time: t.slice(0, 19).replace('T', ' '),
  62. isMine: !!(m.replyContent && m.replyContent.trim())
  63. }
  64. })
  65. }
  66. } catch (e) {
  67. console.error(e)
  68. this.messages = [
  69. { content: '老师,今天的任务完成了', time: '2024-01-15 10:20', isMine: false },
  70. { content: '好的,我看一下', time: '2024-01-15 10:25', isMine: true },
  71. { content: '谢谢老师', time: '2024-01-15 10:30', isMine: false }
  72. ]
  73. }
  74. this.$nextTick(() => {
  75. this.scrollToBottom()
  76. })
  77. },
  78. async sendMessage() {
  79. if (!this.inputText.trim()) return
  80. const newMessage = {
  81. content: this.inputText,
  82. time: formatDateTime(new Date()),
  83. isMine: true
  84. }
  85. this.messages.push(newMessage)
  86. this.inputText = ''
  87. this.$nextTick(() => {
  88. this.scrollToBottom()
  89. })
  90. try {
  91. await uni.request({
  92. url: `${config.API_BASE_URL}/api/guide/messages/list`,
  93. method: 'POST',
  94. header: { Authorization: `Bearer ${uni.getStorageSync('token')}` },
  95. data: {
  96. conversationId: this.conversationId,
  97. content: newMessage.content
  98. }
  99. })
  100. } catch (e) {
  101. console.error(e)
  102. }
  103. },
  104. scrollToBottom() {
  105. const len = this.messages.length
  106. if (len > 0) {
  107. this.scrollInto = 'msg-' + (len - 1)
  108. }
  109. }
  110. }
  111. }
  112. </script>
  113. <style scoped lang="scss">
  114. .chat-container {
  115. display: flex;
  116. flex-direction: column;
  117. height: 100vh;
  118. background: $facade-fill-2;
  119. }
  120. .chat-header {
  121. background: $app-surface;
  122. padding: $uni-space-page;
  123. text-align: center;
  124. border-bottom: 1rpx solid $facade-hairline-2;
  125. }
  126. .chat-title {
  127. font-size: calc(32rpx * var(--font-scale, 1));
  128. font-weight: bold;
  129. color: $facade-text-strong;
  130. }
  131. .chat-messages {
  132. flex: 1;
  133. padding: $uni-space-page;
  134. overflow-y: auto;
  135. }
  136. .message-item {
  137. display: flex;
  138. flex-direction: column;
  139. margin-bottom: $uni-space-lg;
  140. }
  141. .message-item.mine {
  142. align-items: flex-end;
  143. }
  144. .message-time {
  145. font-size: calc(22rpx * var(--font-scale, 1));
  146. color: $facade-text-muted;
  147. margin-bottom: 10rpx;
  148. }
  149. .message-bubble {
  150. max-width: 70%;
  151. padding: 20rpx $uni-space-page;
  152. background: $app-surface;
  153. border-radius: 16rpx;
  154. font-size: calc(28rpx * var(--font-scale, 1));
  155. color: $facade-text-strong;
  156. word-wrap: break-word;
  157. }
  158. .message-item.mine .message-bubble {
  159. background: $accent-indigo;
  160. color: $app-surface;
  161. }
  162. .chat-input {
  163. display: flex;
  164. align-items: center;
  165. padding: 20rpx $uni-space-page;
  166. background: $app-surface;
  167. border-top: 1rpx solid $facade-hairline-2;
  168. }
  169. .input-field {
  170. flex: 1;
  171. padding: 20rpx $uni-space-page;
  172. background: $facade-fill-2;
  173. border-radius: 24rpx;
  174. font-size: calc(28rpx * var(--font-scale, 1));
  175. }
  176. .send-btn {
  177. margin-left: 20rpx;
  178. padding: 20rpx 40rpx;
  179. background: $accent-indigo;
  180. color: $app-surface;
  181. border-radius: 24rpx;
  182. font-size: calc(28rpx * var(--font-scale, 1));
  183. }
  184. </style>