Commit 9aed20af by Sixong.Zhu

msg

parent 4ef8859e
......@@ -47,6 +47,7 @@
import MessageInput from "@/customer-service/components/message-input.vue";
import messages from "@/customer-service/components/message-list.vue";
import { ChatStore, chatStore } from "@/customer-service/store/model";
import Chat from "@/customer-service/xim";
type RoomInfoTab = "customer" | "order";
......@@ -125,7 +126,7 @@
}
private onError(msg: string) {
this.$message.error(msg);
Chat.error(msg);
}
private dragControllerDiv(e: MouseEvent) {
......
......@@ -69,6 +69,13 @@ export interface ChatOption {
* 用户信息(头像,别名)可选
*/
user?: { icon?: string; username?: string };
message?: ChatMessageController;
}
export interface ChatMessageController {
error: (msg: string) => void;
info: (msg: string) => void;
}
export interface ChatServiceLogger {
......
......@@ -7,13 +7,14 @@ import {
TokenStringGetter,
ServiceType,
CustomerServiceProduct,
ChatMessageController,
} from "./../model";
import { ChatLoggerService } from "./logger";
import tokenManager from "./token";
import xim from "./xim";
import { dbController } from "../database";
class Chat {
class Chat implements ChatMessageController {
private _sdk?: () => UniplatSdk;
private _orgId: () => string | number = () => "0";
private token!: TokenStringGetter;
......@@ -24,6 +25,7 @@ class Chat {
private ws = "";
private connectedActions: (() => void)[] = [];
private connected = false;
private messageController: ChatMessageController | null = null;
private userMapping: { [key: string]: { name: string; avatar: string } } =
{};
......@@ -59,6 +61,8 @@ class Chat {
};
}
option.message && (this.messageController = option.message);
this.setupIndexDb();
this.token = async () => option.sdk().global.jwtToken;
tokenManager.save(this.token);
......@@ -88,7 +92,7 @@ class Chat {
s.global.uid + "-" + (s.global.initData.orgId || 0)
);
}
return Promise.reject();
return Promise.reject(new Error('Sdk is not defined'));
}
public resetup(org: () => string | number) {
......@@ -140,7 +144,7 @@ class Chat {
if (xim.isConnected()) {
return Promise.resolve(uri);
}
return new Promise((resolve) => {
return new Promise<void>((resolve) => {
xim.open(uri, this.token).finally(() => {
this.registerXimEvent();
if (xim.isConnected()) {
......@@ -172,7 +176,7 @@ class Chat {
}
private debug(message: string) {
ChatLoggerService.logger?.debug(message);
ChatLoggerService.logger && ChatLoggerService.logger.debug(message);
}
public $emit(event: string, ...args: any[]) {
......@@ -190,6 +194,14 @@ class Chat {
public getMatchedTextKeywords() {
return this.keywords;
}
public error(msg: string) {
this.messageController && this.messageController.error(msg);
}
public info(msg: string) {
this.messageController && this.messageController.info(msg);
}
}
export default new Chat();
......@@ -96,6 +96,10 @@ export class Xim {
return token.replace(/^Bearer\s/, "");
}
private buildError() {
return new Error(`Connect is not ready`);
}
/**
* token过期或者切换用户登录时,需要设置新的token
*/
......@@ -108,22 +112,22 @@ export class Xim {
}
public fetchMsgInBox(chatId: number, msgId: number) {
if (this.client == null) return Promise.reject();
if (this.client == null) return Promise.reject(this.buildError());
return this.client.fetchMsgInBox(chatType, chatId, msgId);
}
public fetchChatList() {
if (this.client == null) return Promise.reject();
if (this.client == null) return Promise.reject(this.buildError());
return this.client.fetchChatList({});
}
public fetchChatListAfter(lastMsgTs: number) {
if (this.client == null) return Promise.reject();
if (this.client == null) return Promise.reject(this.buildError());
return this.client.fetchChatList({ last_msg_ts: lastMsgTs });
}
public fetchChat(chat_id: number) {
if (this.client == null) return Promise.reject();
if (this.client == null) return Promise.reject(this.buildError());
return this.client.fetchChat(chat_id);
}
......@@ -137,13 +141,13 @@ export class Xim {
msg: string
) {
this.checkConnected();
if (this.client == null) return Promise.reject();
if (this.client == null) return Promise.reject(this.buildError());
return this.client.sendMsg(chatType, chatId, msgType, msg, "", {});
}
public inputing(chatId: number) {
this.checkConnected();
if (this.client == null) return Promise.reject();
if (this.client == null) return Promise.reject(this.buildError());
return this.client.userInput(chatType, chatId);
}
......@@ -152,7 +156,7 @@ export class Xim {
*/
public fetchChatMembers(chat_id: number) {
this.checkConnected();
if (this.client == null) return Promise.reject();
if (this.client == null) return Promise.reject(this.buildError());
return this.client.fetchChatMembers(chat_id);
}
......@@ -384,7 +388,7 @@ export class Xim {
if (this.client == null) return;
if (!this.client.connected) {
try {
this.client?.open();
this.client && this.client.open();
} catch (e) {
// eslint-disable-next-line no-console
console.error("checkConnected", e);
......@@ -394,7 +398,7 @@ export class Xim {
}
private debug(message: any, ...params: any[]) {
ChatLoggerService.logger?.debug(message, params);
ChatLoggerService.logger && ChatLoggerService.logger.debug(message, params);
}
public registerOnMessage(vue: Vue, action: (e: Message) => void) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment