library-picturebook-activity/backend/scripts/update-password.ts
2025-11-23 14:04:20 +08:00

121 lines
3.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
// 加载环境变量(必须在其他导入之前)
import * as dotenv from 'dotenv';
import * as path from 'path';
// 根据 NODE_ENV 加载对应的环境配置文件
const nodeEnv = process.env.NODE_ENV || 'development';
const envFile = `.env.${nodeEnv}`;
// scripts 目录的父目录就是 backend 目录
const backendDir = path.resolve(__dirname, '..');
const envPath = path.resolve(backendDir, envFile);
// 尝试加载环境特定的配置文件
dotenv.config({ path: envPath });
// 如果环境特定文件不存在,尝试加载默认的 .env 文件
if (!process.env.DATABASE_URL) {
dotenv.config({ path: path.resolve(backendDir, '.env') });
}
// 验证必要的环境变量
if (!process.env.DATABASE_URL) {
console.error('❌ 错误: 未找到 DATABASE_URL 环境变量');
console.error(` 请确保存在以下文件之一:`);
console.error(` - ${envPath}`);
console.error(` - ${path.resolve(backendDir, '.env')}`);
console.error(` 或者设置 NODE_ENV 环境变量(当前: ${nodeEnv})`);
process.exit(1);
}
import { PrismaClient } from '@prisma/client';
import * as bcrypt from 'bcrypt';
const prisma = new PrismaClient();
async function updatePassword() {
try {
const tenantCode = 'super';
const username = 'admin';
const newPassword = process.argv[2] || 'cms@admin'; // 支持命令行参数传入新密码
console.log(`🔐 开始修改租户 "${tenantCode}" 的 admin 用户密码...\n`);
// 1. 查找租户
console.log(`📋 步骤 1: 查找租户 "${tenantCode}"...`);
const tenant = await prisma.tenant.findUnique({
where: { code: tenantCode },
});
if (!tenant) {
console.error(`❌ 错误: 租户 "${tenantCode}" 不存在!`);
process.exit(1);
}
console.log(`✅ 找到租户: ${tenant.name} (${tenant.code})\n`);
// 2. 查找用户
console.log(`👤 步骤 2: 查找用户 "${username}"...`);
const existingUser = await prisma.user.findFirst({
where: {
tenantId: tenant.id,
username: username,
},
});
if (!existingUser) {
console.error(
`❌ 错误: 租户 "${tenantCode}" 下不存在用户 "${username}"`,
);
console.error(` 请先创建该用户`);
process.exit(1);
}
console.log(
`✅ 找到用户: ${existingUser.username} (${existingUser.nickname})\n`,
);
// 3. 加密新密码
console.log(`🔒 步骤 3: 加密新密码...`);
const hashedPassword = await bcrypt.hash(newPassword, 10);
console.log(`✅ 密码加密完成\n`);
// 4. 更新密码
console.log(`💾 步骤 4: 更新用户密码...`);
const updatedUser = await prisma.user.update({
where: { id: existingUser.id },
data: {
password: hashedPassword,
},
});
console.log(`✅ 密码修改成功!\n`);
console.log(`📊 更新结果:`);
console.log(` 租户名称: ${tenant.name}`);
console.log(` 租户编码: ${tenant.code}`);
console.log(` 用户ID: ${updatedUser.id}`);
console.log(` 用户名: ${updatedUser.username}`);
console.log(` 昵称: ${updatedUser.nickname}`);
console.log(` 新密码: ${newPassword}`);
console.log(` 修改时间: ${updatedUser.modifyTime}\n`);
} catch (error) {
console.error('❌ 修改密码时发生错误:');
console.error(error);
process.exit(1);
} finally {
await prisma.$disconnect();
}
}
// 执行脚本
updatePassword()
.then(() => {
console.log('🎉 密码修改脚本执行完成!');
process.exit(0);
})
.catch((error) => {
console.error('\n💥 密码修改脚本执行失败:', error);
process.exit(1);
});