chat.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <div class="cl-chat__wrap">
  3. <!-- 聊天窗口 -->
  4. <cl-dialog
  5. :visible.sync="visible"
  6. :title="title"
  7. :height="height"
  8. :width="width"
  9. :props="{
  10. modal: true,
  11. customClass: 'cl-chat__dialog',
  12. 'append-to-body': true,
  13. 'close-on-click-modal': false
  14. }"
  15. :controls="['slot-session', 'cl-flex1', 'fullscreen', 'close']"
  16. >
  17. <div class="cl-chat">
  18. <chat-session />
  19. <div class="cl-chat__detail" v-if="session">
  20. <chat-message />
  21. <chat-input />
  22. </div>
  23. </div>
  24. <template #slot-session>
  25. <button v-if="session">
  26. <i
  27. class="el-icon-notebook-2"
  28. v-if="sessionVisible"
  29. @click="CLOSE_SESSION()"
  30. ></i>
  31. <i class="el-icon-arrow-left" v-else @click="OPEN_SESSION()"></i>
  32. </button>
  33. </template>
  34. </cl-dialog>
  35. <!-- MP3 -->
  36. <div class="mp3">
  37. <audio style="display: none" ref="sound" src="../static/notify.mp3" controls></audio>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. import dayjs from "dayjs";
  43. import { mapGetters, mapMutations } from "vuex";
  44. import io from "socket.io-client";
  45. import { socketUrl } from "@/config/env";
  46. import Session from "./session";
  47. import Message from "./message";
  48. import Input from "./input";
  49. import eventBus from "../utils/event-bus";
  50. import { parseContent } from "../utils";
  51. export default {
  52. name: "cl-chat",
  53. components: {
  54. "chat-session": Session,
  55. "chat-message": Message,
  56. "chat-input": Input
  57. },
  58. props: {
  59. height: {
  60. type: String,
  61. default: "650px"
  62. },
  63. width: {
  64. type: String,
  65. default: "1000px"
  66. }
  67. },
  68. data() {
  69. return {
  70. visible: false,
  71. socket: null
  72. };
  73. },
  74. provide() {
  75. return {
  76. chat: this
  77. };
  78. },
  79. computed: {
  80. ...mapGetters(["token", "session", "sessionList", "sessionVisible"]),
  81. title() {
  82. return this.session ? `与 ${this.session.nickname} 聊天中` : "聊天对话框";
  83. }
  84. },
  85. created() {
  86. // this.socket = io(`${socketUrl}/?isAdmin=true&token=${token}`);
  87. // this.socket.on("connect", () => {
  88. // console.log("socket connect");
  89. // });
  90. // this.socket.on("admin", msg => {
  91. // this.onMessage(msg);
  92. // });
  93. // this.socket.on("error", err => {
  94. // console.log(err);
  95. // });
  96. // this.socket.on("disconnect", () => {
  97. // console.log("disconnect connect");
  98. // });
  99. },
  100. destroyed() {
  101. if (this.socket) {
  102. this.socket.close();
  103. }
  104. },
  105. methods: {
  106. ...mapMutations(["OPEN_SESSION", "CLOSE_SESSION", "UPDATE_SESSION"]),
  107. open() {
  108. this.visible = true;
  109. },
  110. close() {
  111. this.visible = false;
  112. },
  113. // 监听消息
  114. onMessage(msg) {
  115. // 回调
  116. this.$emit("message", this.visible);
  117. // 消息通知
  118. this.notification(msg);
  119. try {
  120. const { contentType, fromId, content, msgId } = JSON.parse(msg);
  121. // 是否当前
  122. const same = this.session && this.session.userId == fromId;
  123. if (same) {
  124. // 更新消息
  125. this.UPDATE_SESSION({
  126. contentType,
  127. content
  128. });
  129. // 追加消息
  130. eventBus.$emit("message-append", {
  131. contentType,
  132. content: JSON.parse(content),
  133. type: 1
  134. });
  135. // 阅读消息
  136. this.$service.im.message.read({
  137. ids: [msgId],
  138. session: this.session.id
  139. });
  140. }
  141. // 查找会话
  142. const item = this.sessionList.find(e => e.userId == fromId);
  143. if (item) {
  144. if (!same) {
  145. item.serviceUnreadCount += 1;
  146. }
  147. // 更新消息
  148. Object.assign(item, {
  149. updateTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  150. contentType,
  151. content
  152. });
  153. } else {
  154. // 刷新会话列表
  155. eventBus.$emit("session-refresh");
  156. }
  157. } catch (e) {
  158. console.error("消息格式异常", e);
  159. }
  160. },
  161. // 消息通知
  162. notification(msg) {
  163. const { _text } = parseContent(JSON.parse(msg));
  164. // 播放音乐
  165. if (this.$refs.sound) {
  166. this.$refs.sound.play();
  167. }
  168. if (!this.visible) {
  169. // 页面消息提示
  170. this.$notify({
  171. title: "提示",
  172. message: this.$createElement("span", _text)
  173. });
  174. // 浏览器消息通知
  175. const NotificationInstance = Notification || window.Notification;
  176. if (!!NotificationInstance) {
  177. if (NotificationInstance.permission !== "denied") {
  178. NotificationInstance.requestPermission(status => {
  179. let n = new Notification("COOL-MALL", {
  180. body: _text,
  181. icon: "/favicon.ico"
  182. });
  183. setTimeout(() => {
  184. n.close();
  185. }, 2000);
  186. });
  187. }
  188. }
  189. }
  190. }
  191. }
  192. };
  193. </script>
  194. <style lang="scss">
  195. .cl-chat__dialog {
  196. .el-dialog__body {
  197. padding: 0 !important;
  198. }
  199. }
  200. .cl-chat {
  201. display: flex;
  202. height: 100%;
  203. background-color: #f7f7f7;
  204. padding: 5px;
  205. box-sizing: border-box;
  206. &__detail {
  207. display: flex;
  208. flex-direction: column;
  209. flex: 1;
  210. height: 100%;
  211. }
  212. }
  213. </style>