diff --git a/database/migrations/20260816204000_system_mcp_menu_seed.php b/database/migrations/20260816204000_system_mcp_menu_seed.php new file mode 100644 index 0000000..b5624e0 --- /dev/null +++ b/database/migrations/20260816204000_system_mcp_menu_seed.php @@ -0,0 +1,88 @@ +getAdapter()->getOption('table_prefix'); + $now = time(); + $rows = [ + [ + 'id' => 312, + 'title' => 'MCP密钥管理', + 'icon' => 'fa fa-key', + 'href' => 'system.mcp_key/index', + 'auth_node' => 'system.mcp_key/index', + ], + [ + 'id' => 313, + 'title' => 'MCP调用日志', + 'icon' => 'fa fa-file-text-o', + 'href' => 'system.mcp_log/index', + 'auth_node' => 'system.mcp_log/index', + ], + ]; + + foreach ($rows as $row) { + $href = $this->quoteLiteral($row['href']); + $exists = $this->fetchRow( + "SELECT `id` FROM `{$prefix}system_menu` WHERE `href` = {$href} AND `delete_time` = 0" + ); + if (!empty($exists)) { + continue; + } + + $sql = sprintf( + "INSERT INTO `%ssystem_menu` (`id`, `pid`, `title`, `icon`, `href`, `auth_node`, `params`, `target`, `sort`, `status`, `remark`, `create_time`, `update_time`, `delete_time`)" + . " VALUES (%d, 228, %s, %s, %s, %s, '', '_self', 0, 1, '', %d, %d, 0)", + $prefix, + $row['id'], + $this->quoteLiteral($row['title']), + $this->quoteLiteral($row['icon']), + $this->quoteLiteral($row['href']), + $this->quoteLiteral($row['auth_node']), + $now, + $now + ); + $this->execute($sql); + } + } + + public function down() + { + $prefix = $this->getAdapter()->getOption('table_prefix'); + $hrefs = implode(', ', [ + $this->quoteLiteral('system.mcp_key/index'), + $this->quoteLiteral('system.mcp_log/index'), + ]); + $this->execute("DELETE FROM `{$prefix}system_menu` WHERE `href` IN ({$hrefs})"); + } + + /** + * MySQL 字面量包装:单引号包裹 + 反斜杠转义(照 XhprofWatchSeed 惯例). + */ + private function quoteLiteral(string $value): string + { + return "'" . addcslashes($value, "\\") . "'"; + } +}