Commit 7abbf4ee by Sixong.Zhu

Merge branch 'master' into pre

parents 257f730d 8b24464a
......@@ -191,9 +191,10 @@
this.handleScrollWrapper();
this.onNewMessage((e) => {
if (e.type === MessageType.Withdraw) {
this.executeWithDraw(e.ref_id);
const ids = e.ref_id ? [e.ref_id] : JSON.parse(e.msg);
this.executeWithDraw(ids);
dbController
.removeMessage(e.chat_id, e.ref_id)
.removeMessage(e.chat_id, ids)
.finally(() => this.refresh());
}
});
......@@ -339,6 +340,23 @@
// eslint-disable-next-line no-console
console.log("没有更多新消息了");
}
const removingIds: number[] = [];
for (const item of data) {
if (item.type === MessageType.Withdraw) {
item.msg && removingIds.push(...JSON.parse(item.msg));
}
}
if (removingIds.length) {
dbController
.removeMessage(this.chatId, removingIds)
.then(() => {
this.refresh();
this.executeWithDraw(removingIds);
});
}
this.$emit("next-page", msgId);
this.endLoadingNew();
})
......
......@@ -486,15 +486,14 @@
}
private withdraw() {
if (new Date().valueOf() - this.data.ts * 1000 > twoMinutes) {
Xim.error("超过两分钟的消息不能撤回");
return;
if (!this.hoverWithdraw()) {
return Xim.error("不能撤回");
}
ximInstance.withdraw(this.chatId!, this.data.id).finally(() => {
ximInstance.withdraw(this.chatId, this.data.id).finally(() => {
dbController
.removeMessage(this.chatId!, this.data.id)
.removeMessage(this.chatId, [this.data.id])
.finally(() => {
this.executeWithDraw(this.data.id);
this.executeWithDraw([this.data.id]);
this.$emit("withdraw", this.data.id);
});
});
......@@ -502,11 +501,11 @@
private hoverWithdraw() {
if (!this.isWithdraw || !this.isMyMessage) {
return;
return false;
}
this.isWithdraw = this.needReadTip
return (this.isWithdraw = this.needReadTip
? new Date().valueOf() - this.data.ts * 1000 < twoMinutes
: new Date().valueOf() - this.data.ts * 1000 < twoHours;
: new Date().valueOf() - this.data.ts * 1000 < twoHours);
}
private openReaderList(e: MouseEvent) {
......
......@@ -20,7 +20,9 @@ class ChatCacheDatabaseController {
return new Promise<void>((resolve, reject) => {
const checker = () => {
if (!this.setuping) {
this.setupError ? reject(new Error(`IM index database setup failed`)) : resolve();
this.setupError
? reject(new Error(`IM index database setup failed`))
: resolve();
} else {
setTimeout(() => checker(), 200);
}
......@@ -30,16 +32,17 @@ class ChatCacheDatabaseController {
}
public setup(uid: string) {
if (this.db) {
return Promise.resolve();
}
if (this.setuping) {
return this.waitSetupCompleted();
}
return new Promise<void>((resolve, reject) => {
if (uid && indexedDB) {
this.setuping = true;
const r = indexedDB.open(
"u-" + (this.uid = uid),
this.listVersion
);
const key = "u-" + (this.uid = uid);
const r = indexedDB.open(key, this.listVersion);
const setupDb = () => {
if (this.setuping) {
if (this.db) {
......@@ -131,8 +134,9 @@ class ChatCacheDatabaseController {
}
private buildTransaction(key: string) {
try { return this.db.transaction(key, "readwrite"); }
catch{
try {
return this.db.transaction(key, "readwrite");
} catch {
window.location.reload();
throw new Error(`transition failed`);
}
......@@ -329,7 +333,7 @@ class ChatCacheDatabaseController {
const r = store.getAll();
r.onsuccess = (o) => {
const items = (o.target as any).result as Chat[];
resolve(items.find(i => i.biz_type_code === code) as Chat);
resolve(items.find((i) => i.biz_type_code === code) as Chat);
};
r.onerror = () => resolve(null);
});
......@@ -379,13 +383,22 @@ class ChatCacheDatabaseController {
});
}
public removeMessage(chat: number, msg: number) {
public removeMessage(chat: number, msgs: number[]) {
return new Promise<void>((resolve, reject) => {
if (this.db) {
const store = this.buildChatMessageStore(chat);
const d = store.delete(msg);
d.onsuccess = () => setTimeout(() => resolve(), 100);
d.onerror = () => reject();
let count = 0;
const action = () => {
count++;
if (count === msgs.length) {
resolve();
}
};
for (const item of msgs) {
const d = store.delete(item);
d.onsuccess = () => action();
d.onerror = () => action();
}
} else {
resolve();
}
......@@ -396,7 +409,10 @@ class ChatCacheDatabaseController {
for (const item of source2) {
const t = source1.find((i) => i.id === item.id);
if (t) {
item.unread_msg_count = Math.max(item.unread_msg_count, t.unread_msg_count);
item.unread_msg_count = Math.max(
item.unread_msg_count,
t.unread_msg_count
);
const index = source1.indexOf(t);
source1[index] = item;
} else {
......
import type { UniplatSdk } from "uniplat-sdk";
export * from "./order";
export * from "./order-product"
export * from "./order-product";
export const enum ChatRole {
Default = 25,
......@@ -115,12 +115,12 @@ export const enum MessageType {
Action = "action",
Notify = "notify",
MpNavigate = "mp-navigate",
PayV1 = 'gpay',
PayV1 = "gpay",
Pay = "gpay2",
PayResult = "gresult",
RefundV1 = 'grefund',
RefundV1 = "grefund",
Refund = "grefund2",
Card = 'card'
Card = "card",
}
export const enum MessageHandled {
......@@ -195,6 +195,7 @@ export interface BaseChatItem extends BaseChatItemBusinessData {
create_time: number;
update_time: number;
last_msg_ts: number;
last_msg_sender: string;
members_updated: number;
user_updated: number;
}
......
......@@ -94,6 +94,10 @@ function buildChatItem(chat: RawChatItem) {
const b = JSON.parse(chat.business_data) as BaseChatItemBusinessData;
b.detail_name && (chat.detail_name = b.detail_name);
}
// 系统发的自动消息,重新校准未读消息数为0
if (!+chat.last_msg_sender && chat.msg_id === 1) {
chat.unread_msg_count = 0;
}
return { ...chat, chat_id: chat.id } as ChatType;
}
......@@ -148,7 +152,7 @@ export default {
[ChatStore.STATE_MY_CHAT_ROOM_LIST]: [],
[ChatStore.STATE_SINGLE_CHAT]: null,
[ChatStore.STATE_CHAT_CURRENT_CHAT_VERSION]: null,
[ChatStore.STATE_CHAT_CURRENT_CHAT_ID]: null,
[ChatStore.STATE_CHAT_CURRENT_CHAT_ID]: 0,
[ChatStore.STATE_CHAT_MY_ID]: null,
[ChatStore.STATE_CHAT_MY_UID]: null,
[ChatStore.STATE_CHAT_SOURCE]: ServiceType.Frontend,
......@@ -183,7 +187,7 @@ export default {
state[ChatStore.STATE_CHAT_MSG_HISTORY] = null;
},
[ChatStore.MUTATION_CLEAR_CURRENT_CHAT_ID](state) {
state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID] = null;
state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID] = 0;
},
[ChatStore.MUTATION_SAVE_CURRENT_CHAT_ID](
state,
......@@ -259,10 +263,10 @@ export default {
data: ChatStore.STATE_CHAT_MSG_HISTORY
) {
const old = state[ChatStore.STATE_CHAT_MSG_HISTORY] || [];
const chatid = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatid == null) return;
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (!chatId) return;
state[ChatStore.STATE_CHAT_MSG_HISTORY] = Object.freeze(
filterMessages([...(data || []), ...old], chatid)
filterMessages([...(data || []), ...old], chatId)
);
},
[ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS](
......@@ -369,7 +373,7 @@ export default {
payload: Parameters<ChatStore.MUTATION_SAVE_CURRENT_CHAT_INPUTING>[0]
) => {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) return;
if (!chatId) return;
if (payload.chat_id !== chatId) return;
const arr = state[ChatStore.STATE_CURRENT_CHAT_INPUTING];
const eid = payload.eid;
......@@ -409,17 +413,17 @@ export default {
) => {
Vue.set(state[ChatStore.STATE_CHAT_USERNAME], param.id, param.name);
},
[ChatStore.MUTATION_WITHDRAW]: (state, id: number) => {
[ChatStore.MUTATION_WITHDRAW]: (state, ids: number[]) => {
const old = state[ChatStore.STATE_CHAT_MSG_HISTORY] || [];
const chatid = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatid == null) return;
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (!chatId) return;
state[ChatStore.STATE_CHAT_MSG_HISTORY] = old.filter(
(i) => i.id !== id
(i) => !ids.includes(i.id)
);
},
},
actions: {
async [ChatStore.ACTION_GET_MY_CHAT_LIST]({ commit, state }) {
async [ChatStore.ACTION_GET_MY_CHAT_LIST]({ commit, dispatch }) {
commit(ChatStore.MUTATION_SAVE_MYSELF_ID);
if (loadingChatList) {
......@@ -453,15 +457,6 @@ export default {
}
}
const buildUnreadMessage = (items: ChatType[]) => {
let sum = 0;
items.forEach((i) => (sum += i.unread_msg_count));
state[ChatStore.STATE_CURRENT_UNREAD_MESSAGE_COUNT] = sum;
return items.sort((x, y) =>
x.last_msg_ts < y.last_msg_ts ? 1 : -1
);
};
if (cache && cache.length) {
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, cache);
const ts = cache
......@@ -471,19 +466,23 @@ export default {
const execute = () =>
new Promise<ChatType[]>((resolve, reject) => {
Chat.onReady(() => {
xim.fetchChatListAfter(last)!.then((r) => {
xim.fetchChatListAfter(last)!
.then((r) => {
const list = filterActiveChats(
r.args[0] as RawChatItem[]
);
const items = list.map((i) => buildChatItem(i));
const items = list.map((i) =>
buildChatItem(i)
);
if (items && items.length) {
cache = dbController.mergeChatList(
cache,
items
);
}
resolve(buildUnreadMessage(cache));
}).catch(reject);
dispatch(ChatStore.ACTION_REBUILD_UNREAD_MESSAGE_COUNT).finally(resolve);
})
.catch(reject);
});
}).finally(() => (loadingChatList = false));
......@@ -493,7 +492,8 @@ export default {
const execute = () =>
new Promise<ChatType[]>((resolve, reject) => {
Chat.onReady(() => {
xim.fetchChatList().then((data) => {
xim.fetchChatList()
.then((data) => {
if (!data) {
return resolve([]);
}
......@@ -504,9 +504,13 @@ export default {
buildChatItem(chat)
);
dbController.saveChatList(items);
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, items);
resolve(buildUnreadMessage(items));
}).catch(reject);
commit(
ChatStore.MUTATION_SAVE_CHAT_LIST,
items
);
dispatch(ChatStore.ACTION_REBUILD_UNREAD_MESSAGE_COUNT).finally(resolve);
})
.catch(reject);
});
}).finally(() => (loadingChatList = false));
......@@ -539,9 +543,12 @@ export default {
state[ChatStore.STATE_CURRENT_UNREAD_MESSAGE_COUNT] = sum;
},
async [ChatStore.ACTION_UPDATE_CHAT_UNREAD_MESSAGE_COUNT](
{ dispatch },
{ dispatch, state },
p: { chat: number; unread: number }
) {
const list = state[ChatStore.STATE_MY_CHAT_ROOM_LIST] as ChatType[];
const t = list.find(i => i.id === p.chat)
t && (t.unread_msg_count = p.unread)
return dbController
.updateChat4UnreadCount(p.chat, p.unread)
.then(() =>
......@@ -550,29 +557,25 @@ export default {
},
async [ChatStore.ACTION_GET_CHAT_MESSAGES]({ state, commit, getters }) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
const chat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType;
const isMember = state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER];
if (chatId == null) return;
if (!chatId) return;
let data: Message[] = [];
const getMessages = async () => {
const o = {
const getMessages = async () =>
await xim.queryLastPageMsg(chatType, chatId, 20, {
model: chat.model_name,
obj: chat.obj_id,
isMember,
};
data = await xim.queryLastPageMsg(chatType, chatId, 20, o);
};
});
if (isMember) {
const cache = await dbController.getChatMessages(chatId);
if (cache && cache.length) {
data = cache;
} else {
await getMessages();
data = await getMessages();
}
} else {
await getMessages();
data = await getMessages();
}
try {
......@@ -591,7 +594,7 @@ export default {
msgId: Parameters<ChatStore.ACTION_GET_CHAT_MESSAGES_BEFORE_SPECIFIC_ID>[0]
) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) return;
if (!chatId) return;
const chat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
......@@ -611,12 +614,12 @@ export default {
dbController.appendMessages(chatId, data);
return data;
},
async [ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID](
async[ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID](
{ state, commit, getters },
msgId: Parameters<ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID>[0]
) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) return;
if (!chatId) return;
const chat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
......@@ -636,7 +639,7 @@ export default {
dbController.appendMessages(chatId, data);
return data;
},
async [ChatStore.ACTION_SEND_MESSAGE](
async[ChatStore.ACTION_SEND_MESSAGE](
{ state, dispatch, getters, commit },
params: Parameters<ChatStore.ACTION_SEND_MESSAGE>[0]
) {
......@@ -664,7 +667,7 @@ export default {
return Promise.reject(error);
}
},
async [ChatStore.ACTION_GET_FRESH_MESSAGE]({
async[ChatStore.ACTION_GET_FRESH_MESSAGE]({
state,
dispatch,
commit,
......@@ -684,7 +687,7 @@ export default {
await preCacheImgs(newMsgsArr);
commit(ChatStore.MUTATION_SCROLL_TO_BOTTOM);
},
async [ChatStore.ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN](
async[ChatStore.ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN](
{ commit, dispatch },
params: Parameters<ChatStore.ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN>[0]
) {
......@@ -701,7 +704,7 @@ export default {
);
return { chatId, catalog };
},
async [ChatStore.ACTION_CREATE_NEW_CHAT_BY_CLIENT](
async[ChatStore.ACTION_CREATE_NEW_CHAT_BY_CLIENT](
{ commit, dispatch },
params: Parameters<ChatStore.ACTION_CREATE_NEW_CHAT_BY_CLIENT>[0]
) {
......@@ -719,7 +722,7 @@ export default {
dispatch(ChatStore.ACTION_GET_MY_CHAT_LIST);
return chatId;
},
async [ChatStore.ACTION_REGISTER_EVENT]({
async[ChatStore.ACTION_REGISTER_EVENT]({
dispatch,
commit,
state,
......@@ -729,7 +732,7 @@ export default {
const onNewMsg = (e: Message) => {
const thenAction = () => {
if (e.type === MessageType.Withdraw) {
commit(ChatStore.MUTATION_WITHDRAW, +e.msg);
commit(ChatStore.MUTATION_WITHDRAW, e.msg.startsWith('[') ? JSON.parse(e.msg) : [+e.msg]);
}
const scroll = () =>
state[ChatStore.STATE_FUNC_ON_NEW_MSG](e);
......@@ -743,7 +746,7 @@ export default {
};
if (e.type === MessageType.Withdraw) {
dbController
.removeMessage(e.chat_id, +e.msg)
.removeMessage(e.chat_id, e.msg.startsWith('[') ? JSON.parse(e.msg) : [+e.msg])
.finally(() => thenAction());
} else {
thenAction();
......@@ -810,7 +813,7 @@ export default {
xim.on("chat_notify", "read", onMsgRead);
xim.on("chat_notify", "user.input", onInputing);
},
async [ChatStore.ACTION_SAVE_CURRENT_CHAT_ID_VERSION](
async[ChatStore.ACTION_SAVE_CURRENT_CHAT_ID_VERSION](
{ state, commit, dispatch },
chatId: ChatStore.STATE_CHAT_CURRENT_CHAT_ID
) {
......@@ -876,7 +879,7 @@ export default {
commit(ChatStore.MUTATION_SCROLL_TO_BOTTOM);
(<any>state)[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_ERROR] = null;
},
async [ChatStore.ACTION_CLEAR_CURRENT_CHAT_DATA]({ commit, state }) {
async[ChatStore.ACTION_CLEAR_CURRENT_CHAT_DATA]({ commit, state }) {
commit(ChatStore.MUTATION_CLEAR_CURRENT_CHAT_ID);
commit(ChatStore.MUTATION_CLEAR_MYSELF_ID);
commit(ChatStore.MUTATION_CLEAR_CHAT_MSG_HISTORY);
......@@ -884,7 +887,7 @@ export default {
commit(ChatStore.MUTATION_CLEAR_CURRENT_CHAT_MEMBERS);
(<any>state)[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_ERROR] = null;
},
async [ChatStore.ACTION_GET_CHAT_MEMBERS]({ commit, state }) {
async[ChatStore.ACTION_GET_CHAT_MEMBERS]({ commit, state }) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (!chatId) return;
const getChatMembersResult = await xim.fetchChatMembers(chatId);
......@@ -927,7 +930,7 @@ export default {
})
);
},
async [ChatStore.ACTION_TERINATE_CHAT]({ state, dispatch }) {
async[ChatStore.ACTION_TERINATE_CHAT]({ state, dispatch }) {
const v = state[ChatStore.STATE_CHAT_CURRENT_CHAT_VERSION];
if (v == null) return;
const id = Number(
......@@ -955,7 +958,7 @@ export default {
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
] as ChatType;
......@@ -979,7 +982,7 @@ export default {
);
});
},
async [ChatStore.ACTION_CHAT_FINISH_RECEPTION]({ getters, dispatch }) {
async[ChatStore.ACTION_CHAT_FINISH_RECEPTION]({ getters, dispatch }) {
const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
......@@ -996,7 +999,7 @@ export default {
.finishChat()
.finally(() => 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
] as ChatType;
......@@ -1014,7 +1017,7 @@ export default {
.userExitChat()
.finally(() => 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
] as ChatType;
......@@ -1032,7 +1035,7 @@ export default {
.csExitChat()
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
},
async [ChatStore.ACTION_CHAT_ADD_MEMBERS](
async[ChatStore.ACTION_CHAT_ADD_MEMBERS](
{ getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_MEMBERS>[0]
) {
......@@ -1052,7 +1055,7 @@ export default {
.addMember(uids.map((id) => +id))
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
},
async [ChatStore.ACTION_CHAT_REMOVE_MEMBER](
async[ChatStore.ACTION_CHAT_REMOVE_MEMBER](
{ getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_MEMBER>[0]
) {
......@@ -1072,7 +1075,7 @@ export default {
.removeMember(uids.map((id) => +id))
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
},
async [ChatStore.ACTION_CHAT_ADD_CS](
async[ChatStore.ACTION_CHAT_ADD_CS](
{ getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_CS>[0]
) {
......@@ -1092,7 +1095,7 @@ export default {
.addCs(uids.map((id) => Number(id)))
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
},
async [ChatStore.ACTION_CHAT_REMOVE_CS](
async[ChatStore.ACTION_CHAT_REMOVE_CS](
{ getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_CS>[0]
) {
......
......@@ -31,7 +31,7 @@ export namespace ChatStore {
export type STATE_CHAT_SENDING_MESSAGE = dto.Message;
export const STATE_CHAT_CURRENT_CHAT_ID = "当前chat-id";
export type STATE_CHAT_CURRENT_CHAT_ID = number | null;
export type STATE_CHAT_CURRENT_CHAT_ID = number;
export const STATE_CHAT_CURRENT_CHAT_VERSION = "当前chat的Uniplat version";
export type STATE_CHAT_CURRENT_CHAT_VERSION = number | null;
export const STATE_CHAT_CURRENT_IS_CHAT_MEMBER = "是否是当前chat的成员";
......@@ -169,7 +169,7 @@ export namespace ChatStore {
export type MUTATION_CLEAR_CURRENT_CHAT_UNIPLAT_ID = () => void;
export const MUTATION_WITHDRAW = "撤回";
export type MUTATION_WITHDRAW = (id: number) => void;
export type MUTATION_WITHDRAW = (ids: number[]) => void;
export const MUTATION_SAVE_MYSELF_ID =
"保存我的id:聊天窗口显示在右边那个人的id";
......
......@@ -200,7 +200,9 @@ export class Xim {
}
private setMessagesRead(chatId: number, msg: Message[]) {
if (msg.length === 0) return;
if (!msg.length) {
return this.setRead(chatId, 1, 1);
}
return this.setRead(chatId, msg[0].id, msg[msg.length - 1].id);
}
......
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