index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <Dialog
  3. :visible="showDialog"
  4. :styleConfig="styleConfig"
  5. :handleClose="handleClose"
  6. >
  7. <view
  8. v-for="(item, index) in chatList"
  9. :key="index"
  10. class="chat-container"
  11. @click.stop="handleContentClick(item)"
  12. >
  13. <image
  14. class="item-icon"
  15. v-if="item.imgType === 'SINGLE'"
  16. src="../../assets/icon/singlePerson.svg"
  17. />
  18. <image class="item-icon" v-else src="../../assets/icon/multiPerson.svg" />
  19. <view>{{ item.content }}</view>
  20. </view>
  21. </Dialog>
  22. <view class="TUI-conversation">
  23. <view class="create-group" @click="handleShow"> + 发起聊天</view>
  24. <TUIConversationList
  25. :currentID="currrentConversationID"
  26. :conversationList="conversationList"
  27. @handleGotoItem="handleCurrrentConversation"
  28. />
  29. </view>
  30. </template>
  31. <script lang="ts">
  32. import { defineComponent, reactive, toRefs, computed, onMounted } from "vue";
  33. import { onNavigationBarButtonTap, onShow, onUnload } from "@dcloudio/uni-app";
  34. import TUIConversationList from "./conversation-list";
  35. import Dialog from "./components/dialog.vue";
  36. import store from "../../TUICore/store";
  37. import { TUIConversationServer, TUIProfileServer } from "../../TUICore/server";
  38. const TUIConversation = defineComponent({
  39. name: "TUIConversation",
  40. components: {
  41. TUIConversationList,
  42. Dialog,
  43. },
  44. setup(props) {
  45. const timStore = store.state.timStore;
  46. uni.$TUIKit.TUIConversationServer = new TUIConversationServer();
  47. uni.$TUIKit.TUIProfileServer = new TUIProfileServer();
  48. // const { t } = uni.$TUIKit.config.i18n.useI18n();
  49. const data: any = reactive({
  50. conversationList: computed(() => timStore.conversationList),
  51. currrentConversationID: "",
  52. open: false,
  53. searchUserID: "",
  54. selectedList: [],
  55. searchUserList: [],
  56. step: 1,
  57. showDialog: false,
  58. item: {
  59. flow: "out",
  60. status: "success",
  61. },
  62. styleConfig: {
  63. width: "150px",
  64. // height: '160px',
  65. padding: "16px 16px 0px",
  66. top: "8px",
  67. right: "8px",
  68. },
  69. chatList: [
  70. {
  71. imgType: "SINGLE",
  72. type: uni.$TIM.TYPES.CONV_C2C,
  73. id: 1,
  74. content: "发起会话",
  75. },
  76. {
  77. imgType: "GROUP",
  78. type: uni.$TIM.TYPES.GRP_WORK,
  79. id: 1,
  80. content: "工作群",
  81. },
  82. {
  83. imgType: "GROUP",
  84. type: uni.$TIM.TYPES.GRP_PUBLIC,
  85. id: 2,
  86. content: "社交群",
  87. },
  88. {
  89. imgType: "GROUP",
  90. type: uni.$TIM.TYPES.GRP_MEETING,
  91. id: 3,
  92. content: "会议群",
  93. },
  94. ],
  95. });
  96. onUnload(() => {
  97. uni.$TUIKit.TUIConversationServer.destroyed();
  98. });
  99. onNavigationBarButtonTap(() => {
  100. data.showDialog = !data.showDialog;
  101. });
  102. onShow(() => {
  103. store.commit("timStore/setConversationID", "");
  104. });
  105. // 切换当前会话
  106. const handleCurrrentConversation = (value: any) => {
  107. data.currrentConversationID = value.conversationID;
  108. store.commit("timStore/setConversationID", value.conversationID);
  109. //uni.$TUIKit.TUIChatServer.updateStore(value.conversationID)
  110. uni.navigateTo({
  111. url: `../TUIChat/index?conversationName=${handleItemName(value)}`,
  112. });
  113. uni.$TUIKit.TUIConversationServer.setMessageRead(value.conversationID);
  114. const curConversation = data.conversationList.filter((item: any) => {
  115. return item.conversationID === value.conversationID;
  116. });
  117. store.commit("timStore/setConversation", curConversation);
  118. uni.$TUIKit.TUIConversationServer.getConversationProfile(
  119. value.conversationID
  120. ).then((res: any) => {
  121. // 通知 TUIChat 关闭当前会话
  122. store.commit("timStore/setConversation", res.data.conversation);
  123. // uni.$TUIKit.getStore()['TUIChat'].conversation = res.data.conversation;
  124. });
  125. };
  126. const handleShow = () => {
  127. data.showDialog = true;
  128. };
  129. // dialog 内部有效区域点击
  130. const handleContentClick = (item) => {
  131. data.showDialog = false;
  132. uni.navigateTo({
  133. url: `../TUIConversation/create?title=${item.content}&type=${item.type}`,
  134. });
  135. };
  136. // 关闭 dialog
  137. const handleClose = () => {
  138. data.showDialog = false;
  139. };
  140. const handleItemName = (item: any) => {
  141. let name = "";
  142. switch (item.type) {
  143. case uni.$TIM.TYPES.CONV_C2C:
  144. name = item?.userProfile.nick || item?.userProfile?.userID || "";
  145. break;
  146. case uni.$TIM.TYPES.CONV_GROUP:
  147. name = item.groupProfile.name || item?.groupProfile?.groupID || "";
  148. break;
  149. case uni.$TIM.TYPES.CONV_SYSTEM:
  150. name = "系统通知";
  151. break;
  152. }
  153. return name;
  154. };
  155. return {
  156. ...toRefs(data),
  157. handleCurrrentConversation,
  158. handleContentClick,
  159. handleItemName,
  160. handleClose,
  161. handleShow,
  162. };
  163. },
  164. });
  165. export default TUIConversation;
  166. </script>
  167. <style lang="scss" scoped>
  168. .TUI-conversation {
  169. .create-group {
  170. font-weight: 800px;
  171. padding: 10px;
  172. font-size: 14px;
  173. text-align: center;
  174. }
  175. }
  176. .chat-container {
  177. display: flex;
  178. align-items: center;
  179. margin-bottom: 16px;
  180. color: #444444;
  181. .item-icon {
  182. display: inline-block;
  183. width: 21px;
  184. height: 21px;
  185. margin-right: 12px;
  186. }
  187. }
  188. </style>