您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

222 行
5.2 KiB

  1. <template>
  2. <view id="chat" class="page">
  3. <!-- 聊天消息列表 -->
  4. <l-chat :nomore="false">
  5. <!-- 单条聊天消息 -->
  6. <l-chat-msg
  7. v-for="item of msgList"
  8. :key="item.F_MsgId"
  9. :content="item.F_Content"
  10. :type="item.F_SendUserId === chatUserId ? 'left' : 'right'"
  11. :date="dateDisplay(item.F_CreateDate)"
  12. :roundAvatar="roundAvatar"
  13. :imgAvatar="avatar(item.F_SendUserId)"
  14. iconAvatar="notice"
  15. iconStyle="background-color: #e4f2fd; color: #98c0da;"
  16. />
  17. </l-chat>
  18. <!-- 消息输入框 -->
  19. <l-chat-input
  20. v-model="msgInput"
  21. @sendMsg="sendMsg"
  22. :buttonDisabled="buttonDisabled"
  23. placeholder="输入要发送的消息内容"
  24. />
  25. </view>
  26. </template>
  27. <script>
  28. import moment from 'moment'
  29. export default {
  30. data() {
  31. return {
  32. chatUserId: null,
  33. isSystem: false,
  34. msgList: [],
  35. msgInput: '',
  36. earliestTimePoint: '',
  37. nextTime: '',
  38. timer: null,
  39. ready: false,
  40. buttonDisabled: false
  41. }
  42. },
  43. async onLoad(query) {
  44. await this.init(query)
  45. },
  46. // 本页面开启下拉刷新,用于立即拉取消息
  47. onPullDownRefresh() {
  48. this.fetchBefore().then(uni.stopPullDownRefresh)
  49. },
  50. // 页面添加定时器,自动收取消息
  51. onShow() {
  52. const intervalTime = this.CONFIG('pageConfig.chat.fetchMsg')
  53. this.timer = setInterval(this.fetchMsg, intervalTime)
  54. },
  55. // 离开页面,清除定时器
  56. onHide() {
  57. clearInterval(this.timer)
  58. },
  59. // 卸载页面,清除定时器
  60. onUnload() {
  61. clearInterval(this.timer)
  62. },
  63. methods: {
  64. // 页面初始化
  65. async init({ id, name, sys }) {
  66. this.LOADING('加载消息…')
  67. this.chatUserId = id
  68. this.isSystem = sys
  69. this.SET_TITLE(name)
  70. const message = await this.HTTP_GET('learun/adms/im/msg/lastlist', id)
  71. // 记录最晚消息的时间点
  72. const nextTime = message[0] && message[0].F_CreateDate
  73. this.nextTime = moment(nextTime).format('YYYY-MM-DD HH:mm:ss')
  74. const msgList = message.reverse()
  75. // 记录最早消息的时间点
  76. const earliest = msgList[0] && msgList[0].F_CreateDate
  77. this.earliestTimePoint = moment(earliest)
  78. .subtract(1, 'second')
  79. .format('YYYY-MM-DD HH:mm:ss')
  80. this.msgList = msgList
  81. this.ready = true
  82. // 打开页面后,滚动到底部
  83. setTimeout(() => {
  84. uni.pageScrollTo({ scrollTop: 9999, duration: 100 })
  85. }, 500)
  86. this.HIDE_LOADING()
  87. },
  88. // 拉取消息
  89. async fetchMsg() {
  90. if (!this.ready) {
  91. return
  92. }
  93. const message = await this.HTTP_GET('learun/adms/im/msg/list2', {
  94. otherUserId: this.chatUserId,
  95. time: this.nextTime
  96. })
  97. if (message && message.length > 0) {
  98. // 记录最晚消息的时间点
  99. const nextTime = message[0] && message[0].F_CreateDate
  100. this.nextTime = moment(nextTime).format('YYYY-MM-DD HH:mm:ss')
  101. this.msgList = this.msgList.concat(message.reverse())
  102. }
  103. },
  104. // 拉取之前的消息,用于下拉加载
  105. async fetchBefore() {
  106. const message = await this.HTTP_GET('learun/adms/im/msg/list', {
  107. otherUserId: this.chatUserId,
  108. time: this.earliestTimePoint
  109. })
  110. const oldMsgList = message.reverse()
  111. if (oldMsgList.length > 0) {
  112. this.earliestTimePoint = moment(oldMsgList[0].F_CreateDate)
  113. .subtract(1, 'second')
  114. .format('YYYY-MM-DD HH:mm:ss')
  115. this.msgList = oldMsgList.concat(this.msgList)
  116. }
  117. },
  118. // 发送消息按钮
  119. async sendMsg() {
  120. if (this.msgInput.trim().length <= 0) {
  121. this.TOAST('发送的消息不能为空')
  122. return
  123. }
  124. this.ready = false
  125. this.buttonDisabled = true
  126. const result = await this.HTTP_POST(
  127. 'learun/adms/im/send',
  128. { userId: this.chatUserId, content: this.msgInput },
  129. '消息发送失败'
  130. )
  131. if (!result) {
  132. return
  133. }
  134. this.msgList.push({
  135. F_Content: this.msgInput,
  136. F_CreateDate: result.time,
  137. F_IsSystem: null,
  138. F_MsgId: result.msgId,
  139. F_RecvUserId: this.chatUserId,
  140. F_SendUserId: this.GET_GLOBAL('loginUser').userId
  141. })
  142. this.nextTime = result.time
  143. this.msgInput = ''
  144. setTimeout(() => {
  145. uni.pageScrollTo({ scrollTop: 9999, duration: 100 })
  146. }, 10)
  147. this.buttonDisabled = false
  148. this.ready = true
  149. },
  150. // 格式化显示时间日期
  151. dateDisplay(date) {
  152. return this.TABLEITEM_DATEFORMAT(date).toString()
  153. },
  154. // 获取用户头像图片 url
  155. avatar(id) {
  156. return id === this.chatUserId && this.isSystem ? null : this.API + `/user/img?data=${id}`
  157. }
  158. },
  159. computed: {
  160. // 头像圆形/方形显示参数
  161. roundAvatar() {
  162. return this.CONFIG('pageConfig.roundAvatar')
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="less">
  168. page {
  169. padding-bottom: 100rpx;
  170. padding-bottom: calc(100rpx + constant(safe-area-inset-bottom));
  171. padding-bottom: calc(100rpx + env(safe-area-inset-bottom));
  172. }
  173. /* #ifdef MP-ALIPAY */
  174. .cu-chat {
  175. padding-top: 100rpx;
  176. }
  177. #chat {
  178. height: 100%;
  179. padding-bottom: 1rpx;
  180. margin-top: -100rpx;
  181. overflow: hidden;
  182. }
  183. /* #endif */
  184. </style>