Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

msg.vue 4.2 KiB

il y a 4 ans
il y a 4 ans
il y a 4 ans
il y a 4 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view id="msg" class="page">
  3. <!-- 消息列表 -->
  4. <l-list :empty="msgCount <= 0" emptyTips="消息列表为空" nomoreTips="已显示全部消息" message nomore>
  5. <l-list-item
  6. v-for="item of msgList"
  7. @click="goTo(item)"
  8. :key="item.F_Id"
  9. :imgAvatar="avatarSrc(item)"
  10. :roundAvatar="roundAvatar"
  11. :extra="item.F_Content"
  12. iconAvatar="notice"
  13. iconStyle="background-color: #e4f2fd; color: #98c0da;"
  14. >
  15. <!-- 消息发送人 -->
  16. <text class="text-black">{{ msgTitle(item) }}</text>
  17. <!-- 消息时间 -->
  18. <view class="time" slot="time">
  19. <view class="text-right">{{ msgDateTime(item.F_Time)[0] }}</view>
  20. <view v-if="msgDateTime(item.F_Time)[1]" class="text-right">{{ msgDateTime(item.F_Time)[1] }}</view>
  21. </view>
  22. </l-list-item>
  23. </l-list>
  24. </view>
  25. </template>
  26. <script>
  27. import moment from 'moment'
  28. import keyBy from 'lodash/keyBy'
  29. export default {
  30. data() {
  31. return {
  32. msgList: [],
  33. sysUserTable: {},
  34. timer: null,
  35. nextTime: '1888-10-10 10:10:10'
  36. }
  37. },
  38. async onLoad() {
  39. await this.init()
  40. },
  41. // 本页面开启下拉刷新,用于立即拉取消息
  42. onPullDownRefresh() {
  43. this.msgList = []
  44. this.nextTime = '1888-10-10 10:10:10'
  45. this.fetchMsg().then(() => {
  46. this.TOAST('已更新消息列表')
  47. uni.stopPullDownRefresh()
  48. })
  49. },
  50. // 页面添加定时器,自动收取消息
  51. onShow() {
  52. const intervalTime = this.CONFIG('pageConfig.msg.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() {
  66. this.LOADING('读取消息列表')
  67. await this.fetchMsg()
  68. this.HIDE_LOADING()
  69. },
  70. // 拉取消息
  71. async fetchMsg() {
  72. const message = await this.HTTP_GET('learun/adms/im/contacts', this.nextTime, '加载信息时通讯出错')
  73. if (!message) {
  74. return
  75. }
  76. this.nextTime = message.time
  77. this.sysUserTable = Object.assign(this.sysUserTable, keyBy(message.sysUserList, 'F_Code'))
  78. // 不在列表里的消息,则插入;已在列表里的消息,则替换
  79. // 最后按照时间排序,最后的消息在最上
  80. const newMsg = message.data
  81. const allMsg = this.msgList
  82. newMsg.forEach(item => {
  83. const idx = allMsg.findIndex(t => t.F_Id === item.F_Id)
  84. if (idx === -1) {
  85. allMsg.push(item)
  86. return
  87. }
  88. allMsg[idx] = item
  89. })
  90. this.msgList = allMsg.sort((a, b) => moment(b.F_Time).valueOf() - moment(a.F_Time).valueOf())
  91. },
  92. // 点击后,跳转到详细聊天页
  93. goTo(item) {
  94. const user = this.sysUserTable[item.F_OtherUserId] || this.userTable[item.F_OtherUserId]
  95. const sys = this.sysUserTable[item.F_OtherUserId] ? '&sys' : ''
  96. const query = `id=${item.F_OtherUserId}&name=${user.name || user.F_Name}${sys}`
  97. this.NAV_TO(`/pages/msg/chat?${query}`)
  98. },
  99. // 获取消息发送人名称显示
  100. msgTitle(item) {
  101. const user = this.sysUserTable[item.F_OtherUserId] || this.userTable[item.F_OtherUserId] || {}
  102. return user.F_Name || user.name || '(未知用户)'
  103. },
  104. // 格式化显示消息时间
  105. msgDateTime(date) {
  106. return this.TABLEITEM_DATEFORMAT(date)
  107. },
  108. // 获取用户头像 url
  109. avatarSrc(item) {
  110. const user = this.userTable[item.F_OtherUserId]
  111. if (!user) {
  112. return null
  113. }
  114. return this.API + `/learun/adms/user/img?data=${item.F_OtherUserId}`
  115. }
  116. },
  117. computed: {
  118. // 所用用户表
  119. userTable() {
  120. return this.GET_GLOBAL('user')
  121. },
  122. // msgList 消息数组的长度
  123. msgCount() {
  124. return this.msgList.length
  125. },
  126. // 头像圆形/方形显示参数
  127. roundAvatar() {
  128. return this.CONFIG('pageConfig.roundAvatar')
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="less" scoped>
  134. .avatar {
  135. width: 96rpx;
  136. height: 96rpx;
  137. }
  138. .time {
  139. display: flex;
  140. flex-direction: column;
  141. align-items: flex-end;
  142. }
  143. </style>