zory 3 weeks ago
parent
commit
39f03fbd60
5 changed files with 146 additions and 57 deletions
  1. 6 0
      .env
  2. 133 0
      app/command/Model.php
  3. 0 50
      app/command/ShopwwiAuthCommand.php
  4. 1 1
      app/functions.php
  5. 6 6
      config/think-orm.php

+ 6 - 0
.env

@@ -0,0 +1,6 @@
+DB_HOST = 127.0.0.1
+DB_PORT = 3306
+DB_NAME = test
+DB_USER = foo
+DB_PASSWORD = 123456
+DB_DEFAULT = mysql

+ 133 - 0
app/command/Model.php

@@ -0,0 +1,133 @@
+<?php
+
+namespace app\command;
+
+use support\think\Db;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use think\db\exception\BindParamException;
+use Webman\Console\Util;
+
+class Model extends Command
+{
+
+
+    protected static string $defaultName = 'model:all';
+    protected static string $defaultDescription = '统一生成Model';
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $output->writeln("生成Model开始====>".getDateFull());
+        try {
+            $table = Db::query("SHOW TABLES");
+        } catch (BindParamException $e) {
+
+        }
+        $connection = config("think-orm.default");
+        $database = config("think-orm.connections.$connection.database");
+        foreach ($table as $val) {
+            $dir = explode("_",$val["Tables_in_{$database}"]);
+            $dirPath = app_path("model").DIRECTORY_SEPARATOR.$dir[0];
+            $namespace = str_replace('/', '\\', 'app/model/' . $dir[0]);
+            $class = Util::nameToClass($val["Tables_in_{$database}"]);
+            $file = $dirPath . DIRECTORY_SEPARATOR . "$class.php";
+            if (file_exists($file)) { // 存在,跳过
+                $output->writeln("已存在,跳过====>".getDateFull()."====>".$file);
+            } else {
+                $path = pathinfo($file, PATHINFO_DIRNAME);
+                if (!is_dir($path)) {
+                    mkdir($path, 0777, true);
+                }
+                $properties = '';
+                $pk = 'id';
+                foreach (Db::query("select COLUMN_NAME,DATA_TYPE,COLUMN_KEY,COLUMN_COMMENT from INFORMATION_SCHEMA.COLUMNS where table_name = '".$val["Tables_in_{$database}"]."' and table_schema = '$database' ORDER BY ordinal_position") as $item) {
+                    if ($item['COLUMN_KEY'] === 'PRI') {
+                        $pk = $item['COLUMN_NAME'];
+                        $item["COLUMN_COMMENT"] .= "(主键)";
+                    }
+                    $type = $this->getType($item["DATA_TYPE"]);
+                    $properties .= " * @property $type \${$item["COLUMN_NAME"]} {$item["COLUMN_COMMENT"]}\n";
+                }
+                $properties = rtrim($properties) ?: ' *';
+                $table_val = $val["Tables_in_{$database}"];
+                $model_content = <<<EOF
+<?php
+
+namespace $namespace;
+
+use app\\extra\\basic\\Model;
+
+
+/**
+$properties
+ */
+class $class extends Model
+{
+    /**
+     * The connection name for the model.
+     *
+     * @var string|null
+     */
+    protected \$connection = 'mysql';
+    
+    /**
+     * The table associated with the model.
+     *
+     * @var string
+     */
+    protected string \$table = "$table_val";
+    
+    /**
+     * The primary key associated with the table.
+     *
+     * @var string
+     */
+    protected string \$primaryKey = "$pk";
+    
+    /**
+     * Indicates if the model should be timestamped.
+     *
+     * @var bool
+     */
+    public bool \$timestamps = false;
+
+
+}
+
+EOF;
+                file_put_contents($file, $model_content);
+                $output->writeln("生成成功====>".getDateFull()."====>".$file);
+            }
+        }
+        return self::SUCCESS;
+    }
+
+
+    protected function getType(string $type): string
+    {
+        if (str_contains($type, 'int')) {
+            return 'integer';
+        }
+        switch ($type) {
+            case 'varchar':
+            case 'string':
+            case 'text':
+            case 'date':
+            case 'time':
+            case 'guid':
+            case 'datetimetz':
+            case 'datetime':
+            case 'decimal':
+            case 'enum':
+                return 'string';
+            case 'boolean':
+                return 'integer';
+            case 'float':
+                return 'float';
+            default:
+                return 'mixed';
+        }
+    }
+
+}

+ 0 - 50
app/command/ShopwwiAuthCommand.php

@@ -1,50 +0,0 @@
-<?php
-
-namespace app\command;
-
-use Shopwwi\WebmanAuth\Facade\Str;
-use Symfony\Component\Console\Command\Command;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Input\InputOption;
-use Symfony\Component\Console\Input\InputArgument;
-use Symfony\Component\Console\Output\OutputInterface;
-
-
-class ShopwwiAuthCommand extends Command
-{
-    protected static $defaultName = 'shopwwi:auth';
-    protected static $defaultDescription = 'shopwwi auth';
-
-    /**
-     * @return void
-     */
-    protected function configure()
-    {
-        $this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
-    }
-
-    /**
-     * @param InputInterface $input
-     * @param OutputInterface $output
-     * @return int
-     */
-    protected function execute(InputInterface $input, OutputInterface $output)
-    {
-        $name = $input->getArgument('name');
-        $output->writeln('生成jwtKey 开始');
-        $key = Str::random(64);
-        file_put_contents(base_path()."/config/plugin/shopwwi/auth/app.php", str_replace(
-            "'access_secret_key' => '".config('plugin.shopwwi.auth.app.jwt.access_secret_key')."'",
-            "'access_secret_key' => '".$key."'",
-            file_get_contents(base_path()."/config/plugin/shopwwi/auth/app.php")
-        ));
-        file_put_contents(base_path()."/config/plugin/shopwwi/auth/app.php", str_replace(
-            "'refresh_secret_key' => '".config('plugin.shopwwi.auth.app.jwt.refresh_secret_key')."'",
-            "'refresh_secret_key' => '".$key."'",
-            file_get_contents(base_path()."/config/plugin/shopwwi/auth/app.php")
-        ));
-        $output->writeln('生成jwtKey 结束'.$key);
-        return self::SUCCESS;
-    }
-
-}

+ 1 - 1
app/functions.php

@@ -236,7 +236,7 @@ if (!function_exists("getDateFull"))
     function getDateFull(string $format = "Y-m-d H:i:s",string $time = ""): string
     {
         if (empty($time)) $time = time();
-        return date($format);
+        return date($format,$time);
     }
 }
 

+ 6 - 6
config/think-orm.php

@@ -1,21 +1,21 @@
 <?php
 
 return [
-    'default' => 'mysql',
+    'default' => getenv('DB_DEFAULT'),
     'connections' => [
         'mysql' => [
             // 数据库类型
             'type' => 'mysql',
             // 服务器地址
-            'hostname' => '127.0.0.1',
+            'hostname' => getenv('DB_HOST'),
             // 数据库名
-            'database' => 'test',
+            'database' => getenv('DB_NAME'),
             // 数据库用户名
-            'username' => 'root',
+            'username' => getenv('DB_USER'),
             // 数据库密码
-            'password' => '123456',
+            'password' => getenv('DB_PASSWORD'),
             // 数据库连接端口
-            'hostport' => '3306',
+            'hostport' => getenv('DB_PORT'),
             // 数据库连接参数
             'params' => [
                 // 连接超时3秒