import { yxgApi, getYxgToken, setYxgToken } from './yxgApi.js' let cachedNick = '' export function setAccountNickname(v) { cachedNick = String(v || '').trim() } export function selfDisplayName(fallback = '我') { return cachedNick || fallback } export async function loadAccountNickname(force = false) { if (cachedNick && !force) return cachedNick try { const me = await yxgApi.authMe() cachedNick = (me.nickname || '').trim() return cachedNick } catch { return cachedNick } } export function openWechatLogin() { const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : [] const cur = pages.length ? pages[pages.length - 1] : null const route = (cur && (cur.route || cur.__route__)) || '' if (String(route).indexOf('login-pop') !== -1) return uni.navigateTo({ url: '/pages/login-pop/login-pop?type=2' }) } export async function ensureAccount(redirect) { if (!getYxgToken()) { openWechatLogin() return null } try { return await yxgApi.authMe() } catch { setYxgToken('') openWechatLogin() return null } } export async function findSelfProfile() { const { items } = await yxgApi.listProfiles() return (items || []).find((p) => p.relation === 'self') || null } export async function ensureSelfProfile(input) { await loadAccountNickname() const nick = selfDisplayName('我') const nameIn = (input.display_name || '').trim() const preferred = !nameIn || nameIn === '我' ? nick : nameIn const existing = await findSelfProfile() if (existing) { const keep = existing.display_name?.trim() const display = keep && keep !== '我' ? keep : preferred return yxgApi.updateProfile(existing.id, { display_name: display, birth_date: input.birth_date, birth_time: input.birth_time, birth_place: input.birth_place, }) } try { return await yxgApi.createProfile({ relation: 'self', birth_date: input.birth_date, display_name: preferred, birth_time: input.birth_time, birth_place: input.birth_place, }) } catch (e) { const msg = e instanceof Error ? e.message : '' if (!/already exists|已有|409|self profile/.test(msg)) throw e const again = await findSelfProfile() if (!again) throw e const keep = again.display_name?.trim() return yxgApi.updateProfile(again.id, { display_name: keep && keep !== '我' ? keep : preferred, birth_date: input.birth_date, birth_time: input.birth_time, birth_place: input.birth_place, }) } } export async function ensureOtherProfile(input) { const name = (input.display_name || 'TA').trim() || 'TA' const birth = String(input.birth_date || '').slice(0, 10) const { items } = await yxgApi.listProfiles() const hit = (items || []).find( (p) => p.relation === 'other' && String(p.birth_date).slice(0, 10) === birth && (p.display_name || 'TA') === name, ) if (hit) return hit return yxgApi.createProfile({ relation: 'other', birth_date: birth, display_name: name, relation_type: input.relation_type, birth_place: input.birth_place, }) } export function pickableArchiveList(items) { const list = items || [] const self = list.find((p) => p.relation === 'self') const others = list.filter((p) => p.relation === 'other') return self ? [self, ...others] : others } export async function loadSelfLatest(type) { const profile = await findSelfProfile() if (!profile) return { profile: null, report: null } try { const report = await yxgApi.getLatestReport(profile.id, type) return { profile, report } } catch { return { profile, report: null } } }