user-info.vue 4.7 KB

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