feat: 实现登录鉴权 RBAC 与用户管理模块
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { PrismaClient, RoleType } from '@prisma/client';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const username = process.env.SEED_ADMIN_USERNAME || 'admin';
|
||||
const password = process.env.SEED_ADMIN_PASSWORD || 'Admin@12345';
|
||||
const realName = process.env.SEED_ADMIN_NAME || '超级管理员';
|
||||
|
||||
const existing = await prisma.sysUser.findUnique({ where: { username } });
|
||||
if (existing) {
|
||||
console.log(`超级管理员 ${username} 已存在,跳过。`);
|
||||
} else {
|
||||
const passwordHash = await bcrypt.hash(password, 10);
|
||||
await prisma.sysUser.create({
|
||||
data: { username, passwordHash, realName, role: RoleType.SUPER_ADMIN },
|
||||
});
|
||||
console.log(`已创建超级管理员:${username} / ${password}`);
|
||||
}
|
||||
|
||||
// 默认系统设置
|
||||
const defaults: Record<string, string> = {
|
||||
appName: process.env.APP_NAME || '艺术品可信溯源与认证平台',
|
||||
logRetentionDays: process.env.LOG_RETENTION_DAYS || '365',
|
||||
watermarkText: process.env.WATERMARK_TEXT || '仅供溯源核验使用',
|
||||
};
|
||||
for (const [key, value] of Object.entries(defaults)) {
|
||||
await prisma.systemSetting.upsert({
|
||||
where: { key },
|
||||
update: {},
|
||||
create: { key, value },
|
||||
});
|
||||
}
|
||||
console.log('系统默认设置已就绪。');
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(() => {
|
||||
void prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user