mirror of
https://github.com/vastxie/99AI.git
synced 2025-11-12 03:33:42 +08:00
v4.1.0
This commit is contained in:
50
AIWebQuickDeploy/dist/modules/database/database.module.js
vendored
Normal file
50
AIWebQuickDeploy/dist/modules/database/database.module.js
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
var DatabaseModule_1;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.DatabaseModule = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const typeorm_1 = require("@nestjs/typeorm");
|
||||
const typeorm_2 = require("typeorm");
|
||||
const database_service_1 = require("./database.service");
|
||||
let DatabaseModule = DatabaseModule_1 = class DatabaseModule {
|
||||
constructor(connection) {
|
||||
this.connection = connection;
|
||||
this.logger = new common_1.Logger(DatabaseModule_1.name);
|
||||
}
|
||||
onModuleInit() {
|
||||
const { database } = this.connection.options;
|
||||
this.logger.log(`Your MySQL database named ${database} has been connected`);
|
||||
}
|
||||
};
|
||||
DatabaseModule = DatabaseModule_1 = __decorate([
|
||||
(0, common_1.Module)({
|
||||
imports: [
|
||||
typeorm_1.TypeOrmModule.forRootAsync({
|
||||
useFactory: () => ({
|
||||
type: 'mysql',
|
||||
host: process.env.DB_HOST,
|
||||
port: parseInt(process.env.DB_PORT, 10),
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_DATABASE,
|
||||
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
|
||||
logging: false,
|
||||
charset: 'utf8mb4',
|
||||
timezone: '+08:00',
|
||||
}),
|
||||
}),
|
||||
],
|
||||
providers: [database_service_1.DatabaseService],
|
||||
}),
|
||||
__metadata("design:paramtypes", [typeorm_2.DataSource])
|
||||
], DatabaseModule);
|
||||
exports.DatabaseModule = DatabaseModule;
|
||||
198
AIWebQuickDeploy/dist/modules/database/database.service.js
vendored
Normal file
198
AIWebQuickDeploy/dist/modules/database/database.service.js
vendored
Normal file
@@ -0,0 +1,198 @@
|
||||
"use strict";
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.DatabaseService = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const bcrypt = require("bcryptjs");
|
||||
const typeorm_1 = require("typeorm");
|
||||
let DatabaseService = class DatabaseService {
|
||||
constructor(connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
async onModuleInit() {
|
||||
await this.checkSuperAdmin();
|
||||
await this.checkSiteBaseConfig();
|
||||
}
|
||||
async checkSuperAdmin() {
|
||||
const user = await this.connection.query(`SELECT * FROM users WHERE role = 'super'`);
|
||||
if (!user || user.length === 0) {
|
||||
const superPassword = bcrypt.hashSync('123456', 10);
|
||||
const adminPassword = bcrypt.hashSync('123456', 10);
|
||||
const superEmail = 'super';
|
||||
const adminEmail = 'admin';
|
||||
const superUserinfo = {
|
||||
username: 'super',
|
||||
password: superPassword,
|
||||
status: 1,
|
||||
email: superEmail,
|
||||
sex: 1,
|
||||
role: 'super',
|
||||
};
|
||||
const adminUserinfo = {
|
||||
username: 'admin',
|
||||
password: adminPassword,
|
||||
status: 0,
|
||||
email: adminEmail,
|
||||
sex: 1,
|
||||
role: 'admin',
|
||||
};
|
||||
await this.createDefaultUser(superUserinfo);
|
||||
await this.createDefaultUser(adminUserinfo);
|
||||
}
|
||||
}
|
||||
async createDefaultUser(userInfo) {
|
||||
try {
|
||||
const { username, password, status, email, role } = userInfo;
|
||||
const user = await this.connection.query(`INSERT INTO users (username, password, status, email, role) VALUES ('${username}', '${password}', '${status}', '${email}', '${role}')`);
|
||||
const userId = user.insertId;
|
||||
await this.connection.query(`INSERT INTO balance (userId, balance, usesLeft, paintCount) VALUES ('${userId}', 0, 1000, 100)`);
|
||||
common_1.Logger.log(`初始化创建${role}用户成功、用户名为[${username}]、初始密码为[${username === 'super' ? 'super' : '123456'}] ==============> 请注意查阅`, 'DatabaseService');
|
||||
}
|
||||
catch (error) {
|
||||
console.log('error: ', error);
|
||||
throw new common_1.HttpException('创建默认超级管理员失败!', common_1.HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
async checkSiteBaseConfig() {
|
||||
const keys = ['siteName', 'robotAvatar'];
|
||||
const result = await this.connection.query(`
|
||||
SELECT COUNT(*) AS count FROM config WHERE \`configKey\` IN (${keys
|
||||
.map((k) => `'${k}'`)
|
||||
.join(',')})
|
||||
`);
|
||||
const count = parseInt(result[0].count);
|
||||
if (count === 0) {
|
||||
await this.createBaseSiteConfig();
|
||||
}
|
||||
}
|
||||
async createBaseSiteConfig() {
|
||||
try {
|
||||
const code = ``;
|
||||
const noticeInfo = `
|
||||
#### AIWeb 欢迎您
|
||||
- 欢迎使用 AIWeb
|
||||
- 初始管理员账号密码 super 123456 【前台后台登录都可以修改】
|
||||
- 初始预览账号密码 admin 123456 【为后台查看账号 仅可查看部分非敏感数据】
|
||||
`;
|
||||
const defaultConfig = [
|
||||
{ configKey: 'siteName', configVal: '', public: 1, encry: 0 },
|
||||
{ configKey: 'robotAvatar', configVal: '', public: 1, encry: 0 },
|
||||
{
|
||||
configKey: 'userDefautlAvatar',
|
||||
configVal: '',
|
||||
public: 0,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'baiduCode', configVal: code, public: 1, encry: 0 },
|
||||
{ configKey: 'baiduSiteId', configVal: '', public: 0, encry: 0 },
|
||||
{
|
||||
configKey: 'baiduToken',
|
||||
configVal: '',
|
||||
public: 0,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'buyCramiAddress', configVal: '', public: 1, encry: 0 },
|
||||
{
|
||||
configKey: 'openaiBaseUrl',
|
||||
configVal: 'https://api.lightai.io',
|
||||
public: 0,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'openaiTimeout', configVal: '300', public: 0, encry: 0 },
|
||||
{ configKey: 'openaiBaseKey', configVal: 'sk-', public: 0, encry: 0 },
|
||||
{
|
||||
configKey: 'mjTranslatePrompt',
|
||||
configVal: `Translate any given phrase from any language into English. For instance, when I input '{可爱的熊猫}', you should output '{cute panda}', with no period at the end.`,
|
||||
public: 0,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'noticeInfo', configVal: noticeInfo, public: 1, encry: 0 },
|
||||
{
|
||||
configKey: 'registerSendStatus',
|
||||
configVal: '1',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'registerSendModel3Count',
|
||||
configVal: '30',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'registerSendModel4Count',
|
||||
configVal: '3',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'registerSendDrawMjCount',
|
||||
configVal: '3',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRegisterSendStatus',
|
||||
configVal: '1',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRegisterSendRank',
|
||||
configVal: '500',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRregisterSendModel3Count',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRregisterSendModel4Count',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{
|
||||
configKey: 'firstRregisterSendDrawMjCount',
|
||||
configVal: '10',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
{ configKey: 'isVerifyEmail', configVal: '1', public: 1, encry: 0 },
|
||||
{ configKey: 'model3Name', configVal: '普通积分', public: 1, encry: 0 },
|
||||
{ configKey: 'model4Name', configVal: '高级积分', public: 1, encry: 0 },
|
||||
{ configKey: 'drawMjName', configVal: '绘画积分', public: 1, encry: 0 },
|
||||
{
|
||||
configKey: 'drawingStyles',
|
||||
configVal: '油画风格,像素风格,赛博朋克,动漫,日系,超现实主义,油画,卡通,插画,海报,写实,扁平,中国风,水墨画,唯美二次元,印象派,炫彩插画,像素艺术,艺术创想,色彩主义,数字艺术',
|
||||
public: 1,
|
||||
encry: 0,
|
||||
},
|
||||
];
|
||||
const res = await this.connection.query(`INSERT INTO config (configKey, configVal, public, encry) VALUES ${defaultConfig
|
||||
.map((d) => `('${d.configKey}', '${d.configVal.replace(/'/g, "\\'")}', '${d.public}', '${d.encry}')`)
|
||||
.join(', ')}`);
|
||||
common_1.Logger.log(`初始化网站配置信息成功、如您需要修改网站配置信息,请前往管理系统系统配置设置 ==============> 请注意查阅`, 'DatabaseService');
|
||||
}
|
||||
catch (error) {
|
||||
console.log('error: ', error);
|
||||
throw new common_1.HttpException('创建默认网站配置失败!', common_1.HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
};
|
||||
DatabaseService = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__metadata("design:paramtypes", [typeorm_1.Connection])
|
||||
], DatabaseService);
|
||||
exports.DatabaseService = DatabaseService;
|
||||
95
AIWebQuickDeploy/dist/modules/database/initDatabase.js
vendored
Normal file
95
AIWebQuickDeploy/dist/modules/database/initDatabase.js
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.initDatabase = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const dotenv_1 = require("dotenv");
|
||||
const mysql = require("mysql2/promise");
|
||||
const typeorm_1 = require("typeorm");
|
||||
(0, dotenv_1.config)();
|
||||
const dataSourceOptions = {
|
||||
type: 'mysql',
|
||||
port: parseInt(process.env.DB_PORT, 10),
|
||||
host: process.env.DB_HOST,
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_DATABASE,
|
||||
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
|
||||
logging: false,
|
||||
synchronize: true,
|
||||
charset: 'utf8mb4',
|
||||
timezone: '+08:00',
|
||||
};
|
||||
async function validateDatabase() {
|
||||
const conn = await mysql.createConnection({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
port: parseInt(process.env.DB_PORT, 10),
|
||||
});
|
||||
try {
|
||||
const [rows] = (await conn.execute(`SHOW DATABASES LIKE '${process.env.DB_DATABASE}'`));
|
||||
if (Array.isArray(rows) && rows.length === 0) {
|
||||
await conn.execute(`CREATE DATABASE ${process.env.DB_DATABASE}`);
|
||||
common_1.Logger.log(`数据库创建成功[${process.env.DB_DATABASE}]`, 'Database');
|
||||
}
|
||||
else {
|
||||
common_1.Logger.log(`数据库已存在[${process.env.DB_DATABASE}]`, 'Database');
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
common_1.Logger.error('Error during database validation:', error, 'Database');
|
||||
}
|
||||
finally {
|
||||
await conn.end();
|
||||
}
|
||||
}
|
||||
async function updateColumnType() {
|
||||
const conn = await mysql.createConnection({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
port: parseInt(process.env.DB_PORT, 10),
|
||||
database: process.env.DB_DATABASE,
|
||||
});
|
||||
async function checkAndUpdateColumnType(tableName, columnName) {
|
||||
try {
|
||||
const [rows] = (await conn.execute(`SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME = ?`, [tableName, columnName]));
|
||||
if (rows.length > 0 && rows[0].DATA_TYPE !== 'text') {
|
||||
await conn.execute(`ALTER TABLE ?? MODIFY COLUMN ?? TEXT`, [
|
||||
tableName,
|
||||
columnName,
|
||||
]);
|
||||
common_1.Logger.log(`Column ${columnName} type updated to TEXT in table ${tableName}`, 'Database');
|
||||
}
|
||||
else {
|
||||
common_1.Logger.log(`Column ${columnName} is already of type TEXT in table ${tableName}`, 'Database');
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
common_1.Logger.error(`Error updating column type in table ${tableName}:`, error);
|
||||
}
|
||||
}
|
||||
try {
|
||||
await checkAndUpdateColumnType('config', 'configVal');
|
||||
await checkAndUpdateColumnType('app', 'coverImg');
|
||||
}
|
||||
finally {
|
||||
await conn.end();
|
||||
}
|
||||
}
|
||||
async function initDatabase() {
|
||||
await validateDatabase();
|
||||
await updateColumnType();
|
||||
const dataSource = new typeorm_1.DataSource(dataSourceOptions);
|
||||
try {
|
||||
await dataSource.initialize();
|
||||
common_1.Logger.log('Database connected and synchronized successfully', 'Database');
|
||||
}
|
||||
catch (error) {
|
||||
common_1.Logger.error('Error during TypeORM initialization:', error);
|
||||
}
|
||||
finally {
|
||||
await dataSource.destroy();
|
||||
}
|
||||
}
|
||||
exports.initDatabase = initDatabase;
|
||||
Reference in New Issue
Block a user