Commit 9aed20af by Sixong.Zhu

msg

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