message-header.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div class="base" :class="[ isMine ? 'right' : 'left']">
  3. <div class="name text-ellipsis">{{ from }}</div>
  4. <div class="date">{{ date }}</div>
  5. </div>
  6. </template>
  7. <script>
  8. import { mapState } from 'vuex'
  9. import { getFullDate } from '../../utils/date'
  10. export default {
  11. name: 'MessageHeader',
  12. props: {
  13. message: {
  14. type: Object,
  15. required: true
  16. }
  17. },
  18. computed: {
  19. ...mapState({
  20. currentConversation: state => state.conversation.currentConversation,
  21. currentUserProfile: state => state.imuser.currentUserProfile,
  22. currentMemberList: state => state.group.currentMemberList
  23. }),
  24. date() {
  25. return getFullDate(new Date(this.message.time * 1000))
  26. },
  27. from() {
  28. const isC2C = this.currentConversation.type === 1
  29. // 自己发送的用昵称渲染
  30. if (this.isMine) {
  31. return this.message.nameCard || this.currentUserProfile.nick || this.currentUserProfile.userID
  32. }
  33. // 1. C2C 的消息体中还无 nick / avatar 字段,需从 conversation.userProfile 中获取
  34. if (isC2C) {
  35. return (
  36. this.currentConversation.userProfile.nick ||
  37. this.currentConversation.userProfile.userID
  38. )
  39. } else if (this.currentConversation.type === 4) {
  40. return this.message.type === this.OpenIM.TYPES.MSG_GRP_SYS_NOTICE
  41. ? '群系统通知'
  42. : '系统通知'
  43. }
  44. // 2. 群组消息,用消息体中的 nick 渲染。nameCard暂时支持不完善
  45. return this.message.nameCard || this.message.nick || this.message.from
  46. },
  47. isMine() {
  48. return this.message.flow === 'out'
  49. }
  50. }
  51. }
  52. </script>
  53. <style lang="stylus" scoped>
  54. .right {
  55. display: flex;
  56. flex-direction: row-reverse;
  57. }
  58. .left {
  59. display: flex;
  60. flex-direction: row;
  61. }
  62. .base {
  63. color: $secondary;
  64. font-size: 12px;
  65. }
  66. .name {
  67. padding: 0 4px;
  68. max-width: 100px;
  69. overflow: hidden;
  70. text-overflow: ellipsis;
  71. white-space: nowrap;
  72. }
  73. </style>