瑶都万能墙
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
6.7 KiB

  1. <template>
  2. <view class="page">
  3. <navbar title="群组聊天" bgColor="#5baaff" color="#fff" leftClick @leftClick="$utils.navigateBack" />
  4. <!-- 消息列表 -->
  5. <scroll-view
  6. class="message-list"
  7. scroll-y="true"
  8. :scroll-top="scrollTop"
  9. @scrolltoupper="loadMoreMessages"
  10. >
  11. <view class="message-container">
  12. <view
  13. v-for="(message, index) in messageList"
  14. :key="message.id || index"
  15. class="message-item"
  16. :class="{ 'message-mine': message.isMine }"
  17. >
  18. <image
  19. :src="message.avatar || '/static/image/logo.jpg'"
  20. class="message-avatar"
  21. mode="aspectFill"
  22. ></image>
  23. <view class="message-content">
  24. <view class="message-info">
  25. <text class="sender-name">{{ message.senderName }}</text>
  26. <text class="message-time">{{ message.time }}</text>
  27. </view>
  28. <view class="message-text">{{ message.content }}</view>
  29. </view>
  30. </view>
  31. </view>
  32. </scroll-view>
  33. <!-- 输入框 -->
  34. <view class="input-container">
  35. <view class="input-box">
  36. <uv-input
  37. v-model="inputMessage"
  38. placeholder="输入消息..."
  39. :border="false"
  40. :clearable="true"
  41. @confirm="sendMessage"
  42. ></uv-input>
  43. </view>
  44. <view class="send-btn" @click="sendMessage">
  45. <uv-icon name="paperplane" size="40rpx" color="#fff"></uv-icon>
  46. </view>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. export default {
  52. data() {
  53. return {
  54. groupId: '',
  55. groupInfo: {},
  56. inputMessage: '',
  57. scrollTop: 0,
  58. messageList: [
  59. {
  60. id: 1,
  61. senderName: '张三',
  62. avatar: '/static/image/logo.jpg',
  63. content: '大家好,欢迎加入群组!',
  64. time: '10:30',
  65. isMine: false
  66. },
  67. {
  68. id: 2,
  69. senderName: '我',
  70. avatar: '/static/image/logo.jpg',
  71. content: '谢谢,很高兴认识大家',
  72. time: '10:32',
  73. isMine: true
  74. },
  75. {
  76. id: 3,
  77. senderName: '李四',
  78. avatar: '/static/image/logo.jpg',
  79. content: '有人知道江华哪里有好的租房信息吗?',
  80. time: '10:35',
  81. isMine: false
  82. },
  83. {
  84. id: 4,
  85. senderName: '王五',
  86. avatar: '/static/image/logo.jpg',
  87. content: '我这边有个单间出租,位置不错',
  88. time: '10:38',
  89. isMine: false
  90. }
  91. ]
  92. }
  93. },
  94. onLoad(options) {
  95. if (options.id) {
  96. this.groupId = options.id
  97. this.getGroupInfo()
  98. }
  99. },
  100. onShow() {
  101. this.scrollToBottom()
  102. },
  103. methods: {
  104. // 获取群组信息
  105. getGroupInfo() {
  106. // 模拟群组信息
  107. const mockGroupInfo = {
  108. 1: { id: 1, name: '江华同城交流群' },
  109. 2: { id: 2, name: '江华美食分享群' },
  110. 3: { id: 3, name: '江华租房信息群' },
  111. 4: { id: 4, name: '江华求职招聘群' },
  112. 5: { id: 5, name: '江华旅游攻略群' },
  113. 6: { id: 6, name: '江华学习交流群' },
  114. 7: { id: 7, name: '江华二手交易群' },
  115. 8: { id: 8, name: '江华宠物交流群' },
  116. 9: { id: 9, name: '江华汽车交流群' },
  117. 10: { id: 10, name: '江华宝妈交流群' },
  118. 11: { id: 11, name: '江华IT技术交流群' },
  119. 12: { id: 12, name: '江华健身运动群' },
  120. 13: { id: 13, name: '江华摄影爱好者群' },
  121. 14: { id: 14, name: '江华创业交流群' },
  122. 15: { id: 15, name: '江华医疗健康群' },
  123. 16: { id: 16, name: '江华读书会' },
  124. 17: { id: 17, name: '江华音乐爱好者群' },
  125. 18: { id: 18, name: '江华电商交流群' },
  126. 19: { id: 19, name: '江华园艺爱好者群' },
  127. 20: { id: 20, name: '江华法律咨询群' }
  128. }
  129. setTimeout(() => {
  130. this.groupInfo = mockGroupInfo[this.groupId] || { name: '群组聊天' }
  131. }, 300)
  132. },
  133. // 发送消息
  134. sendMessage() {
  135. if (!this.inputMessage.trim()) {
  136. return
  137. }
  138. const message = {
  139. id: Date.now(),
  140. senderName: '我',
  141. avatar: '/static/image/logo.jpg',
  142. content: this.inputMessage,
  143. time: this.getCurrentTime(),
  144. isMine: true
  145. }
  146. this.messageList.push(message)
  147. this.inputMessage = ''
  148. // 滚动到底部
  149. this.$nextTick(() => {
  150. this.scrollToBottom()
  151. })
  152. // 这里可以调用发送消息API
  153. // this.$api('sendGroupMessage', {
  154. // groupId: this.groupId,
  155. // content: message.content
  156. // }, res => {
  157. // if (res.code == 200) {
  158. // console.log('消息发送成功')
  159. // }
  160. // })
  161. },
  162. // 加载更多消息
  163. loadMoreMessages() {
  164. // 这里可以实现加载历史消息的逻辑
  165. console.log('加载更多消息')
  166. },
  167. // 滚动到底部
  168. scrollToBottom() {
  169. this.scrollTop = 999999
  170. },
  171. // 获取当前时间
  172. getCurrentTime() {
  173. const now = new Date()
  174. const hours = now.getHours().toString().padStart(2, '0')
  175. const minutes = now.getMinutes().toString().padStart(2, '0')
  176. return `${hours}:${minutes}`
  177. }
  178. }
  179. }
  180. </script>
  181. <style scoped lang="scss">
  182. .page {
  183. display: flex;
  184. flex-direction: column;
  185. height: 100vh;
  186. background-color: #f5f5f5;
  187. .message-list {
  188. flex: 1;
  189. padding: 20rpx;
  190. .message-container {
  191. .message-item {
  192. display: flex;
  193. margin-bottom: 30rpx;
  194. &.message-mine {
  195. flex-direction: row-reverse;
  196. .message-content {
  197. margin-left: 0;
  198. margin-right: 20rpx;
  199. align-items: flex-end;
  200. .message-info {
  201. flex-direction: row-reverse;
  202. .sender-name {
  203. margin-left: 10rpx;
  204. margin-right: 0;
  205. }
  206. }
  207. .message-text {
  208. background-color: #5baaff;
  209. color: #fff;
  210. border-radius: 20rpx 20rpx 4rpx 20rpx;
  211. }
  212. }
  213. }
  214. .message-avatar {
  215. width: 80rpx;
  216. height: 80rpx;
  217. border-radius: 50%;
  218. }
  219. .message-content {
  220. flex: 1;
  221. margin-left: 20rpx;
  222. display: flex;
  223. flex-direction: column;
  224. .message-info {
  225. display: flex;
  226. align-items: center;
  227. margin-bottom: 8rpx;
  228. .sender-name {
  229. font-size: 24rpx;
  230. color: #666;
  231. margin-right: 10rpx;
  232. }
  233. .message-time {
  234. font-size: 22rpx;
  235. color: #999;
  236. }
  237. }
  238. .message-text {
  239. background-color: #fff;
  240. padding: 20rpx;
  241. border-radius: 20rpx 20rpx 20rpx 4rpx;
  242. font-size: 28rpx;
  243. color: #333;
  244. line-height: 1.4;
  245. word-break: break-all;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. .input-container {
  252. background-color: #fff;
  253. padding: 20rpx;
  254. border-top: 1rpx solid #f0f0f0;
  255. display: flex;
  256. align-items: center;
  257. gap: 20rpx;
  258. .input-box {
  259. flex: 1;
  260. background-color: #f5f5f5;
  261. border-radius: 30rpx;
  262. padding: 0 20rpx;
  263. }
  264. .send-btn {
  265. width: 80rpx;
  266. height: 80rpx;
  267. background-color: #5baaff;
  268. border-radius: 50%;
  269. display: flex;
  270. align-items: center;
  271. justify-content: center;
  272. }
  273. }
  274. }
  275. </style>