Files
CKB-DataSourceCenter/app/controller/IndexController.php
乘风 db3b80d3ff 增强 MongoDB 集成及用户管理功能
更新 composer.json 文件,添加新的依赖项:vlucas/phpdotenv、predis/predis、dragonmantank/cron-expression、php-amqplib/php-amqplib 以及 ramsey/uuid。
在 IndexController 中添加 MongoDB 连接测试接口。
在 UserController 中实现用户管理接口,用于创建、更新、删除和搜索用户。
在 database.php 中配置 MongoDB 连接设置。
更新路由,以包含新的与用户相关的 API 接口。
增强用户操作中的错误处理和日志记录功能。
在 process.php 中引入数据同步和工作进程配置。
2026-01-05 10:46:44 +08:00

155 lines
5.4 KiB
PHP
Raw 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.

<?php
namespace app\controller;
use support\Request;
class IndexController
{
public function index(Request $request)
{
return "我是数据中心,有何贵干?";
}
public function view(Request $request)
{
return view('index/view', ['name' => 'webman']);
}
public function json(Request $request)
{
return json(['code' => 0, 'msg' => 'ok']);
}
/**
* 测试 MongoDB 数据库连接
* GET /api/test/db
*/
public function testDb(Request $request)
{
$result = [
'code' => 0,
'msg' => 'ok',
'data' => [
'config' => [],
'connection' => [],
'test_query' => [],
],
];
try {
// 读取数据库配置
$dbConfig = config('database', []);
$mongoConfig = $dbConfig['connections']['mongodb'] ?? null;
if (!$mongoConfig) {
throw new \Exception('MongoDB 配置不存在');
}
$result['data']['config'] = [
'driver' => $mongoConfig['driver'] ?? 'unknown',
'database' => $mongoConfig['database'] ?? 'unknown',
'dsn' => $mongoConfig['dsn'] ?? 'unknown',
'has_username' => !empty($mongoConfig['username']),
'has_password' => !empty($mongoConfig['password']),
];
// 尝试使用 MongoDB 客户端直接连接
try {
// 构建包含认证信息的 DSN如果配置了用户名和密码
$dsn = $mongoConfig['dsn'];
if (!empty($mongoConfig['username']) && !empty($mongoConfig['password'])) {
// 如果 DSN 中不包含认证信息,则添加
if (strpos($dsn, '@') === false) {
// 从 mongodb://host:port 格式转换为 mongodb://username:password@host:port/database
$dsn = str_replace(
'mongodb://',
'mongodb://' . urlencode($mongoConfig['username']) . ':' . urlencode($mongoConfig['password']) . '@',
$dsn
);
// 添加数据库名和认证源
$dsn .= '/' . $mongoConfig['database'];
if (!empty($mongoConfig['options']['authSource'])) {
$dsn .= '?authSource=' . urlencode($mongoConfig['options']['authSource']);
}
}
}
// 过滤掉空字符串的选项MongoDB 客户端不允许空字符串)
$options = array_filter($mongoConfig['options'] ?? [], function ($value) {
return $value !== '';
});
$client = new \MongoDB\Client(
$dsn,
$options
);
// 尝试执行 ping 命令
$adminDb = $client->selectDatabase('admin');
$pingResult = $adminDb->command(['ping' => 1])->toArray();
$result['data']['connection'] = [
'status' => 'connected',
'ping' => 'ok',
'server_info' => $client->getManager()->getServers(),
];
// 尝试选择目标数据库并列出集合
$targetDb = $client->selectDatabase($mongoConfig['database']);
$collections = $targetDb->listCollections();
$collectionNames = [];
foreach ($collections as $collection) {
$collectionNames[] = $collection->getName();
}
$result['data']['test_query'] = [
'database' => $mongoConfig['database'],
'collections_count' => count($collectionNames),
'collections' => $collectionNames,
];
} catch (\MongoDB\Driver\Exception\Exception $e) {
$result['data']['connection'] = [
'status' => 'failed',
'error' => $e->getMessage(),
'code' => $e->getCode(),
];
$result['code'] = 500;
$result['msg'] = 'MongoDB 连接失败';
}
// 尝试使用 Repository 查询(如果连接成功)
if ($result['data']['connection']['status'] === 'connected') {
try {
$userRepo = new \app\repository\UserProfileRepository();
$count = $userRepo->newQuery()->count();
$result['data']['repository_test'] = [
'status' => 'ok',
'user_profile_count' => $count,
];
} catch (\Throwable $e) {
$result['data']['repository_test'] = [
'status' => 'failed',
'error' => $e->getMessage(),
];
}
}
} catch (\Throwable $e) {
$result = [
'code' => 500,
'msg' => '测试失败: ' . $e->getMessage(),
'data' => [
'error' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
],
];
}
return json($result);
}
}