feat: migrate web preview to Next.js
Verify mini program and deploy preview / verify-deploy (push) Successful in 3m30s

This commit is contained in:
2026-09-20 14:22:47 +08:00
parent 36aee09dfa
commit b5ee950142
40 changed files with 6897 additions and 1362 deletions
+34
View File
@@ -0,0 +1,34 @@
'use client';
export async function apiRequest(path, options = {}) {
const token = typeof window === 'undefined' ? null : localStorage.getItem('pk_token');
const headers = { Accept: 'application/json', ...(options.headers || {}) };
if (options.body !== undefined) headers['Content-Type'] = 'application/json';
if (token) headers.Authorization = `Bearer ${token}`;
const response = await fetch(path, {
method: options.method || 'GET',
headers,
body: options.body === undefined ? undefined : JSON.stringify(options.body),
cache: 'no-store'
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
const detail = typeof data.detail === 'string' ? data.detail : '请求失败';
throw new Error(detail);
}
return data;
}
export async function ensureLogin() {
if (localStorage.getItem('pk_token')) return;
let previewId = localStorage.getItem('pk_preview_id');
if (!previewId) {
previewId = `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`;
localStorage.setItem('pk_preview_id', previewId);
}
const session = await apiRequest('/api/v1/auth/xhs', {
method: 'POST',
body: { app_key: 'hub', code: `dev:web-${previewId}` }
});
localStorage.setItem('pk_token', session.token);
}
+53
View File
@@ -0,0 +1,53 @@
export function buildMatches(pool) {
const matches = [];
for (let index = 0; index < pool.length; index += 2) {
matches.push({
left: pool[index],
right: pool[index + 1] || null,
winner: pool[index + 1] ? null : pool[index]
});
}
return matches;
}
export function createBracket(candidates) {
return {
round: 1,
pool: [...candidates],
matches: buildMatches(candidates),
eliminated: [],
decisions: [],
offset: 0
};
}
export function chooseMatch(state, matchIndex, side) {
const match = state.matches[matchIndex];
if (!match?.right || match.winner) return { state, finished: false };
const winner = side === 'right' ? match.right : match.left;
const loser = side === 'right' ? match.left : match.right;
const matches = state.matches.map((item, index) =>
index === matchIndex ? { ...item, winner } : item
);
let next = {
...state,
matches,
eliminated: [loser, ...state.eliminated],
decisions: [...state.decisions, {
round: state.round,
left_id: match.left.id,
right_id: match.right.id,
winner_id: winner.id
}]
};
if (matches.every(item => item.winner)) {
const pool = matches.map(item => item.winner);
if (pool.length === 1) return { state: { ...next, pool }, finished: true };
next = { ...next, round: state.round + 1, pool, matches: buildMatches(pool), offset: 0 };
}
return { state: next, finished: false };
}
export function firstOpenMatch(state) {
return state.matches.findIndex(match => match.right && !match.winner);
}