Friend.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <el-container>
  3. <el-aside width="250px" class="l-friend-box">
  4. <div class="l-friend-header" height="60px">
  5. <div class="l-friend-search">
  6. <el-input width="200px" placeholder="搜索好友" v-model="searchText">
  7. <el-button slot="append" icon="el-icon-search"></el-button>
  8. </el-input>
  9. </div>
  10. <el-button plain icon="el-icon-plus" style="border: none; padding:12px; font-size: 20px;color: black;" title="添加好友"
  11. @click="handleShowAddFriend()"></el-button>
  12. <add-friend :dialogVisible="showAddFriend" @close="handleCloseAddFriend">
  13. </add-friend>
  14. </div>
  15. <div v-for="(friend,index) in $store.state.friendStore.friends" :key="friend.id">
  16. <friend-item v-show="friend.nickName.startsWith(searchText)" :friend="friend" :index="index" :active="index === $store.state.friendStore.activeIndex"
  17. @del="handleDelItem(friend,index)" @click.native="handleActiveItem(friend,index)">
  18. </friend-item>
  19. </div>
  20. </el-aside>
  21. <el-container class="r-friend-box">
  22. <div v-show="userInfo.id">
  23. <div class="user-detail">
  24. <head-image class="detail-head-image" :size="200" :url="userInfo.headImage"></head-image>
  25. <div class="info-item">
  26. <el-descriptions title="好友信息" class="description" :column="1">
  27. <el-descriptions-item label="用户名">{{ userInfo.userName }}
  28. </el-descriptions-item>
  29. <el-descriptions-item label="昵称">{{ userInfo.nickName }}
  30. </el-descriptions-item>
  31. <el-descriptions-item label="性别">{{ userInfo.sex==0?"男":"女" }}</el-descriptions-item>
  32. <el-descriptions-item label="签名">{{ userInfo.signature }}</el-descriptions-item>
  33. </el-descriptions>
  34. </div>
  35. </div>
  36. <div class="btn-group">
  37. <el-button class="send-btn" @click="handleSendMessage()">发消息</el-button>
  38. </div>
  39. </div>
  40. </el-container>
  41. </el-container>
  42. </template>
  43. <script>
  44. import FriendItem from "../components/friend/FriendItem.vue";
  45. import AddFriend from "../components/friend/AddFriend.vue";
  46. import HeadImage from "../components/common/HeadImage.vue";
  47. export default {
  48. name: "friend",
  49. components: {
  50. FriendItem,
  51. AddFriend,
  52. HeadImage
  53. },
  54. data() {
  55. return {
  56. searchText: "",
  57. showAddFriend: false,
  58. userInfo: {}
  59. }
  60. },
  61. methods: {
  62. handleShowAddFriend() {
  63. this.showAddFriend = true;
  64. },
  65. handleCloseAddFriend() {
  66. this.showAddFriend = false;
  67. },
  68. handleActiveItem(friend, index) {
  69. this.$store.commit("activeFriend", index);
  70. this.loadUserInfo(friend,index);
  71. },
  72. handleDelItem(friend, index) {
  73. this.$confirm(`确认要解除与 '${friend.nickName}'的好友关系吗?`, '确认解除?', {
  74. confirmButtonText: '确定',
  75. cancelButtonText: '取消',
  76. type: 'warning'
  77. }).then(() => {
  78. this.$http({
  79. url: `/api/friend/delete/${friend.id}`,
  80. method: 'delete'
  81. }).then((data) => {
  82. this.$message.success("删除好友成功");
  83. this.$store.commit("removeFriend", index);
  84. this.$store.commit("removePrivateChat", friend.id);
  85. })
  86. })
  87. },
  88. handleSendMessage() {
  89. let user = this.userInfo;
  90. let chat = {
  91. type: 'PRIVATE',
  92. targetId: user.id,
  93. showName: user.nickName,
  94. headImage: user.headImage,
  95. };
  96. this.$store.commit("openChat", chat);
  97. this.$store.commit("activeChat", 0);
  98. this.$router.push("/home/chat");
  99. },
  100. updateFriendInfo(friend, user, index) {
  101. // store的数据不能直接修改,深拷贝一份store的数据
  102. friend = JSON.parse(JSON.stringify(friend));
  103. friend.headImage = user.headImageThumb;
  104. friend.nickName = user.nickName;
  105. this.$http({
  106. url: "/api/friend/update",
  107. method: "put",
  108. data: friend
  109. }).then(() => {
  110. this.$store.commit("updateFriend", friend);
  111. this.$store.commit("updateChatFromUser", user);
  112. })
  113. },
  114. loadUserInfo(friend,index){
  115. this.$http({
  116. url: `/api/user/find/${friend.id}`,
  117. method: 'get'
  118. }).then((user) => {
  119. this.userInfo = user;
  120. // 如果发现好友的头像和昵称改了,进行更新
  121. if (user.headImageThumb != friend.headImage ||
  122. user.nickName != friend.nickName) {
  123. this.updateFriendInfo(friend, user, index)
  124. }
  125. })
  126. }
  127. },
  128. computed:{
  129. friendStore(){
  130. return this.$store.state.friendStore;
  131. }
  132. },
  133. mounted() {
  134. if(this.friendStore.activeIndex>=0){
  135. let friend = this.friendStore.friends[this.friendStore.activeIndex];
  136. this.loadUserInfo(friend,this.friendStore.activeIndex);
  137. }
  138. }
  139. }
  140. </script>
  141. <style scoped lang="scss">
  142. .el-container {
  143. .l-friend-box {
  144. border: #dddddd solid 1px;
  145. background: white;
  146. .l-friend-header {
  147. display: flex;
  148. align-items: center;
  149. padding: 5px;
  150. background-color: white;
  151. .l-friend-search {
  152. flex: 1;
  153. }
  154. }
  155. }
  156. .r-friend-box {
  157. .user-detail {
  158. width: 100%;
  159. display: flex;
  160. padding: 50px 10px 10px 50px;
  161. text-align: center;
  162. justify-content: space-around;
  163. .info-item {
  164. width: 400px;
  165. height: 200px;
  166. background-color: #ffffff;
  167. }
  168. .description {
  169. padding: 20px 20px 0px 20px;
  170. }
  171. }
  172. .btn-group {
  173. text-align: left !important;
  174. padding-left: 100px;
  175. }
  176. }
  177. }
  178. </style>