user-info.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. deleted: false
  85. }
  86. this.friendStore.addFriend(friend);
  87. uni.showToast({
  88. title: '对方已成为您的好友',
  89. icon: 'none'
  90. })
  91. })
  92. },
  93. onDelFriend() {
  94. uni.showModal({
  95. title: "确认删除",
  96. content: `确认删除 '${this.userInfo.nickName}',并删除聊天记录吗?`,
  97. success: (res) => {
  98. if (res.cancel)
  99. return;
  100. this.$http({
  101. url: `/friend/delete/${this.userInfo.id}`,
  102. method: 'delete'
  103. }).then((data) => {
  104. this.friendStore.removeFriend(this.userInfo.id);
  105. this.chatStore.removePrivateChat(this.userInfo.id);
  106. uni.showToast({
  107. title: `与 '${this.userInfo.nickName}'的好友关系已解除`,
  108. icon: 'none'
  109. })
  110. })
  111. }
  112. })
  113. },
  114. updateFriendInfo() {
  115. if (this.isFriend) {
  116. // store的数据不能直接修改,深拷贝一份store的数据
  117. let friend = JSON.parse(JSON.stringify(this.friendInfo));
  118. friend.headImage = this.userInfo.headImageThumb;
  119. friend.nickName = this.userInfo.nickName;
  120. // 更新好友列表中的昵称和头像
  121. this.friendStore.updateFriend(friend);
  122. // 更新会话中的头像和昵称
  123. this.chatStore.updateChatFromFriend(this.userInfo);
  124. }
  125. },
  126. loadUserInfo(id) {
  127. this.$http({
  128. url: "/user/find/" + id,
  129. method: 'GET'
  130. }).then((user) => {
  131. this.userInfo = user;
  132. // 如果发现好友的头像和昵称改了,进行更新
  133. this.updateFriendInfo()
  134. })
  135. }
  136. },
  137. computed: {
  138. isFriend() {
  139. return this.friendStore.isFriend(this.userInfo.id);
  140. },
  141. friendInfo() {
  142. return this.friendStore.findFriend(this.userInfo.id);
  143. }
  144. },
  145. onLoad(options) {
  146. // 查询用户信息
  147. this.loadUserInfo(options.id);
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. .user-info {
  153. .content {
  154. height: 200rpx;
  155. display: flex;
  156. align-items: center;
  157. justify-content: space-between;
  158. padding: 20rpx;
  159. .info-item {
  160. display: flex;
  161. align-items: flex-start;
  162. flex-direction: column;
  163. padding-left: 40rpx;
  164. flex: 1;
  165. .info-text {
  166. line-height: 1.5;
  167. //margin-bottom: 10rpx;
  168. }
  169. .label-text {
  170. font-size: $im-font-size-small;
  171. color: $im-text-color-light;
  172. }
  173. .content-text {
  174. font-size: $im-font-size-small;
  175. color: $im-text-color-light;
  176. }
  177. .info-primary {
  178. display: flex;
  179. align-items: center;
  180. margin-bottom: 10rpx;
  181. .info-username {
  182. font-size: $im-font-size-large;
  183. font-weight: 600;
  184. }
  185. .icon-man {
  186. color: $im-text-color;
  187. font-size: $im-font-size-large;
  188. padding-left: 10rpx;
  189. }
  190. .icon-girl {
  191. color: darkred;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. </style>