update
This commit is contained in:
39
src/middleware/index.ts
Normal file
39
src/middleware/index.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { getHost } from "../../utils/host";
|
||||
import { websiteConfig } from "../../utils/rpc";
|
||||
|
||||
export async function onRequest (context: any, next: any) {
|
||||
|
||||
// 拦截一个请求里的数据
|
||||
// 可选地修改 `locals` 中的属性
|
||||
const host = await getHost(context.request);
|
||||
|
||||
if (host == 'www.zhishihuisheng.com' || host == 'zhishihuisheng.com') {
|
||||
return next();
|
||||
}
|
||||
|
||||
|
||||
// 防止无线跳转
|
||||
if (context.locals.is_write) {
|
||||
return next();
|
||||
}
|
||||
|
||||
if (!host) {
|
||||
context.locals.is_write = 1;
|
||||
return context.rewrite('/404');
|
||||
}
|
||||
|
||||
const {code, data} = await websiteConfig(host);
|
||||
if (!data || code !== 0) {
|
||||
context.locals.is_write = 1;
|
||||
return context.rewrite('/404');
|
||||
}
|
||||
|
||||
if (!context.locals.is_write) {
|
||||
context.locals.is_write = 1;
|
||||
context.locals.template_number = 1;
|
||||
context.locals.template_config = data;
|
||||
return context.rewrite('/template/' + context.locals.template_number + context.url.pathname);
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
Reference in New Issue
Block a user