Commit 8c3c9d5a by Sixong.Zhu

update

parent 3f147148
......@@ -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 = [];
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);
}
......@@ -97,17 +99,17 @@ class ChatCacheDatabaseController {
}
setTimeout(() => resolve(), 200);
};
r.onsuccess = function(e) {
r.onsuccess = function (e) {
const db = (e.target as any).result;
that.messageDatabases.set(k, db);
setupDb();
};
r.onupgradeneeded = function(e) {
r.onupgradeneeded = function (e) {
const db = (e.target as any).result;
that.messageDatabases.set(k, db);
setupDb();
};
r.onerror = function(e) {
r.onerror = function (e) {
console.log(
`chat message index database init failed, ${e}`
);
......@@ -131,8 +133,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 +332,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 +382,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 +408,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 {
......
......@@ -32,7 +32,7 @@ function uniqueMessages(
messages: NonNullable<ChatStore.STATE_CHAT_MSG_HISTORY>
) {
const arr = [...messages];
return unique(arr, function(item, all) {
return unique(arr, function (item, all) {
return all.findIndex((k) => k.id === item.id);
});
}
......@@ -362,7 +362,7 @@ export default {
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 } = {};
return (
state: ChatStoreState,
......@@ -409,12 +409,12 @@ 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;
state[ChatStore.STATE_CHAT_MSG_HISTORY] = old.filter(
(i) => i.id !== id
(i) => !ids.includes(i.id)
);
},
},
......@@ -471,19 +471,23 @@ export default {
const execute = () =>
new Promise<ChatType[]>((resolve, reject) => {
Chat.onReady(() => {
xim.fetchChatListAfter(last)!.then((r) => {
const list = filterActiveChats(
r.args[0] as RawChatItem[]
);
const items = list.map((i) => buildChatItem(i));
if (items && items.length) {
cache = dbController.mergeChatList(
cache,
items
xim.fetchChatListAfter(last)!
.then((r) => {
const list = filterActiveChats(
r.args[0] as RawChatItem[]
);
}
resolve(buildUnreadMessage(cache));
}).catch(reject);
const items = list.map((i) =>
buildChatItem(i)
);
if (items && items.length) {
cache = dbController.mergeChatList(
cache,
items
);
}
resolve(buildUnreadMessage(cache));
})
.catch(reject);
});
}).finally(() => (loadingChatList = false));
......@@ -493,20 +497,25 @@ export default {
const execute = () =>
new Promise<ChatType[]>((resolve, reject) => {
Chat.onReady(() => {
xim.fetchChatList().then((data) => {
if (!data) {
return resolve([]);
}
const chatList = filterActiveChats(
data.args[0] as RawChatItem[]
);
const items = chatList.map((chat) =>
buildChatItem(chat)
);
dbController.saveChatList(items);
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, items);
resolve(buildUnreadMessage(items));
}).catch(reject);
xim.fetchChatList()
.then((data) => {
if (!data) {
return resolve([]);
}
const chatList = filterActiveChats(
data.args[0] as RawChatItem[]
);
const items = chatList.map((chat) =>
buildChatItem(chat)
);
dbController.saveChatList(items);
commit(
ChatStore.MUTATION_SAVE_CHAT_LIST,
items
);
resolve(buildUnreadMessage(items));
})
.catch(reject);
});
}).finally(() => (loadingChatList = false));
......@@ -729,7 +738,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]);
}
const scroll = () =>
state[ChatStore.STATE_FUNC_ON_NEW_MSG](e);
......@@ -743,7 +752,7 @@ export default {
};
if (e.type === MessageType.Withdraw) {
dbController
.removeMessage(e.chat_id, +e.msg)
.removeMessage(e.chat_id, [+e.msg])
.finally(() => thenAction());
} else {
thenAction();
......@@ -922,7 +931,7 @@ export default {
}
commit(
ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS,
unique(newChatMembers, function(item, all) {
unique(newChatMembers, function (item, all) {
return all.findIndex((k) => k.eid === item.eid);
})
);
......@@ -1179,8 +1188,8 @@ export default {
p.read_count = option.all
? p.total_read_count
: option.readed
? option.readed
: p.read_count + 1;
? option.readed
: p.read_count + 1;
}
}
} else {
......@@ -1189,8 +1198,8 @@ export default {
p.read_count = option.all
? p.total_read_count
: option.readed
? option.readed
: p.read_count + 1;
? option.readed
: p.read_count + 1;
}
}
}
......
......@@ -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";
......
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