238 lines
8.5 KiB
TypeScript
238 lines
8.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
||
import { getPublicAppOrigin } from '@/lib/public-app-url'
|
||
import { consumeDingtalkSessionTicket } from '@/lib/dingtalk-session-ticket'
|
||
|
||
export const runtime = 'nodejs'
|
||
export const dynamic = 'force-dynamic'
|
||
|
||
const SURVEY_OPENING_PAGE_STYLE = `
|
||
html,body{margin:0;min-height:100%;background:#f7fbff;color:#111827;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}
|
||
body{min-height:100vh;min-height:100dvh;overflow:hidden}
|
||
.topbar{height:56px;display:flex;align-items:center;justify-content:space-between;padding:0 20px;color:#fff;background:linear-gradient(90deg,#1677ff,#4f9cff);box-shadow:0 10px 24px rgba(22,119,255,.18)}
|
||
.topbar span:first-child{font-size:28px;line-height:1}
|
||
.topbar span:nth-child(2){font-size:15px;font-weight:600}
|
||
.topbar span:last-child{font-size:20px;line-height:1}
|
||
main{box-sizing:border-box;width:min(390px,100%);margin:0 auto;padding:48px 28px 0}
|
||
.brand{text-align:center}
|
||
.brand img{width:42px;height:42px;border-radius:10px;object-fit:cover}
|
||
.brand h1{margin:12px 0 0;font-size:23px;font-weight:600;line-height:1}
|
||
.brand p{margin:8px 0 0;color:#8a94a6;font-size:14px;font-weight:500}
|
||
.open-pill{min-height:58px;margin-top:40px;padding:0 20px;display:flex;align-items:center;justify-content:space-between;border-radius:999px;background:#fff;box-shadow:0 12px 30px rgba(15,23,42,.12)}
|
||
.open-pill strong{display:flex;align-items:center;gap:12px;font-size:16px;font-weight:600}
|
||
.open-pill em{font-style:normal;color:#1677ff;font-size:15px;font-weight:600}
|
||
.spinner{width:28px;height:28px;border:4px solid #d7e8ff;border-top-color:#1677ff;border-radius:999px;animation:spin .85s linear infinite}
|
||
#message{margin:28px 0 0;text-align:center;color:#8a94a6;font-size:13px;font-weight:500;line-height:1.7}
|
||
.ghost-card{margin-top:44px;padding:20px;border-radius:18px;background:rgba(255,255,255,.9);box-shadow:0 18px 42px rgba(15,23,42,.08)}
|
||
.ghost-title{font-size:15px;font-weight:600;color:#7b8494}
|
||
.ghost-line{height:12px;border-radius:999px;background:#eef3f9}
|
||
.ghost-line.one{margin-top:24px}
|
||
.ghost-line.two{width:74%;margin-top:12px}
|
||
.ghost-list{margin-top:32px;display:grid;gap:20px}
|
||
.ghost-row{display:flex;align-items:center;gap:12px}
|
||
.ghost-dot{width:16px;height:16px;border-radius:999px;border:1px solid #e3eaf3}
|
||
.ghost-row .ghost-line{flex:1;margin:0}
|
||
.ghost-row:last-child .ghost-line{flex:.67}
|
||
@keyframes spin{to{transform:rotate(360deg)}}
|
||
`
|
||
|
||
const SURVEY_OPENING_PAGE_MARKUP = `
|
||
<div class="topbar"><span>‹</span><span>素材库</span><span>•••</span></div>
|
||
<main>
|
||
<div class="brand">
|
||
<img src="/api/favicon" alt="素材库" />
|
||
<h1>素材库</h1>
|
||
<p>内部素材协作平台</p>
|
||
</div>
|
||
<div class="open-pill">
|
||
<strong><span class="spinner" aria-hidden="true"></span>正在打开问卷</strong>
|
||
<em>马上进入</em>
|
||
</div>
|
||
<p id="message">正在打开问卷...</p>
|
||
<div class="ghost-card" aria-hidden="true">
|
||
<div class="ghost-title">绩效考核问卷</div>
|
||
<div class="ghost-line one"></div>
|
||
<div class="ghost-line two"></div>
|
||
<div class="ghost-list">
|
||
<div class="ghost-row"><span class="ghost-dot"></span><span class="ghost-line"></span></div>
|
||
<div class="ghost-row"><span class="ghost-dot"></span><span class="ghost-line"></span></div>
|
||
<div class="ghost-row"><span class="ghost-dot"></span><span class="ghost-line"></span></div>
|
||
</div>
|
||
</div>
|
||
</main>`
|
||
|
||
function stringifyForInlineScript(value: string) {
|
||
return JSON.stringify(value).replace(/</g, '\\u003c')
|
||
}
|
||
|
||
function buildPublicUrl(path: string, request?: NextRequest) {
|
||
try {
|
||
return new URL(path, getPublicAppOrigin()).toString()
|
||
} catch {
|
||
return new URL(path, request?.url || 'https://sc.jiansun.vip').toString()
|
||
}
|
||
}
|
||
|
||
function appendSetCookies(target: NextResponse, cookies: string[]) {
|
||
const cookieNames: string[] = []
|
||
|
||
for (const cookie of cookies) {
|
||
target.headers.append('set-cookie', cookie)
|
||
const cookieName = String(cookie).split('=', 1)[0]?.trim()
|
||
if (cookieName) {
|
||
cookieNames.push(cookieName)
|
||
}
|
||
}
|
||
|
||
return cookieNames
|
||
}
|
||
|
||
function getRequestHeaderSnapshot(request: NextRequest) {
|
||
return {
|
||
host: request.headers.get('host') || '',
|
||
forwardedHost: request.headers.get('x-forwarded-host') || '',
|
||
forwardedProto: request.headers.get('x-forwarded-proto') || '',
|
||
}
|
||
}
|
||
|
||
function createErrorPage(message: string, status = 410) {
|
||
const safeMessage = stringifyForInlineScript(message)
|
||
return new NextResponse(
|
||
`<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<title>无法打开问卷</title>
|
||
<meta http-equiv="Cache-Control" content="no-store" />
|
||
<style>
|
||
body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;background:#0f172a;color:#f8fafc;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}
|
||
main{width:min(420px,calc(100vw - 48px));padding:32px 24px;text-align:center}
|
||
h1{margin:0 0 12px;font-size:22px}
|
||
p{margin:0;color:#cbd5e1;line-height:1.7}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<main>
|
||
<h1>无法打开问卷</h1>
|
||
<p id="message"></p>
|
||
</main>
|
||
<script>
|
||
document.getElementById('message').textContent = ${safeMessage};
|
||
</script>
|
||
</body>
|
||
</html>`,
|
||
{
|
||
status,
|
||
headers: {
|
||
'Content-Type': 'text/html; charset=utf-8',
|
||
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
|
||
},
|
||
},
|
||
)
|
||
}
|
||
|
||
function createSessionRedirectResponse({
|
||
request,
|
||
redirectPath,
|
||
cookies,
|
||
}: {
|
||
request: NextRequest
|
||
redirectPath: string
|
||
cookies: string[]
|
||
}) {
|
||
const redirectUrl = buildPublicUrl(redirectPath, request)
|
||
const response = new NextResponse(
|
||
`<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<title>正在打开问卷</title>
|
||
<meta http-equiv="Cache-Control" content="no-store" />
|
||
<style>${SURVEY_OPENING_PAGE_STYLE}</style>
|
||
</head>
|
||
<body>
|
||
${SURVEY_OPENING_PAGE_MARKUP}
|
||
<script>
|
||
(function () {
|
||
var targetUrl = ${stringifyForInlineScript(redirectUrl)};
|
||
var message = document.getElementById('message');
|
||
var attempts = 0;
|
||
var maxAttempts = 12;
|
||
|
||
function setMessage(text) {
|
||
if (message) message.textContent = text;
|
||
}
|
||
|
||
function wait(ms) {
|
||
return new Promise(function (resolve) {
|
||
window.setTimeout(resolve, ms);
|
||
});
|
||
}
|
||
|
||
async function confirmSessionThenEnter() {
|
||
while (attempts < maxAttempts) {
|
||
attempts += 1;
|
||
setMessage('正在打开问卷...');
|
||
|
||
try {
|
||
var response = await fetch('/api/auth/session?source=dingtalk_session_ticket&attempt=' + attempts + '&_=' + Date.now(), {
|
||
cache: 'no-store',
|
||
credentials: 'include'
|
||
});
|
||
var data = response.ok ? await response.json().catch(function () { return null; }) : null;
|
||
|
||
if (data && data.user) {
|
||
setMessage('正在打开问卷...');
|
||
window.location.replace(targetUrl);
|
||
return;
|
||
}
|
||
} catch (error) {
|
||
// 下一轮继续确认,避免钉钉内置容器偶发网络抖动导致误判。
|
||
}
|
||
|
||
await wait(450);
|
||
}
|
||
|
||
setMessage('暂时无法打开问卷,请关闭当前页面后从最新通知重新进入。');
|
||
}
|
||
|
||
window.setTimeout(confirmSessionThenEnter, 250);
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>`,
|
||
{
|
||
status: 200,
|
||
headers: {
|
||
'Content-Type': 'text/html; charset=utf-8',
|
||
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
|
||
Pragma: 'no-cache',
|
||
Expires: '0',
|
||
},
|
||
},
|
||
)
|
||
|
||
const cookieNames = appendSetCookies(response, cookies)
|
||
console.log('[Dingtalk H5] 通过一次性票据写入会话Cookie并返回落地页:', {
|
||
redirectPath,
|
||
redirectUrl,
|
||
cookieNames,
|
||
requestHeaders: getRequestHeaderSnapshot(request),
|
||
})
|
||
return response
|
||
}
|
||
|
||
export async function GET(request: NextRequest) {
|
||
const ticket = consumeDingtalkSessionTicket(request.nextUrl.searchParams.get('ticket'))
|
||
if (!ticket) {
|
||
return createErrorPage('暂时无法打开问卷,请从最新通知重新进入。')
|
||
}
|
||
|
||
return createSessionRedirectResponse({
|
||
request,
|
||
redirectPath: ticket.redirectPath,
|
||
cookies: ticket.cookies,
|
||
})
|
||
}
|