同步聊天修复到根目录pages/utils(与副本工程结构一致)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* 打手-商家/老板配对群:group_Ds{打手}_Sj{商家} 或 group_Ds{打手}_Boss{老板}
|
||||
* 打手-商家/老板配对群:group_Ds{打手}_Sj{商家}
|
||||
*/
|
||||
export const ORDER_CARD_PREFIX = '[ORDER_CARD]';
|
||||
|
||||
@@ -29,6 +29,67 @@ export function getCounterpartGoEasyId(groupId, myGoEasyId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function fixAvatarUrl(avatar, defaultAvatar, ossBase) {
|
||||
if (!avatar) return defaultAvatar;
|
||||
if (avatar.startsWith('http')) return avatar;
|
||||
return (ossBase || '') + avatar.replace(/^\//, '');
|
||||
}
|
||||
|
||||
/** 根据群 to.data 与当前身份,解析对方/双方展示信息 */
|
||||
export function applyPairMetaToConversation(item, myGoEasyId, defaultAvatar, ossBase) {
|
||||
if (!item || !item.data) return;
|
||||
const d = item.data;
|
||||
|
||||
if (d.dashouGoEasyId && d.partnerGoEasyId) {
|
||||
const isDashou = myGoEasyId === d.dashouGoEasyId;
|
||||
const isPartner = myGoEasyId === d.partnerGoEasyId;
|
||||
d.dashouNicheng = d.dashouName || d.dashouNicheng || '';
|
||||
d.partnerNicheng = d.partnerName || d.partnerNicheng || '';
|
||||
d.dashouAvatar = fixAvatarUrl(d.dashouAvatar, defaultAvatar, ossBase);
|
||||
d.partnerAvatar = fixAvatarUrl(d.partnerAvatar, defaultAvatar, ossBase);
|
||||
|
||||
if (isDashou) {
|
||||
d.counterpartId = d.partnerGoEasyId;
|
||||
d.counterpartYonghuid = d.partnerYonghuid || d.partnerGoEasyId.replace(/^(Sj|Boss)/, '');
|
||||
d.partnerRoleLabel = d.partnerGoEasyId.startsWith('Boss') ? '老板' : '商家';
|
||||
d.name = d.partnerNicheng || `${d.partnerRoleLabel}${d.counterpartYonghuid}`;
|
||||
d.avatar = d.partnerAvatar || defaultAvatar;
|
||||
} else if (isPartner) {
|
||||
d.counterpartId = d.dashouGoEasyId;
|
||||
d.counterpartYonghuid = d.dashouYonghuid || d.dashouGoEasyId.replace(/^Ds/, '');
|
||||
d.name = d.dashouNicheng || `打手${d.counterpartYonghuid}`;
|
||||
d.avatar = d.dashouAvatar || defaultAvatar;
|
||||
} else {
|
||||
const cpId = getCounterpartGoEasyId(item.groupId, myGoEasyId);
|
||||
if (cpId === d.partnerGoEasyId) {
|
||||
d.name = d.partnerNicheng;
|
||||
d.avatar = d.partnerAvatar || defaultAvatar;
|
||||
} else if (cpId === d.dashouGoEasyId) {
|
||||
d.name = d.dashouNicheng;
|
||||
d.avatar = d.dashouAvatar || defaultAvatar;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const counterpartId = getCounterpartGoEasyId(item.groupId, myGoEasyId);
|
||||
if (counterpartId) {
|
||||
d.counterpartId = counterpartId;
|
||||
d.counterpartYonghuid = counterpartId.replace(/^(Ds|Sj|Boss)/, '');
|
||||
if (!d.name || d.name === item.groupId) {
|
||||
d.name = `用户${d.counterpartYonghuid.slice(-6)}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (item.lastMessage && item.lastMessage.senderId && item.lastMessage.senderId !== myGoEasyId) {
|
||||
const sd = item.lastMessage.senderData || {};
|
||||
if (sd.avatar) d.avatar = fixAvatarUrl(sd.avatar, defaultAvatar, ossBase);
|
||||
if (sd.name) d.name = sd.name;
|
||||
}
|
||||
|
||||
d.avatar = fixAvatarUrl(d.avatar, defaultAvatar, ossBase);
|
||||
}
|
||||
|
||||
export function parseOrderCardText(text) {
|
||||
if (!text || !text.startsWith(ORDER_CARD_PREFIX)) return null;
|
||||
try {
|
||||
@@ -77,36 +138,49 @@ export function enrichGroupConversation(item, myGoEasyId, defaultAvatar, ossBase
|
||||
if (!item || item.type !== 'group') return item;
|
||||
if (!item.data) item.data = {};
|
||||
|
||||
const counterpartId = getCounterpartGoEasyId(item.groupId, myGoEasyId);
|
||||
if (counterpartId) {
|
||||
item.data.counterpartId = counterpartId;
|
||||
if (!item.data.name || item.data.name === item.groupId) {
|
||||
item.data.name = `用户${counterpartId.replace(/^(Ds|Sj|Boss)/, '').slice(-6)}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (item.lastMessage && item.lastMessage.senderId && item.lastMessage.senderId !== myGoEasyId) {
|
||||
const sd = item.lastMessage.senderData || {};
|
||||
if (sd.avatar) item.data.avatar = sd.avatar;
|
||||
if (sd.name) item.data.name = sd.name;
|
||||
}
|
||||
|
||||
let avatar = item.data.avatar || '';
|
||||
if (avatar && !avatar.startsWith('http')) {
|
||||
avatar = (ossBase || '') + avatar.replace(/^\//, '');
|
||||
}
|
||||
item.data.avatar = avatar || defaultAvatar;
|
||||
applyPairMetaToConversation(item, myGoEasyId, defaultAvatar, ossBase);
|
||||
|
||||
if (item.data.orderZhuangtai != null) {
|
||||
item.data.orderZhuangtaiText = ZHUANGTAI_MAP[item.data.orderZhuangtai] || '';
|
||||
}
|
||||
|
||||
item.displayLastMsg = formatLastMessagePreview(item.lastMessage);
|
||||
if (!item.data.avatar) item.data.avatar = defaultAvatar;
|
||||
return item;
|
||||
}
|
||||
|
||||
export function isOrderGroupConversation(c) {
|
||||
if (!c || c.type !== 'group') return false;
|
||||
if (c.data && c.data.orderId) return true;
|
||||
if (c.data && c.data.dashouGoEasyId) return true;
|
||||
return isPairGroupId(c.groupId) || /^group_\w+$/.test(c.groupId || '');
|
||||
}
|
||||
|
||||
export function getConversationSortTime(conversation, openTimes) {
|
||||
const id = conversation.groupId || conversation.userId || '';
|
||||
const msgTs = conversation.lastMessage?.timestamp || 0;
|
||||
const openTs = (openTimes && id && openTimes[id]) ? openTimes[id] : 0;
|
||||
return Math.max(msgTs, openTs);
|
||||
}
|
||||
|
||||
export function sortConversations(list, openTimes) {
|
||||
return list.sort((a, b) => {
|
||||
if (a.top && !b.top) return -1;
|
||||
if (!a.top && b.top) return 1;
|
||||
if (a.unread > 0 && b.unread <= 0) return -1;
|
||||
if (a.unread <= 0 && b.unread > 0) return 1;
|
||||
const ta = getConversationSortTime(a, openTimes);
|
||||
const tb = getConversationSortTime(b, openTimes);
|
||||
return tb - ta;
|
||||
});
|
||||
}
|
||||
|
||||
export function recordConversationOpen(conversation) {
|
||||
const id = conversation?.groupId || conversation?.userId;
|
||||
if (!id) return;
|
||||
try {
|
||||
const times = wx.getStorageSync('conversationOpenTimes') || {};
|
||||
times[id] = Date.now();
|
||||
wx.setStorageSync('conversationOpenTimes', times);
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user