chat-model.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <!-- v-if="isConnect" -->
  3. <div class="chat-message">
  4. <!-- 会话列表界面 -->
  5. <div class="chat-header">
  6. <i class="el-icon-close" @click="$listeners.close()"></i>
  7. <div class="header-card">
  8. <el-tooltip
  9. effect="dark"
  10. content="会话"
  11. placement="bottom"
  12. :open-delay="500"
  13. >
  14. <i
  15. class="icon icon-chat"
  16. :class="active === 'conversation' && 'active'"
  17. @click="active = 'conversation'"
  18. ></i>
  19. </el-tooltip>
  20. <el-tooltip
  21. effect="dark"
  22. content="联系人"
  23. placement="bottom"
  24. :open-delay="500"
  25. >
  26. <i
  27. class="icon icon-message"
  28. :class="active === 'contacts' && 'active'"
  29. @click="
  30. () => {
  31. firstLoad = true;
  32. active = 'contacts';
  33. }
  34. "
  35. ></i>
  36. </el-tooltip>
  37. <el-tooltip
  38. effect="dark"
  39. content="班级"
  40. placement="bottom"
  41. :open-delay="500"
  42. >
  43. <i
  44. class="icon icon-class"
  45. :class="active === 'class' && 'active'"
  46. @click="
  47. () => {
  48. firstLoad = true;
  49. active = 'class';
  50. }
  51. "
  52. ></i>
  53. </el-tooltip>
  54. </div>
  55. </div>
  56. <el-container style="margin-top: 56px">
  57. <el-aside
  58. width="280px"
  59. style="border-right: 1px solid #f7f7f7;position: relative;"
  60. >
  61. <conversation-list
  62. ref="conversationList"
  63. v-show="active === 'conversation'"
  64. />
  65. <template v-if="firstLoad">
  66. <contacts-list
  67. ref="contactsList"
  68. v-show="active === 'contacts'"
  69. @select="onSelect"
  70. />
  71. <class-list
  72. ref="classList"
  73. v-show="active === 'class'"
  74. @select="onSelect"
  75. />
  76. </template>
  77. </el-aside>
  78. <!-- 判断是否有会话 -->
  79. <el-container
  80. v-show="currentConversation.targetId && active === 'conversation'"
  81. >
  82. <el-main style="padding: 0" id="el-main">
  83. <message-list ref="messageList"></message-list>
  84. </el-main>
  85. <el-footer
  86. style="height: auto; border-top: 1px solid #f7f7f7; padding: 0 20px;"
  87. >
  88. <message-editor ref="messageEditor"></message-editor>
  89. </el-footer>
  90. </el-container>
  91. <el-empty
  92. v-if="!currentConversation.targetId || active !== 'conversation'"
  93. :image="emptyImg"
  94. style="width: 100%;height: 100%"
  95. description=" "
  96. ></el-empty>
  97. </el-container>
  98. </div>
  99. </template>
  100. <script>
  101. import ConversationList from "../components/conversation-list.vue";
  102. import MessageList from "../components/message-list.vue";
  103. import MessageEditor from "../components/message-editor.vue";
  104. import ContactsList from "../components/contacts-list.vue";
  105. import ClassList from "../components/class-list.vue";
  106. import { core, CoreEvent } from "../imkit";
  107. import * as RongIMLib from "@rongcloud/imlib-next";
  108. import { custom_service } from "./chat.js";
  109. import ItemVue from "../Sidebar/Item.vue";
  110. // 接入时需要将 '请更换您应用的 appkey' 替换为您的应用的 appkey
  111. let libOption = { appkey: "c9kqb3rdc451j" };
  112. // 初始化 SDK
  113. core.init({
  114. service: custom_service,
  115. libOption: libOption,
  116. conversationPullCount: 20
  117. });
  118. export default {
  119. components: {
  120. ConversationList,
  121. MessageList,
  122. MessageEditor,
  123. ContactsList,
  124. ClassList
  125. },
  126. data() {
  127. return {
  128. emptyImg: require("../imkit/images/no-conversation.svg"),
  129. active: "conversation", // 会话 conversation; 联系人 contacts; 班级 class
  130. firstLoad: false, // 是否加载了一次
  131. isConnect: false, // 连接中
  132. currentConversation: {
  133. channelId: "",
  134. conversationType: 0,
  135. targetId: ""
  136. }
  137. };
  138. },
  139. mounted() {
  140. core.connect(this.$store.state.user.imToken).then(res => {
  141. if (res.code === RongIMLib.ErrorCode.SUCCESS) {
  142. console.log("链接成功, 链接用户 id 为: ", res.data.userId);
  143. this.isConnect = true;
  144. } else {
  145. console.warn("链接失败, code:", res.code);
  146. }
  147. });
  148. core.on(CoreEvent.SWITCH_CONVERSATION, this.switchConversation);
  149. // 注册消息监听器
  150. // core.on("tapConversation", function(e) {
  151. // console.log("tapConversation", e);
  152. // });
  153. },
  154. methods: {
  155. onSelect(item) {
  156. console.log(item, "item");
  157. core.selectConversation({
  158. conversationType: item.conversationType,
  159. targetId: item.targetId + "",
  160. channelId: item.channelId
  161. });
  162. this.active = "conversation";
  163. },
  164. switchConversation(conversation) {
  165. console.log(conversation, "switchConversation chat-model");
  166. this.currentConversation = conversation
  167. ? conversation
  168. : {
  169. channelId: "",
  170. conversationType: 0,
  171. targetId: ""
  172. };
  173. }
  174. },
  175. destoryed() {
  176. core.off(CoreEvent.SWITCH_CONVERSATION, this.switchConversation);
  177. }
  178. };
  179. </script>
  180. <style lang="scss" scoped>
  181. .chat-message {
  182. height: 100%;
  183. display: flex;
  184. }
  185. .chat-header {
  186. position: absolute;
  187. left: 0;
  188. right: 0;
  189. background: #f7f7f7;
  190. width: 100%;
  191. height: 56px;
  192. .el-icon-close {
  193. position: absolute;
  194. top: 0;
  195. right: 12px;
  196. line-height: 56px;
  197. font-size: 18px;
  198. font-weight: 600;
  199. text-align: center;
  200. color: #333;
  201. cursor: pointer;
  202. }
  203. .header-card {
  204. cursor: pointer;
  205. line-height: 56px;
  206. text-align: center;
  207. font-size: 30px;
  208. color: #333;
  209. display: flex;
  210. align-items: center;
  211. justify-content: center;
  212. height: 56px;
  213. .icon-chat {
  214. background: url("../images/icon_chat_default.svg") no-repeat;
  215. background-size: contain;
  216. &:hover,
  217. &.active {
  218. background: url("../images/icon_chat.svg") no-repeat;
  219. background-size: contain;
  220. }
  221. }
  222. .icon-message {
  223. background: url("../images/icon_message_default.svg") no-repeat;
  224. background-size: contain;
  225. &:hover,
  226. &.active {
  227. background: url("../images/icon_message.svg") no-repeat;
  228. background-size: contain;
  229. }
  230. }
  231. .icon-class {
  232. background: url("../images/icon_class_default.svg") no-repeat;
  233. background-size: contain;
  234. &:hover,
  235. &.active {
  236. background: url("../images/icon_class.svg") no-repeat;
  237. background-size: contain;
  238. }
  239. }
  240. .icon {
  241. margin: 0 25px;
  242. width: 24px;
  243. height: 24px;
  244. display: inline-block;
  245. }
  246. }
  247. }
  248. .chat-conversation {
  249. float: left;
  250. width: 20vw;
  251. height: 100%;
  252. overflow: hidden;
  253. }
  254. .chat-container {
  255. width: calc(100% - 20vw);
  256. }
  257. .chat-massage-list {
  258. height: calc(100% - 150px);
  259. }
  260. .chat-massage-editor {
  261. height: 150px;
  262. }
  263. /deep/.no-data::before {
  264. background-size: cover;
  265. }
  266. </style>