feat: 站点个性化设置

This commit is contained in:
xiaojunnuo
2024-10-05 01:46:25 +08:00
parent ce9a9862f1
commit 11a9fe9014
57 changed files with 710 additions and 763 deletions
@@ -14,7 +14,9 @@ import { PipelineEntity } from '../modules/pipeline/entity/pipeline.js';
import { mergeConfig } from './loader.js';
import { libServerEntities } from '@certd/lib-server';
import { commercialEntities } from '@certd/commercial-core';
import { tmpdir } from 'node:os';
import { uploadWhiteList, DefaultUploadFileMimeType } from '@midwayjs/upload';
import path from 'path';
const env = process.env.NODE_ENV || 'development';
const development = {
@@ -90,6 +92,23 @@ const development = {
plus: {
serverBaseUrls: ['http://127.0.0.1:11007'],
},
upload: {
// mode: UploadMode, 默认为file,即上传到服务器临时目录,可以配置为 stream
mode: 'file',
// fileSize: string, 最大上传文件大小,默认为 10mb
fileSize: '10mb',
whitelist: uploadWhiteList, //文件扩展名白名单
mimeTypeWhiteList: DefaultUploadFileMimeType, //文件MIME类型白名单
// whitelist: uploadWhiteList.filter(ext => ext !== '.pdf'),
// tmpdir: string,上传的文件临时存储路径
tmpdir: path.join(tmpdir(), 'certd-upload-files'),
// cleanTimeout: number,上传的文件在临时目录中多久之后自动删除,默认为 5 分钟
cleanTimeout: 5 * 60 * 1000,
// base64: boolean,设置原始body是否是base64格式,默认为false,一般用于腾讯云的兼容
base64: false,
// 仅在匹配路径到 /api/upload 的时候去解析 body 中的文件信息
match: /\/api\/basic\/file\/upload/,
},
} as MidwayConfig;
mergeConfig(development, 'development');
@@ -16,6 +16,7 @@ import { ResetPasswdMiddleware } from './middleware/reset-passwd/middleware.js';
import DefaultConfig from './config/config.default.js';
import * as libServer from '@certd/lib-server';
import * as commercial from '@certd/commercial-core';
import * as upload from '@midwayjs/upload';
process.on('uncaughtException', error => {
console.error('未捕获的异常:', error);
// 在这里可以添加日志记录、发送错误通知等操作
@@ -30,12 +31,13 @@ process.on('uncaughtException', error => {
cron,
staticFile,
validate,
upload,
libServer,
commercial,
{
component: info,
enabledEnvironment: ['local'],
},
libServer,
commercial,
],
importConfigs: [
{
@@ -50,11 +50,11 @@ export class AuthorityMiddleware implements IWebMiddleware {
let token = ctx.get('Authorization') || '';
token = token.replace('Bearer ', '').trim();
if (token === '') {
if (!token) {
//尝试从cookie中获取token
token = ctx.cookies.get('token') || '';
}
if (token === '') {
if (!token) {
//尝试从query中获取token
token = (ctx.query.token as string) || '';
}
@@ -0,0 +1,46 @@
import { Controller, Fields, Files, Get, Inject, Post, Provide, Query } from '@midwayjs/core';
import { BaseController, Constants, FileService, UploadFileItem, uploadTmpFileCacheKey } from '@certd/lib-server';
import send from 'koa-send';
import { nanoid } from 'nanoid';
import { cache } from '@certd/pipeline';
import { UploadFileInfo } from '@midwayjs/upload';
/**
*/
@Provide()
@Controller('/api/basic/file')
export class FileController extends BaseController {
@Inject()
fileService: FileService;
@Post('/upload', { summary: 'sys:settings:view' })
async upload(@Files() files: UploadFileInfo<string>[], @Fields() fields: any) {
console.log('files', files, fields);
const cacheKey = uploadTmpFileCacheKey + nanoid();
const file = files[0];
cache.set(
cacheKey,
{
filename: file.filename,
tmpFilePath: file.data,
} as UploadFileItem,
{
ttl: 1000 * 60 * 60,
}
);
return this.ok({
key: cacheKey,
});
}
@Get('/download', { summary: Constants.per.guest })
async download(@Query('key') key: string) {
let userId: any = null;
if (!key.startsWith('/public')) {
userId = this.getUserId();
}
const filePath = this.fileService.getFile(key, userId);
this.ctx.response.attachment(filePath);
await send(this.ctx, filePath);
}
}
@@ -1,9 +1,6 @@
import { Config, Controller, Get, Inject, Provide } from '@midwayjs/core';
import { BaseController } from '@certd/lib-server';
import { Constants } from '@certd/lib-server';
import { SysSettingsService } from '@certd/lib-server';
import { SysInstallInfo, SysPublicSettings, SysSiteInfo } from '@certd/lib-server';
import { AppKey } from '@certd/pipeline';
import { ALL, Body, Config, Controller, Get, Inject, Provide } from '@midwayjs/core';
import { BaseController, Constants, SysInstallInfo, SysPublicSettings, SysSettingsService, SysSiteInfo } from '@certd/lib-server';
import { AppKey, getPlusInfo } from '@certd/pipeline';
/**
*/
@@ -34,4 +31,12 @@ export class BasicSettingsController extends BaseController {
const settings: SysSiteInfo = await this.sysSettingsService.getSetting(SysSiteInfo);
return this.ok(settings);
}
@Get('/plusInfo', { summary: Constants.per.guest })
async plusInfo(@Body(ALL) body: any) {
const info = getPlusInfo();
return this.ok({
...info,
});
}
}
@@ -1,8 +1,6 @@
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
import { BaseController } from '@certd/lib-server';
import { Constants } from '@certd/lib-server';
import { BaseController, Constants } from '@certd/lib-server';
import { UserService } from '../../authority/service/user-service.js';
import { getPlusInfo } from '@certd/pipeline';
import { RoleService } from '../../authority/service/role-service.js';
/**
@@ -29,12 +27,4 @@ export class MineController extends BaseController {
await this.userService.changePassword(userId, body);
return this.ok({});
}
@Post('/plusInfo', { summary: Constants.per.authOnly })
async plusInfo(@Body(ALL) body) {
const info = getPlusInfo();
return this.ok({
...info,
});
}
}