Commit c28d3aa0 by Sixong.Zhu

eslint

parent 5d95f6cd
import { Component, Vue } from "vue-property-decorator"; import { Component, Vue } from "vue-property-decorator";
import { chatStore, ChatStore } from "@/customer-service/store/model";
import { parserMessage } from "."; import { parserMessage } from ".";
import { Chat as ChatItem } from "@/customer-service/xim/models/chat";
import { chatStore, ChatStore } from "@/customer-service/store/model";
import { formatTime, TimeFormatRule } from "@/customer-service/utils/time"; import { formatTime, TimeFormatRule } from "@/customer-service/utils/time";
import { Chat as ChatItem } from "@/customer-service/xim/models/chat";
@Component({ components: {} }) @Component({ components: {} })
export default class ChatList extends Vue { export default class ChatList extends Vue {
...@@ -40,7 +42,7 @@ export default class ChatList extends Vue { ...@@ -40,7 +42,7 @@ export default class ChatList extends Vue {
protected readonly hideChat: ChatStore.MUTATION_HIDE_CHAT; protected readonly hideChat: ChatStore.MUTATION_HIDE_CHAT;
protected parseMesage(data: ChatItem) { protected parseMesage(data: ChatItem) {
if (data.last_msg_sender && data.last_msg_sender != "0") { if (data.last_msg_sender && data.last_msg_sender !== "0") {
if (this.userNames[data.last_msg_sender] === undefined) { if (this.userNames[data.last_msg_sender] === undefined) {
this.updateUserName({ id: data.last_msg_sender, name: "" }); this.updateUserName({ id: data.last_msg_sender, name: "" });
this.sdk this.sdk
......
...@@ -3,7 +3,7 @@ import { Chat, Message } from "./../xim/models/chat"; ...@@ -3,7 +3,7 @@ import { Chat, Message } from "./../xim/models/chat";
class ChatCacheDatabaseController { class ChatCacheDatabaseController {
private db: IDBDatabase; private db: IDBDatabase;
private readonly version = new Date().valueOf(); private readonly version = 1;
private readonly chatListKey = "chat-list"; private readonly chatListKey = "chat-list";
private readonly chatMessageKey = "chat-message"; private readonly chatMessageKey = "chat-message";
...@@ -11,6 +11,7 @@ class ChatCacheDatabaseController { ...@@ -11,6 +11,7 @@ class ChatCacheDatabaseController {
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
if (uid && indexedDB) { if (uid && indexedDB) {
const r = indexedDB.open(uid, this.version); const r = indexedDB.open(uid, this.version);
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this; const that = this;
const setupDb = () => { const setupDb = () => {
if (that.db) { if (that.db) {
...@@ -112,6 +113,29 @@ class ChatCacheDatabaseController { ...@@ -112,6 +113,29 @@ class ChatCacheDatabaseController {
r.onerror = () => resolve(); r.onerror = () => resolve();
}); });
} }
public mergeChatList(source1: Chat[], source2: Chat[]) {
for (const item of source2) {
const t = source1.find((i) => i.id === item.id);
if (t) {
const index = source1.indexOf(t);
source1[index] = item;
} else {
source1.push(item);
}
}
const source = source1.sort((x, y) => (x.last_msg_ts < y.last_msg_ts ? 1 : -1));
if (this.db) {
const store = this.buildStore(this.chatListKey);
store.clear();
for (const item of source) {
store.add(item, item.id);
}
}
return source;
}
} }
export const dbController = new ChatCacheDatabaseController(); export const dbController = new ChatCacheDatabaseController();
import { RootStoreState } from "@/store/model";
import { Module } from "vuex";
import Vue from "vue"; import Vue from "vue";
import { Module } from "vuex";
import { dbController } from "../database";
import { ChatMember } from "../model"; import { ChatMember } from "../model";
import { isAccessibleUrl } from "../service/tools"; import { isAccessibleUrl } from "../service/tools";
import { unique } from "../utils"; import { unique } from "../utils";
...@@ -13,7 +14,8 @@ import { Chat as ChatType, Message } from "../xim/models/chat"; ...@@ -13,7 +14,8 @@ import { Chat as ChatType, Message } from "../xim/models/chat";
import xim, { ChatNotifyListener } from "../xim/xim"; import xim, { ChatNotifyListener } from "../xim/xim";
import { ChatStatus, ChatStore, ChatStoreState } from "./model"; import { ChatStatus, ChatStore, ChatStoreState } from "./model";
import { dbController } from "../database";
import { RootStoreState } from "@/store/model";
export const ns = ChatStore.ns; export const ns = ChatStore.ns;
...@@ -70,6 +72,22 @@ async function preCacheImgs(msgs: any[]) { ...@@ -70,6 +72,22 @@ async function preCacheImgs(msgs: any[]) {
}) })
); );
} }
function buildChatItem(chat: any) {
let business_data;
if (chat.business_data) {
business_data = JSON.parse(chat.business_data);
}
if (business_data?.model_name == null) {
business_data = null;
}
return {
...chat,
chat_id: chat.id,
business_data,
} as ChatType;
}
export default { export default {
namespaced: true, namespaced: true,
state: () => ({ state: () => ({
...@@ -332,36 +350,34 @@ export default { ...@@ -332,36 +350,34 @@ export default {
}, },
actions: { actions: {
async [ChatStore.ACTION_GET_MY_CHAT_LIST]({ async [ChatStore.ACTION_GET_MY_CHAT_LIST]({
state,
commit, commit,
}) /* ...params: Parameters<ChatStore.ACTION_GET_MY_CHAT_LIST> */ { }) /* ...params: Parameters<ChatStore.ACTION_GET_MY_CHAT_LIST> */ {
const cache = await dbController.getChatList(); let cache = await dbController.getChatList();
if (cache && cache.length) { if (cache && cache.length) {
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, { commit(ChatStore.MUTATION_SAVE_CHAT_LIST, {
list: cache, list: cache,
total: 9999, total: 9999,
}); });
const ts = cache.map((i) => i.last_msg_ts).sort();
const last = ts[ts.length - 1];
await xim.fetchChatListAfter(last).then((r) => {
const list = r.args[0] as any[];
const items = list.map((i) => buildChatItem(i));
if (items && items.length) {
cache = dbController.mergeChatList(cache, items);
}
});
return cache; return cache;
} }
const data = await xim.fetchChatList(); const data = await xim.fetchChatList();
if (data == null) return; if (data == null) return;
const chatList = data.args[0]; const chatList = data.args[0];
const items = chatList.map((chat: any) => { const items = chatList.map((chat: any) => buildChatItem(chat));
let business_data;
if (chat.business_data) {
business_data = JSON.parse(chat.business_data);
}
if (business_data?.model_name == null) {
business_data = null;
}
return {
...chat,
chat_id: chat.id,
business_data,
} as ChatType;
});
dbController.saveChatList(items); dbController.saveChatList(items);
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, { commit(ChatStore.MUTATION_SAVE_CHAT_LIST, {
list: items, list: items,
...@@ -512,13 +528,13 @@ export default { ...@@ -512,13 +528,13 @@ export default {
); );
}, },
async [ChatStore.ACTION_REGISTER_EVENT]({ dispatch, commit, state }) { async [ChatStore.ACTION_REGISTER_EVENT]({ dispatch, commit, state }) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
const onNewMsg = (e: Message) => { const onNewMsg = (e: Message) => {
if (e.chat_id === chatId) { if (e.chat_id === chatId) {
dispatch(ChatStore.ACTION_GET_FRESH_MESSAGE); dispatch(ChatStore.ACTION_GET_FRESH_MESSAGE);
} }
state[ChatStore.STATE_FUNC_ON_NEW_MSG](e); state[ChatStore.STATE_FUNC_ON_NEW_MSG](e);
}; };
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) { if (chatId == null) {
xim.off("msg", onNewMsg); xim.off("msg", onNewMsg);
xim.on("msg", onNewMsg); xim.on("msg", onNewMsg);
...@@ -588,7 +604,7 @@ export default { ...@@ -588,7 +604,7 @@ export default {
if (!data) { if (!data) {
return; return;
} }
let chat = data.args[0]; const chat = data.args[0];
let business_data; let business_data;
if (chat.business_data) { if (chat.business_data) {
business_data = JSON.parse(chat.business_data); business_data = JSON.parse(chat.business_data);
...@@ -709,7 +725,7 @@ export default { ...@@ -709,7 +725,7 @@ export default {
await dispatch(ChatStore.ACTION_GET_MY_CHAT_LIST); await dispatch(ChatStore.ACTION_GET_MY_CHAT_LIST);
const firstChat = state[ChatStore.STATE_MY_CHAT_ROOM_LIST]?.list[0]; const firstChat = state[ChatStore.STATE_MY_CHAT_ROOM_LIST]?.list[0];
if (firstChat == null) return; if (firstChat == null) return;
const chatInfo = await getChatModelInfo( await getChatModelInfo(
firstChat.business_data.model_name, firstChat.business_data.model_name,
firstChat.business_data.obj_id firstChat.business_data.obj_id
); );
...@@ -718,7 +734,6 @@ export default { ...@@ -718,7 +734,6 @@ export default {
firstChat.chat_id firstChat.chat_id
); );
}, },
async [ChatStore.ACTION_CHAT_START_RECEPTION]({ getters, dispatch }) { async [ChatStore.ACTION_CHAT_START_RECEPTION]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT]; const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
if (currentChat == null) return; if (currentChat == null) return;
...@@ -739,7 +754,6 @@ export default { ...@@ -739,7 +754,6 @@ export default {
await new Promise((resolve) => setTimeout(resolve, 500)); await new Promise((resolve) => setTimeout(resolve, 500));
await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS); await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS);
}, },
async [ChatStore.ACTION_CHAT_USER_EXIT]({ getters, dispatch }) { async [ChatStore.ACTION_CHAT_USER_EXIT]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT]; const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
if (currentChat == null) return; if (currentChat == null) return;
...@@ -750,7 +764,6 @@ export default { ...@@ -750,7 +764,6 @@ export default {
await new Promise((resolve) => setTimeout(resolve, 500)); await new Promise((resolve) => setTimeout(resolve, 500));
await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS); await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS);
}, },
async [ChatStore.ACTION_CHAT_CS_EXIT]({ getters, dispatch }) { async [ChatStore.ACTION_CHAT_CS_EXIT]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT]; const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
if (currentChat == null) return; if (currentChat == null) return;
...@@ -761,7 +774,6 @@ export default { ...@@ -761,7 +774,6 @@ export default {
await new Promise((resolve) => setTimeout(resolve, 500)); await new Promise((resolve) => setTimeout(resolve, 500));
await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS); await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS);
}, },
async [ChatStore.ACTION_CHAT_ADD_MEMBERS]( async [ChatStore.ACTION_CHAT_ADD_MEMBERS](
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_MEMBERS>[0] uids: Parameters<ChatStore.ACTION_CHAT_ADD_MEMBERS>[0]
...@@ -794,7 +806,6 @@ export default { ...@@ -794,7 +806,6 @@ export default {
await new Promise((resolve) => setTimeout(resolve, 500)); await new Promise((resolve) => setTimeout(resolve, 500));
await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS); await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS);
}, },
async [ChatStore.ACTION_CHAT_ADD_CS]( async [ChatStore.ACTION_CHAT_ADD_CS](
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_CS>[0] uids: Parameters<ChatStore.ACTION_CHAT_ADD_CS>[0]
...@@ -852,7 +863,7 @@ export default { ...@@ -852,7 +863,7 @@ export default {
[ChatStore.GETTER_CURRENT_CURRENT_CHAT](state) { [ChatStore.GETTER_CURRENT_CURRENT_CHAT](state) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID]; const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
const singleChat = state[ChatStore.STATE_SINGLE_CHAT]; const singleChat = state[ChatStore.STATE_SINGLE_CHAT];
if (singleChat && singleChat.chat_id == chatId) { if (singleChat && singleChat.chat_id === chatId) {
return singleChat; return singleChat;
} }
const chatList = const chatList =
......
...@@ -6,6 +6,7 @@ import chatType from "../xim/chat-type"; ...@@ -6,6 +6,7 @@ import chatType from "../xim/chat-type";
import { TokenStringGetter } from "./../model"; import { TokenStringGetter } from "./../model";
import { ChatLoggerService } from "./logger"; import { ChatLoggerService } from "./logger";
import { Message, NotifyMessage } from "./models/chat"; import { Message, NotifyMessage } from "./models/chat";
import chat from "./index"; import chat from "./index";
wampDebug(true); wampDebug(true);
...@@ -16,11 +17,11 @@ function emptyFunc() { ...@@ -16,11 +17,11 @@ function emptyFunc() {
return null; return null;
} }
export type MsgListener = (msg: Message) => void export type MsgListener = (msg: Message) => void;
export type ChatNotifyListener = (msg: NotifyMessage) => void export type ChatNotifyListener = (msg: NotifyMessage) => void;
export type StatusChangeListener = (status: any, details: any) => void export type StatusChangeListener = (status: any, details: any) => void;
export enum Events { export enum Events {
Msg = "msg", Msg = "msg",
...@@ -34,14 +35,14 @@ export enum Kind { ...@@ -34,14 +35,14 @@ export enum Kind {
} }
export class Xim { export class Xim {
private eventBus = new Vue() private eventBus = new Vue();
private client?: XChatClient private client?: XChatClient;
private paramsForReconnection?: { private paramsForReconnection?: {
url: string; url: string;
token: TokenStringGetter; token: TokenStringGetter;
} };
public close() { public close() {
if (this.client) { if (this.client) {
...@@ -56,7 +57,7 @@ export class Xim { ...@@ -56,7 +57,7 @@ export class Xim {
} }
} }
private connectionPending = false private connectionPending = false;
public async open(url: string, token: TokenStringGetter) { public async open(url: string, token: TokenStringGetter) {
this.connectionPending = true; this.connectionPending = true;
...@@ -112,6 +113,11 @@ export class Xim { ...@@ -112,6 +113,11 @@ export class Xim {
return this.client.fetchChatList({}); return this.client.fetchChatList({});
} }
public fetchChatListAfter(lastMsgTs: number) {
if (this.client == null) return;
return this.client.fetchChatList({ last_msg_ts: lastMsgTs });
}
public fetchChat(chat_id: number) { public fetchChat(chat_id: number) {
if (this.client == null) return; if (this.client == null) return;
return this.client.fetchChat(chat_id); return this.client.fetchChat(chat_id);
...@@ -160,7 +166,7 @@ export class Xim { ...@@ -160,7 +166,7 @@ export class Xim {
this.checkConnected(); this.checkConnected();
if (this.client == null) { if (this.client == null) {
throw new Error("client shouldn't undefined"); throw new Error("client shouldn't undefined");
}; }
const res = await this.client.fetchChatMsgs(chatType, chatId, { const res = await this.client.fetchChatMsgs(chatType, chatId, {
lid, lid,
rid, rid,
...@@ -233,43 +239,43 @@ export class Xim { ...@@ -233,43 +239,43 @@ export class Xim {
return data; return data;
} }
public on(event: "msg", chatId: number, listener: MsgListener): this public on(event: "msg", chatId: number, listener: MsgListener): this;
public on(event: "msg", listener: MsgListener): this public on(event: "msg", listener: MsgListener): this;
public on( public on(
event: "chat_notify", event: "chat_notify",
kind: "chat_change", kind: "chat_change",
listener: ChatNotifyListener listener: ChatNotifyListener
): this ): this;
public on( public on(
event: "chat_notify", event: "chat_notify",
kind: string, kind: string,
listener: ChatNotifyListener listener: ChatNotifyListener
): this ): this;
public on(event: "chat_notify", listener: ChatNotifyListener): this public on(event: "chat_notify", listener: ChatNotifyListener): this;
public on(event: "status", listener: StatusChangeListener): this public on(event: "status", listener: StatusChangeListener): this;
public on(...args: any[]): this { public on(...args: any[]): this {
this.eventBus.$on(...this.parseEventListener(...args)); this.eventBus.$on(...this.parseEventListener(...args));
return this; return this;
} }
public off(event: "msg", chatId: number, listener: MsgListener): this public off(event: "msg", chatId: number, listener: MsgListener): this;
public off(event: "msg", listener: MsgListener): this public off(event: "msg", listener: MsgListener): this;
public off( public off(
event: "chat_notify", event: "chat_notify",
kind: "chat_change", kind: "chat_change",
listener: ChatNotifyListener listener: ChatNotifyListener
): this ): this;
public off( public off(
event: "chat_notify", event: "chat_notify",
kind: string, kind: string,
listener: ChatNotifyListener listener: ChatNotifyListener
): this ): this;
public off(event: "chat_notify", listener: ChatNotifyListener): this public off(event: "chat_notify", listener: ChatNotifyListener): this;
public off(event: "status", listener: StatusChangeListener): this public off(event: "status", listener: StatusChangeListener): this;
public off(...args: any[]): this { public off(...args: any[]): this {
this.eventBus.$off(...this.parseEventListener(...args)); this.eventBus.$off(...this.parseEventListener(...args));
return this; return this;
...@@ -312,7 +318,7 @@ export class Xim { ...@@ -312,7 +318,7 @@ export class Xim {
private onConnected() { private onConnected() {
if (this.client == null) return; if (this.client == null) return;
// 连接成功后,需要调用pubUserInfo, 否则服务端会认为此连接无效 // 连接成功后,需要调用pubUserInfo, 否则服务端会认为此连接无效
this.client.pubUserInfo(JSON.stringify({org_id: chat.getOrgId()})); this.client.pubUserInfo(JSON.stringify({ org_id: chat.getOrgId() }));
this.debug("xim connected"); this.debug("xim connected");
} }
......
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