user-info.vue 4.8 KB

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