user-info.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <view class="page user-info">
  3. <view class="content">
  4. <head-image :name="userInfo.nickName" :url="userInfo.headImage"
  5. :size="160" @click="onShowFullImage()"></head-image>
  6. <view class="info-item">
  7. <view class="info-primary">
  8. <text class="info-username">
  9. {{userInfo.userName}}
  10. </text>
  11. <uni-icons v-show="userInfo.sex==0" class="sex-boy" type="person-filled" size="20"
  12. color="darkblue"></uni-icons>
  13. <uni-icons v-show="userInfo.sex==1" class="sex-girl" type="person-filled" size="20"
  14. color="darkred"></uni-icons>
  15. </view>
  16. <text>
  17. 昵称 :{{userInfo.nickName}}
  18. </text>
  19. <text>
  20. 签名 :{{userInfo.signature}}
  21. </text>
  22. </view>
  23. </view>
  24. <view class="line"></view>
  25. <view class="btn-group">
  26. <button class="btn" v-show="isFriend" type="primary" @click="onSendMessage()">发消息</button>
  27. <button class="btn" v-show="!isFriend" type="primary" @click="onAddFriend()">加为好友</button>
  28. <button class="btn" v-show="isFriend" type="warn" @click="onDelFriend()">删除好友</button>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. userInfo: {}
  37. }
  38. },
  39. methods: {
  40. onShowFullImage(){
  41. let imageUrl = this.userInfo.headImage;
  42. if(imageUrl){
  43. uni.previewImage({
  44. urls: [imageUrl]
  45. })
  46. }
  47. },
  48. onSendMessage() {
  49. let chat = {
  50. type: 'PRIVATE',
  51. targetId: this.userInfo.id,
  52. showName: this.userInfo.nickName,
  53. headImage: this.userInfo.headImage,
  54. };
  55. this.$store.commit("openChat", chat);
  56. uni.navigateTo({
  57. url:"/pages/chat/chat-box?chatIdx=0"
  58. })
  59. },
  60. onAddFriend() {
  61. this.$http({
  62. url: "/friend/add?friendId=" + this.userInfo.id,
  63. method: "POST"
  64. }).then((data) => {
  65. let friend = {
  66. id: this.userInfo.id,
  67. nickName: this.userInfo.nickName,
  68. headImage: this.userInfo.headImageThumb,
  69. online: this.userInfo.online
  70. }
  71. this.$store.commit("addFriend", friend);
  72. uni.showToast({
  73. title: '对方已成为您的好友',
  74. icon: 'none'
  75. })
  76. })
  77. },
  78. onDelFriend(){
  79. uni.showModal({
  80. title: "确认删除",
  81. content: `确认要删除与 '${this.userInfo.nickName}'的好友关系吗?`,
  82. success: (res)=> {
  83. if(res.cancel)
  84. return;
  85. this.$http({
  86. url: `/friend/delete/${this.userInfo.id}`,
  87. method: 'delete'
  88. }).then((data) => {
  89. this.$store.commit("removeFriend", this.userInfo.id);
  90. this.$store.commit("removePrivateChat", this.userInfo.id);
  91. uni.showToast({
  92. title: `与 '${this.userInfo.nickName}'的好友关系已解除`,
  93. icon: 'none'
  94. })
  95. })
  96. }
  97. })
  98. },
  99. updateFriendInfo() {
  100. // store的数据不能直接修改,深拷贝一份store的数据
  101. let friend = JSON.parse(JSON.stringify(this.friendInfo));
  102. friend.headImage = this.userInfo.headImageThumb;
  103. friend.nickName = this.userInfo.nickName;
  104. this.$http({
  105. url: "/friend/update",
  106. method: "PUT",
  107. data: friend
  108. }).then(() => {
  109. // 更新好友列表中的昵称和头像
  110. this.$store.commit("updateFriend", friend);
  111. // 更新会话中的头像和昵称
  112. this.$store.commit("updateChatFromFriend", this.userInfo);
  113. })
  114. },
  115. loadUserInfo(id){
  116. this.$http({
  117. url: "/user/find/" + id,
  118. method: 'GET'
  119. }).then((user) => {
  120. this.userInfo = user;
  121. // 如果发现好友的头像和昵称改了,进行更新
  122. if (this.isFriend && (this.userInfo.headImageThumb != this.friendInfo.headImage ||
  123. this.userInfo.nickName != this.friendInfo.nickName)) {
  124. this.updateFriendInfo()
  125. }
  126. })
  127. }
  128. },
  129. computed: {
  130. isFriend() {
  131. return this.friendInfo != undefined;
  132. },
  133. friendInfo(){
  134. let friends = this.$store.state.friendStore.friends;
  135. let friend = friends.find((f) => f.id == this.userInfo.id);
  136. return friend;
  137. }
  138. },
  139. onLoad(options) {
  140. // 查询用户信息
  141. this.loadUserInfo(options.id);
  142. }
  143. }
  144. </script>
  145. <style lang="scss" scoped>
  146. .user-info {
  147. .content {
  148. height: 200rpx;
  149. display: flex;
  150. align-items: center;
  151. justify-content: space-between;
  152. padding: 20rpx;
  153. .info-item {
  154. display: flex;
  155. align-items: flex-start;
  156. flex-direction: column;
  157. padding-left: 40rpx;
  158. flex: 1;
  159. .info-primary {
  160. display: flex;
  161. .info-username {
  162. font-size: 40rpx;
  163. font-weight: 600;
  164. }
  165. }
  166. }
  167. }
  168. .line {
  169. margin: 20rpx;
  170. border-bottom: 1px solid #aaaaaa;
  171. }
  172. .btn-group {
  173. margin: 100rpx;
  174. .btn{
  175. margin-top: 20rpx;
  176. }
  177. }
  178. }
  179. </style>