index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <div :class="['tui-contact-search', !isPC && 'tui-contact-search-h5']">
  3. <div
  4. v-if="!isSearching || !isPC"
  5. :class="[
  6. 'tui-contact-search-header',
  7. !isPC && 'tui-contact-search-h5-header',
  8. isSearching && 'tui-contact-searching-h5-header',
  9. ]"
  10. @click="changeContactSearchingStatus(true)"
  11. >
  12. <div
  13. :class="[
  14. 'tui-contact-search-header-icon',
  15. !isPC && 'tui-contact-search-h5-header-icon',
  16. ]"
  17. @click.stop="changeContactSearchingStatus(!isSearching)"
  18. >
  19. <Icon
  20. :file="isSearching ? backSVG : addSVG"
  21. :width="isSearching ? '20px' : '14px'"
  22. :height="isSearching ? '20px' : '14px'"
  23. />
  24. </div>
  25. <div
  26. :class="[
  27. 'tui-contact-search-header-title',
  28. !isPC && 'tui-contact-search-h5-header-title',
  29. ]"
  30. >
  31. {{ TUITranslateService.t("TUIContact.添加好友/群聊") }}
  32. </div>
  33. </div>
  34. <div
  35. v-if="isSearching"
  36. :class="[
  37. 'tui-contact-search-main',
  38. !isPC && 'tui-contact-search-h5-main',
  39. ]"
  40. >
  41. <input
  42. v-model="searchValue"
  43. class="tui-contact-search-main-input"
  44. type="text"
  45. :placeholder="searchingPlaceholder"
  46. enterkeyhint="search"
  47. @keyup.enter="search"
  48. @blur="search"
  49. @confirm="search"
  50. >
  51. <div
  52. class="tui-contact-search-main-cancel"
  53. @click="isSearching = false"
  54. >
  55. {{ TUITranslateService.t("取消") }}
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <script setup lang="ts">
  61. import {
  62. TUITranslateService,
  63. TUIStore,
  64. StoreName,
  65. } from '@tencentcloud/chat-uikit-engine';
  66. import TUICore, { TUIConstants } from '@tencentcloud/tui-core';
  67. import { TUIGlobal } from '@tencentcloud/universal-api';
  68. import { debounce } from 'lodash';
  69. import { isPC } from '../../../utils/env';
  70. import Icon from '../../common/Icon.vue';
  71. import addSVG from '../../../assets/icon/add.svg';
  72. import backSVG from '../../../assets/icon/back.svg';
  73. import { ref, watch } from '../../../adapter-vue';
  74. import { IContactSearchResult } from '../../../interface';
  75. const searchingPlaceholder = TUITranslateService.t('TUIContact.输入ID');
  76. const isSearching = ref<boolean>(false);
  77. const searchValue = ref<string>('');
  78. const searchResult = ref<IContactSearchResult>({
  79. user: {
  80. label: '联系人',
  81. list: [],
  82. },
  83. group: {
  84. label: '群聊',
  85. list: [],
  86. },
  87. });
  88. const changeContactSearchingStatus = debounce(function (status: boolean) {
  89. isSearching.value = status;
  90. }, 200);
  91. const search = async () => {
  92. if (!searchValue.value) {
  93. return;
  94. }
  95. TUICore.callService({
  96. serviceName: TUIConstants.TUISearch.SERVICE.NAME,
  97. method: TUIConstants.TUISearch.SERVICE.METHOD.SEARCH_USER,
  98. params: {
  99. userID: searchValue.value,
  100. },
  101. })
  102. .then((res: any) => {
  103. searchResult.value.user.list = res.data;
  104. })
  105. .catch((error: any) => {
  106. searchResult.value.user.list = [];
  107. console.warn('search user error', error);
  108. });
  109. TUICore.callService({
  110. serviceName: TUIConstants.TUISearch.SERVICE.NAME,
  111. method: TUIConstants.TUISearch.SERVICE.METHOD.SEARCH_GROUP,
  112. params: {
  113. groupID: searchValue.value,
  114. },
  115. })
  116. .then((res: any) => {
  117. searchResult.value.group.list = [res.data.group];
  118. })
  119. .catch((error: any) => {
  120. searchResult.value.group.list = [];
  121. console.warn('search group error', error);
  122. });
  123. };
  124. watch(
  125. () => searchResult.value,
  126. () => {
  127. TUIStore.update(
  128. StoreName.CUSTOM,
  129. 'currentContactSearchResult',
  130. searchResult.value,
  131. );
  132. },
  133. {
  134. deep: true,
  135. immediate: true,
  136. },
  137. );
  138. watch(
  139. () => isSearching.value,
  140. () => {
  141. TUIStore.update(
  142. StoreName.CUSTOM,
  143. 'currentContactSearchingStatus',
  144. isSearching.value,
  145. );
  146. if (isSearching.value) {
  147. searchValue.value = '';
  148. searchResult.value.user.list = [];
  149. searchResult.value.group.list = [];
  150. }
  151. },
  152. {
  153. deep: true,
  154. immediate: true,
  155. },
  156. );
  157. TUIGlobal.updateContactSearch = search;
  158. TUIGlobal.closeSearching = () => {
  159. isSearching.value = false;
  160. };
  161. </script>
  162. <style lang="scss" scoped>
  163. .tui-contact-search {
  164. position: sticky;
  165. top: 0;
  166. z-index: 1;
  167. padding: 12px;
  168. display: flex;
  169. justify-content: space-between;
  170. align-items: center;
  171. background: #fff;
  172. border-bottom: 1px solid #f4f5f9;
  173. flex-direction: column;
  174. &-header,
  175. &-main {
  176. width: 100%;
  177. height: 30px;
  178. display: flex;
  179. flex-direction: row;
  180. align-items: center;
  181. }
  182. &-header {
  183. user-select: none;
  184. cursor: pointer;
  185. &-icon {
  186. padding-right: 10px;
  187. }
  188. &-title {
  189. font-size: 14px;
  190. }
  191. }
  192. &-main {
  193. display: flex;
  194. flex-direction: row;
  195. flex: 1;
  196. justify-content: center;
  197. align-items: center;
  198. width: 100%;
  199. &-input {
  200. flex: 1;
  201. font-size: 14px;
  202. border-radius: 5px;
  203. padding: 7px;
  204. border: 1px solid #ddd;
  205. }
  206. &-input:focus {
  207. outline: none;
  208. border: 1px solid #006eff;
  209. }
  210. &-cancel {
  211. padding-left: 10px;
  212. user-select: none;
  213. cursor: pointer;
  214. }
  215. }
  216. &-h5 {
  217. &-header {
  218. width: 100%;
  219. }
  220. }
  221. }
  222. .tui-contact-searching-h5-header {
  223. padding-bottom: 10px;
  224. display: flex;
  225. flex-direction: row;
  226. .tui-contact-search-h5-header-title {
  227. flex: 1;
  228. text-align: center;
  229. font-weight: 500;
  230. font-size: 14px;
  231. margin-right: 30px;
  232. }
  233. }
  234. </style>