refactor: 使用官方support\Db和support\Model
- 创建 support/Db.php 和 support/Model.php - 在 bootstrap.php 中自动初始化数据库 - 移除 app/util/Db.php - 更新所有模型和控制器使用官方类 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,6 @@ namespace app\controller;
|
|||||||
|
|
||||||
use app\service\FeishuService;
|
use app\service\FeishuService;
|
||||||
use app\util\JwtUtil;
|
use app\util\JwtUtil;
|
||||||
use app\util\Db;
|
|
||||||
use app\model\User;
|
use app\model\User;
|
||||||
use app\model\Company;
|
use app\model\Company;
|
||||||
use Webman\Http\Request;
|
use Webman\Http\Request;
|
||||||
@@ -12,11 +11,6 @@ use Webman\Http\Response;
|
|||||||
|
|
||||||
class AuthController extends BaseController
|
class AuthController extends BaseController
|
||||||
{
|
{
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
Db::init();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录
|
* 用户登录
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class ApprovalFlow extends Model
|
class ApprovalFlow extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class ApprovalNode extends Model
|
class ApprovalNode extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class ApprovalOrder extends Model
|
class ApprovalOrder extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class ApprovalRecord extends Model
|
class ApprovalRecord extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class BusinessTrip extends Model
|
class BusinessTrip extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class BusinessTripItinerary extends Model
|
class BusinessTripItinerary extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class Company extends Model
|
class Company extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class CompanyFeishuApp extends Model
|
class CompanyFeishuApp extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace app\model;
|
namespace app\model;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use support\Model;
|
||||||
|
|
||||||
class User extends Model
|
class User extends Model
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use app\model\ApprovalNode;
|
|||||||
use app\model\ApprovalOrder;
|
use app\model\ApprovalOrder;
|
||||||
use app\model\ApprovalRecord;
|
use app\model\ApprovalRecord;
|
||||||
use app\model\User;
|
use app\model\User;
|
||||||
use app\util\Db;
|
use support\Db;
|
||||||
use Illuminate\Database\Connection;
|
use Illuminate\Database\Connection;
|
||||||
|
|
||||||
class ApprovalService
|
class ApprovalService
|
||||||
@@ -16,8 +16,6 @@ class ApprovalService
|
|||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
// Initialize database if not already initialized
|
|
||||||
Db::init();
|
|
||||||
$this->db = Db::connection();
|
$this->db = Db::connection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use app\model\ApprovalOrder;
|
|||||||
use app\model\ApprovalFlow;
|
use app\model\ApprovalFlow;
|
||||||
use app\model\ApprovalRecord;
|
use app\model\ApprovalRecord;
|
||||||
use app\model\User;
|
use app\model\User;
|
||||||
use app\util\Db;
|
use support\Db;
|
||||||
use Illuminate\Database\Connection;
|
use Illuminate\Database\Connection;
|
||||||
|
|
||||||
class BusinessTripService
|
class BusinessTripService
|
||||||
@@ -17,7 +17,6 @@ class BusinessTripService
|
|||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
Db::init();
|
|
||||||
$this->db = Db::connection();
|
$this->db = Db::connection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace app\service;
|
|||||||
use app\model\Company;
|
use app\model\Company;
|
||||||
use app\model\CompanyFeishuApp;
|
use app\model\CompanyFeishuApp;
|
||||||
use app\model\User;
|
use app\model\User;
|
||||||
use app\util\Db;
|
use support\Db;
|
||||||
|
|
||||||
class FeishuService
|
class FeishuService
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,66 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace app\util;
|
|
||||||
|
|
||||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
||||||
use Illuminate\Events\Dispatcher;
|
|
||||||
use Illuminate\Container\Container;
|
|
||||||
use Illuminate\Database\Eloquent\Model as EloquentModel;
|
|
||||||
|
|
||||||
class Db
|
|
||||||
{
|
|
||||||
private static ?Capsule $capsule = null;
|
|
||||||
|
|
||||||
public static function init(): void
|
|
||||||
{
|
|
||||||
if (self::$capsule !== null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$config = config('database');
|
|
||||||
|
|
||||||
self::$capsule = new Capsule();
|
|
||||||
|
|
||||||
// Add connection
|
|
||||||
self::$capsule->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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -19,7 +19,8 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"app\\": "app/"
|
"app\\": "app/",
|
||||||
|
"support\\": "support/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace support;
|
||||||
|
|
||||||
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||||
|
|
||||||
|
class Db
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Capsule
|
||||||
|
*/
|
||||||
|
protected static $capsule = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化
|
||||||
|
*/
|
||||||
|
public static function init(): void
|
||||||
|
{
|
||||||
|
if (self::$capsule !== null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$config = config('database');
|
||||||
|
$connections = $config['connections'] ?? [];
|
||||||
|
|
||||||
|
self::$capsule = new Capsule();
|
||||||
|
|
||||||
|
foreach ($connections as $name => $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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace support;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model as EloquentModel;
|
||||||
|
|
||||||
|
class Model extends EloquentModel
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 获取数据库连接
|
||||||
|
*/
|
||||||
|
public function getConnection()
|
||||||
|
{
|
||||||
|
return Db::connection($this->getConnectionName());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
use Dotenv\Dotenv;
|
use Dotenv\Dotenv;
|
||||||
|
use support\Db;
|
||||||
use support\Log;
|
use support\Log;
|
||||||
use Webman\Bootstrap;
|
use Webman\Bootstrap;
|
||||||
use Webman\Config;
|
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', []));
|
Middleware::load(config('middleware', []));
|
||||||
foreach (config('plugin', []) as $firm => $projects) {
|
foreach (config('plugin', []) as $firm => $projects) {
|
||||||
foreach ($projects as $name => $project) {
|
foreach ($projects as $name => $project) {
|
||||||
|
|||||||
Reference in New Issue
Block a user