conversation-list.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <view class="TUI-conversation-list" @click="dialogID = ''">
  3. <view class="no-data-box" v-if="conversationList.length == 0 && noticeList.length == 0">
  4. <image src="https://cos.his.cdwjyyh.com/fs/20240423/cf4a86b913a04341bb44e34bb4d37aa2.png"></image>
  5. <view class="empty-title">暂无数据</view>
  6. </view>
  7. <view v-if="conversationList.length > 0" v-for="(item, index) in conversationList" :key="index"
  8. style='background-color: #fff;'>
  9. <view
  10. class="TUI-conversation-item"
  11. :class="[dialogID === item.conversationID && 'selected', item.isPinned && 'pinned']"
  12. @click="handleGotoItem(item)"
  13. @longpress="handleItemLongpress(item)">
  14. <aside class="left">
  15. <image mode="aspectFill" class="avatar" :src="handleItemAvator(item)" />
  16. <span class="num" v-if="item.unreadCount > 0 && item.messageRemindType !== 'AcceptNotNotify'">
  17. {{ item.unreadCount > 99 ? '99+' : item.unreadCount }}
  18. </span>
  19. <span class="num-notNotify" v-if="item.unreadCount > 0 && item.messageRemindType === 'AcceptNotNotify'"></span>
  20. </aside>
  21. <main class="content">
  22. <header class="content-header">
  23. <label>
  24. <p class="name">{{ handleItemName(item) }}</p>
  25. </label>
  26. </header>
  27. <footer class="content-footer">
  28. <span class="content-footer-unread" v-if="item.unreadCount > 0 && item.messageRemindType === 'AcceptNotNotify'">
  29. [{{ item.unreadCount > 99 ? '99+' : item.unreadCount }}条]
  30. </span>
  31. <span class="message-text">{{ handleItemMessage(item.lastMessage) }}</span>
  32. <view class="conversation-line"></view>
  33. </footer>
  34. <view class="item-footer">
  35. <span class="time">{{ handleItemTime(item.lastMessage.lastTime) }}</span>
  36. <image class="mute-icon" v-if="item.messageRemindType === 'AcceptNotNotify'" src="../../assets/icon/mute.svg"></image>
  37. </view>
  38. </main>
  39. <view class="dialog-box dialog-item" v-if="item.conversationID === dialogID">
  40. <view class="conversation-options" @click.stop="handleConversation('delete', dialogID)">删除会话</view>
  41. <view class="conversation-options" v-if="!item.isPinned" @tap.stop="handleConversation('ispinned', dialogID)">置顶会话</view>
  42. <view class="conversation-options" v-if="item.isPinned" @click.stop="handleConversation('dispinned', dialogID)">取消置顶</view>
  43. <view
  44. class="conversation-options"
  45. v-if="item.messageRemindType === '' || item.messageRemindType === 'AcceptAndNotify'"
  46. @click.stop="handleConversation('mute', dialogID)">
  47. 消息免打扰
  48. </view>
  49. <view class="conversation-options" v-if="item.messageRemindType === 'AcceptNotNotify'" @click.stop="handleConversation('notMute', dialogID)">取消免打扰</view>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. </template>
  55. <script lang="ts">
  56. import { defineComponent, reactive, toRefs, watchEffect } from 'vue';
  57. import { caculateTimeago } from '../../utils/date';
  58. const TUIConversationList = defineComponent({
  59. props: {
  60. conversationList: {
  61. type: Array,
  62. default: () => {
  63. return [];
  64. }
  65. },
  66. currentID: {
  67. type: String,
  68. default: () => {
  69. return '';
  70. }
  71. },
  72. noticeList: {
  73. type: [String, Array],
  74. default: []
  75. }
  76. },
  77. setup(props: any, ctx: any) {
  78. const obj = reactive({
  79. conversationList: [],
  80. currentID: '',
  81. isOpened: 'none',
  82. currentConversation: {},
  83. dialogID: ''
  84. });
  85. watchEffect(() => {
  86. obj.conversationList = props.conversationList;
  87. obj.currentID = props.currentID;
  88. });
  89. // 处理头像
  90. const handleItemAvator = (item: any) => {
  91. let avatar = '';
  92. switch (item.type) {
  93. case uni.$TIM.TYPES.CONV_C2C:
  94. avatar = item?.userProfile?.avatar || 'https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png';
  95. break;
  96. case uni.$TIM.TYPES.CONV_GROUP:
  97. avatar = item?.groupProfile?.avatar || 'https://web.sdk.qcloud.com/component/TUIKit/assets/group_avatar.png';
  98. break;
  99. case uni.$TIM.TYPES.CONV_SYSTEM:
  100. avatar = item?.groupProfile?.avatar || 'https://web.sdk.qcloud.com/component/TUIKit/assets/group_avatar.png';
  101. break;
  102. }
  103. return avatar;
  104. };
  105. // 处理昵称
  106. const handleItemName = (item: any) => {
  107. let name = '';
  108. switch (item.type) {
  109. case uni.$TIM.TYPES.CONV_C2C:
  110. name = item?.userProfile.nick || item?.userProfile?.userID || '';
  111. break;
  112. case uni.$TIM.TYPES.CONV_GROUP:
  113. name = item.groupProfile.name || item?.groupProfile?.groupID || '';
  114. break;
  115. case uni.$TIM.TYPES.CONV_SYSTEM:
  116. name = '系统通知';
  117. break;
  118. }
  119. return name;
  120. };
  121. // 处理时间
  122. const handleItemTime = (time: number) => {
  123. if (time > 0) {
  124. return caculateTimeago(time * 1000);
  125. }
  126. return '';
  127. };
  128. // 处理lastMessage
  129. const handleItemMessage = (message: any) => {
  130. switch (message.type) {
  131. case uni.$TIM.TYPES.MSG_TEXT:
  132. return message.payload.text;
  133. default:
  134. return message.messageForShow;
  135. }
  136. };
  137. const handleGotoItem = (item: any) => {
  138. ctx.emit('handleGotoItem', item);
  139. };
  140. const handleItemLongpress = (item: any) => {
  141. //长按
  142. obj.currentConversation = item;
  143. obj.dialogID = item.conversationID;
  144. if (item.type === 'C2C') {
  145. obj.currentuserID = item.userProfile.userID;
  146. } else if (item.type === 'GROUP') {
  147. obj.currentuserID = item.groupProfile.groupID;
  148. }
  149. obj.conversationType = item.type;
  150. };
  151. // todo
  152. const handlerIsOpened = (item: any) => {
  153. if (item.conversationID === obj.doalogID) {
  154. return 'right';
  155. } else {
  156. return 'none';
  157. }
  158. };
  159. // 删除会话,后续TODO,置顶聊天,消息免打扰
  160. const handleConversation = (type: string) => {
  161. switch (type) {
  162. case 'delete':
  163. uni.$TUIKit.TUIConversationServer.deleteConversation(obj.dialogID).then((imResponse: any) => {
  164. const { conversationID } = imResponse.data;
  165. });
  166. obj.dialogID = '';
  167. break;
  168. case 'ispinned':
  169. if (type === 'ispinned') {
  170. const options: any = {
  171. conversationID: obj.dialogID,
  172. isPinned: true
  173. };
  174. uni.$TUIKit.TUIConversationServer.pinConversation(options).then((imResponse: any) => {
  175. console.log(imResponse);
  176. });
  177. }
  178. obj.dialogID = '';
  179. break;
  180. case 'dispinned':
  181. if (type === 'dispinned') {
  182. const options: any = {
  183. conversationID: obj.dialogID,
  184. isPinned: false
  185. };
  186. uni.$TUIKit.TUIConversationServer.pinConversation(options).then((imResponse: any) => {});
  187. }
  188. obj.dialogID = '';
  189. break;
  190. case 'mute':
  191. if (type === 'mute' && obj.conversationType === 'C2C') {
  192. const options: any = {
  193. userIDList: [obj.currentuserID],
  194. messageRemindType: uni.$TIM.TYPES.MSG_REMIND_ACPT_NOT_NOTE
  195. };
  196. uni.$TUIKit.TUIConversationServer.muteConversation(options).then((imResponse: any) => {
  197. console.log(imResponse);
  198. });
  199. } else if (type === 'mute' && obj.conversationType === 'GROUP') {
  200. const options: any = {
  201. groupID: obj.currentuserID,
  202. messageRemindType: uni.$TIM.TYPES.MSG_REMIND_ACPT_NOT_NOTE
  203. };
  204. uni.$TUIKit.TUIConversationServer.muteConversation(options).then((imResponse: any) => {
  205. console.log(imResponse);
  206. });
  207. }
  208. obj.dialogID = '';
  209. break;
  210. case 'notMute':
  211. if (type === 'notMute' && obj.conversationType === 'C2C') {
  212. const options: any = {
  213. userIDList: [obj.currentuserID],
  214. messageRemindType: uni.$TIM.TYPES.MSG_REMIND_ACPT_AND_NOTE
  215. };
  216. uni.$TUIKit.TUIConversationServer.muteConversation(options).then((imResponse: any) => {
  217. console.log(imResponse);
  218. });
  219. } else if (type === 'notMute' && obj.conversationType === 'GROUP') {
  220. const options: any = {
  221. groupID: obj.currentuserID,
  222. messageRemindType: uni.$TIM.TYPES.MSG_REMIND_ACPT_AND_NOTE
  223. };
  224. uni.$TUIKit.TUIConversationServer.muteConversation(options).then((imResponse: any) => {
  225. console.log(imResponse);
  226. });
  227. }
  228. obj.dialogID = '';
  229. break;
  230. }
  231. };
  232. return {
  233. ...toRefs(obj),
  234. handleGotoItem,
  235. handleItemAvator,
  236. handleItemTime,
  237. handleItemMessage,
  238. handleItemName,
  239. handleItemLongpress,
  240. handleConversation,
  241. handlerIsOpened
  242. };
  243. }
  244. });
  245. export default TUIConversationList;
  246. </script>
  247. <style lang="scss" scoped>
  248. .TUI-conversation {
  249. font-family: PingFangSC-Regular;
  250. font-weight: 400;
  251. letter-spacing: 0;
  252. &-list {
  253. list-style: none;
  254. }
  255. &-item {
  256. position: relative;
  257. padding: 12px;
  258. display: flex;
  259. align-items: center;
  260. .left {
  261. position: relative;
  262. margin-right: 10px;
  263. .num {
  264. font-family: PingFangSC-Regular;
  265. position: absolute;
  266. display: inline-block;
  267. right: -8px;
  268. top: -8px;
  269. background: red;
  270. width: 20px;
  271. height: 20px;
  272. font-size: 11px;
  273. text-align: center;
  274. line-height: 20px;
  275. border-radius: 50%;
  276. color: #ffffff;
  277. font-weight: 600;
  278. letter-spacing: 0;
  279. }
  280. .num-notNotify {
  281. position: absolute;
  282. display: inline-block;
  283. right: -4px;
  284. top: -4px;
  285. background: red;
  286. width: 11px;
  287. height: 11px;
  288. border-radius: 50%;
  289. color: #ffffff;
  290. }
  291. .avatar {
  292. width: 48px;
  293. height: 48px;
  294. border-radius: 6px;
  295. }
  296. }
  297. .content {
  298. flex: 1;
  299. // padding-left: 10px;
  300. position: relative;
  301. p {
  302. width: 200px;
  303. overflow: hidden;
  304. text-overflow: ellipsis;
  305. white-space: nowrap;
  306. font-weight: 400;
  307. font-size: 14px;
  308. color: #999999;
  309. letter-spacing: 0;
  310. line-height: 19px;
  311. font-family: PingFangSC-Regular;
  312. }
  313. .conversation-line {
  314. position: absolute;
  315. display: block;
  316. left: 0;
  317. right: -12px;
  318. height: 0.5px;
  319. transform: scaleY(0.2);
  320. background: #b0b0b0;
  321. /* padding-top: 10px; */
  322. bottom: -15px;
  323. }
  324. .name {
  325. font-weight: 400;
  326. font-size: 16px;
  327. color: #000000;
  328. letter-spacing: 0;
  329. margin-bottom: 5px;
  330. font-family: PingFangSC-Regular;
  331. }
  332. &-header {
  333. display: flex;
  334. justify-content: space-between;
  335. align-items: center;
  336. label {
  337. flex: 1;
  338. font-size: 14px;
  339. color: #000000;
  340. .name {
  341. width: 110px;
  342. overflow: hidden;
  343. text-overflow: ellipsis;
  344. white-space: nowrap;
  345. }
  346. }
  347. .time {
  348. font-size: 12px;
  349. color: #b0b0b0;
  350. line-height: 16px;
  351. display: inline-block;
  352. max-width: 75px;
  353. font-weight: 400;
  354. }
  355. }
  356. &-footer {
  357. color: #999999;
  358. line-height: 16px;
  359. display: flex;
  360. .message-text {
  361. margin-left: 4px;
  362. display: block;
  363. width: 240px;
  364. overflow: hidden;
  365. text-overflow: ellipsis;
  366. white-space: nowrap;
  367. }
  368. }
  369. .item-footer {
  370. position: absolute;
  371. right: 0;
  372. top: 0;
  373. justify-items: center;
  374. display: flex;
  375. flex-direction: column;
  376. align-items: flex-end;
  377. .time {
  378. font-size: 12px;
  379. color: #b0b0b0;
  380. line-height: 16px;
  381. display: inline-block;
  382. max-width: 75px;
  383. font-weight: 400;
  384. margin-bottom: 5px;
  385. }
  386. .mute-icon {
  387. display: block;
  388. width: 20px;
  389. height: 20px;
  390. }
  391. }
  392. }
  393. .dialog-box {
  394. position: absolute;
  395. z-index: 5;
  396. background: #fff;
  397. border: 1px solid #dddddd;
  398. padding: 15px 20px;
  399. right: 15px;
  400. top: 30px;
  401. &-item {
  402. top: 60px;
  403. right: 40px;
  404. box-shadow: 0 11px 20px 0 rgb(0 0 0 / 30%);
  405. background: #ffffff;
  406. border: 1px solid #e0e0e0;
  407. box-shadow: 0 -4px 12px 0 rgb(0 0 0 / 6%);
  408. border-radius: 8px;
  409. }
  410. .conversation-options {
  411. height: 35px;
  412. font-family: PingFangSC-Regular;
  413. font-weight: 400;
  414. font-size: 14px;
  415. color: #4f4f4f;
  416. letter-spacing: 0;
  417. line-height: 35px;
  418. }
  419. }
  420. }
  421. }
  422. .selected {
  423. background: #edf0f5;
  424. }
  425. .pinned {
  426. background: #eef0f3;
  427. }
  428. </style>