From 66b61eada4e539263773cce6833bc464e80be90f Mon Sep 17 00:00:00 2001 From: root Date: Wed, 25 Feb 2026 18:15:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E5=AE=98?= =?UTF-8?q?=E6=96=B9support\Db=E5=92=8Csupport\Model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 support/Db.php 和 support/Model.php - 在 bootstrap.php 中自动初始化数据库 - 移除 app/util/Db.php - 更新所有模型和控制器使用官方类 Co-Authored-By: Claude Opus 4.6 --- backend/app/controller/AuthController.php | 6 -- backend/app/model/ApprovalFlow.php | 2 +- backend/app/model/ApprovalNode.php | 2 +- backend/app/model/ApprovalOrder.php | 2 +- backend/app/model/ApprovalRecord.php | 2 +- backend/app/model/BusinessTrip.php | 2 +- backend/app/model/BusinessTripItinerary.php | 2 +- backend/app/model/Company.php | 2 +- backend/app/model/CompanyFeishuApp.php | 2 +- backend/app/model/User.php | 2 +- backend/app/service/ApprovalService.php | 4 +- backend/app/service/BusinessTripService.php | 3 +- backend/app/service/FeishuService.php | 2 +- backend/app/util/Db.php | 66 ------------------ backend/composer.json | 3 +- backend/support/Db.php | 77 +++++++++++++++++++++ backend/support/Model.php | 16 +++++ backend/support/bootstrap.php | 8 +++ 18 files changed, 115 insertions(+), 88 deletions(-) delete mode 100644 backend/app/util/Db.php create mode 100644 backend/support/Db.php create mode 100644 backend/support/Model.php diff --git a/backend/app/controller/AuthController.php b/backend/app/controller/AuthController.php index 5b1b513..6c22fdc 100644 --- a/backend/app/controller/AuthController.php +++ b/backend/app/controller/AuthController.php @@ -4,7 +4,6 @@ namespace app\controller; use app\service\FeishuService; use app\util\JwtUtil; -use app\util\Db; use app\model\User; use app\model\Company; use Webman\Http\Request; @@ -12,11 +11,6 @@ use Webman\Http\Response; class AuthController extends BaseController { - public function __construct() - { - Db::init(); - } - /** * 用户登录 */ diff --git a/backend/app/model/ApprovalFlow.php b/backend/app/model/ApprovalFlow.php index ad79961..3846b16 100644 --- a/backend/app/model/ApprovalFlow.php +++ b/backend/app/model/ApprovalFlow.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class ApprovalFlow extends Model { diff --git a/backend/app/model/ApprovalNode.php b/backend/app/model/ApprovalNode.php index 5083beb..6a1ba79 100644 --- a/backend/app/model/ApprovalNode.php +++ b/backend/app/model/ApprovalNode.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class ApprovalNode extends Model { diff --git a/backend/app/model/ApprovalOrder.php b/backend/app/model/ApprovalOrder.php index bdfdb8b..169ddba 100644 --- a/backend/app/model/ApprovalOrder.php +++ b/backend/app/model/ApprovalOrder.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class ApprovalOrder extends Model { diff --git a/backend/app/model/ApprovalRecord.php b/backend/app/model/ApprovalRecord.php index ab369d0..ed90966 100644 --- a/backend/app/model/ApprovalRecord.php +++ b/backend/app/model/ApprovalRecord.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class ApprovalRecord extends Model { diff --git a/backend/app/model/BusinessTrip.php b/backend/app/model/BusinessTrip.php index 6eb671f..0cc5fb9 100644 --- a/backend/app/model/BusinessTrip.php +++ b/backend/app/model/BusinessTrip.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class BusinessTrip extends Model { diff --git a/backend/app/model/BusinessTripItinerary.php b/backend/app/model/BusinessTripItinerary.php index a2a9514..4559aa6 100644 --- a/backend/app/model/BusinessTripItinerary.php +++ b/backend/app/model/BusinessTripItinerary.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class BusinessTripItinerary extends Model { diff --git a/backend/app/model/Company.php b/backend/app/model/Company.php index 5ab0764..bc52778 100644 --- a/backend/app/model/Company.php +++ b/backend/app/model/Company.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class Company extends Model { diff --git a/backend/app/model/CompanyFeishuApp.php b/backend/app/model/CompanyFeishuApp.php index 459368a..8b2fd70 100644 --- a/backend/app/model/CompanyFeishuApp.php +++ b/backend/app/model/CompanyFeishuApp.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class CompanyFeishuApp extends Model { diff --git a/backend/app/model/User.php b/backend/app/model/User.php index af8cca0..6881595 100644 --- a/backend/app/model/User.php +++ b/backend/app/model/User.php @@ -2,7 +2,7 @@ namespace app\model; -use Illuminate\Database\Eloquent\Model; +use support\Model; class User extends Model { diff --git a/backend/app/service/ApprovalService.php b/backend/app/service/ApprovalService.php index d9148ce..b3d775e 100644 --- a/backend/app/service/ApprovalService.php +++ b/backend/app/service/ApprovalService.php @@ -7,7 +7,7 @@ use app\model\ApprovalNode; use app\model\ApprovalOrder; use app\model\ApprovalRecord; use app\model\User; -use app\util\Db; +use support\Db; use Illuminate\Database\Connection; class ApprovalService @@ -16,8 +16,6 @@ class ApprovalService public function __construct() { - // Initialize database if not already initialized - Db::init(); $this->db = Db::connection(); } diff --git a/backend/app/service/BusinessTripService.php b/backend/app/service/BusinessTripService.php index e862108..149ce17 100644 --- a/backend/app/service/BusinessTripService.php +++ b/backend/app/service/BusinessTripService.php @@ -8,7 +8,7 @@ use app\model\ApprovalOrder; use app\model\ApprovalFlow; use app\model\ApprovalRecord; use app\model\User; -use app\util\Db; +use support\Db; use Illuminate\Database\Connection; class BusinessTripService @@ -17,7 +17,6 @@ class BusinessTripService public function __construct() { - Db::init(); $this->db = Db::connection(); } diff --git a/backend/app/service/FeishuService.php b/backend/app/service/FeishuService.php index ea6b371..43927c4 100644 --- a/backend/app/service/FeishuService.php +++ b/backend/app/service/FeishuService.php @@ -5,7 +5,7 @@ namespace app\service; use app\model\Company; use app\model\CompanyFeishuApp; use app\model\User; -use app\util\Db; +use support\Db; class FeishuService { diff --git a/backend/app/util/Db.php b/backend/app/util/Db.php deleted file mode 100644 index 9461b9c..0000000 --- a/backend/app/util/Db.php +++ /dev/null @@ -1,66 +0,0 @@ -addConnection([ - 'driver' => 'mysql', - 'host' => getenv('DB_HOST') ?: '1Panel-mysql-409s', - 'port' => getenv('DB_PORT') ?: '3306', - 'database' => getenv('DB_DATABASE') ?: 'xserver', - 'username' => getenv('DB_USERNAME') ?: 'root', - 'password' => getenv('DB_PASSWORD') ?: 'mysql_xEsdPa', - 'charset' => 'utf8mb4', - 'collation' => 'utf8mb4_unicode_ci', - 'prefix' => '', - 'strict' => true, - 'engine' => null, - ]); - - // Set event dispatcher - self::$capsule->setEventDispatcher(new Dispatcher(new Container())); - - // Make this Capsule instance available globally via static methods - self::$capsule->setAsGlobal(); - - // Setup the Eloquent ORM - self::$capsule->bootEloquent(); - } - - public static function connection(?string $name = null): \Illuminate\Database\Connection - { - if (self::$capsule === null) { - self::init(); - } - return self::$capsule->connection($name); - } - - public static function table(string $table): \Illuminate\Database\Query\Builder - { - return self::connection()->table($table); - } - - public static function raw(string $value): \Illuminate\Database\Query\Expression - { - return self::connection()->raw($value); - } -} diff --git a/backend/composer.json b/backend/composer.json index 82a5eca..6783bc5 100644 --- a/backend/composer.json +++ b/backend/composer.json @@ -19,7 +19,8 @@ }, "autoload": { "psr-4": { - "app\\": "app/" + "app\\": "app/", + "support\\": "support/" } }, "scripts": { diff --git a/backend/support/Db.php b/backend/support/Db.php new file mode 100644 index 0000000..0bb82c4 --- /dev/null +++ b/backend/support/Db.php @@ -0,0 +1,77 @@ + $connection) { + self::$capsule->addConnection($connection, $name); + } + + self::$capsule->setAsGlobal(); + self::$capsule->bootEloquent(); + + if ($default = $config['default'] ?? null) { + self::$capsule->getDatabaseManager()->setDefaultConnection($default); + } + } + + /** + * 获取连接 + */ + public static function connection(?string $name = null): \Illuminate\Database\Connection + { + if (self::$capsule === null) { + self::init(); + } + return self::$capsule->connection($name); + } + + /** + * 获取表查询构造器 + */ + public static function table(string $table, ?string $connection = null): \Illuminate\Database\Query\Builder + { + return self::connection($connection)->table($table); + } + + /** + * 原始SQL表达式 + */ + public static function raw(string $value): \Illuminate\Database\Query\Expression + { + return self::connection()->raw($value); + } + + /** + * 获取 Capsule 实例 + */ + public static function getCapsule(): Capsule + { + if (self::$capsule === null) { + self::init(); + } + return self::$capsule; + } +} diff --git a/backend/support/Model.php b/backend/support/Model.php new file mode 100644 index 0000000..1cb5898 --- /dev/null +++ b/backend/support/Model.php @@ -0,0 +1,16 @@ +getConnectionName()); + } +} diff --git a/backend/support/bootstrap.php b/backend/support/bootstrap.php index d913def..e23c9f5 100644 --- a/backend/support/bootstrap.php +++ b/backend/support/bootstrap.php @@ -13,6 +13,7 @@ */ use Dotenv\Dotenv; +use support\Db; use support\Log; use Webman\Bootstrap; use Webman\Config; @@ -73,6 +74,13 @@ foreach (config('plugin', []) as $firm => $projects) { } } +// 加载数据库类 +include_once __DIR__ . '/Db.php'; +include_once __DIR__ . '/Model.php'; + +// 初始化数据库 +Db::init(); + Middleware::load(config('middleware', [])); foreach (config('plugin', []) as $firm => $projects) { foreach ($projects as $name => $project) {