AddFriend.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <el-dialog title="添加好友" :visible.sync="dialogVisible" width="400px" :before-close="onClose"
  3. custom-class="add-friend-dialog">
  4. <el-input placeholder="输入用户名或昵称按下enter搜索,最多展示20条" class="input-with-select" v-model="searchText" size="small"
  5. @keyup.enter.native="onSearch()">
  6. <i class="el-icon-search el-input__icon" slot="suffix" @click="onSearch()"> </i>
  7. </el-input>
  8. <el-scrollbar style="height:400px">
  9. <div v-for="(user) in users" :key="user.id" v-show="user.id != $store.state.userStore.userInfo.id">
  10. <div class="item">
  11. <div class="avatar">
  12. <head-image :name="user.nickName" :url="user.headImage" :online="user.online"></head-image>
  13. </div>
  14. <div class="add-friend-text">
  15. <div class="text-user-name">
  16. <div>{{ user.userName }}</div>
  17. <div :class="user.online ? 'online-status online' : 'online-status'">{{
  18. user.online ? "[在线]" :"[离线]"}}</div>
  19. </div>
  20. <div class="text-nick-name">
  21. <div>昵称:{{ user.nickName }}</div>
  22. </div>
  23. </div>
  24. <el-button type="success" size="mini" v-show="!isFriend(user.id)"
  25. @click="onAddFriend(user)">添加</el-button>
  26. <el-button type="info" size="mini" v-show="isFriend(user.id)" plain disabled>已添加</el-button>
  27. </div>
  28. </div>
  29. </el-scrollbar>
  30. </el-dialog>
  31. </template>
  32. <script>
  33. import HeadImage from '../common/HeadImage.vue'
  34. export default {
  35. name: "addFriend",
  36. components: { HeadImage },
  37. data() {
  38. return {
  39. users: [],
  40. searchText: ""
  41. }
  42. },
  43. props: {
  44. dialogVisible: {
  45. type: Boolean
  46. }
  47. },
  48. methods: {
  49. onClose() {
  50. this.$emit("close");
  51. },
  52. onSearch() {
  53. if (!this.searchText) {
  54. this.users = [];
  55. return;
  56. }
  57. this.$http({
  58. url: "/user/findByName",
  59. method: "get",
  60. params: {
  61. name: this.searchText
  62. }
  63. }).then((data) => {
  64. this.users = data;
  65. })
  66. },
  67. onAddFriend(user) {
  68. this.$http({
  69. url: "/friend/add",
  70. method: "post",
  71. params: {
  72. friendId: user.id
  73. }
  74. }).then((data) => {
  75. this.$message.success("添加成功,对方已成为您的好友");
  76. let friend = {
  77. id: user.id,
  78. nickName: user.nickName,
  79. headImage: user.headImage,
  80. online: user.online
  81. }
  82. this.$store.commit("addFriend", friend);
  83. })
  84. },
  85. isFriend(userId) {
  86. let friends = this.$store.state.friendStore.friends;
  87. let friend = friends.find((f) => f.id == userId);
  88. return friend != undefined;
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="scss">
  94. .add-friend-dialog {
  95. .item {
  96. height: 65px;
  97. display: flex;
  98. position: relative;
  99. padding-left: 15px;
  100. align-items: center;
  101. padding-right: 25px;
  102. .add-friend-text {
  103. margin-left: 15px;
  104. flex: 3;
  105. display: flex;
  106. flex-direction: column;
  107. flex-shrink: 0;
  108. overflow: hidden;
  109. .text-user-name {
  110. display: flex;
  111. flex-direction: row;
  112. font-weight: 600;
  113. font-size: 16px;
  114. line-height: 25px;
  115. .online-status {
  116. font-size: 12px;
  117. font-weight: 600;
  118. &.online {
  119. color: #5fb878;
  120. }
  121. }
  122. }
  123. .text-nick-name {
  124. display: flex;
  125. flex-direction: row;
  126. font-size: 12px;
  127. line-height: 20px;
  128. }
  129. }
  130. }
  131. }
  132. </style>