初始化本地仓库

This commit is contained in:
2026-08-31 11:33:59 +08:00
commit 663f74b633
2390 changed files with 402918 additions and 0 deletions
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
/**
*+------------------
* madong
*+------------------
* Copyright (c) https://gitee.com/motion-code All rights reserved.
*+------------------
* Author: Mr. April (405784684@qq.com)
*+------------------
* Official Website: https://madong.tech
*/
namespace app\service\core\install\traits;
use core\uuid\Snowflake;
/**
* 管理员创建 Trait
*/
trait AdminTrait
{
/**
* 创建管理员
*/
public function createAdmin(array $adminParams): void
{
$adminTable = $this->table('sys_admin');
$pdo = $this->getPdo();
$stmt = $pdo->query("SELECT COUNT(*) as cnt FROM `{$adminTable}`");
$result = $stmt->fetch();
if ($result['cnt'] > 0) {
return;
}
$username = $adminParams['username'] ?? 'admin';
$password = $adminParams['password'] ?? '123456';
$email = $adminParams['email'] ?? 'admin@example.com';
$this->insert($adminTable, [
'id' => 1,
'user_name' => $username,
'password' => password_hash($password, PASSWORD_DEFAULT),
'real_name' => '超级管理员',
'nick_name' => '超级管理员',
'email' => $email,
'is_super' => 1,
'enabled' => 1,
'created_at' => $this->currentTime,
'updated_at' => $this->currentTime,
]);
}
}
@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
/**
*+------------------
* madong
*+------------------
* Copyright (c) https://gitee.com/motion-code All rights reserved.
*+------------------
* Author: Mr. April (405784684@qq.com)
*+------------------
* Official Website: https://madong.tech
*/
namespace app\service\core\install\traits;
use core\uuid\Snowflake;
/**
* 配置导入 Trait
*/
trait ConfigTrait
{
/**
* 运行配置种子
*/
public function runConfig(): void
{
$configFile = base_path('resource/data/config/config.php');
if (!file_exists($configFile)) {
return;
}
$configs = require $configFile;
$configTable = $this->table('sys_config');
$pdo = $this->getPdo();
$pdo->exec("TRUNCATE TABLE `{$configTable}`");
foreach ($configs as $config) {
$content = isset($config['content'])
? (is_array($config['content'])
? json_encode($config['content'], JSON_UNESCAPED_UNICODE)
: $config['content'])
: '';
$this->insert($configTable, [
'id' => Snowflake::generate(),
'group_code' => $config['group_code'] ?? 'system',
'code' => $config['code'] ?? '',
'name' => $config['name'] ?? '',
'content' => $content,
'is_sys' => $config['is_sys'] ?? 0,
'enabled' => $config['enabled'] ?? 0,
'remark' => $config['remark'] ?? '',
'created_at' => $this->currentTime,
'created_by' => $config['created_by'] ?? null,
'updated_at' => $this->currentTime,
'updated_by' => $config['updated_by'] ?? null,
'deleted_at' => $config['deleted_at'] ?? null,
]);
}
}
}
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
/**
*+------------------
* madong
*+------------------
* Copyright (c) https://gitee.com/motion-code All rights reserved.
*+------------------
* Author: Mr. April (405784684@qq.com)
*+------------------
* Official Website: https://madong.tech
*/
namespace app\service\core\install\traits;
use core\uuid\Snowflake;
/**
* 字典导入 Trait
*/
trait DictTrait
{
/**
* 运行字典种子
*/
public function runDict(): void
{
$dictFile = base_path('resource/data/dict/dict.php');
if (!file_exists($dictFile)) {
return;
}
$dicts = require $dictFile;
$dictTable = $this->table('sys_dict');
$dictItemTable = $this->table('sys_dict_item');
$pdo = $this->getPdo();
$pdo->exec("TRUNCATE TABLE `{$dictItemTable}`");
$pdo->exec("TRUNCATE TABLE `{$dictTable}`");
foreach ($dicts as $dict) {
$dictId = Snowflake::generate();
$this->insert($dictTable, [
'id' => $dictId,
'group_code' => $dict['group'] ?? 'system',
'code' => $dict['code'] ?? '',
'name' => $dict['name'] ?? '',
'sort' => $dict['sort'] ?? 0,
'data_type' => $dict['data_type'] ?? 1,
'description' => $dict['remark'] ?? '',
'enabled' => $dict['status'] ?? 1,
'created_at' => $this->currentTime,
'updated_at' => $this->currentTime,
'created_by' => 1,
'updated_by' => 1,
]);
if (!empty($dict['items'])) {
foreach ($dict['items'] as $item) {
$this->insert($dictItemTable, [
'id' => Snowflake::generate(),
'dict_id' => $dictId,
'code' => $dict['code'] ?? '',
'value' => $item['value'] ?? '',
'label' => $item['label'] ?? '',
'color' => $item['color'] ?? '',
'sort' => $item['sort'] ?? 0,
'enabled' => $item['status'] ?? 1,
'created_at' => $this->currentTime,
'updated_at' => $this->currentTime,
'created_by' => 1,
'updated_by' => 1,
]);
}
}
}
}
}
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
/**
*+------------------
* madong
*+------------------
* Copyright (c) https://gitee.com/motion-code All rights reserved.
*+------------------
* Author: Mr. April (405784684@qq.com)
*+------------------
* Official Website: https://madong.tech
*/
namespace app\service\core\install\traits;
/**
* 安装数据库基础 Trait
*/
trait InstallDatabaseTrait
{
protected string $prefix;
protected ?\PDO $pdo = null;
protected array $dbConfig = [];
protected int $currentTime;
/**
* 初始化配置
*/
protected function initDbConfig(array $dbConfig): void
{
$this->dbConfig = $dbConfig;
$this->prefix = $dbConfig['prefix'] ?? 'ma_';
$this->currentTime = time();
}
/**
* 获取 PDO 连接(单例)
*/
protected function getPdo(): \PDO
{
if ($this->pdo === null) {
$config = $this->dbConfig;
$dsn = sprintf(
'mysql:host=%s;port=%s;dbname=%s;charset=utf8mb4',
$config['host'] ?? '127.0.0.1',
$config['port'] ?? '3306',
$config['database'] ?? 'md_admin'
);
$this->pdo = new \PDO(
$dsn,
$config['username'] ?? 'root',
$config['password'] ?? '',
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]
);
}
return $this->pdo;
}
/**
* 获取带前缀的表名
*/
protected function table(string $name): string
{
return $this->prefix . $name;
}
/**
* 统一插入数据
*/
protected function insert(string $tableName, array $data): bool
{
$fields = array_keys($data);
$values = array_values($data);
$placeholders = array_fill(0, count($values), '?');
$sql = "INSERT INTO `{$tableName}` (`" . implode('`, `', $fields) . "`) VALUES (" . implode(', ', $placeholders) . ")";
$stmt = $this->getPdo()->prepare($sql);
return $stmt->execute($values);
}
}
@@ -0,0 +1,171 @@
<?php
declare(strict_types=1);
/**
*+------------------
* madong
*+------------------
* Copyright (c) https://gitee.com/motion-code All rights reserved.
*+------------------
* Author: Mr. April (405784684@qq.com)
*+------------------
* Official Website: https://madong.tech
*/
namespace app\service\core\install\traits;
use core\uuid\Snowflake;
/**
* 菜单导入 Trait
*/
trait MenuTrait
{
/**
* 运行菜单种子
*/
public function runMenu(): void
{
$this->runAdminMenu();
$this->runWebMenu();
}
/**
* 运行管理端菜单种子
*/
public function runAdminMenu(): void
{
$menuFile = base_path('resource/data/menu/admin.php');
if (!file_exists($menuFile)) {
return;
}
$menus = require $menuFile;
$menuTable = $this->table('sys_menu');
$this->getPdo()->exec("TRUNCATE TABLE `{$menuTable}`");
foreach ($menus as $menu) {
$this->insertAdminMenu($menuTable, $menu, 0);
}
}
/**
* 运行Web端菜单种子
*/
public function runWebMenu(): void
{
$menuFile = base_path('resource/data/menu/web.php');
if (!file_exists($menuFile)) {
return;
}
$menus = require $menuFile;
$menuTable = $this->table('web_menu');
$this->getPdo()->exec("TRUNCATE TABLE `{$menuTable}`");
foreach ($menus as $menu) {
$this->insertWebMenu($menuTable, $menu, 0);
}
}
/**
* 递归插入管理端菜单
*/
private function insertAdminMenu(string $tableName, array $menu, int|string $pid): int|string
{
$id = Snowflake::generate();
$data = [
'id' => $id,
'pid' => $pid,
'app' => $menu['app'] ?? 'admin',
'source' => $menu['source'] ?? 'system',
'title' => $menu['title'] ?? '',
'code' => $menu['code'] ?? '',
'level' => $this->getMenuLevel($pid),
'type' => $menu['type'] ?? 1,
'sort' => $menu['sort'] ?? 999,
'path' => $menu['path'] ?? '',
'component' => $menu['component'] ?? '',
'redirect' => $menu['redirect'] ?? '',
'icon' => $menu['icon'] ?? '',
'is_show' => $menu['is_show'] ?? 1,
'is_link' => $menu['is_link'] ?? 0,
'link_url' => $menu['link_url'] ?? '',
'enabled' => $menu['enabled'] ?? 1,
'open_type' => $menu['open_type'] ?? 0,
'is_cache' => $menu['is_cache'] ?? 0,
'is_sync' => $menu['is_sync'] ?? 1,
'is_affix' => $menu['is_affix'] ?? 0,
'is_global' => $menu['is_global'] ?? 0,
'variable' => $menu['variable'] ?? '',
'methods' => strtolower($menu['methods'] ?? 'get'),
'is_frame' => $menu['is_frame'] ?? 1,
'created_at' => $this->currentTime,
'created_by' => 0,
'updated_at' => $this->currentTime,
'updated_by' => 0,
];
$this->insert($tableName, $data);
if (!empty($menu['children'])) {
foreach ($menu['children'] as $child) {
$this->insertAdminMenu($tableName, $child, $id);
}
}
return $id;
}
/**
* 递归插入Web端菜单
*/
private function insertWebMenu(string $tableName, array $menu, int|string $pid): int|string
{
$id = (int)Snowflake::generate();
$data = [
'id' => $id,
'pid' => $pid,
'app' => $menu['app'] ?? 'web',
'category' => $menu['category'] ?? 1,
'source' => $menu['source'] ?? 'system',
'code' => $menu['code'] ?? '',
'name' => $menu['name'] ?? '',
'url' => $menu['url'] ?? '',
'icon' => $menu['icon'] ?? '',
'level' => $menu['level'] ?? 1,
'type' => $menu['type'] ?? 1,
'sort' => $menu['sort'] ?? 999,
'target' => $menu['target'] ?? 1,
'is_show' => $menu['is_show'] ?? 1,
'enabled' => $menu['enabled'] ?? 1,
'created_at' => $this->currentTime,
'updated_at' => $this->currentTime,
'deleted_at' => null,
];
$this->insert($tableName, $data);
if (!empty($menu['children'])) {
foreach ($menu['children'] as $child) {
$this->insertWebMenu($tableName, $child, $id);
}
}
return $id;
}
/**
* 获取菜单层级
*/
private function getMenuLevel(int|string $pid): int
{
if ($pid == 0) return 1;
$menuTable = $this->table('sys_menu');
$stmt = $this->getPdo()->prepare("SELECT `level` FROM `{$menuTable}` WHERE id = ?");
$stmt->execute([$pid]);
$parent = $stmt->fetch();
return ($parent['level'] ?? 1) + 1;
}
}