index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <view>
  3. <u-navbar
  4. title="选择朋友"
  5. :safeAreaInsetTop="true"
  6. :placeholder="true"
  7. :fixed="true"
  8. leftIcon="close"
  9. :autoBack="true"
  10. >
  11. </u-navbar>
  12. <!-- 好友列表 -->
  13. <view>
  14. <u-list @scrolltolower="scrolltolower">
  15. <u-list-item v-for="(friendItem, index) in friendList" :key="index">
  16. <!-- 展示的cell不能为当前聊天中的好友 -->
  17. <u-cell
  18. v-if="chattingId !== friendItem.userId"
  19. @click="openWaitSendModal(friendItem.userId)"
  20. >
  21. <u-avatar
  22. slot="icon"
  23. shape="square"
  24. size="40"
  25. :src="showFriendAvatar(friendItem)"
  26. customStyle="margin: -3px 5px -3px 0"
  27. ></u-avatar>
  28. <view slot="title">
  29. <u--text
  30. :lines="1"
  31. :text="showFriendNickname(friendItem)"
  32. iconStyle="margin-left: 2px;"
  33. ></u--text>
  34. </view>
  35. </u-cell>
  36. </u-list-item>
  37. </u-list>
  38. </view>
  39. <!-- 待发送modal -->
  40. <u-modal
  41. :show="isShowWaitSendModal"
  42. :zoom="true"
  43. title="发送名片"
  44. async-close
  45. show-cancel-button
  46. confirm-text="发送"
  47. cancel-text="取消"
  48. @confirm="sendUserCardMessage"
  49. @cancel="closeWaitSendModal"
  50. >
  51. <view class="content_box">
  52. <text class="send_target_title">发送给:</text>
  53. <view class="send_target_user_info">
  54. <image
  55. class="send_target_avatar"
  56. :src="showFriendAvatar(chattingId)"
  57. ></image>
  58. <text class="send_target_nickname">{{
  59. showFriendNickname({ userId: chattingId })
  60. }}</text>
  61. </view>
  62. <text class="wait_send_user_nickname"
  63. >[个人名片] {{ waitSendUserNickname || waitSendUserId || '' }}</text
  64. >
  65. <u-input
  66. class="leave_message_input"
  67. v-model="leaveMessageInput"
  68. placeholder="给朋友留言"
  69. type="text"
  70. maxlength="100"
  71. />
  72. </view>
  73. </u-modal>
  74. </view>
  75. </template>
  76. <script>
  77. import { emMessages } from '@/EaseIM/emApis';
  78. import { MESSAGE_TYPE } from '@/EaseIM/constant';
  79. const { sendDisplayMessages } = emMessages();
  80. export default {
  81. data() {
  82. return {
  83. sendTargetInfo: {
  84. userId: '',
  85. chatType: '',
  86. },
  87. isShowWaitSendModal: false,
  88. waitSendUserId: '',
  89. waitSendUserNickname: '',
  90. leaveMessageInput: '',
  91. defaultAvatar: '/static/images/new_ui/defaultAvatar.png',
  92. };
  93. },
  94. computed: {
  95. chattingId() {
  96. return this.$store.getters.chattingId;
  97. },
  98. chattingChatType() {
  99. return this.$store.getters.chattingChatType;
  100. },
  101. //好友列表
  102. friendList() {
  103. return this.$store.state.ContactsStore.friendList;
  104. },
  105. friendUserInfoCollection() {
  106. return this.$store.getters.friendUserInfoCollection;
  107. },
  108. //好友头像展示
  109. showFriendAvatar() {
  110. return (hxId) => {
  111. if (
  112. this.friendUserInfoCollection[hxId] &&
  113. this.friendUserInfoCollection[hxId]?.avatarurl
  114. ) {
  115. return this.friendUserInfoCollection[hxId].avatarurl;
  116. } else {
  117. return this.defaultAvatar;
  118. }
  119. };
  120. },
  121. //好友昵称展示
  122. showFriendNickname() {
  123. return (userItem) => {
  124. const { userId, remark } = userItem;
  125. if (remark) {
  126. return remark;
  127. }
  128. if (
  129. this.friendUserInfoCollection[userId] &&
  130. this.friendUserInfoCollection[userId]?.nickname
  131. ) {
  132. return this.friendUserInfoCollection[userId].nickname;
  133. } else {
  134. return userId;
  135. }
  136. };
  137. },
  138. },
  139. methods: {
  140. scrolltolower() {},
  141. loginUserAvactar() {
  142. return (
  143. this.$store.state.LoginStore.loginUserProfiles?.avatarurl ||
  144. this.defaultAvatar
  145. );
  146. },
  147. openWaitSendModal(userId) {
  148. console.log('>>>>>>>111', userId);
  149. this.isShowWaitSendModal = true;
  150. this.$nextTick(() => {
  151. this.waitSendUserId = userId;
  152. this.getWaitSendUserInfo(userId);
  153. });
  154. },
  155. getWaitSendUserInfo(userId) {
  156. if (this.friendUserInfoCollection[userId]) {
  157. this.waitSendUserNickname =
  158. this.friendUserInfoCollection[userId].nickname;
  159. return this.friendUserInfoCollection[userId];
  160. }
  161. },
  162. closeWaitSendModal() {
  163. this.isShowWaitSendModal = false;
  164. this.waitSendUserId = '';
  165. this.waitSendUserNickname = '';
  166. },
  167. async sendUserCardMessage() {
  168. const customEvent = 'userCard';
  169. if (!this.chattingId) return;
  170. const friendInfo = this.getWaitSendUserInfo(this.waitSendUserId);
  171. const customExtParams = {
  172. uid: this.waitSendUserId,
  173. ...friendInfo,
  174. };
  175. const params = {
  176. type: MESSAGE_TYPE.CUSTOM,
  177. // 消息接收方:单聊为对方用户 ID,群聊和聊天室分别为群组 ID 和聊天室 ID。
  178. to: this.chattingId,
  179. // 会话类型:单聊、群聊和聊天室分别为 `singleChat`、`groupChat` 和 `chatRoom`。
  180. chatType: this.chattingChatType,
  181. customEvent,
  182. customExts: { ...customExtParams },
  183. };
  184. try {
  185. await sendDisplayMessages({ ...params });
  186. if (this.leaveMessageInput) {
  187. this.sendLeaveTextMessage();
  188. }
  189. } catch (error) {
  190. uni.showToast({
  191. title: '消息发送失败',
  192. icon: 'none',
  193. });
  194. } finally {
  195. this.isShowWaitSendModal = false;
  196. uni.navigateBack();
  197. }
  198. },
  199. async sendLeaveTextMessage() {
  200. const params = {
  201. // 消息类型。
  202. type: 'txt',
  203. // 消息内容。
  204. msg: this.leaveMessageInput,
  205. // 消息接收方:单聊为对方用户 ID,群聊和聊天室分别为群组 ID 和聊天室 ID。
  206. to: this.chattingId,
  207. // 会话类型:单聊、群聊和聊天室分别为 `singleChat`、`groupChat` 和 `chatRoom`。
  208. chatType: this.chattingChatType,
  209. };
  210. try {
  211. const res = await sendDisplayMessages({ ...params });
  212. console.log('>>>>>留言消息发送成功', res);
  213. } catch (error) {
  214. console.log('>>>>>留言消息发送失败', error);
  215. uni.showToast({
  216. title: '消息发送失败',
  217. icon: 'none',
  218. });
  219. } finally {
  220. this.leaveMessageInput = '';
  221. uni.hideKeyboard && uni.hideKeyboard();
  222. }
  223. },
  224. },
  225. };
  226. </script>
  227. <style scoped>
  228. .content_box {
  229. width: 100%;
  230. display: flex;
  231. flex-direction: column;
  232. }
  233. .send_target_user_info {
  234. display: flex;
  235. justify-content: flex-start;
  236. align-items: center;
  237. border-bottom: 1rpx solid #eee;
  238. }
  239. .send_target_title {
  240. font-size: 33rpx;
  241. color: #333;
  242. font-weight: 600;
  243. }
  244. .send_target_avatar {
  245. width: 60rpx;
  246. height: 60rpx;
  247. border-radius: 50%;
  248. margin-right: 20rpx;
  249. }
  250. .send_target_nickname {
  251. font-size: 35rpx;
  252. color: #333;
  253. font-weight: 500;
  254. }
  255. .wait_send_user_nickname {
  256. font-size: 22rpx;
  257. color: rgba(51, 51, 51, 0.6);
  258. font-weight: 500;
  259. overflow: hidden;
  260. margin-top: 20rpx;
  261. }
  262. .leave_message_input {
  263. margin-top: 10rpx;
  264. background-color: #f7f7f7;
  265. border-radius: 5rpx;
  266. }
  267. </style>