110 lines
3.8 KiB
JavaScript
110 lines
3.8 KiB
JavaScript
import { reactive } from 'vue'
|
|||
|
|
import { api } from './api.js'
|
||
|
|
import { bindingStatusLabel } from './binding-status.js'
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 当前 QQ 账号选择的持久化键。
|
||
|
|
* 账号是全局必需上下文:不再支持「全部账号」,仅当实例没有任何 ACTIVE 且回填 QQ 号的绑定时,
|
||
|
|
* current 才为 null(此时各页面显示空态并提示去接入管理绑定)。
|
||
|
|
*/
|
||
|
|
const STORAGE_KEY = 'qlinker.account.current'
|
||
|
|
|
||
|
|
function readStored() {
|
||
|
|
const value = localStorage.getItem(STORAGE_KEY)
|
||
|
|
return value && value.trim() ? value.trim() : null
|
||
|
|
}
|
||
|
|
|
||
|
|
export const accountState = reactive({
|
||
|
|
loaded: false,
|
||
|
|
loading: false,
|
||
|
|
items: [],
|
||
|
|
unattributedBindings: 0,
|
||
|
|
/** 当前 qqAccountId;仅在没有可用账号时为 null。 */
|
||
|
|
current: readStored(),
|
||
|
|
/** 加载/回退提示,供切换器展示,不代表后端错误。 */
|
||
|
|
message: '',
|
||
|
|
error: ''
|
||
|
|
})
|
||
|
|
|
||
|
|
export function accountParam() {
|
||
|
|
return accountState.current
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 是否有可用的当前账号;无账号时页面应展示空态而不是拉全量数据。 */
|
||
|
|
export function hasCurrentAccount() {
|
||
|
|
return Boolean(accountState.current)
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isCurrent(qqAccountId) {
|
||
|
|
return String(qqAccountId) === String(accountState.current)
|
||
|
|
}
|
||
|
|
|
||
|
|
export function currentAccount() {
|
||
|
|
return accountState.items.find((item) => isCurrent(item.qqAccountId)) || null
|
||
|
|
}
|
||
|
|
|
||
|
|
export function setCurrentAccount(qqAccountId) {
|
||
|
|
const next = qqAccountId == null ? '' : String(qqAccountId).trim()
|
||
|
|
// 不再允许空值代表「全部账号」;空值仅用于无账号可选的场景。
|
||
|
|
if (!next) return
|
||
|
|
accountState.current = next
|
||
|
|
accountState.message = ''
|
||
|
|
localStorage.setItem(STORAGE_KEY, next)
|
||
|
|
}
|
||
|
|
|
||
|
|
function eventTime(item) {
|
||
|
|
const time = Date.parse(item?.lastEventAt || '')
|
||
|
|
return Number.isFinite(time) ? time : 0
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 默认账号:优先在线,其次最近有事件的(lastEventAt),最后回退目录首项。 */
|
||
|
|
function pickDefaultAccount(items) {
|
||
|
|
if (!items.length) return null
|
||
|
|
const online = items.filter((item) => item.online === true)
|
||
|
|
const pool = online.length ? online : items
|
||
|
|
const latest = [...pool].sort((a, b) => eventTime(b) - eventTime(a))[0]
|
||
|
|
return latest || items[0]
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 加载账号目录并校正当前账号(绑定可能停用或 QQ 号变更)。
|
||
|
|
* 当前账号失效时自动切到默认账号;没有任何可用账号时置空并提示去接入管理,
|
||
|
|
* 不回退到「全部账号」聚合,避免不同账号的数据混在一起。
|
||
|
|
*/
|
||
|
|
export async function loadAccounts() {
|
||
|
|
if (accountState.loading) return
|
||
|
|
accountState.loading = true
|
||
|
|
try {
|
||
|
|
const data = await api.accounts()
|
||
|
|
accountState.items = data?.items || []
|
||
|
|
accountState.unattributedBindings = Number(data?.unattributedBindings || 0)
|
||
|
|
accountState.loaded = true
|
||
|
|
accountState.error = ''
|
||
|
|
if (!accountState.items.length) {
|
||
|
|
accountState.current = null
|
||
|
|
localStorage.removeItem(STORAGE_KEY)
|
||
|
|
accountState.message = '当前实例没有可用 QQ 账号,请先到接入管理绑定并确认连接在线。'
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if (!accountState.current || !accountState.items.some((item) => isCurrent(item.qqAccountId))) {
|
||
|
|
const fallback = pickDefaultAccount(accountState.items)
|
||
|
|
const hadCurrent = Boolean(accountState.current)
|
||
|
|
accountState.current = fallback.qqAccountId
|
||
|
|
localStorage.setItem(STORAGE_KEY, fallback.qqAccountId)
|
||
|
|
accountState.message = hadCurrent
|
||
|
|
? '原选账号已不可用,已切换到当前在线账号'
|
||
|
|
: ''
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
accountState.error = e?.message || '账号目录加载失败'
|
||
|
|
accountState.message = ''
|
||
|
|
} finally {
|
||
|
|
accountState.loading = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 账号在线文案与接入管理、画布共用同一判定(connection_status),避免各处口径分裂。 */
|
||
|
|
export function statusText(status) {
|
||
|
|
return bindingStatusLabel({ connectionStatus: status })
|
||
|
|
}
|