user-info.vue 4.6 KB

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