交付双端基础、实时联系人和隔离浏览器

This commit is contained in:
xhgit committed 2026-11-13 18:03:32 +08:00
1 parent 43687d5e7e
commit 67fef3ff87
59 files changed
+5094 -1

No files matched your search

+55
View File
@@ -0,0 +1,55 @@
<script setup lang="ts">
import { ref } from 'vue'
import { platform } from '../identity'
import { useSession } from '../session'
const session = useSession()
const mode = ref<'login' | 'register'>('login')
const username = ref('')
const password = ref('')
const deviceName = ref(platform === 'windows' ? 'Windows 电脑' : '网页浏览器')
const error = ref('')
const busy = ref(false)
function changeMode(value: 'login' | 'register') {
mode.value = value
error.value = ''
password.value = ''
}
async function submit() {
busy.value = true
error.value = ''
try {
await session.authenticate(mode.value, username.value, password.value, deviceName.value)
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '操作失败,请稍后重试'
} finally {
busy.value = false
}
}
</script>
<template>
<main class="auth-shell">
<aside class="auth-brand">
<div class="brand"><span class="mark" aria-hidden="true">X</span>XHChat</div>
<div class="brand-art" aria-hidden="true"><span></span><span></span><span></span><span></span></div>
<p class="platform">{{ platform === 'windows' ? 'Windows 客户端' : 'Web 客户端' }}</p>
</aside>
<section class="auth-content">
<form class="auth-form" @submit.prevent="submit">
<div class="tabs" aria-label="账号操作">
<button type="button" :disabled="busy" :aria-pressed="mode === 'login'" @click="changeMode('login')">登录</button>
<button type="button" :disabled="busy" :aria-pressed="mode === 'register'" @click="changeMode('register')">注册</button>
</div>
<h1>{{ mode === 'login' ? '登录 XHChat' : '创建 XHChat 账号' }}</h1>
<label>用户名<input v-model="username" autocomplete="username" maxlength="32" pattern="[A-Za-z0-9_]{3,32}" placeholder="3–32 位字母、数字或下划线" required></label>
<label>密码<input v-model="password" :autocomplete="mode === 'login' ? 'current-password' : 'new-password'" type="password" minlength="12" maxlength="256" placeholder="至少 12 个字符" required></label>
<label>设备名称<input v-model="deviceName" maxlength="64" autocomplete="off" required></label>
<p v-if="error" class="error" role="alert">{{ error }}</p>
<button class="primary" :disabled="busy">{{ busy ? '正在验证设备…' : mode === 'login' ? '登录' : '创建账号' }}</button>
</form>
</section>
</main>
</template>
+135
View File
@@ -0,0 +1,135 @@
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import { useSession } from '../session'
import type { Contact, ContactAction } from '../contacts'
const session = useSession()
const list = ref<Contact[]>([])
const search = ref('')
const result = ref<Contact>()
const selected = ref('')
const error = ref('')
const busy = ref(false)
const alias = ref('')
const category = ref('')
const detail = computed(() => list.value.find((contact) => contact.id === selected.value) ??
(result.value?.id === selected.value ? result.value : undefined))
const labels = { none: '可添加', incoming: '收到申请', outgoing: '等待接受', friend: '联系人', blocked: '已拉黑' }
const sections = [
{ state: 'incoming', label: '好友申请' },
{ state: 'friend', label: '好友' },
{ state: 'outgoing', label: '发出的申请' },
{ state: 'blocked', label: '黑名单' },
] as const
const pendingAction = ref<'delete' | 'block'>()
let refreshVersion = 0
async function refresh() {
const version = ++refreshVersion
const next = await session.contacts()
if (version === refreshVersion) list.value = next
}
async function operation(callback: () => Promise<void>) {
busy.value = true
error.value = ''
try {
await callback()
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '操作失败,请稍后重试'
} finally {
busy.value = false
}
}
function refreshQuietly() {
void refresh().catch((cause: unknown) => {
error.value = cause instanceof Error ? cause.message : '无法加载联系人'
})
}
onMounted(refreshQuietly)
watch(() => [session.contactChanges, session.connection], ([, connection]) => {
if (connection === '实时连接已建立') refreshQuietly()
})
watch(selected, () => {
alias.value = detail.value?.alias ?? ''
category.value = detail.value?.category ?? ''
pendingAction.value = undefined
})
async function find() {
await operation(async () => {
const contact = await session.searchContact(search.value.trim())
result.value = contact
selected.value = contact.id
})
}
async function change(action: ContactAction) {
const contact = detail.value
if (!contact) return
await operation(async () => {
await session.changeContact(contact.id, action, alias.value, category.value)
pendingAction.value = undefined
if (['cancel', 'reject', 'delete', 'unblock'].includes(action)) {
result.value = undefined
selected.value = ''
}
await refresh()
})
}
</script>
<template>
<section class="contacts-layout">
<div class="contact-list">
<form class="contact-search" @submit.prevent="find">
<label>按用户名查找<input v-model="search" required maxlength="32" placeholder="输入完整用户名"></label>
<button class="plain" :disabled="busy">查找</button>
</form>
<section v-for="section in sections" :key="section.state" class="contact-group">
<template v-if="list.some((contact) => contact.state === section.state)">
<h2>{{ section.label }}</h2>
<button v-for="contact in list.filter((item) => item.state === section.state)" :key="contact.id"
type="button" class="contact-item" :aria-pressed="selected === contact.id" @click="selected = contact.id">
<span class="avatar" aria-hidden="true">{{ contact.username[0]?.toUpperCase() }}</span>
<span><strong>{{ contact.alias || contact.username }}</strong><small>{{ contact.category || '@' + contact.username }}</small></span>
</button>
</template>
</section>
<p v-if="!list.length" class="muted">还没有联系人</p>
</div>
<section class="contact-detail">
<p v-if="error" class="error" role="alert">{{ error }}</p>
<template v-if="detail">
<div class="contact-heading">
<span class="avatar" aria-hidden="true">{{ detail.username[0]?.toUpperCase() }}</span>
<div><h1>{{ detail.alias || detail.username }}</h1><small>@{{ detail.username }} · {{ labels[detail.state] }}</small></div>
</div>
<div v-if="detail.state === 'incoming'" class="contact-actions">
<button class="plain" :disabled="busy" @click="change('accept')">接受申请</button>
<button class="plain" :disabled="busy" @click="change('reject')">拒绝申请</button>
</div>
<button v-if="detail.state === 'none'" class="primary" :disabled="busy" @click="change('request')">添加好友</button>
<button v-if="detail.state === 'outgoing'" class="plain" :disabled="busy" @click="change('cancel')">取消申请</button>
<button v-if="detail.state === 'blocked'" class="plain" :disabled="busy" @click="change('unblock')">解除拉黑</button>
<form v-if="detail.state === 'friend'" class="contact-preferences" @submit.prevent="change('preferences')">
<label>备注<input v-model="alias" maxlength="64"></label>
<label>分组<input v-model="category" maxlength="32"></label>
<button class="plain" :disabled="busy">保存</button>
</form>
<div v-if="detail.state !== 'blocked'" class="contact-actions">
<button v-if="detail.state === 'friend'" class="plain danger" :disabled="busy" @click="pendingAction = 'delete'">删除好友</button>
<button class="plain danger" :disabled="busy" @click="pendingAction = 'block'">拉黑</button>
</div>
<section v-if="pendingAction" class="confirmation" aria-label="确认联系人操作">
<p>{{ pendingAction === 'block' ? '拉黑将移除好友关系并阻止新的好友申请。' : '删除后,双方的好友关系将解除。' }}</p>
<button class="plain" @click="pendingAction = undefined">取消</button>
<button class="plain danger" :disabled="busy" @click="change(pendingAction)">确认</button>
</section>
</template>
<p v-else-if="!error" class="muted">选择联系人,或按用户名查找。</p>
</section>
</section>
</template>
+62
View File
@@ -0,0 +1,62 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useSession } from '../session'
import type { Device } from '../session'
const session = useSession()
const devices = ref<Device[]>([])
const error = ref('')
const busy = ref(false)
const target = ref<Device>()
const dates = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'medium', timeStyle: 'short' })
async function perform(operation: () => Promise<void>) {
busy.value = true
error.value = ''
try {
await operation()
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '操作失败,请稍后重试'
} finally {
busy.value = false
}
}
const refresh = () => perform(async () => { devices.value = await session.devices() })
onMounted(refresh)
async function revoke() {
if (!target.value) return
const id = target.value.id
target.value = undefined
await perform(async () => {
await session.revoke(id)
devices.value = await session.devices()
})
}
</script>
<template>
<section v-if="session.identity" class="device-page">
<header class="page-header"><h1>设备管理</h1><button class="plain" :disabled="busy" @click="refresh">刷新</button></header>
<div class="device-content">
<p v-if="error" class="error" role="alert">{{ error }}</p>
<p v-if="busy && !devices.length" role="status">正在载入设备…</p>
<article v-for="device in devices" :key="device.id" class="device-row">
<span class="device-icon" aria-hidden="true">▣</span>
<div class="device-meta">
<div><strong>{{ device.name }}</strong><span v-if="device.id === session.identity.device.id" class="tag">当前设备</span></div>
<small>{{ device.platform === 'windows' ? 'Windows · 桌面端' : 'Web · 浏览器' }} · {{ device.signedIn ? '已登录' : '已退出' }}</small>
<small>首次登录 {{ dates.format(new Date(device.createdAt)) }} · 最近活动 {{ dates.format(new Date(device.lastActiveAt)) }}</small>
</div>
<span v-if="device.revokedAt" class="muted">已撤销</span>
<button v-else-if="device.id !== session.identity.device.id" class="plain danger" :disabled="busy" @click="target = device">撤销</button>
</article>
<section v-if="target" class="confirmation" aria-label="确认撤销">
<p>撤销「{{ target.name }}」后,该设备的登录会话将结束。</p>
<button class="plain" @click="target = undefined">取消</button>
<button class="plain danger" @click="revoke">确认撤销</button>
</section>
</div>
</section>
</template>
+49
View File
@@ -0,0 +1,49 @@
<script setup lang="ts">
import { ref } from 'vue'
import { useSession } from '../session'
const session = useSession()
const error = ref('')
const busy = ref(false)
const browser = window.xhchat?.openBrowser
async function act(action: () => Promise<void>) {
busy.value = true
error.value = ''
try {
await action()
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '操作失败,请稍后重试'
} finally {
busy.value = false
}
}
</script>
<template>
<main v-if="session.identity" class="workspace">
<nav class="rail" aria-label="主导航">
<RouterLink class="mark" to="/devices" aria-label="XHChat">X</RouterLink>
<RouterLink class="rail-link" to="/contacts" title="联系人" aria-label="联系人">人</RouterLink>
<RouterLink class="rail-link" to="/devices" title="设备管理" aria-label="设备管理">▣</RouterLink>
<button v-if="browser" type="button" class="rail-link browser-button" title="浏览器" aria-label="浏览器" @click="act(browser)">◎</button>
</nav>
<aside class="account-panel">
<div class="account-summary">
<span class="avatar" aria-hidden="true">{{ session.identity.user.username[0]?.toUpperCase() }}</span>
<div><strong>{{ session.identity.user.username }}</strong><small>我的账号</small></div>
</div>
<nav class="panel-links" aria-label="工作区">
<RouterLink to="/contacts">联系人</RouterLink>
<RouterLink to="/devices">设备管理</RouterLink>
<button v-if="browser" class="plain mobile-browser" @click="act(browser)">浏览器</button>
</nav>
<div class="account-footer">
<p aria-live="polite"><span class="dot" :class="{ online: session.connection === '实时连接已建立' }"></span>{{ session.connection }}</p>
<p v-if="error" class="error" role="alert">{{ error }}</p>
<button class="plain" :disabled="busy" @click="act(session.logout)">退出登录</button>
</div>
</aside>
<RouterView />
</main>
</template>