index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <BottomPopup
  3. :show="showAtList"
  4. @onClose="closeAt"
  5. >
  6. <div
  7. ref="MessageInputAt"
  8. :class="[isPC ? 'message-input-at' : 'message-input-at-h5']"
  9. >
  10. <div
  11. ref="dialog"
  12. class="member-list"
  13. >
  14. <header
  15. v-if="!isPC"
  16. class="member-list-title"
  17. >
  18. <span class="title">{{
  19. TUITranslateService.t("TUIChat.选择提醒的人")
  20. }}</span>
  21. </header>
  22. <ul class="member-list-box">
  23. <li
  24. v-for="(item, index) in showMemberList"
  25. :key="index"
  26. ref="memberListItems"
  27. class="member-list-box-body"
  28. :class="[index === selectedIndex && 'selected']"
  29. @click="selectItem(index)"
  30. >
  31. <img
  32. class="member-list-box-body-avatar"
  33. :src="handleMemberAvatar(item)"
  34. >
  35. <span class="member-list-box-body-name">
  36. {{ handleMemberName(item) }}
  37. </span>
  38. </li>
  39. </ul>
  40. </div>
  41. </div>
  42. </BottomPopup>
  43. </template>
  44. <script lang="ts" setup>
  45. import TUIChatEngine, {
  46. TUIStore,
  47. StoreName,
  48. TUIGroupService,
  49. TUITranslateService,
  50. } from '@tencentcloud/chat-uikit-engine';
  51. import { TUIGlobal } from '@tencentcloud/universal-api';
  52. import { ref, watch } from '../../../../adapter-vue';
  53. import { isPC, isH5 } from '../../../../utils/env';
  54. import BottomPopup from '../../../common/BottomPopup/index.vue';
  55. const emits = defineEmits(['onAtListOpen', 'insertAt']);
  56. const MessageInputAt = ref();
  57. const memberListItems = ref();
  58. const showAtList = ref(false);
  59. const memberList = ref<Array<any>>();
  60. const allMemberList = ref<Array<any>>();
  61. const showMemberList = ref<Array<any>>();
  62. const isGroup = ref(false);
  63. const position = ref({
  64. left: 0,
  65. top: 0,
  66. });
  67. const selectedIndex = ref(0);
  68. const currentConversationID = ref('');
  69. const all = {
  70. userID: TUIChatEngine.TYPES.MSG_AT_ALL,
  71. nick: '所有人',
  72. isAll: true,
  73. avatar: 'https://web.sdk.qcloud.com/im/assets/images/at.svg',
  74. };
  75. TUIStore.watch(StoreName.CONV, {
  76. currentConversationID: (id: string) => {
  77. if (id !== currentConversationID.value) {
  78. currentConversationID.value = id;
  79. memberList.value = [];
  80. allMemberList.value = [];
  81. showMemberList.value = [];
  82. isGroup.value = false;
  83. TUIStore.update(StoreName.CUSTOM, 'memberList', memberList.value);
  84. if (currentConversationID?.value?.startsWith('GROUP')) {
  85. isGroup.value = true;
  86. const groupID = currentConversationID?.value?.substring(5);
  87. TUIGroupService.switchGroup(groupID);
  88. } else {
  89. TUIGroupService.switchGroup('');
  90. }
  91. }
  92. },
  93. });
  94. TUIStore.watch(StoreName.GRP, {
  95. currentGroupMemberList: (list: Array<any>) => {
  96. memberList.value = list;
  97. allMemberList.value = [all, ...memberList.value];
  98. showMemberList.value = allMemberList.value;
  99. TUIStore.update(StoreName.CUSTOM, 'memberList', memberList.value);
  100. },
  101. });
  102. const toggleAtList = (show: boolean) => {
  103. if (!isGroup.value) {
  104. return;
  105. }
  106. showAtList.value = show;
  107. if (showAtList.value) {
  108. emits('onAtListOpen');
  109. }
  110. };
  111. const handleAtListPosition = (positionData: { top: number; left: number }) => {
  112. position.value = positionData;
  113. };
  114. const setCurrentSelectIndex = (index: any) => {
  115. selectedIndex.value = index;
  116. memberListItems.value?.[selectedIndex.value]?.scrollIntoView(false);
  117. };
  118. const setShowMemberList = (list: any) => {
  119. showMemberList.value = list;
  120. };
  121. TUIGlobal.toggleAtList = toggleAtList;
  122. TUIGlobal.handleAtListPosition = handleAtListPosition;
  123. TUIGlobal.setCurrentSelectIndex = setCurrentSelectIndex;
  124. TUIGlobal.setShowMemberList = setShowMemberList;
  125. defineExpose({
  126. toggleAtList,
  127. });
  128. watch(
  129. () => [position.value, MessageInputAt?.value],
  130. () => {
  131. if (isH5 || !MessageInputAt?.value || !MessageInputAt?.value?.style) {
  132. return;
  133. }
  134. MessageInputAt.value.style.left = position.value.left + 'px';
  135. MessageInputAt.value.style.top
  136. = position.value.top - MessageInputAt.value.clientHeight + 'px';
  137. },
  138. );
  139. const closeAt = () => {
  140. showAtList.value = false;
  141. showMemberList.value = allMemberList.value;
  142. position.value = {
  143. left: 0,
  144. top: 0,
  145. };
  146. };
  147. const selectItem = (index: number) => {
  148. if (isPC && TUIGlobal.selectItem) {
  149. TUIGlobal.selectItem(index);
  150. } else {
  151. if (showMemberList?.value?.length) {
  152. const item = showMemberList?.value[index];
  153. emits('insertAt', {
  154. id: (item as any)?.userID,
  155. label: (item as any)?.nick || (item as any)?.userID,
  156. });
  157. }
  158. }
  159. closeAt();
  160. };
  161. const handleMemberAvatar = (item: any) => {
  162. return (
  163. (item as any)?.avatar
  164. || 'https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png'
  165. );
  166. };
  167. const handleMemberName = (item: any) => {
  168. return (item as any)?.nick ? (item as any)?.nick : (item as any)?.userID;
  169. };
  170. </script>
  171. <style scoped lang="scss">
  172. @import "../../../../assets/styles/common";
  173. .message-input-at {
  174. position: fixed;
  175. max-width: 15rem;
  176. max-height: 10rem;
  177. overflow: hidden auto;
  178. background: #fff;
  179. box-shadow: 0 0.06rem 0.63rem 0 rgba(2,16,43,0.15);
  180. border-radius: 0.13rem;
  181. }
  182. .member-list-box {
  183. &-header {
  184. height: 2.5rem;
  185. padding-top: 5px;
  186. cursor: pointer;
  187. &:hover {
  188. background: rgba(0,110,255,0.1);
  189. }
  190. }
  191. span {
  192. font-family: PingFangSC-Regular;
  193. font-weight: 400;
  194. font-size: 12px;
  195. color: #000;
  196. letter-spacing: 0;
  197. padding: 5px;
  198. }
  199. &-body {
  200. height: 30px;
  201. cursor: pointer;
  202. display: flex;
  203. align-items: center;
  204. .selected,
  205. &:hover {
  206. background: rgba(0,110,255,0.1);
  207. }
  208. &-name {
  209. overflow: hidden;
  210. white-space: nowrap;
  211. word-wrap: break-word;
  212. word-break: break-all;
  213. text-overflow: ellipsis;
  214. }
  215. &-avatar {
  216. width: 20px;
  217. height: 20px;
  218. padding-left: 10px;
  219. }
  220. }
  221. .selected {
  222. background: rgba(0,110,255,0.1);
  223. }
  224. }
  225. .message-input-at-h5 {
  226. .member-list {
  227. height: auto;
  228. max-height: 500px;
  229. width: 100%;
  230. max-width: 100%;
  231. background: white;
  232. border-radius: 12px 12px 0 0;
  233. display: flex;
  234. flex-direction: column;
  235. overflow: hidden;
  236. &-title {
  237. height: fit-content;
  238. width: calc(100% - 30px);
  239. text-align: center;
  240. vertical-align: middle;
  241. padding: 15px;
  242. .title {
  243. vertical-align: middle;
  244. display: inline-block;
  245. font-size: 16px;
  246. }
  247. .close {
  248. vertical-align: middle;
  249. position: absolute;
  250. right: 10px;
  251. display: inline-block;
  252. }
  253. }
  254. &-box {
  255. flex: 1;
  256. overflow-y: scroll;
  257. &-body {
  258. padding: 10px;
  259. img {
  260. width: 26px;
  261. height: 26px;
  262. }
  263. span {
  264. font-size: 14px;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. </style>