'; public function __construct(array $info) { $this->field = $info['Field']; $this->type = $info['Type']; $this->collation = $info['Collation']; $this->key = $info['Key']; $this->default = $info['Default']; $this->extra = $info['Extra']; $this->privileges = $info['Privileges']; $this->wholeComment = $info['Comment']; $this->comment = $info['Comment'] ? explode(' ', $info['Comment'])[0] : ''; $this->allowNull = empty($info['Null']) || $info['Null'] == 'YES'; } /** * 获取字段的ts类型 * @return string */ public function getTsType() :string { $type = 'string'; if (str_contains($this->field, 'ids')) { $type = 'number[]'; } elseif (Helper::hasAnyString($this->type, ['int', 'decimal', 'double', 'float'])) { if (Helper::hasAnyString($this->field, ['is_', 'can_', 'has_', 'status', '_status', 'type'])) { $type = '0|1'; } else { $type = 'number'; } } return $type; } /** * 获取字段的ts定义 * @return string */ public function getTsDefine() :string { $type = $this->getTsType(); $temp = $this->field . ": {$type}"; $comment = $this->comment ? " // {$this->comment}" : ''; return $temp . " {$comment}"; } /** * 获取字段的默认值 * @return string */ public function getDefaultValue() :string { $type = $this->getTsType(); if (in_array($type, ['number', '0|1'])) { $default = $this->default ? : 0; return $this->field . ": {$default},"; } else { $default = $this->default ? : ''; return $this->field . ": '{$default}',"; } } /** * 获取字段的表单html和验证规则 * TODO: 这里可能要vue,react, layui, ... 代码 * @return array */ public function getFormHtmlAndRule() :array { $html = ''; $rule = []; if (!$this->allowNull) { $rule[] = "{ required: true, message: '请输入{$this->comment}', trigger: 'blur' }"; } $type = $this->getTsType(); if ($type == 'string') { // 图片处理 if (Helper::hasAnyString($this->field, ['img', 'image'])) { // 单张图片 } elseif (Helper::hasAnyString($this->field, ['images', 'imgs'])) { // 多张图片 } elseif (Helper::hasAnyString($this->field, ['_id'])) { // select } elseif (Helper::hasAnyString($this->field, ['ids'])) { // 多选框 } else { // input $inputType = 'text'; $max = $this->getMaxLength(); $min = $this->getMinLength(); if ($max > 64) { $inputType = 'textarea'; } $html = $this->getInputHtml($inputType, $max); // rule if (Helper::hasAnyString($this->field, ['phone', 'tel'])) { $rule[] = "{ pattern: /^1[3456789]\\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }"; } else { if ($min > 0 && $max > 0) { $rule[] = "{ min: {$min}, max: {$max}, message: '长度在{$min}到{$max}个字符', trigger: 'blur' }"; } elseif ($min > 0) { $rule[] = "{ min: {$min}, message: '长度最少{$min}个字符', trigger: 'blur' }"; } elseif ($max > 0) { $rule[] = "{ max: {$max}, message: '长度最多{$max}个字符', trigger: 'blur' }"; } } } } elseif ($type == 'number') { $html = $this->getInputHtml('number'); } elseif ($type == '0|1') { $rule = ''; $html = $this->getSelectHtml(); } $ruleHtml = ''; if ($rule) { $rule = implode(',', $rule); $ruleHtml = $this->field . ": [{$rule}],"; } return [$html, $ruleHtml]; } /** * 获取字段的表单html和验证规则 * TODO: 这里可能要vue,react, layui, ... 代码 * @return array */ public function getDetailHtml() :string { $html = "{{ info.{$this->field} }}"; $type = $this->getTsType(); $textType = 'string'; if ($type == 'string') { // 图片处理 if (Helper::hasAnyString($this->field, ['img', 'image'])) { // 单张图片 } elseif (Helper::hasAnyString($this->field, ['images', 'imgs'])) { // 多张图片 } elseif (Helper::hasAnyString($this->field, ['_id'])) { // select } elseif (Helper::hasAnyString($this->field, ['ids'])) { // 多选框 } else { // input $max = $this->getMaxLength(); if ($max > 64 || Helper::hasAnyString($this->field, ['text'])) { $textType = 'textarea'; } } } elseif ($type == '0|1') { $data = $this->getCommentArr(); $html = "{{ info.{$this->field} ? '{$data[1]}' : '$data[0]'}}"; } $elHtml = $textType != 'textarea' ? ':xs="24" :lg="8" :sm="12"' : ':span=24'; return << {$html} Detail; } /** * 生成input * @param string $type * @param int $maxLength * @return string */ public function getInputHtml(string $type, int $maxLength = 0): string { $maxLengthHtml = ''; if ($type != 'number' && $maxLength > 0) { $maxLengthHtml = "maxlength='{$maxLength}'"; } $elHtml = $type != 'textarea' ? ':xs="24" :lg="8" :sm="12"' : ':span=24'; return << input; } /** * 生成select * @return string */ public function getSelectHtml(): string { $selectData = $this->getCommentArr(); if (!$selectData) { return ''; } $data = ''; foreach ($selectData as $key => $item) { if (is_numeric($key)) { $data.= "{label: '{$item}', value: {$key}}, "; } else { $data.= "{label: '{$item}', value: '{$key}'},"; } } return <<