增加一次性群邀请链接和二维码

This commit is contained in:
xhgit committed 2026-11-15 18:02:28 +08:00
1 parent dfeb5bfefa
commit 5ec9c81f90
18 files changed
+727 -14

No files matched your search

+2 -1
View File
@@ -2,11 +2,12 @@
import { watch } from 'vue'
import { useRouter } from 'vue-router'
import { useSession } from './session'
import { incomingGroupInvite } from './chat/group-invites'
const session = useSession()
const router = useRouter()
watch(() => session.identity, (identity) => {
void router.replace(identity ? '/devices' : '/login')
void router.replace(identity ? incomingGroupInvite.value ? '/groups' : '/devices' : '/login')
})
</script>
+77
View File
@@ -0,0 +1,77 @@
<script setup lang="ts">
import { nextTick, ref } from 'vue'
import { useSession } from '../session'
import { groupInviteURL } from './group-invites'
const props = defineProps<{ groupId: string }>()
const session = useSession()
const invite = ref<{ code: string; expiresAt: string }>()
const canvas = ref<HTMLCanvasElement>()
const link = ref<HTMLInputElement>()
const busy = ref(false)
const status = ref('')
const error = ref('')
async function change(revoke: boolean) {
busy.value = true
error.value = ''
status.value = ''
try {
const path = '/groups/' + encodeURIComponent(props.groupId) + '/invite-link'
if (revoke) {
await session.authorized(path, 'DELETE')
invite.value = undefined
status.value = '已撤销邀请链接'
} else {
const next = await session.authorized<{ code: string; expiresAt: string }>(path, 'POST')
invite.value = next
await nextTick()
const image = canvas.value
if (!image) return
const { toCanvas } = await import('qrcode')
await toCanvas(image, groupInviteURL(next.code), { margin: 4, scale: 5 })
}
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '群邀请操作失败'
} finally {
busy.value = false
}
}
async function copy() {
if (!link.value) return
try {
await navigator.clipboard.writeText(link.value.value)
status.value = '已复制邀请链接'
} catch {
link.value.select()
status.value = '链接已选中,可手动复制'
}
}
</script>
<template>
<details class="group-invite-link">
<summary>群邀请链接和二维码</summary>
<p class="muted">链接有效期 24 小时,可使用一次。生成新链接会使旧链接失效。</p>
<div class="message-actions">
<button type="button" class="plain" :disabled="busy" @click="change(false)">{{ invite ? '重新生成邀请链接' : '生成邀请链接' }}</button>
<button type="button" class="plain" :disabled="busy" @click="change(true)">撤销邀请链接</button>
</div>
<div v-if="invite" class="invite-output">
<canvas :key="invite.code" ref="canvas" role="img" aria-label="群邀请二维码" />
<label>邀请链接<input ref="link" :value="groupInviteURL(invite.code)" readonly></label>
<button type="button" class="plain" @click="copy">复制邀请链接</button>
<small>有效至 {{ new Date(invite.expiresAt).toLocaleString() }}</small>
</div>
<p v-if="status" role="status">{{ status }}</p>
<p v-if="error" class="error" role="alert">{{ error }}</p>
</details>
</template>
<style scoped>
.group-invite-link { padding-block: .65rem; }
.invite-output { display: grid; gap: .45rem; justify-items: start; }
.invite-output label { width: 100%; }
.invite-output canvas { max-width: 100%; height: auto; }
</style>
+29
View File
@@ -0,0 +1,29 @@
import { ref } from 'vue'
const inviteCode = /^[A-Za-z0-9_-]{43}$/
export const incomingGroupInvite = ref('')
function serviceURL() {
return new URL(window.xhchat?.apiBase ?? location.origin)
}
export function groupInviteURL(code: string) {
const url = serviceURL()
url.hash = '/groups?invite=' + code
return url.href
}
export function parseGroupInvite(value: string) {
const input = value.trim()
if (inviteCode.test(input)) return input
try {
const url = new URL(input)
if (url.origin !== serviceURL().origin || url.username || url.password) return ''
const [route, query] = url.hash.slice(1).split('?')
const code = new URLSearchParams(query).get('invite') ?? ''
if (route === '/groups' && inviteCode.test(code)) return code
} catch {
return ''
}
return ''
}
+5
View File
@@ -9,6 +9,7 @@ import ChatView from './views/ChatView.vue'
import GroupView from './views/GroupView.vue'
import Workspace from './views/Workspace.vue'
import { useSession } from './session'
import { incomingGroupInvite, parseGroupInvite } from './chat/group-invites'
import './style.css'
const pinia = createPinia()
@@ -29,6 +30,10 @@ const router = createRouter({
router.beforeEach((to) => {
const signedIn = Boolean(useSession(pinia).identity)
if (to.path === '/groups' && typeof to.query.invite === 'string') {
incomingGroupInvite.value = parseGroupInvite(to.query.invite)
return { path: signedIn ? '/groups' : '/login', replace: true }
}
if (!signedIn && to.path !== '/login') return '/login'
if (signedIn && to.path === '/login') return '/devices'
})
+27 -2
View File
@@ -5,6 +5,8 @@ import { useSession } from '../session'
import { APIError } from '../api'
import type { Contact } from '../contacts'
import GroupChat from '../chat/GroupChat.vue'
import GroupInviteLink from '../chat/GroupInviteLink.vue'
import { incomingGroupInvite, parseGroupInvite } from '../chat/group-invites'
import type { GroupPreferences, GroupSummary, ListedGroup } from '../chat/group-chat'
type Group = GroupSummary
@@ -20,6 +22,7 @@ const term = ref('')
const name = ref('')
const nextName = ref('')
const invite = ref('')
const inviteCode = ref('')
const busy = ref(false)
const error = ref('')
const confirmation = ref<{ action: string; target?: string; label: string }>()
@@ -84,10 +87,10 @@ function reload() {
function queueRefresh() {
refreshPending = true
if (busy.value || refreshTimer) return
if (busy.value || confirmation.value || refreshTimer) return
refreshTimer = setTimeout(() => {
refreshTimer = undefined
reload()
if (!confirmation.value) reload()
}, 250)
}
@@ -120,6 +123,17 @@ function setPreference(key: keyof GroupPreferences) {
})
}
function redeemInvite() {
const code = parseGroupInvite(inviteCode.value)
if (!code) { error.value = '请输入当前 XHChat 服务的邀请链接或邀请码'; return }
void run(async () => {
const { groupId } = await session.authorized<{ groupId: string }>('/group-invites/redeem', 'POST', { code })
inviteCode.value = ''
await router.push('/groups/' + groupId)
await refresh()
})
}
watch(id, () => {
detail.value = undefined
confirmation.value = undefined
@@ -129,7 +143,13 @@ watch(id, () => {
else reload()
})
watch(() => session.groupListChanges, queueRefresh)
watch(confirmation, (value) => { if (!value && refreshPending) queueRefresh() })
watch(() => session.groupChange, queueRefresh)
watch(incomingGroupInvite, (code) => {
if (!code) return
inviteCode.value = code
incomingGroupInvite.value = ''
}, { immediate: true })
watch(() => session.connection, (connection) => { if (connection === '实时连接已建立') queueRefresh() })
onMounted(() => {
reload()
@@ -152,6 +172,10 @@ onBeforeUnmount(() => {
<label>群名称<input v-model="name" required maxlength="64"></label>
<button class="plain" :disabled="busy || !name.trim()">创建群组</button>
</form>
<form class="contact-search" @submit.prevent="redeemInvite">
<label>群邀请链接或邀请码<input v-model="inviteCode" maxlength="2048" autocomplete="off" spellcheck="false"></label>
<button type="submit" class="plain" :disabled="busy || !inviteCode.trim()">领取群邀请</button>
</form>
<label>查找群组<input v-model="term" type="search" maxlength="64"></label>
<RouterLink v-for="group in visible" :key="group.id" class="contact-item" :to="'/groups/' + group.id">
<span>
@@ -181,6 +205,7 @@ onBeforeUnmount(() => {
<button type="button" class="plain" :aria-pressed="detail.muted" :disabled="busy" @click="setPreference('muted')">群聊免打扰</button>
</div>
<GroupChat v-if="session.identity" :key="detail.id" :group="detail" @seen="queueRefresh" />
<GroupInviteLink v-if="detail.role === 'owner'" :key="detail.id" :group-id="detail.id" />
<form v-if="manage" class="contact-preferences" @submit.prevent="change('rename', undefined, nextName)">
<label>修改群名称<input v-model="nextName" required maxlength="64"></label>
<button class="plain" :disabled="busy || !nextName.trim() || nextName.trim() === detail.name">保存群名称</button>