mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-25 07:16:51 +00:00
fix(sidebar): prefer valid nickname over wxid
This commit is contained in:
@@ -3250,11 +3250,13 @@ class ChatService {
|
|||||||
if (!connectResult.success) return null
|
if (!connectResult.success) return null
|
||||||
const result = await wcdbService.getContact(username)
|
const result = await wcdbService.getContact(username)
|
||||||
if (!result.success || !result.contact) return null
|
if (!result.success || !result.contact) return null
|
||||||
|
const contact = result.contact as Record<string, any>
|
||||||
return {
|
return {
|
||||||
username: result.contact.username || username,
|
username: String(contact.username || contact.user_name || contact.userName || username || ''),
|
||||||
alias: result.contact.alias || '',
|
alias: String(contact.alias || contact.Alias || ''),
|
||||||
remark: result.contact.remark || '',
|
remark: String(contact.remark || contact.Remark || ''),
|
||||||
nickName: result.contact.nickName || ''
|
// 兼容不同表结构字段,避免 nick_name 丢失导致侧边栏退化到 wxid。
|
||||||
|
nickName: String(contact.nickName || contact.nick_name || contact.nickname || contact.NickName || '')
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -64,13 +64,6 @@ function Sidebar() {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadCurrentUser = async () => {
|
const loadCurrentUser = async () => {
|
||||||
const normalizeName = (value?: string | null): string | undefined => {
|
|
||||||
if (!value) return undefined
|
|
||||||
const trimmed = value.trim()
|
|
||||||
if (!trimmed || trimmed.toLowerCase() === 'self') return undefined
|
|
||||||
return trimmed
|
|
||||||
}
|
|
||||||
|
|
||||||
const patchUserProfile = (patch: Partial<SidebarUserProfile>, expectedWxid?: string) => {
|
const patchUserProfile = (patch: Partial<SidebarUserProfile>, expectedWxid?: string) => {
|
||||||
setUserProfile(prev => {
|
setUserProfile(prev => {
|
||||||
if (expectedWxid && prev.wxid && prev.wxid !== expectedWxid) {
|
if (expectedWxid && prev.wxid && prev.wxid !== expectedWxid) {
|
||||||
@@ -91,6 +84,32 @@ function Sidebar() {
|
|||||||
try {
|
try {
|
||||||
const wxid = await configService.getMyWxid()
|
const wxid = await configService.getMyWxid()
|
||||||
const resolvedWxid = wxid || ''
|
const resolvedWxid = wxid || ''
|
||||||
|
const cleanedWxidMatch = resolvedWxid.match(/^(wxid_[^_]+)/i)
|
||||||
|
const cleanedWxid = cleanedWxidMatch?.[1] || resolvedWxid
|
||||||
|
const wxidCandidates = new Set<string>([
|
||||||
|
resolvedWxid.trim().toLowerCase(),
|
||||||
|
cleanedWxid.trim().toLowerCase()
|
||||||
|
].filter(Boolean))
|
||||||
|
|
||||||
|
const normalizeName = (value?: string | null): string | undefined => {
|
||||||
|
if (!value) return undefined
|
||||||
|
const trimmed = value.trim()
|
||||||
|
if (!trimmed) return undefined
|
||||||
|
const lowered = trimmed.toLowerCase()
|
||||||
|
if (lowered === 'self') return undefined
|
||||||
|
if (lowered.startsWith('wxid_')) return undefined
|
||||||
|
if (wxidCandidates.has(lowered)) return undefined
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
|
||||||
|
const pickFirstValidName = (...candidates: Array<string | null | undefined>): string | undefined => {
|
||||||
|
for (const candidate of candidates) {
|
||||||
|
const normalized = normalizeName(candidate)
|
||||||
|
if (normalized) return normalized
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
const fallbackDisplayName = resolvedWxid || '未识别用户'
|
const fallbackDisplayName = resolvedWxid || '未识别用户'
|
||||||
|
|
||||||
// 第一阶段:先把 wxid/名称打上,保证侧边栏第一时间可见。
|
// 第一阶段:先把 wxid/名称打上,保证侧边栏第一时间可见。
|
||||||
@@ -105,20 +124,26 @@ function Sidebar() {
|
|||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const myContact = await window.electronAPI.chat.getContact(resolvedWxid)
|
const myContact = await window.electronAPI.chat.getContact(resolvedWxid)
|
||||||
const fromContact =
|
const fromContact = pickFirstValidName(
|
||||||
normalizeName(myContact?.remark) ||
|
myContact?.remark,
|
||||||
normalizeName(myContact?.nickName) ||
|
myContact?.nickName,
|
||||||
normalizeName(myContact?.alias)
|
myContact?.alias
|
||||||
|
)
|
||||||
|
|
||||||
if (fromContact) {
|
if (fromContact) {
|
||||||
patchUserProfile({ displayName: fromContact }, resolvedWxid)
|
patchUserProfile({ displayName: fromContact }, resolvedWxid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const enrichedResult = await window.electronAPI.chat.enrichSessionsContactInfo([resolvedWxid, 'self'])
|
const enrichTargets = Array.from(new Set([resolvedWxid, cleanedWxid, 'self'].filter(Boolean)))
|
||||||
const enrichedDisplayName = normalizeName(enrichedResult.contacts?.[resolvedWxid]?.displayName)
|
const enrichedResult = await window.electronAPI.chat.enrichSessionsContactInfo(enrichTargets)
|
||||||
const fallbackSelfName = normalizeName(enrichedResult.contacts?.self?.displayName)
|
const enrichedDisplayName = pickFirstValidName(
|
||||||
const bestName = enrichedDisplayName || fallbackSelfName
|
enrichedResult.contacts?.[resolvedWxid]?.displayName,
|
||||||
|
enrichedResult.contacts?.[cleanedWxid]?.displayName,
|
||||||
|
enrichedResult.contacts?.self?.displayName,
|
||||||
|
myContact?.alias
|
||||||
|
)
|
||||||
|
const bestName = enrichedDisplayName
|
||||||
if (bestName) {
|
if (bestName) {
|
||||||
patchUserProfile({ displayName: bestName }, resolvedWxid)
|
patchUserProfile({ displayName: bestName }, resolvedWxid)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user