index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <template>
  2. <div
  3. v-if="typeof contactInfoData === 'object' && Object.keys(contactInfoData).length"
  4. :class="['tui-contact-info', !isPC && 'tui-contact-info-h5']"
  5. >
  6. <div
  7. v-if="!isPC"
  8. :class="[
  9. 'tui-contact-info-header',
  10. !isPC && 'tui-contact-info-h5-header',
  11. ]"
  12. >
  13. <div
  14. :class="[
  15. 'tui-contact-info-header-icon',
  16. !isPC && 'tui-contact-info-h5-header-icon',
  17. ]"
  18. @click="resetContactSearchingUIData"
  19. >
  20. <Icon :file="backSVG" />
  21. </div>
  22. <div
  23. :class="[
  24. 'tui-contact-info-header-title',
  25. !isPC && 'tui-contact-info-h5-header-title',
  26. ]"
  27. >
  28. {{ TUITranslateService.t("TUIContact.添加好友/群聊") }}
  29. </div>
  30. </div>
  31. <div :class="['tui-contact-info-basic', !isPC && 'tui-contact-info-h5-basic']">
  32. <div
  33. :class="[
  34. 'tui-contact-info-basic-text',
  35. !isPC && 'tui-contact-info-h5-basic-text',
  36. ]"
  37. >
  38. <div
  39. :class="[
  40. 'tui-contact-info-basic-text-name',
  41. !isPC && 'tui-contact-info-h5-basic-text-name',
  42. ]"
  43. >
  44. {{ generateContactInfoName(contactInfoData) }}
  45. </div>
  46. <div
  47. v-for="item in contactInfoBasicList"
  48. :key="item.label"
  49. :class="[
  50. 'tui-contact-info-basic-text-other',
  51. !isPC && 'tui-contact-info-h5-basic-text-other',
  52. ]"
  53. >
  54. {{
  55. `${TUITranslateService.t(`TUIContact.${item.label}`)}:
  56. ${item.data}`
  57. }}
  58. </div>
  59. </div>
  60. <img
  61. :class="[
  62. 'tui-contact-info-basic-avatar',
  63. !isPC && 'tui-contact-info-h5-basic-avatar',
  64. ]"
  65. :src="generateAvatar(contactInfoData)"
  66. >
  67. </div>
  68. <div
  69. v-if="contactInfoMoreList[0]"
  70. :class="['tui-contact-info-more', !isPC && 'tui-contact-info-h5-more']"
  71. >
  72. <div
  73. v-for="item in contactInfoMoreList"
  74. :key="item.key"
  75. :class="[
  76. 'tui-contact-info-more-item',
  77. !isPC && 'tui-contact-info-h5-more-item',
  78. item.labelPosition === CONTACT_INFO_LABEL_POSITION.TOP
  79. ? 'tui-contact-info-more-item-top'
  80. : 'tui-contact-info-more-item-left',
  81. ]"
  82. >
  83. <div
  84. :class="[
  85. 'tui-contact-info-more-item-label',
  86. !isPC && 'tui-contact-info-h5-more-item-label',
  87. ]"
  88. >
  89. {{ `${TUITranslateService.t(`TUIContact.${item.label}`)}` }}
  90. </div>
  91. <div
  92. :class="[
  93. 'tui-contact-info-more-item-content',
  94. !isPC && 'tui-contact-info-h5-more-item-content',
  95. ]"
  96. >
  97. <div
  98. v-if="!item.editing"
  99. :class="[
  100. 'tui-contact-info-more-item-content-text',
  101. !isPC && 'tui-contact-info-h5-more-item-content-text',
  102. ]"
  103. >
  104. <div
  105. :class="[
  106. 'tui-contact-info-more-item-content-text-data',
  107. !isPC && 'tui-contact-info-h5-more-item-content-text-data',
  108. ]"
  109. >
  110. {{ item.data }}
  111. </div>
  112. <div
  113. v-if="item.editable"
  114. :class="[
  115. 'tui-contact-info-more-item-content-text-icon',
  116. !isPC && 'tui-contact-info-h5-more-item-content-text-icon',
  117. ]"
  118. @click="setEditing(item)"
  119. >
  120. <Icon
  121. :file="editSVG"
  122. width="14px"
  123. height="14px"
  124. />
  125. </div>
  126. </div>
  127. <input
  128. v-else-if="item.editType === CONTACT_INFO_MORE_EDIT_TYPE.INPUT"
  129. v-model="item.data"
  130. :class="[
  131. 'tui-contact-info-more-item-content-input',
  132. !isPC && 'tui-contact-info-h5-more-item-content-input',
  133. ]"
  134. type="text"
  135. @confirm="onContactInfoEmitSubmit(item)"
  136. @keyup.enter="onContactInfoEmitSubmit(item)"
  137. >
  138. <textarea
  139. v-else-if="item.editType === CONTACT_INFO_MORE_EDIT_TYPE.TEXTAREA"
  140. v-model="item.data"
  141. :class="[
  142. 'tui-contact-info-more-item-content-textarea',
  143. !isPC && 'tui-contact-info-h5-more-item-content-textarea',
  144. ]"
  145. confirm-type="done"
  146. />
  147. <div
  148. v-else-if="item.editType === CONTACT_INFO_MORE_EDIT_TYPE.SWITCH"
  149. @click="onContactInfoEmitSubmit(item)"
  150. >
  151. <SwitchBar :value="item.data" />
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. <div
  157. :class="[
  158. 'tui-contact-info-button',
  159. !isPC && 'tui-contact-info-h5-button',
  160. ]"
  161. >
  162. <button
  163. v-for="item in contactInfoButtonList"
  164. :key="item.key"
  165. :class="[
  166. 'tui-contact-info-button-item',
  167. !isPC && 'tui-contact-info-h5-button-item',
  168. item.type === CONTACT_INFO_BUTTON_TYPE.CANCEL
  169. ? `tui-contact-info-button-item-cancel`
  170. : `tui-contact-info-button-item-submit`,
  171. ]"
  172. @click="onContactInfoButtonClicked(item)"
  173. >
  174. {{ TUITranslateService.t(`TUIContact.${item.label}`) }}
  175. </button>
  176. </div>
  177. </div>
  178. </template>
  179. <script setup lang="ts">
  180. import TUIChatEngine, {
  181. TUIStore,
  182. StoreName,
  183. TUITranslateService,
  184. IGroupModel,
  185. Friend,
  186. FriendApplication,
  187. } from '@tencentcloud/chat-uikit-engine';
  188. import { TUIGlobal } from '@tencentcloud/universal-api';
  189. import { ref, computed, onMounted, onUnmounted } from '../../../adapter-vue';
  190. import { isPC } from '../../../utils/env';
  191. import {
  192. generateAvatar,
  193. generateContactInfoName,
  194. generateContactInfoBasic,
  195. isFriend,
  196. isApplicationType,
  197. } from '../utils/index';
  198. import {
  199. contactMoreInfoConfig,
  200. contactButtonConfig,
  201. } from './contact-info-config';
  202. import Icon from '../../common/Icon.vue';
  203. import editSVG from '../../../assets/icon/edit.svg';
  204. import backSVG from '../../../assets/icon/back.svg';
  205. import SwitchBar from '../../common/SwitchBar/index.vue';
  206. import {
  207. IBlackListUserItem,
  208. IContactInfoMoreItem,
  209. IContactInfoButton,
  210. } from '../../../interface';
  211. import {
  212. CONTACT_INFO_LABEL_POSITION,
  213. CONTACT_INFO_MORE_EDIT_TYPE,
  214. CONTACT_INFO_BUTTON_TYPE,
  215. } from '../../../constant';
  216. import { deepCopy } from '../../TUIChat/utils/utils';
  217. type IContactInfoType = IGroupModel | Friend | FriendApplication | IBlackListUserItem;
  218. const emits = defineEmits(['switchConversation']);
  219. const contactInfoData = ref<IContactInfoType>({} as IContactInfoType);
  220. const contactInfoBasicList = ref<Array<{ label: string; data: string }>>([]);
  221. const contactInfoMoreList = ref<IContactInfoMoreItem[]>([]);
  222. const contactInfoButtonList = ref<IContactInfoButton[]>([]);
  223. const setEditing = (item: any) => {
  224. item.editing = true;
  225. };
  226. const isGroup = computed((): boolean =>
  227. (contactInfoData.value as IGroupModel)?.groupID ? true : false,
  228. );
  229. const isApplication = computed((): boolean => {
  230. return isApplicationType(contactInfoData?.value);
  231. });
  232. // is both friend, if is group type always false
  233. const isBothFriend = ref<boolean>(false);
  234. // is group member, including ordinary member, admin, group owner
  235. const isGroupMember = computed((): boolean => {
  236. return (contactInfoData.value as IGroupModel)?.selfInfo?.userID ? true : false;
  237. });
  238. // is in black list, if is group type always false
  239. const isInBlackList = computed((): boolean => {
  240. return (
  241. !isGroup.value
  242. && blackList.value?.findIndex(
  243. (item: IBlackListUserItem) =>
  244. item?.userID === (contactInfoData.value as IBlackListUserItem)?.userID,
  245. ) >= 0
  246. );
  247. });
  248. const blackList = ref<IBlackListUserItem[]>([]);
  249. onMounted(() => {
  250. TUIStore.watch(StoreName.CUSTOM, {
  251. currentContactInfo: onCurrentContactInfoUpdated,
  252. });
  253. TUIStore.watch(StoreName.USER, {
  254. userBlacklist: onUserBlacklistUpdated,
  255. });
  256. });
  257. onUnmounted(() => {
  258. TUIStore.unwatch(StoreName.CUSTOM, {
  259. currentContactInfo: onCurrentContactInfoUpdated,
  260. });
  261. TUIStore.unwatch(StoreName.USER, {
  262. userBlacklist: onUserBlacklistUpdated,
  263. });
  264. });
  265. const resetContactInfoUIData = () => {
  266. contactInfoData.value = {} as IContactInfoType;
  267. contactInfoBasicList.value = [];
  268. contactInfoMoreList.value = [];
  269. contactInfoButtonList.value = [];
  270. };
  271. const resetContactSearchingUIData = () => {
  272. TUIStore.update(StoreName.CUSTOM, 'currentContactInfo', {});
  273. TUIStore.update(StoreName.CUSTOM, 'currentContactSearchingStatus', false);
  274. TUIGlobal?.closeSearching && TUIGlobal?.closeSearching();
  275. };
  276. const onContactInfoEmitSubmit = (item: any) => {
  277. item.editSubmitHandler
  278. && item.editSubmitHandler({
  279. item,
  280. contactInfoData: contactInfoData.value,
  281. isBothFriend: isBothFriend.value,
  282. isInBlackList: isInBlackList.value,
  283. });
  284. };
  285. const onContactInfoButtonClicked = (item: any) => {
  286. item.onClick
  287. && item.onClick({
  288. contactInfoData: contactInfoData.value,
  289. contactInfoMoreList: contactInfoMoreList.value,
  290. });
  291. if (
  292. item.key === 'enterGroupConversation'
  293. || item.key === 'enterC2CConversation'
  294. ) {
  295. emits('switchConversation', contactInfoData.value);
  296. resetContactSearchingUIData();
  297. }
  298. };
  299. const generateMoreInfo = async () => {
  300. if (!isApplication.value) {
  301. if (
  302. (!isGroup.value && !isBothFriend.value && !isInBlackList.value)
  303. || (isGroup.value
  304. && !isGroupMember.value
  305. && (contactInfoData.value as IGroupModel)?.type !== TUIChatEngine?.TYPES?.GRP_AVCHATROOM)
  306. ) {
  307. contactMoreInfoConfig.setWords.data = '';
  308. contactInfoMoreList.value.push(contactMoreInfoConfig.setWords);
  309. }
  310. if (!isGroup.value && !isInBlackList.value) {
  311. contactMoreInfoConfig.setRemark.data
  312. = (contactInfoData.value as Friend)?.remark || '';
  313. contactMoreInfoConfig.setRemark.editing = false;
  314. contactInfoMoreList.value.push(contactMoreInfoConfig.setRemark);
  315. }
  316. if (!isGroup.value && (isBothFriend.value || isInBlackList.value)) {
  317. contactMoreInfoConfig.blackList.data = isInBlackList.value || false;
  318. contactInfoMoreList.value.push(contactMoreInfoConfig.blackList);
  319. }
  320. } else {
  321. contactMoreInfoConfig.displayWords.data
  322. = (contactInfoData.value as FriendApplication)?.wording || '';
  323. contactInfoMoreList.value.push(contactMoreInfoConfig.displayWords);
  324. }
  325. };
  326. const generateButton = () => {
  327. if (isInBlackList.value) {
  328. return;
  329. }
  330. if (isApplication.value) {
  331. if (
  332. (contactInfoData.value as FriendApplication)?.type
  333. === TUIChatEngine?.TYPES?.SNS_APPLICATION_SENT_TO_ME
  334. ) {
  335. contactInfoButtonList?.value?.push(
  336. contactButtonConfig.refuseFriendApplication,
  337. );
  338. contactInfoButtonList?.value?.push(
  339. contactButtonConfig.acceptFriendApplication,
  340. );
  341. }
  342. } else {
  343. if (isGroup.value && isGroupMember.value) {
  344. switch ((contactInfoData.value as IGroupModel)?.selfInfo?.role) {
  345. case 'Owner':
  346. contactInfoButtonList?.value?.push(contactButtonConfig.dismissGroup);
  347. break;
  348. default:
  349. contactInfoButtonList?.value?.push(contactButtonConfig.quitGroup);
  350. break;
  351. }
  352. contactInfoButtonList?.value?.push(
  353. contactButtonConfig.enterGroupConversation,
  354. );
  355. } else if (!isGroup.value && isBothFriend.value) {
  356. contactInfoButtonList?.value?.push(contactButtonConfig.deleteFriend);
  357. contactInfoButtonList?.value?.push(
  358. contactButtonConfig.enterC2CConversation,
  359. );
  360. } else {
  361. if (isGroup.value) {
  362. contactInfoButtonList?.value?.push(
  363. (contactInfoData.value as IGroupModel)?.type === TUIChatEngine?.TYPES?.GRP_AVCHATROOM
  364. ? contactButtonConfig.joinAVChatGroup
  365. : contactButtonConfig.joinGroup,
  366. );
  367. } else {
  368. contactInfoButtonList?.value?.push(contactButtonConfig.addFriend);
  369. }
  370. }
  371. }
  372. };
  373. function onUserBlacklistUpdated(userBlacklist: IBlackListUserItem[]) {
  374. blackList.value = userBlacklist;
  375. }
  376. async function onCurrentContactInfoUpdated(contactInfo: IContactInfoType) {
  377. if (
  378. contactInfoData.value
  379. && contactInfo
  380. && JSON.stringify(contactInfoData.value) === JSON.stringify(contactInfo)
  381. ) {
  382. return;
  383. }
  384. resetContactInfoUIData();
  385. // deep clone
  386. contactInfoData.value = deepCopy(contactInfo) || {};
  387. if (!contactInfoData.value || Object.keys(contactInfoData.value)?.length === 0) {
  388. return;
  389. }
  390. contactInfoBasicList.value = generateContactInfoBasic(
  391. contactInfoData.value,
  392. );
  393. isBothFriend.value = await isFriend(contactInfoData.value);
  394. generateMoreInfo();
  395. generateButton();
  396. if (contactInfo.infoKeyList) {
  397. contactInfoMoreList.value = contactInfo.infoKeyList.map((key: string) => {
  398. return (contactMoreInfoConfig as any)[key];
  399. });
  400. }
  401. if (contactInfo.btnKeyList) {
  402. contactInfoButtonList.value = contactInfo.btnKeyList.map((key: string) => {
  403. return (contactButtonConfig as any)[key];
  404. });
  405. }
  406. }
  407. </script>
  408. <style lang="scss" scoped src="./style/index.scss"></style>