状态管理

使用 Zustand,所有 Store 在 libs/console-core/src/stores/ 下。

useUserStore

const { user, fetchUser, clearUser, isAdmin } = useUserStore()
字段/方法说明
user用户信息 { id, email, role, tenant: { role } }
fetchUser()请求 /v1/users/info,返回是否成功
clearUser()清空用户状态
isAdmin()是否系统管理员(user.role === 'owner'

useBillingStore

const { subscription, lifetime, credits, fetchAll, clear, getTotalCredits, hasSubscription } = useBillingStore()
字段/方法说明
subscription当前订阅列表
lifetime终身买断列表
credits积分 { all: [], total: number }
fetchAll()并行加载订阅、终身、积分
clear()清空所有计费数据
getTotalCredits()获取总可用积分
hasSubscription(planId?)是否有活跃订阅
hasLifetime(planId?)是否有终身买断

useTenantStore

const { tenants, current, fetchTenants, setCurrent, clearTenants } = useTenantStore()
字段/方法说明
tenants用户所有组织列表
current当前选中的组织
fetchTenants()请求组织列表,自动恢复上次选中
setCurrent(tenant)切换组织(存入 localStorage)
clearTenants()清空组织状态和 localStorage

current 包含 member_role 字段,表示当前用户在该组织中的角色。

useWebsiteStore

const { config, fetchConfig } = useWebsiteStore()
字段说明
config.subscription订阅配置(enabled、packages、has_lifetime)
config.credit积分配置(enabled、min_credits、packages)
config.tenant租户配置(roles)
config.app_mode应用模式

使用示例

import { useUserStore, useBillingStore, useTenantStore } from '@readystart/console-core'

function MyComponent() {
  const { user } = useUserStore()
  const { credits } = useBillingStore()
  const { current } = useTenantStore()

  return (
    <div>
      <p>用户:{user?.email}</p>
      <p>组织:{current?.name}</p>
      <p>积分:{credits?.total}</p>
    </div>
  )
}