Skip to content

Commit

Permalink
new version
Browse files Browse the repository at this point in the history
  • Loading branch information
smallfangqwq committed Jul 15, 2023
1 parent 20a7637 commit ec461d8
Show file tree
Hide file tree
Showing 22 changed files with 4,585 additions and 754 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@
"source-map-support": "^0.5.21",
"uuid": "^9.0.0",
"yargs": "^17.7.2"
}
},
"workspaces": [
"packages/core",
"packages/ui"
]
}
15 changes: 9 additions & 6 deletions packages/core/declare/perm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ export function checkPerm(value: number, model: string, perm: string) {
}

export function perm(model: string, name: string) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function(target: any, methodName: string, descriptor: any) {
return function(target, methodName: string, descriptor) {
descriptor.originalMethodPerm = descriptor.value;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descriptor.value = async function run(args: any) {
descriptor.value = async function run(args) {
let id = args?.id || 0;
if (id === 0) {
id = await token.stripId(args.token);
id = await token.stripId(args.token || this.token);
if (id === -1) {
id = 0;
}
Expand All @@ -137,12 +135,17 @@ export function perm(model: string, name: string) {
id = 0;
}
}
this.id = id;
const defaultValue = permColl[model].default,
guestValue = permColl[model].guest;
let value = 0;
if (id !== 0) {
const dbData = await db.getone('perm', { id });
value = (dbData || {})[model] || defaultValue;
if (dbData[model] === undefined) {
value = defaultValue;
} else {
value = dbData[model];
}
} else {
value = guestValue;
}
Expand Down
26 changes: 20 additions & 6 deletions packages/core/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import KoaStatic from 'awesome-static';
import * as path from 'path';
// import KoaConnect from 'koa-connect';
import { RenderFromPage } from './service/render';
import { perm } from './declare/perm';
import { user } from './model/user';

export const app = new Koa();
const router = new KoaRouter();
Expand All @@ -30,9 +32,11 @@ function transWord(word: string) {

export class Handler {
ctx: KoaContext;
id: number;
@perm('user', 'view')
async get() {
this.ctx.type = 'text/html';
this.ctx.body = await RenderFromPage();
this.ctx.body = await RenderFromPage(await user.getHeader(this.id));
return;
}
}
Expand All @@ -47,6 +51,7 @@ async function handle(ctx: KoaContext, Handler) {
const h = new Handler();
const args = {};
h.ctx = ctx;
h.token = ctx.cookies.get('token');
Object.assign(args, body);
Object.assign(args, ctx.params);
Object.assign(args, ctx.request.query);
Expand All @@ -64,11 +69,20 @@ async function handle(ctx: KoaContext, Handler) {
}
} catch (err) {
if (['perm', 'validation'].includes(err?.errorType)) {
ctx.body = JSON.stringify({
status: 'error',
type: err?.errorType,
param: err?.errorParam
});
console.log(method);
if (method === 'get' && err?.errorType === 'perm') {
ctx.type = 'text/html';
ctx.body = await RenderFromPage({
type: 'back',
template: 'Blocked',
})
} else {
ctx.body = JSON.stringify({
status: 'error',
type: err?.errorType,
param: err?.errorParam
});
}
ctx.response.status = 200;
} else {
console.error(err);
Expand Down
228 changes: 0 additions & 228 deletions packages/core/handler/discuss.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/core/handler/page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { perm } from '../declare/perm';
import { Handler, Route } from '../handle';
import { user } from '../model/user';
// import * as fs from 'fs';
// import * as path from 'path';
import { RenderFromPage } from '../service/render';
Expand All @@ -8,7 +9,7 @@ class MainPageHandler extends Handler {
@perm('user', 'view')
async get() {
this.ctx.type = 'text/html';
this.ctx.body = await RenderFromPage();
this.ctx.body = await RenderFromPage(await user.getHeader(this.id));
return;
}
}
Expand Down
Loading

0 comments on commit ec461d8

Please sign in to comment.