| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- class DBTable {
- public string $name;
- public string $tableName;
- public array $columnArray;
- public string $line = '<br/>';
- const array FORM_EXCLUDE_COLUMNS = ['create_date', 'update_date', 'is_del', 'id', 'city', 'area', 'province'];
- 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 echoEditHtml(): 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();
- 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 "<h4>命令后执行下面代码</h4>";
- echo "<p style='color: red;'>npx prettier --write $filePath</p>";
- }
- 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 = <<<typescript
- interface {$this->name} {$this->line}
- $content} {$this->line} {$this->line}
- typescript;
- return $template;
- }
- /**
- * 生成form默认值
- * @return string
- */
- private function getFormDefault(): string
- {
- $content = '';
- foreach ($this->columnArray as $column) {
- if (in_array($column['Field'], self::FORM_EXCLUDE_COLUMNS)) {
- continue;
- }
- $content.= (new DBColumn($column))->getDefaultValue() . $this->line;
- }
- return $content;
- }
- /**
- * 生成form验证规则 前后端需要保持一致
- * @return array
- */
- private function getFormAndRule(): array
- {
- $html = '';
- $ruleHtml = '';
- foreach ($this->columnArray as $column) {
- if (in_array($column['Field'], self::FORM_EXCLUDE_COLUMNS)) {
- continue;
- }
- list($template, $rule) = (new DBColumn($column))->getFormHtmlAndRule();
- $html.= $template;
- $ruleHtml.= $rule;
- }
- $content = <<<typescript
- <template>
- <ElForm ref="formRef" :model="formData" :rules="rules" label-width="auto">
- <el-row :gutter="20">
- {$html}
- <el-col :span="24">
- <ElFormItem label=" " prop="">
- <ElButton type="primary" @click="handleSubmit">提交</ElButton>
- </ElFormItem>
- </el-col>
- </el-row>
- </ElForm>
- </template>
- typescript;
- return [$content, $ruleHtml];
- }
- /**
- * 生成详情
- * @return array
- */
- public function getDetailHtml(): string
- {
- echo $this->getFormDefault();
- $html = '';
- foreach ($this->columnArray as $column) {
- if (in_array($column['Field'], self::DETAIL_EXCLUDE_COLUMNS)) {
- continue;
- }
- $html.= (new DBColumn($column))->getDetailHtml();
- }
- $content = <<<typescript
- <template>
- <div>
- <el-row :gutter="20" class="detail">
- {$html}
- </el-row>
- </div>
- </template>
- typescript;
- file_put_contents(PROJECT_PATH . "/protected/runtime/detail.vue", $content);
- return $content;
- }
- /**
- * 生成table字段配置
- * @return string
- */
- public function getTableHtml(): string
- {
- $tableContent = '';
- foreach ($this->columnArray as $column) {
- if (in_array($column['Field'], self::TABLE_EXCLUDE_COLUMNS)) {
- continue;
- }
- $arr = (new DBColumn($column))->getTableAttr();
- $tableContent.= $this->getHtmlWithJsTableArr($arr);
- }
- $template = <<<typescript
- $tableContent
- typescript;
- return $template;
- }
- private function getHtmlWithJsTableArr(array $arr): string
- {
- if (!$arr) {
- return '';
- }
- $str = '{';
- foreach ($arr as $k => $v) {
- $str.= " {$k}:'{$v}',";
- }
- return trim($str, ',') . ' },' . $this->line;
- }
- }
|