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/debounce';
  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. // 全局公共方法
  158. TUIGlobal.updateContactSearch = search;
  159. TUIGlobal.closeSearching = () => {
  160. isSearching.value = false;
  161. };
  162. </script>
  163. <style lang="scss" scoped>
  164. .tui-contact-search {
  165. position: sticky;
  166. top: 0;
  167. z-index: 1;
  168. padding: 12px;
  169. display: flex;
  170. justify-content: space-between;
  171. align-items: center;
  172. background: #fff;
  173. border-bottom: 1px solid #f4f5f9;
  174. flex-direction: column;
  175. &-header,
  176. &-main {
  177. width: 100%;
  178. height: 30px;
  179. display: flex;
  180. flex-direction: row;
  181. align-items: center;
  182. }
  183. &-header {
  184. user-select: none;
  185. cursor: pointer;
  186. &-icon {
  187. padding-right: 10px;
  188. }
  189. &-title {
  190. font-size: 14px;
  191. }
  192. }
  193. &-main {
  194. display: flex;
  195. flex-direction: row;
  196. flex: 1;
  197. justify-content: center;
  198. align-items: center;
  199. width: 100%;
  200. &-input {
  201. flex: 1;
  202. font-size: 14px;
  203. border-radius: 5px;
  204. padding: 7px;
  205. border: 1px solid #ddd;
  206. }
  207. &-input:focus {
  208. outline: none;
  209. border: 1px solid #006eff;
  210. }
  211. &-cancel {
  212. padding-left: 10px;
  213. user-select: none;
  214. cursor: pointer;
  215. }
  216. }
  217. &-h5 {
  218. &-header {
  219. width: 100%;
  220. }
  221. }
  222. }
  223. .tui-contact-searching-h5-header {
  224. padding-bottom: 10px;
  225. display: flex;
  226. flex-direction: row;
  227. .tui-contact-search-h5-header-title {
  228. flex: 1;
  229. text-align: center;
  230. font-weight: 500;
  231. font-size: 14px;
  232. margin-right: 30px;
  233. }
  234. }
  235. </style>