- game_uid is now an internal param: auto-generated server-side (U + 12 hex) on register/bind-identity/install/admin-create/external register; player-supplied game_uid ignored - Removed all player-facing UID inputs: register page, install page, identity bind modal, ticket-create 'your UID' field - Removed player-facing UID display: identity table column, ticket detail/tracking reporter UID - Removed '同一账号可绑定网易端 + 皮肤站身份' / '可绑定多个来源身份' copy - Kept target_game_uid (reported player) and admin panel UID management - settings-page skin-site placeholder updated
127 lines
4.9 KiB
JavaScript
127 lines
4.9 KiB
JavaScript
/*
|
|
* MC Report System
|
|
* Copyright (C) 2026 Sea Network Technology Studio
|
|
* Author: CangLan <admin@sea-studio.top>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
const express = require('express');
|
|
const mysql = require('mysql2/promise');
|
|
const bcrypt = require('bcryptjs');
|
|
const crypto = require('crypto');
|
|
const { isInstalled, saveConfig, getConfig, initSchema } = require('../db');
|
|
|
|
const router = express.Router();
|
|
|
|
router.get('/status', (req, res) => {
|
|
res.json({ installed: isInstalled() });
|
|
});
|
|
|
|
router.post('/check-db', async (req, res) => {
|
|
const { host, port, user, password, database } = req.body;
|
|
if (!/^[A-Za-z0-9_]+$/.test(database || '')) return res.status(400).json({ error: '数据库名仅允许字母、数字、下划线' });
|
|
try {
|
|
const conn = await mysql.createConnection({
|
|
host: host || 'localhost',
|
|
port: parseInt(port) || 3306,
|
|
user,
|
|
password,
|
|
charset: 'utf8mb4',
|
|
multipleStatements: true,
|
|
});
|
|
const [rows] = await conn.query(`SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?`, [database]);
|
|
if (rows.length === 0) {
|
|
await conn.query(`CREATE DATABASE \`${database}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`);
|
|
}
|
|
await conn.end();
|
|
res.json({ ok: true });
|
|
} catch (e) {
|
|
res.status(400).json({ error: '数据库连接失败: ' + e.message });
|
|
}
|
|
});
|
|
|
|
router.post('/complete', async (req, res) => {
|
|
if (isInstalled()) return res.status(400).json({ error: '系统已安装' });
|
|
|
|
const { db_host, db_port, db_user, db_pass, db_name,
|
|
site_name, site_url, admin_username, admin_password, admin_email,
|
|
admin_game_name, admin_game_uid } = req.body;
|
|
|
|
if (!db_host || !db_user || !db_name || !admin_username || !admin_password || !admin_email) {
|
|
return res.status(400).json({ error: '必填字段不完整' });
|
|
}
|
|
if (!/^[A-Za-z0-9_]+$/.test(db_name || '')) return res.status(400).json({ error: '数据库名仅允许字母、数字、下划线' });
|
|
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(admin_email)) return res.status(400).json({ error: '邮箱格式不正确' });
|
|
if (admin_password.length < 6) return res.status(400).json({ error: '密码至少6位' });
|
|
|
|
try {
|
|
const conn = await mysql.createConnection({
|
|
host: db_host,
|
|
port: parseInt(db_port) || 3306,
|
|
user: db_user,
|
|
password: db_pass || '',
|
|
charset: 'utf8mb4',
|
|
multipleStatements: true,
|
|
});
|
|
|
|
const [dbs] = await conn.query(`SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?`, [db_name]);
|
|
if (dbs.length === 0) {
|
|
await conn.query(`CREATE DATABASE \`${db_name}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`);
|
|
}
|
|
|
|
await conn.query(`USE \`${db_name}\``);
|
|
await initSchema(conn);
|
|
|
|
const hashed = bcrypt.hashSync(admin_password, 10);
|
|
// 游戏UID为内置参数: 不接收输入, 由系统自动生成
|
|
const builtinUid = 'U' + crypto.randomBytes(6).toString('hex').toUpperCase();
|
|
await conn.query(
|
|
'INSERT INTO users(username, password, email, game_name, game_uid, role, active, email_verified) VALUES (?,?,?,?,?,?,1,1)',
|
|
[admin_username, hashed, admin_email, admin_game_name || admin_username, builtinUid, 'owner']
|
|
);
|
|
|
|
const jwtSecret = crypto.randomBytes(32).toString('hex');
|
|
const apiKey = crypto.randomBytes(24).toString('hex');
|
|
const externalKey = crypto.randomBytes(24).toString('hex');
|
|
|
|
for (const [k, v, l] of [
|
|
['site_name', site_name || 'MC举报系统', '站点名称'],
|
|
['site_url', site_url || 'http://localhost:3100', '站点地址'],
|
|
['copyright', '© 2024 MC举报系统', '页尾版权'],
|
|
['icp', '', 'ICP备案号'],
|
|
]) {
|
|
await conn.query("INSERT INTO settings(k, v, label) VALUES (?,?,?) ON DUPLICATE KEY UPDATE v=VALUES(v)", [k, v, l]);
|
|
}
|
|
|
|
await conn.end();
|
|
|
|
saveConfig({
|
|
db_host, db_port: parseInt(db_port) || 3306,
|
|
db_user, db_pass: db_pass || '', db_name,
|
|
jwt_secret: jwtSecret,
|
|
api_key: apiKey,
|
|
external_api_key: externalKey,
|
|
});
|
|
|
|
try { if (typeof global.__loadBusinessRoutes === 'function') global.__loadBusinessRoutes(); } catch {}
|
|
|
|
res.json({ ok: true, message: '安装完成,无需重启,系统已就绪' });
|
|
} catch (e) {
|
|
res.status(500).json({ error: '安装失败: ' + e.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|