fix(island): 合并常驻与自定义岛并统一经典通知和即时课表同步
This commit is contained in:
1 parent
05848570d1
commit
e84e43544a
19 files changed
+1565
-1445
No files matched your search
@@ -1192,9 +1192,121 @@ export async function aboutPageSelfTest(): Promise<SmokeCheckResult> {
|
||||
}
|
||||
}
|
||||
|
||||
/** 本地替换 API,不连接真实服务:覆盖变更推送、重连与迟到响应。 */
|
||||
export async function islandScheduleSyncSelfTest(): Promise<SmokeCheckResult> {
|
||||
const [{ api }, { useAuthStore }, { useAppStore }, { useRealtimeStore }, bridge, { SOCKET_EVENTS }] =
|
||||
await Promise.all([
|
||||
import('../api/http.js'),
|
||||
import('../stores/auth.js'),
|
||||
import('../stores/app.js'),
|
||||
import('../stores/realtime.js'),
|
||||
import('../island/bridge.js'),
|
||||
import('@classhelper/shared'),
|
||||
]);
|
||||
const auth = useAuthStore(),
|
||||
app = useAppStore(),
|
||||
realtime = useRealtimeStore();
|
||||
const token = auth.token,
|
||||
week = app.currentWeek,
|
||||
originalGet = api.get,
|
||||
originalOn = realtime.on;
|
||||
const callbacks = new Map<string, (value: unknown) => void>();
|
||||
const terms: Array<(value: unknown) => void> = [];
|
||||
let name = '变更后课程',
|
||||
calls = 0,
|
||||
delay = false,
|
||||
release: (() => void) | undefined;
|
||||
const snapshot = (courseName: string) => ({
|
||||
days: [
|
||||
{
|
||||
schedules: [
|
||||
{
|
||||
id: 'local-live-course',
|
||||
classId: 'local',
|
||||
courseId: 'local-course',
|
||||
dayOfWeek: new Date().getDay() || 7,
|
||||
startTime: '00:00',
|
||||
endTime: '23:59',
|
||||
weekStart: 1,
|
||||
weekEnd: 40,
|
||||
weekParity: 'ALL',
|
||||
course: { id: 'local-course', name: courseName },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
bridge.stopIslandBridge();
|
||||
const key = `week:${week}`,
|
||||
cached = await cacheGet('schedules', key);
|
||||
try {
|
||||
auth.applyToken('local-smoke-schedule');
|
||||
api.get = <T>(url: string): Promise<T> => {
|
||||
if (url === '/schedules/active-week') {
|
||||
calls++;
|
||||
const data = snapshot(name);
|
||||
if (delay) {
|
||||
delay = false;
|
||||
return new Promise<T>((resolve) => {
|
||||
release = () => resolve(data as T);
|
||||
});
|
||||
}
|
||||
return Promise.resolve(data as T);
|
||||
}
|
||||
// 模拟很慢的学期接口,不能再挡住课表推送。
|
||||
return new Promise<T>((resolve) => terms.push((value) => resolve(value as T)));
|
||||
};
|
||||
realtime.on = (event, handler) => {
|
||||
callbacks.set(event, handler);
|
||||
originalOn(event, handler);
|
||||
};
|
||||
bridge.startIslandBridge();
|
||||
const wait = async (expected: string) => {
|
||||
const deadline = performance.now() + 700;
|
||||
while (performance.now() < deadline) {
|
||||
if (bridge.getIslandSchedules()[0]?.course?.name === expected) return true;
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
}
|
||||
return false;
|
||||
};
|
||||
const started = performance.now();
|
||||
callbacks.get(SOCKET_EVENTS.scheduleUpdated)?.({});
|
||||
const changed = await wait(name),
|
||||
elapsed = performance.now() - started;
|
||||
name = '断线重连课程';
|
||||
callbacks.get('__connected__')?.({});
|
||||
const reconnected = await wait(name);
|
||||
name = '迟到的旧课程';
|
||||
delay = true;
|
||||
const old = bridge.refreshIslandSchedules();
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
name = '最新课程';
|
||||
await bridge.refreshIslandSchedules();
|
||||
release?.();
|
||||
await old;
|
||||
const fresh = bridge.getIslandSchedules()[0]?.course?.name === name;
|
||||
const saved = await cacheGet<Array<{ course?: { name: string } }>>('schedules', key);
|
||||
const cacheFresh = saved?.value[0]?.course?.name === name;
|
||||
return {
|
||||
ok: changed && reconnected && fresh && cacheFresh && calls === 5,
|
||||
detail: `本地推送刷新 ${Math.round(elapsed)}ms,重连=${reconnected},迟到响应隔离=${fresh && cacheFresh},课表请求=${calls}`,
|
||||
};
|
||||
} finally {
|
||||
bridge.stopIslandBridge();
|
||||
api.get = originalGet;
|
||||
realtime.on = originalOn;
|
||||
for (const resolve of terms) resolve({ currentWeek: week, maxWeek: 40 });
|
||||
auth.applyToken(token);
|
||||
app.currentWeek = week;
|
||||
if (cached) await cacheSet('schedules', key, cached.value);
|
||||
else await cacheRemove('schedules', key);
|
||||
}
|
||||
}
|
||||
|
||||
/** 注册到 window,供主进程冒烟脚本调用 */
|
||||
export function registerSmokeHooks(): void {
|
||||
window.__classhelperSmoke__ = {
|
||||
islandScheduleSyncSelfTest,
|
||||
cacheSelfTest,
|
||||
cacheIsolationSelfTest,
|
||||
offlineScenario,
|
||||
|
||||
@@ -23,11 +23,11 @@ import { useRealtimeStore } from '../stores/realtime.js';
|
||||
* 职责:把"当前是否在上课时间段"这件事算出来并同步给主进程 ——
|
||||
* 主进程据此决定通知是"立即弹出"还是"上课期间隐藏、下课后自动弹出"。
|
||||
*
|
||||
* 判定完全基于本地缓存的课表(离线也能算),并每 10 秒复算一次,
|
||||
* 因此下课后最多 10 秒内会自动弹出暂存的通知。
|
||||
* 判定完全基于本地缓存的课表(离线也能算),并每秒复算一次,
|
||||
* 因此下课后最多 1 秒内会自动弹出暂存的通知。
|
||||
*/
|
||||
const TICK_INTERVAL_MS = 10_000;
|
||||
const REFRESH_INTERVAL_MS = 5 * 60_000;
|
||||
const TICK_INTERVAL_MS = 1_000;
|
||||
const REFRESH_INTERVAL_MS = 30_000;
|
||||
|
||||
let schedules: ScheduleDto[] = [];
|
||||
let currentPeriod: ClassPeriod | null = null;
|
||||
@@ -54,27 +54,38 @@ export async function refreshIslandSchedules(): Promise<void> {
|
||||
if (!auth.token) return;
|
||||
const scope = getCacheScope();
|
||||
const classId = auth.classId ?? undefined;
|
||||
await appStore.refreshTerm(classId);
|
||||
if (scope !== getCacheScope() || !auth.token) return;
|
||||
const request = ++scheduleRequest;
|
||||
if (scope !== getCacheScope() || !auth.token || request !== scheduleRequest) return;
|
||||
const week = appStore.currentWeek;
|
||||
const result = await fetchWithCache<ScheduleDto[]>(
|
||||
'schedules',
|
||||
`week:${week}`,
|
||||
async () =>
|
||||
(
|
||||
async () => {
|
||||
const data = (
|
||||
await api.get<ContractActiveClassPlanWeekDto>('/schedules/active-week', { classId, week })
|
||||
).days.flatMap((day) => day.schedules),
|
||||
).days.flatMap((day) => day.schedules);
|
||||
// 网络结果先上岛,IndexedDB 写入不再挡住可见内容;旧请求与切换账号的结果不得回写。
|
||||
if (scope !== getCacheScope() || request !== scheduleRequest || !auth.token)
|
||||
throw new Error('课表刷新已被新请求替代');
|
||||
schedules = data;
|
||||
scheduleWeek = week;
|
||||
recomputeClassState(true);
|
||||
return data;
|
||||
},
|
||||
[],
|
||||
);
|
||||
if (scope !== getCacheScope() || request !== scheduleRequest) return;
|
||||
if (result.error && !result.updatedAt) return;
|
||||
schedules = result.data;
|
||||
scheduleWeek = week;
|
||||
recomputeClassState(true);
|
||||
if (result.fromCache) {
|
||||
schedules = result.data;
|
||||
scheduleWeek = week;
|
||||
recomputeClassState(true);
|
||||
}
|
||||
}
|
||||
|
||||
let sentDay = '';
|
||||
function recomputeClassState(force = false): void {
|
||||
const day = new Date().toDateString();
|
||||
const appStore = useAppStore();
|
||||
const status = resolveClassStatus(schedules, new Date(), scheduleWeek);
|
||||
const nextInClass = status.inClass;
|
||||
@@ -83,7 +94,8 @@ function recomputeClassState(force = false): void {
|
||||
currentPeriod?.endTime === status.current?.endTime;
|
||||
currentPeriod = status.current;
|
||||
|
||||
if (!force && nextInClass === inClass && samePeriod) return;
|
||||
if (!force && day === sentDay && nextInClass === inClass && samePeriod) return;
|
||||
sentDay = day;
|
||||
inClass = nextInClass;
|
||||
|
||||
window.desktop?.islandSetClassState({
|
||||
@@ -329,6 +341,10 @@ export function subscribeIslandMarkAllRead(): void {
|
||||
|
||||
/** 具名的课表变更处理器:匿名函数在停止时无法 off 掉,会随"登出→登录"越挂越多 */
|
||||
function onScheduleUpdated(): void {
|
||||
// 教学周校正并行进行,课表变更不再等待另一个接口;周次改变由 watch 补取。
|
||||
void useAppStore()
|
||||
.refreshTerm(useAuthStore().classId ?? undefined)
|
||||
.catch(() => undefined);
|
||||
void refreshIslandSchedules().catch(() => undefined);
|
||||
}
|
||||
|
||||
@@ -341,14 +357,15 @@ export function startIslandBridge(): void {
|
||||
},
|
||||
);
|
||||
|
||||
void refreshIslandSchedules().catch(() => undefined);
|
||||
onScheduleUpdated();
|
||||
useRealtimeStore().on('__connected__', onScheduleUpdated);
|
||||
|
||||
// 课表变更(教师改课)立即复算,避免"下课后不弹出"或"上课没隐藏"
|
||||
useRealtimeStore().on(SOCKET_EVENTS.scheduleUpdated, onScheduleUpdated);
|
||||
|
||||
tickTimer = setInterval(() => recomputeClassState(), TICK_INTERVAL_MS);
|
||||
refreshTimer = setInterval(() => {
|
||||
void refreshIslandSchedules().catch(() => undefined);
|
||||
onScheduleUpdated();
|
||||
}, REFRESH_INTERVAL_MS);
|
||||
}
|
||||
|
||||
@@ -370,6 +387,8 @@ export function stopIslandBridge(): void {
|
||||
stopWeekWatch = null;
|
||||
|
||||
useRealtimeStore().off(SOCKET_EVENTS.scheduleUpdated, onScheduleUpdated);
|
||||
useRealtimeStore().off('__connected__', onScheduleUpdated);
|
||||
sentDay = '';
|
||||
schedules = [];
|
||||
currentPeriod = null;
|
||||
inClass = false;
|
||||
@@ -381,5 +400,6 @@ export function stopIslandBridge(): void {
|
||||
inClass: false,
|
||||
currentPeriodEnd: null,
|
||||
week: useAppStore().currentWeek,
|
||||
schedules: [],
|
||||
});
|
||||
}
|
||||
@@ -44,12 +44,7 @@ async function changeLayout() {
|
||||
}
|
||||
async function chooseLayout(mode: 'fixed' | 'custom') {
|
||||
layoutMode.value = mode;
|
||||
if (mode === 'fixed') residentEnabled.value = false;
|
||||
await changeLayout();
|
||||
}
|
||||
async function changeResident(value: boolean | string | number) {
|
||||
residentEnabled.value = value === true;
|
||||
if (residentEnabled.value) layoutMode.value = 'custom';
|
||||
residentEnabled.value = mode === 'custom';
|
||||
await changeLayout();
|
||||
}
|
||||
async function changePointerFade(
|
||||
@@ -152,7 +147,7 @@ onMounted(async () => {
|
||||
<div class="page-header">
|
||||
<div>
|
||||
<h2 class="page-title">灵动岛</h2>
|
||||
<p class="page-subtitle">屏幕上方的消息浮窗:外观、停靠位置与动画</p>
|
||||
<p class="page-subtitle">经典岛与自定义岛共用外观、停靠位置、动画与提醒规则</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -175,36 +170,20 @@ onMounted(async () => {
|
||||
@click="chooseLayout('custom')"
|
||||
>
|
||||
<strong>自定义灵动岛</strong>
|
||||
<span>在岛内排列时钟、课程和消息等组件</span>
|
||||
<span>常驻显示时钟、课程等组件,通知沿用经典岛</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="resident-setting">
|
||||
<div>
|
||||
<strong>常驻岛</strong>
|
||||
<p>始终显示自定义内容;收到消息后显示铃铛,点击查看。没有通知时可展开另一套自定义内容。</p>
|
||||
</div>
|
||||
<el-switch :model-value="residentEnabled" aria-label="常驻岛" @change="changeResident" />
|
||||
</div>
|
||||
<div v-if="layoutMode === 'custom'" class="layout-edit-entry">
|
||||
<div>
|
||||
<strong>按你的习惯布置岛内内容</strong>
|
||||
<p>
|
||||
{{
|
||||
residentEnabled
|
||||
? '分别编辑「收起时」与「无通知展开时」的内容,点选组件即可设置。'
|
||||
: '在预览中添加和排列组件,点选组件即可设置;更改自动保存到本机。'
|
||||
}}
|
||||
</p>
|
||||
<p>编辑常态内容与无通知展开内容;通知样式、弹出规则和外观设置与经典岛共用。</p>
|
||||
</div>
|
||||
<el-button type="primary" @click="editLayout">编辑岛内组件</el-button>
|
||||
</div>
|
||||
<div v-if="residentEnabled" class="resident-window-options">
|
||||
<div v-if="layoutMode === 'custom'" class="resident-window-options">
|
||||
<div>
|
||||
<strong>ClassIsland 窗口设置</strong>
|
||||
<p>
|
||||
常驻行基准高度 40 px。指针移入背景后淡化至 5%
|
||||
并穿透,右侧铃铛或展开按钮保持可用;编辑、展开和触摸交互期间暂停避让。
|
||||
</p>
|
||||
<strong>指针避让</strong>
|
||||
<p>指针移入后淡化并穿透,右侧展开入口保持可用;展开和触摸交互期间暂停避让。</p>
|
||||
</div>
|
||||
<label>
|
||||
<span>指针移入淡化</span>
|
||||
@@ -225,87 +204,86 @@ onMounted(async () => {
|
||||
|
||||
<el-card shadow="never">
|
||||
<template #header>
|
||||
<span>{{ layoutMode === 'fixed' ? '外观与位置' : '位置与提醒' }}</span>
|
||||
<span>外观、位置与弹出</span>
|
||||
</template>
|
||||
<el-form label-width="96px" label-position="left">
|
||||
<template v-if="layoutMode === 'fixed'">
|
||||
<el-form-item :label="`高度 ${island.height}px`">
|
||||
<el-slider
|
||||
:model-value="island.height"
|
||||
:min="RANGES.height.min"
|
||||
:max="RANGES.height.max"
|
||||
:step="1"
|
||||
@input="(value: number) => applyIslandAppearance({ height: value })"
|
||||
<p class="text-muted">紧急消息立即展开;课中普通通知暂存,下课后展开。自定义岛可点击铃铛主动查看。</p>
|
||||
<el-form label-width="110px" label-position="left">
|
||||
<el-form-item :label="`${layoutMode === 'custom' ? '通知高度' : '高度'} ${island.height}px`">
|
||||
<el-slider
|
||||
:model-value="island.height"
|
||||
:min="RANGES.height.min"
|
||||
:max="RANGES.height.max"
|
||||
:step="1"
|
||||
@input="(value: number) => applyIslandAppearance({ height: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`${layoutMode === 'custom' ? '通知宽度' : '宽度'} ${island.width}px`">
|
||||
<el-slider
|
||||
:model-value="island.width"
|
||||
:min="RANGES.width.min"
|
||||
:max="RANGES.width.max"
|
||||
:step="2"
|
||||
@input="(value: number) => applyIslandAppearance({ width: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`圆角 ${island.radius}px`">
|
||||
<el-slider
|
||||
:model-value="island.radius"
|
||||
:min="RANGES.radius.min"
|
||||
:max="RANGES.radius.max"
|
||||
:step="1"
|
||||
@input="(value: number) => applyIslandAppearance({ radius: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`不透明度 ${Math.round(island.opacity * 100)}%`">
|
||||
<el-slider
|
||||
:model-value="island.opacity"
|
||||
:min="RANGES.opacity.min"
|
||||
:max="RANGES.opacity.max"
|
||||
:step="0.05"
|
||||
@input="(value: number) => applyIslandAppearance({ opacity: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`字号 ${island.fontSize}px`">
|
||||
<el-slider
|
||||
:model-value="island.fontSize"
|
||||
:min="RANGES.fontSize.min"
|
||||
:max="RANGES.fontSize.max"
|
||||
:step="1"
|
||||
@input="(value: number) => applyIslandAppearance({ fontSize: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`动画速度 ${island.speed.toFixed(1)}x`">
|
||||
<el-slider
|
||||
:model-value="island.speed"
|
||||
:min="RANGES.speed.min"
|
||||
:max="RANGES.speed.max"
|
||||
:step="0.1"
|
||||
@input="(value: number) => applyIslandAppearance({ speed: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="主题色">
|
||||
<el-color-picker
|
||||
:model-value="island.accent"
|
||||
@change="(value: string | null) => applyIslandAppearance({ accent: value ?? '#0a84ff' })"
|
||||
/>
|
||||
<span class="text-muted ml-8">用于高亮、按钮与进度条</span>
|
||||
</el-form-item>
|
||||
<el-form-item label="视觉风格">
|
||||
<el-select
|
||||
:model-value="island.style"
|
||||
style="width: 180px"
|
||||
@change="(value: IslandStyle) => applyIslandAppearance({ style: value })"
|
||||
>
|
||||
<el-option
|
||||
v-for="item in styleOptions"
|
||||
:key="item.value"
|
||||
:label="item.label"
|
||||
:value="item.value"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`宽度 ${island.width}px`">
|
||||
<el-slider
|
||||
:model-value="island.width"
|
||||
:min="RANGES.width.min"
|
||||
:max="RANGES.width.max"
|
||||
:step="2"
|
||||
@input="(value: number) => applyIslandAppearance({ width: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`圆角 ${island.radius}px`">
|
||||
<el-slider
|
||||
:model-value="island.radius"
|
||||
:min="RANGES.radius.min"
|
||||
:max="RANGES.radius.max"
|
||||
:step="1"
|
||||
@input="(value: number) => applyIslandAppearance({ radius: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`不透明度 ${Math.round(island.opacity * 100)}%`">
|
||||
<el-slider
|
||||
:model-value="island.opacity"
|
||||
:min="RANGES.opacity.min"
|
||||
:max="RANGES.opacity.max"
|
||||
:step="0.05"
|
||||
@input="(value: number) => applyIslandAppearance({ opacity: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`字号 ${island.fontSize}px`">
|
||||
<el-slider
|
||||
:model-value="island.fontSize"
|
||||
:min="RANGES.fontSize.min"
|
||||
:max="RANGES.fontSize.max"
|
||||
:step="1"
|
||||
@input="(value: number) => applyIslandAppearance({ fontSize: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item :label="`动画速度 ${island.speed.toFixed(1)}x`">
|
||||
<el-slider
|
||||
:model-value="island.speed"
|
||||
:min="RANGES.speed.min"
|
||||
:max="RANGES.speed.max"
|
||||
:step="0.1"
|
||||
@input="(value: number) => applyIslandAppearance({ speed: value })"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="主题色">
|
||||
<el-color-picker
|
||||
:model-value="island.accent"
|
||||
@change="(value: string | null) => applyIslandAppearance({ accent: value ?? '#0a84ff' })"
|
||||
/>
|
||||
<span class="text-muted ml-8">用于高亮、按钮与进度条</span>
|
||||
</el-form-item>
|
||||
<el-form-item label="视觉风格">
|
||||
<el-select
|
||||
:model-value="island.style"
|
||||
style="width: 180px"
|
||||
@change="(value: IslandStyle) => applyIslandAppearance({ style: value })"
|
||||
>
|
||||
<el-option
|
||||
v-for="item in styleOptions"
|
||||
:key="item.value"
|
||||
:label="item.label"
|
||||
:value="item.value"
|
||||
/>
|
||||
</el-select>
|
||||
<span class="text-muted ml-8">纯黑 / 毛玻璃(半透明)/ 主题色渐变</span>
|
||||
</el-form-item>
|
||||
</template>
|
||||
</el-select>
|
||||
<span class="text-muted ml-8">纯黑 / 毛玻璃(半透明)/ 主题色渐变</span>
|
||||
</el-form-item>
|
||||
<el-form-item label="显示位置">
|
||||
<el-select
|
||||
:model-value="island.position"
|
||||
|
||||
Reference in new issue
Block a user