user-info.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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">{{ userInfo.nickName }}</text>
  11. <text v-show="userInfo.sex == 0" class="iconfont icon-man" color="darkblue"></text>
  12. <text v-show="userInfo.sex == 1" class="iconfont icon-girl" color="darkred"></text>
  13. </view>
  14. <view class="info-text">
  15. <text class="label-text">用户名:</text>
  16. <text class="content-text"> {{ userInfo.userName }}</text>
  17. </view>
  18. <view class="info-text">
  19. <view>
  20. <text class="label-text">签名:</text>
  21. <text class="content-text"> {{ userInfo.signature }} </text>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </uni-card>
  27. <bar-group v-if="isFriend">
  28. <switch-bar title="消息免打扰" :checked="friendInfo.isDnd" @change="onDndChange"></switch-bar>
  29. </bar-group>
  30. <bar-group v-if="chatIdx>=0">
  31. <arrow-bar title="清空聊天记录" @tap="onCleanMessage()"></arrow-bar>
  32. </bar-group>
  33. <bar-group>
  34. <btn-bar v-show="isFriend" type="primary" title="发送消息" @tap="onSendMessage()">
  35. </btn-bar>
  36. <btn-bar v-show="!isFriend" type="primary" title="加为好友" @tap="onAddFriend()"></btn-bar>
  37. <btn-bar v-show="isFriend" type="danger" title="删除好友" @tap="onDelFriend()"></btn-bar>
  38. </bar-group>
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. data() {
  44. return {
  45. userInfo: {}
  46. }
  47. },
  48. methods: {
  49. onShowFullImage() {
  50. let imageUrl = this.userInfo.headImage;
  51. if (imageUrl) {
  52. uni.previewImage({
  53. urls: [imageUrl]
  54. })
  55. }
  56. },
  57. onSendMessage() {
  58. let chat = {
  59. type: 'PRIVATE',
  60. targetId: this.userInfo.id,
  61. showName: this.userInfo.nickName,
  62. headImage: this.userInfo.headImage,
  63. };
  64. if (this.isFriend) {
  65. chat.isDnd = this.friendInfo.isDnd;
  66. }
  67. this.chatStore.openChat(chat);
  68. let chatIdx = this.chatStore.findChatIdx(chat);
  69. uni.navigateTo({
  70. url: "/pages/chat/chat-box?chatIdx=" + chatIdx
  71. })
  72. },
  73. onAddFriend() {
  74. this.$http({
  75. url: "/friend/add?friendId=" + this.userInfo.id,
  76. method: "POST"
  77. }).then((data) => {
  78. let friend = {
  79. id: this.userInfo.id,
  80. nickName: this.userInfo.nickName,
  81. headImage: this.userInfo.headImageThumb,
  82. online: this.userInfo.online,
  83. deleted: false
  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. onCleanMessage() {
  114. uni.showModal({
  115. title: '清空聊天记录',
  116. content: `确认删除与'${this.userInfo.nickName}'的聊天记录吗?`,
  117. confirmText: '确认',
  118. success: (res) => {
  119. if (res.cancel)
  120. return;
  121. this.chatStore.cleanMessage(this.chatIdx);
  122. uni.showToast({
  123. title: `您清空了'${this.userInfo.nickName}'的聊天记录`,
  124. icon: 'none'
  125. })
  126. }
  127. })
  128. },
  129. onDndChange(e) {
  130. let isDnd = e.detail.value;
  131. let friendId = this.userInfo.id;
  132. let formData = {
  133. friendId: friendId,
  134. isDnd: isDnd
  135. }
  136. this.$http({
  137. url: '/friend/dnd',
  138. method: 'PUT',
  139. data: formData
  140. }).then(() => {
  141. this.friendStore.setDnd(friendId, isDnd)
  142. let chat = this.chatStore.findChatByFriend(friendId)
  143. if (chat) {
  144. this.chatStore.setDnd(chat, isDnd)
  145. }
  146. })
  147. },
  148. updateFriendInfo() {
  149. if (this.isFriend) {
  150. // store的数据不能直接修改,深拷贝一份store的数据
  151. let friend = JSON.parse(JSON.stringify(this.friendInfo));
  152. friend.headImage = this.userInfo.headImageThumb;
  153. friend.nickName = this.userInfo.nickName;
  154. // 更新好友列表中的昵称和头像
  155. this.friendStore.updateFriend(friend);
  156. // 更新会话中的头像和昵称
  157. this.chatStore.updateChatFromFriend(this.userInfo);
  158. }
  159. },
  160. loadUserInfo(id) {
  161. this.$http({
  162. url: "/user/find/" + id,
  163. method: 'GET'
  164. }).then((user) => {
  165. this.userInfo = user;
  166. // 如果发现好友的头像和昵称改了,进行更新
  167. this.updateFriendInfo()
  168. })
  169. }
  170. },
  171. computed: {
  172. isFriend() {
  173. return this.friendStore.isFriend(this.userInfo.id);
  174. },
  175. friendInfo() {
  176. return this.friendStore.findFriend(this.userInfo.id);
  177. },
  178. chatIdx() {
  179. let chat = this.chatStore.findChatByFriend(this.userInfo.id);
  180. if (chat) {
  181. return this.chatStore.findChatIdx(chat);
  182. }
  183. return -1;
  184. }
  185. },
  186. onLoad(options) {
  187. // 查询用户信息
  188. this.loadUserInfo(options.id);
  189. }
  190. }
  191. </script>
  192. <style lang="scss" scoped>
  193. .user-info {
  194. .content {
  195. height: 200rpx;
  196. display: flex;
  197. align-items: center;
  198. justify-content: space-between;
  199. padding: 20rpx;
  200. .info-item {
  201. display: flex;
  202. align-items: flex-start;
  203. flex-direction: column;
  204. padding-left: 40rpx;
  205. flex: 1;
  206. .info-text {
  207. line-height: 1.5;
  208. //margin-bottom: 10rpx;
  209. }
  210. .label-text {
  211. font-size: $im-font-size-small;
  212. color: $im-text-color-light;
  213. }
  214. .content-text {
  215. font-size: $im-font-size-small;
  216. color: $im-text-color-light;
  217. margin-left: 10rpx;
  218. }
  219. .info-primary {
  220. display: flex;
  221. align-items: center;
  222. margin-bottom: 10rpx;
  223. .info-username {
  224. font-size: $im-font-size-large;
  225. font-weight: 600;
  226. }
  227. .icon-man {
  228. color: $im-text-color;
  229. font-size: $im-font-size-large;
  230. padding-left: 10rpx;
  231. }
  232. .icon-girl {
  233. color: darkred;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. </style>