状态管理
使用 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>
)
}