common.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. use think\facade\Db;
  3. if (!function_exists('get_controller_list')) {
  4. function get_controller_list($app = 'admin'): array
  5. {
  6. $controllerDir = root_path() . 'app' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;
  7. return get_dir_files($controllerDir);
  8. }
  9. }
  10. if (!function_exists('get_dir_files')) {
  11. function get_dir_files($dir): array
  12. {
  13. $files = new RecursiveIteratorIterator(
  14. new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY
  15. );
  16. $fileList = [];
  17. foreach ($files as $file) {
  18. if (!$file->isDir() && $file->getExtension() == 'php') {
  19. $filePath = $file->getRealPath();
  20. $name = str_replace($dir, '', $filePath);
  21. $name = str_replace(DIRECTORY_SEPARATOR, "/", $name);
  22. $fileList[$name] = $name;
  23. }
  24. }
  25. return $fileList;
  26. }
  27. }
  28. if (!function_exists('get_table_list')) {
  29. function get_table_list(): array
  30. {
  31. $tableList = [];
  32. $database = config('database.connections.mysql.database');
  33. $tables = Db::query("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema = ? ", [$database]);
  34. foreach ($tables as $row) {
  35. $tableList[$row['TABLE_NAME']] = $row['TABLE_NAME'] . ($row['TABLE_COMMENT'] ? ' - ' . str_replace('表', '', $row['TABLE_COMMENT']) : '');
  36. }
  37. return $tableList;
  38. }
  39. }
  40. if (!function_exists('get_table_fields')) {
  41. function get_table_fields($table, $onlyCleanComment = false): array
  42. {
  43. if (!$table) return [];
  44. $dbname = config('database.connections.mysql.database');
  45. $prefix = config('database.connections.mysql.prefix');
  46. // 从数据库中获取表字段信息
  47. $sql = "SELECT * FROM `information_schema`.`columns` "
  48. . "WHERE TABLE_SCHEMA = ? AND table_name = ? "
  49. . "ORDER BY ORDINAL_POSITION";
  50. $columnList = Db::query($sql, [$dbname, $table]);
  51. if (!$columnList) {
  52. $columnList = Db::query($sql, [$dbname, $prefix . $table]);
  53. }
  54. $fieldList = [];
  55. foreach ($columnList as $item) {
  56. if ($onlyCleanComment) {
  57. $fieldList[$item['COLUMN_NAME']] = '';
  58. if ($item['COLUMN_COMMENT']) {
  59. $comment = explode(':', $item['COLUMN_COMMENT']);
  60. $fieldList[$item['COLUMN_NAME']] = $comment[0];
  61. }
  62. continue;
  63. }
  64. $fieldList[$item['COLUMN_NAME']] = $item;
  65. }
  66. return $fieldList;
  67. }
  68. }
  69. if (!function_exists('mb_ucfirst')) {
  70. function mb_ucfirst($string): string
  71. {
  72. return mb_strtoupper(mb_substr($string, 0, 1)) . mb_strtolower(mb_substr($string, 1));
  73. }
  74. }