user-info.vue 5.0 KB

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