'; const array FORM_EXCLUDE_COLUMNS = ['create_date', 'update_date', 'is_del', 'id', 'city', 'area', 'province', 'last_user_id', 'last_date']; const array INFO_EXCLUDE_COLUMNS = ['create_date', 'update_date', 'is_del']; const array TABLE_EXCLUDE_COLUMNS = ['create_date', 'update_date', 'is_del']; const array DETAIL_EXCLUDE_COLUMNS = ['is_del']; public function __construct(string $name) { $this->name = strtolower($name); $this->tableName = DB::formTableName($name); $sql = "show full columns from {$this->tableName }"; $this->columnArray = \Yii::app()->db->createCommand($sql)->queryAll(); if (!$this->columnArray) { Helper::error('该表不存在'); } } public function echoEditPhp(): void { $notNullField = []; $allowEmptyField = []; echo '$data = [' . $this->line; foreach ($this->columnArray as $column) { if (in_array($column['Field'], self::FORM_EXCLUDE_COLUMNS)) { continue; } $name = $column['Field']; $tmpl = "'{$name}' => Helper::{{method}}('{$name}')," . $this->line; if (Helper::hasAnyString($column['Type'], ['decimal', 'double', 'float'])) { $method = 'getPostFloat'; } elseif (Helper::hasAnyString($column['Type'], ['int'])) { $method = 'getPostInt'; } elseif (Helper::hasAnyString($column['Type'], ['datetime'])) { $method = 'getPostDatetime'; } elseif (Helper::hasAnyString($column['Type'], ['date'])) { $method = 'getPostDate'; } else { $method = 'getPostString'; } echo str_replace('{{method}}', $method, $tmpl); if ($column['Null'] == 'NO') { $notNullField[] = $name; } if ($column['Default'] != null || $method == 'getPostInt') { $allowEmptyField[] = $name; } } echo '];' . $this->line . $this->line; echo '$notNullField = ' . json_encode($notNullField) . ';' . $this->line; echo '$allowEmptyField = ' . json_encode($allowEmptyField) . ';' . $this->line; } public function editVue() { $formDefault = $this->getFormDefault(false); list($template, $rule) = $this->getFormAndRule(); $editTmpl = str_replace( ['{{template}}', '{{table}}', '{{formDefault}}', '{{formRule}}', '{{ucTable}}'], [$template, $this->name, $formDefault, $rule, ucfirst($this->name)], file_get_contents(RUNTIME_PATH . '/edit.tmpl') ); $filePath = RUNTIME_PATH . "/edit.vue"; file_put_contents($filePath, $editTmpl); // 格式化代码 echo "
npx prettier --write $filePath
"; } public function echoDetail() { echo $this->getFormDefault(); list($template, $rule) = $this->getFormAndRule(); } /** * 生成ts接口 * @return string */ public function getTsInterFace(): string { $content = ''; foreach ($this->columnArray as $column) { if (in_array($column['Field'], self::INFO_EXCLUDE_COLUMNS)) { continue; } $content.= (new DBColumn($column))->getTsDefine() . $this->line; } $template = <<