feat: add GameGroup2 project with frontend and backend
- Add .gitignore for Node.js and PocketBase projects - Add frontend (Vue 3 + Vite + TypeScript) - Add backend (PocketBase) - Add deployment scripts and Docker compose configs Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// src/api/groups.ts
|
||||
import pb from './pocketbase'
|
||||
import type { Group } from '@/types'
|
||||
|
||||
// 创建群组
|
||||
export async function createGroup(data: {
|
||||
name: string
|
||||
description: string
|
||||
maxMembers: number
|
||||
}) {
|
||||
const user = pb.authStore.model
|
||||
if (!user) throw new Error('未登录')
|
||||
|
||||
return pb.collection('groups').create({
|
||||
...data,
|
||||
owner: user.id,
|
||||
members: [user.id]
|
||||
})
|
||||
}
|
||||
|
||||
// 获取用户的群组列表
|
||||
export async function getUserGroups(): Promise<Group[]> {
|
||||
const user = pb.authStore.model
|
||||
if (!user) return []
|
||||
|
||||
// 通过 members 字段过滤
|
||||
return pb.collection('groups').getList(1, 50, {
|
||||
filter: `members ~ "${user.id}"`
|
||||
}).then(res => res.items as unknown as Group[])
|
||||
}
|
||||
|
||||
// 获取群组详情
|
||||
export async function getGroup(groupId: string): Promise<Group> {
|
||||
return pb.collection('groups').getOne(groupId, {
|
||||
expand: 'members'
|
||||
}) as unknown as Group
|
||||
}
|
||||
|
||||
// 加入群组
|
||||
export async function joinGroup(groupId: string) {
|
||||
const user = pb.authStore.model
|
||||
if (!user) throw new Error('未登录')
|
||||
|
||||
const group = await pb.collection('groups').getOne(groupId)
|
||||
const members = group.members as string[]
|
||||
|
||||
if (members.length >= group.maxMembers) {
|
||||
throw new Error('群组已满')
|
||||
}
|
||||
|
||||
if (members.includes(user.id)) {
|
||||
throw new Error('已是群组成员')
|
||||
}
|
||||
|
||||
return pb.collection('groups').update(groupId, {
|
||||
members: [...members, user.id]
|
||||
})
|
||||
}
|
||||
|
||||
// 退出群组
|
||||
export async function leaveGroup(groupId: string) {
|
||||
const user = pb.authStore.model
|
||||
if (!user) throw new Error('未登录')
|
||||
|
||||
const group = await pb.collection('groups').getOne(groupId)
|
||||
|
||||
if (group.owner === user.id) {
|
||||
throw new Error('群主不能退出群组,请先转让群主或解散群组')
|
||||
}
|
||||
|
||||
const members = group.members as string[]
|
||||
return pb.collection('groups').update(groupId, {
|
||||
members: members.filter(id => id !== user.id)
|
||||
})
|
||||
}
|
||||
|
||||
// 解散群组
|
||||
export async function dissolveGroup(groupId: string) {
|
||||
const user = pb.authStore.model
|
||||
if (!user) throw new Error('未登录')
|
||||
|
||||
const group = await pb.collection('groups').getOne(groupId)
|
||||
|
||||
if (group.owner !== user.id) {
|
||||
throw new Error('只有群主可以解散群组')
|
||||
}
|
||||
|
||||
return pb.collection('groups').delete(groupId)
|
||||
}
|
||||
|
||||
// 订阅群组变更
|
||||
export function subscribeGroup(groupId: string, callback: (group: Group) => void) {
|
||||
return pb.collection('groups').subscribe('*', (payload) => {
|
||||
if (payload.record.id === groupId) {
|
||||
callback(payload.record as unknown as Group)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 获取群组成员
|
||||
export async function getGroupMembers(groupId: string) {
|
||||
const group = await getGroup(groupId)
|
||||
const members = group.members as string[]
|
||||
|
||||
// 批量获取用户信息
|
||||
const users = await pb.collection('users').getList(1, 50, {
|
||||
filter: members.map(id => `id="${id}"`).join(' || ')
|
||||
})
|
||||
|
||||
return users.items
|
||||
}
|
||||
Reference in New Issue
Block a user