Commit 8c3c9d5a by Sixong.Zhu

update

parent 3f147148
...@@ -191,9 +191,10 @@ ...@@ -191,9 +191,10 @@
this.handleScrollWrapper(); this.handleScrollWrapper();
this.onNewMessage((e) => { this.onNewMessage((e) => {
if (e.type === MessageType.Withdraw) { 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 dbController
.removeMessage(e.chat_id, e.ref_id) .removeMessage(e.chat_id, ids)
.finally(() => this.refresh()); .finally(() => this.refresh());
} }
}); });
...@@ -339,6 +340,23 @@ ...@@ -339,6 +340,23 @@
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log("没有更多新消息了"); console.log("没有更多新消息了");
} }
const removingIds = [];
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.$emit("next-page", msgId);
this.endLoadingNew(); this.endLoadingNew();
}) })
......
...@@ -486,15 +486,14 @@ ...@@ -486,15 +486,14 @@
} }
private withdraw() { private withdraw() {
if (new Date().valueOf() - this.data.ts * 1000 > twoMinutes) { if (!this.hoverWithdraw()) {
Xim.error("超过两分钟的消息不能撤回"); return Xim.error("不能撤回");
return;
} }
ximInstance.withdraw(this.chatId!, this.data.id).finally(() => { ximInstance.withdraw(this.chatId, this.data.id).finally(() => {
dbController dbController
.removeMessage(this.chatId!, this.data.id) .removeMessage(this.chatId, [this.data.id])
.finally(() => { .finally(() => {
this.executeWithDraw(this.data.id); this.executeWithDraw([this.data.id]);
this.$emit("withdraw", this.data.id); this.$emit("withdraw", this.data.id);
}); });
}); });
...@@ -502,11 +501,11 @@ ...@@ -502,11 +501,11 @@
private hoverWithdraw() { private hoverWithdraw() {
if (!this.isWithdraw || !this.isMyMessage) { 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 < twoMinutes
: new Date().valueOf() - this.data.ts * 1000 < twoHours; : new Date().valueOf() - this.data.ts * 1000 < twoHours);
} }
private openReaderList(e: MouseEvent) { private openReaderList(e: MouseEvent) {
......
...@@ -20,7 +20,9 @@ class ChatCacheDatabaseController { ...@@ -20,7 +20,9 @@ class ChatCacheDatabaseController {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
const checker = () => { const checker = () => {
if (!this.setuping) { 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 { } else {
setTimeout(() => checker(), 200); setTimeout(() => checker(), 200);
} }
...@@ -97,17 +99,17 @@ class ChatCacheDatabaseController { ...@@ -97,17 +99,17 @@ class ChatCacheDatabaseController {
} }
setTimeout(() => resolve(), 200); setTimeout(() => resolve(), 200);
}; };
r.onsuccess = function(e) { r.onsuccess = function (e) {
const db = (e.target as any).result; const db = (e.target as any).result;
that.messageDatabases.set(k, db); that.messageDatabases.set(k, db);
setupDb(); setupDb();
}; };
r.onupgradeneeded = function(e) { r.onupgradeneeded = function (e) {
const db = (e.target as any).result; const db = (e.target as any).result;
that.messageDatabases.set(k, db); that.messageDatabases.set(k, db);
setupDb(); setupDb();
}; };
r.onerror = function(e) { r.onerror = function (e) {
console.log( console.log(
`chat message index database init failed, ${e}` `chat message index database init failed, ${e}`
); );
...@@ -131,8 +133,9 @@ class ChatCacheDatabaseController { ...@@ -131,8 +133,9 @@ class ChatCacheDatabaseController {
} }
private buildTransaction(key: string) { private buildTransaction(key: string) {
try { return this.db.transaction(key, "readwrite"); } try {
catch{ return this.db.transaction(key, "readwrite");
} catch {
window.location.reload(); window.location.reload();
throw new Error(`transition failed`); throw new Error(`transition failed`);
} }
...@@ -329,7 +332,7 @@ class ChatCacheDatabaseController { ...@@ -329,7 +332,7 @@ class ChatCacheDatabaseController {
const r = store.getAll(); const r = store.getAll();
r.onsuccess = (o) => { r.onsuccess = (o) => {
const items = (o.target as any).result as Chat[]; 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); r.onerror = () => resolve(null);
}); });
...@@ -379,13 +382,22 @@ class ChatCacheDatabaseController { ...@@ -379,13 +382,22 @@ class ChatCacheDatabaseController {
}); });
} }
public removeMessage(chat: number, msg: number) { public removeMessage(chat: number, msgs: number[]) {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
if (this.db) { if (this.db) {
const store = this.buildChatMessageStore(chat); const store = this.buildChatMessageStore(chat);
const d = store.delete(msg); let count = 0;
d.onsuccess = () => setTimeout(() => resolve(), 100); const action = () => {
d.onerror = () => reject(); count++;
if (count === msgs.length) {
resolve();
}
};
for (const item of msgs) {
const d = store.delete(item);
d.onsuccess = () => action();
d.onerror = () => action();
}
} else { } else {
resolve(); resolve();
} }
...@@ -396,7 +408,10 @@ class ChatCacheDatabaseController { ...@@ -396,7 +408,10 @@ class ChatCacheDatabaseController {
for (const item of source2) { for (const item of source2) {
const t = source1.find((i) => i.id === item.id); const t = source1.find((i) => i.id === item.id);
if (t) { 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); const index = source1.indexOf(t);
source1[index] = item; source1[index] = item;
} else { } else {
......
import type { UniplatSdk } from "uniplat-sdk"; import type { UniplatSdk } from "uniplat-sdk";
export * from "./order"; export * from "./order";
export * from "./order-product" export * from "./order-product";
export const enum ChatRole { export const enum ChatRole {
Default = 25, Default = 25,
...@@ -115,12 +115,12 @@ export const enum MessageType { ...@@ -115,12 +115,12 @@ export const enum MessageType {
Action = "action", Action = "action",
Notify = "notify", Notify = "notify",
MpNavigate = "mp-navigate", MpNavigate = "mp-navigate",
PayV1 = 'gpay', PayV1 = "gpay",
Pay = "gpay2", Pay = "gpay2",
PayResult = "gresult", PayResult = "gresult",
RefundV1 = 'grefund', RefundV1 = "grefund",
Refund = "grefund2", Refund = "grefund2",
Card = 'card' Card = "card",
} }
export const enum MessageHandled { export const enum MessageHandled {
......
...@@ -32,7 +32,7 @@ function uniqueMessages( ...@@ -32,7 +32,7 @@ function uniqueMessages(
messages: NonNullable<ChatStore.STATE_CHAT_MSG_HISTORY> messages: NonNullable<ChatStore.STATE_CHAT_MSG_HISTORY>
) { ) {
const arr = [...messages]; const arr = [...messages];
return unique(arr, function(item, all) { return unique(arr, function (item, all) {
return all.findIndex((k) => k.id === item.id); return all.findIndex((k) => k.id === item.id);
}); });
} }
...@@ -362,7 +362,7 @@ export default { ...@@ -362,7 +362,7 @@ export default {
state[ChatStore.STATE_CHAT_SENDING_MESSAGES] = [...current]; state[ChatStore.STATE_CHAT_SENDING_MESSAGES] = [...current];
} }
}, },
[ChatStore.MUTATION_SAVE_CURRENT_CHAT_INPUTING]: (function() { [ChatStore.MUTATION_SAVE_CURRENT_CHAT_INPUTING]: (function () {
const setTimeoutId: { [key: string]: number } = {}; const setTimeoutId: { [key: string]: number } = {};
return ( return (
state: ChatStoreState, state: ChatStoreState,
...@@ -409,12 +409,12 @@ export default { ...@@ -409,12 +409,12 @@ export default {
) => { ) => {
Vue.set(state[ChatStore.STATE_CHAT_USERNAME], param.id, param.name); 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 old = state[ChatStore.STATE_CHAT_MSG_HISTORY] || [];
const chatid = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID]; const chatid = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatid == null) return; if (chatid == null) return;
state[ChatStore.STATE_CHAT_MSG_HISTORY] = old.filter( state[ChatStore.STATE_CHAT_MSG_HISTORY] = old.filter(
(i) => i.id !== id (i) => !ids.includes(i.id)
); );
}, },
}, },
...@@ -471,11 +471,14 @@ export default { ...@@ -471,11 +471,14 @@ export default {
const execute = () => const execute = () =>
new Promise<ChatType[]>((resolve, reject) => { new Promise<ChatType[]>((resolve, reject) => {
Chat.onReady(() => { Chat.onReady(() => {
xim.fetchChatListAfter(last)!.then((r) => { xim.fetchChatListAfter(last)!
.then((r) => {
const list = filterActiveChats( const list = filterActiveChats(
r.args[0] as RawChatItem[] r.args[0] as RawChatItem[]
); );
const items = list.map((i) => buildChatItem(i)); const items = list.map((i) =>
buildChatItem(i)
);
if (items && items.length) { if (items && items.length) {
cache = dbController.mergeChatList( cache = dbController.mergeChatList(
cache, cache,
...@@ -483,7 +486,8 @@ export default { ...@@ -483,7 +486,8 @@ export default {
); );
} }
resolve(buildUnreadMessage(cache)); resolve(buildUnreadMessage(cache));
}).catch(reject); })
.catch(reject);
}); });
}).finally(() => (loadingChatList = false)); }).finally(() => (loadingChatList = false));
...@@ -493,7 +497,8 @@ export default { ...@@ -493,7 +497,8 @@ export default {
const execute = () => const execute = () =>
new Promise<ChatType[]>((resolve, reject) => { new Promise<ChatType[]>((resolve, reject) => {
Chat.onReady(() => { Chat.onReady(() => {
xim.fetchChatList().then((data) => { xim.fetchChatList()
.then((data) => {
if (!data) { if (!data) {
return resolve([]); return resolve([]);
} }
...@@ -504,9 +509,13 @@ export default { ...@@ -504,9 +509,13 @@ export default {
buildChatItem(chat) buildChatItem(chat)
); );
dbController.saveChatList(items); dbController.saveChatList(items);
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, items); commit(
ChatStore.MUTATION_SAVE_CHAT_LIST,
items
);
resolve(buildUnreadMessage(items)); resolve(buildUnreadMessage(items));
}).catch(reject); })
.catch(reject);
}); });
}).finally(() => (loadingChatList = false)); }).finally(() => (loadingChatList = false));
...@@ -729,7 +738,7 @@ export default { ...@@ -729,7 +738,7 @@ export default {
const onNewMsg = (e: Message) => { const onNewMsg = (e: Message) => {
const thenAction = () => { const thenAction = () => {
if (e.type === MessageType.Withdraw) { if (e.type === MessageType.Withdraw) {
commit(ChatStore.MUTATION_WITHDRAW, +e.msg); commit(ChatStore.MUTATION_WITHDRAW, [+e.msg]);
} }
const scroll = () => const scroll = () =>
state[ChatStore.STATE_FUNC_ON_NEW_MSG](e); state[ChatStore.STATE_FUNC_ON_NEW_MSG](e);
...@@ -743,7 +752,7 @@ export default { ...@@ -743,7 +752,7 @@ export default {
}; };
if (e.type === MessageType.Withdraw) { if (e.type === MessageType.Withdraw) {
dbController dbController
.removeMessage(e.chat_id, +e.msg) .removeMessage(e.chat_id, [+e.msg])
.finally(() => thenAction()); .finally(() => thenAction());
} else { } else {
thenAction(); thenAction();
...@@ -922,7 +931,7 @@ export default { ...@@ -922,7 +931,7 @@ export default {
} }
commit( commit(
ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS, ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS,
unique(newChatMembers, function(item, all) { unique(newChatMembers, function (item, all) {
return all.findIndex((k) => k.eid === item.eid); return all.findIndex((k) => k.eid === item.eid);
}) })
); );
......
...@@ -169,7 +169,7 @@ export namespace ChatStore { ...@@ -169,7 +169,7 @@ export namespace ChatStore {
export type MUTATION_CLEAR_CURRENT_CHAT_UNIPLAT_ID = () => void; export type MUTATION_CLEAR_CURRENT_CHAT_UNIPLAT_ID = () => void;
export const MUTATION_WITHDRAW = "撤回"; export const MUTATION_WITHDRAW = "撤回";
export type MUTATION_WITHDRAW = (id: number) => void; export type MUTATION_WITHDRAW = (ids: number[]) => void;
export const MUTATION_SAVE_MYSELF_ID = export const MUTATION_SAVE_MYSELF_ID =
"保存我的id:聊天窗口显示在右边那个人的id"; "保存我的id:聊天窗口显示在右边那个人的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