You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
423 lines
12 KiB
423 lines
12 KiB
3 years ago
|
<?php
|
||
|
/**
|
||
|
*
|
||
|
* Baseics.php
|
||
|
* User: ChenLong
|
||
|
* DateTime: 2020/3/9 12:37
|
||
|
*/
|
||
|
|
||
|
|
||
|
namespace sdModule\makeAdminBasics;
|
||
|
|
||
|
|
||
|
use app\common\ResponseJson;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormCheckbox;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormDate;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormEditor;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormImage;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormMonth;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormRadio;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormRange;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormSelect;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormText;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormTextarea;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\FormTime;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\Js;
|
||
|
use sdModule\makeAdminBasics\htmlUnit\JsFacade;
|
||
|
|
||
|
class Basics
|
||
|
{
|
||
|
/**
|
||
|
* 缩进之前换行
|
||
|
*/
|
||
|
const BEFORE = 1;
|
||
|
/**
|
||
|
* 缩进之后换行
|
||
|
*/
|
||
|
const AFTER = 2;
|
||
|
|
||
|
/**
|
||
|
* 缩进
|
||
|
*/
|
||
|
const INDENT = ' ';
|
||
|
|
||
|
/**
|
||
|
* @return string|\think\response\Json
|
||
|
* @throws \ReflectionException
|
||
|
*/
|
||
|
public static function auxCall()
|
||
|
{
|
||
|
$make = new self();
|
||
|
|
||
|
if (isset($_GET['table_name'])) {
|
||
|
$tableFieldInfo = $make->getFieldInfo($_GET['table_name']);
|
||
|
return ResponseJson::mixin($tableFieldInfo ?: '请确认表名,没有查到该表信息');
|
||
|
}elseif (isset($_GET['table'])) {
|
||
|
return ResponseJson::success();
|
||
|
}
|
||
|
|
||
|
if (!empty($_POST)) {
|
||
|
return ResponseJson::mixin((new Make($_POST))->make());
|
||
|
}
|
||
|
|
||
|
return $make->loadFromForm();
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 加载辅助页面
|
||
|
* @return string
|
||
|
*/
|
||
|
public function loadFromForm()
|
||
|
{
|
||
|
$layuiDir = ConfigHelper::get('layui_dir');
|
||
|
$tableUrl = ConfigHelper::get('get_table_info_url');
|
||
|
$submitUrl = ConfigHelper::get('submit_url');
|
||
|
$makeItem = ConfigHelper::get('make_class');
|
||
|
|
||
|
$formModule = [];
|
||
|
foreach ( ConfigHelper::get('form_module') as $key => $value){
|
||
|
$formModule[] = [
|
||
|
'value' => $key,
|
||
|
'text' => $value[1]
|
||
|
];
|
||
|
}
|
||
|
|
||
|
include __DIR__ . '/src/formHtml.php';
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取字段详情信息
|
||
|
* @param string $table 表名
|
||
|
* @param bool $isPrefix 是否包含表前缀
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function getFieldInfo(string $table, bool $isPrefix = false)
|
||
|
{
|
||
|
$table_info = $this->getTableInfo($table, $isPrefix);
|
||
|
$d = array_map(function ($value) {
|
||
|
$value = $this->formDefault($value);
|
||
|
$value = $this->joinTable($value);
|
||
|
$value = $this->labelHandle($value);
|
||
|
$value = $this->showTypeHandle($value);
|
||
|
return $value;
|
||
|
}, $table_info);
|
||
|
return $d;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取表信息
|
||
|
* @param string $table
|
||
|
* @param bool $isPrefix
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function getTableInfo(string $table, bool $isPrefix = false)
|
||
|
{
|
||
|
$isPrefix or $table = ConfigHelper::get('database_prefix') . $table;
|
||
|
$sql = "select
|
||
|
`COLUMN_NAME` column_name,
|
||
|
DATA_TYPE column_type,
|
||
|
COLUMN_COMMENT column_comment,CHARACTER_MAXIMUM_LENGTH,column_default column_default
|
||
|
from
|
||
|
information_schema.columns
|
||
|
where
|
||
|
table_schema = :table_schema
|
||
|
AND
|
||
|
`table_name` = :table_names";
|
||
|
|
||
|
return $this->sqlExecute($sql, [
|
||
|
'table_names' => $table,
|
||
|
'table_schema' => ConfigHelper::get('database_name')
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 执行 sql
|
||
|
* @param string $sql
|
||
|
* @param array $param
|
||
|
* @return mixed
|
||
|
*/
|
||
|
private function sqlExecute(string $sql, array $param)
|
||
|
{
|
||
|
return ConfigHelper::get('database_connect')::query($sql, $param);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 处理表格展示的类型
|
||
|
* @param $value
|
||
|
* @return mixed
|
||
|
*/
|
||
|
protected function showTypeHandle($value)
|
||
|
{
|
||
|
if (in_array($value['form_type'], ['images', 'editor', 'textarea', 'password'])) {
|
||
|
$value['show_type'] = '';
|
||
|
}elseif($value['form_type'] == 'image'){
|
||
|
$value['show_type'] = 'image';
|
||
|
}else{
|
||
|
$value['show_type'] = 'text';
|
||
|
}
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* label 和选择值处理
|
||
|
* @param $value
|
||
|
* @return mixed
|
||
|
*/
|
||
|
protected function labelHandle($value)
|
||
|
{
|
||
|
$comment = strtr($value['column_comment'], [':' => ':', ',' => ',']);
|
||
|
$join = [];
|
||
|
if (strpos($comment, ':') !== false) {
|
||
|
$value['column_comment'] = substr($comment, 0, strpos($comment, ':'));
|
||
|
$v = substr($comment, strpos($comment, ':') + 1);
|
||
|
$arr = explode(',', $v);
|
||
|
|
||
|
foreach ($arr as $item) {
|
||
|
if (strpos($item, '=') !== false) {
|
||
|
$join[] = $item;
|
||
|
} else {
|
||
|
$join[] = substr($item, 0, 1) . '=' .substr($item, 1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$join and $value['join'] = $join;
|
||
|
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 默认表单类型
|
||
|
* @param $value
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function formDefault($value)
|
||
|
{
|
||
|
$default = [
|
||
|
'char' => 'text',
|
||
|
'varchar' => 'text',
|
||
|
'datetime' => 'datetime',
|
||
|
'date' => 'date',
|
||
|
'text' => 'editor',
|
||
|
'int' => 'text',
|
||
|
'tinyint' => 'select',
|
||
|
'string' => 'text'
|
||
|
];
|
||
|
|
||
|
$except = ['id', 'delete_time', 'create_time', 'update_time'];
|
||
|
|
||
|
if (in_array($value['column_name'] ,$except)){
|
||
|
$value['form_type'] = '';
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
switch (true) {
|
||
|
case strpos($value['column_name'], 'images') !== false:
|
||
|
$value['form_type'] = 'images';
|
||
|
break;
|
||
|
|
||
|
case strpos($value['column_name'], 'password') !== false:
|
||
|
$value['form_type'] = 'password';
|
||
|
break;
|
||
|
|
||
|
case strpos($value['column_name'], 'image') !== false
|
||
|
|| strpos($value['column_name'], 'img') !== false
|
||
|
|| strpos($value['column_name'], 'avatar') !== false
|
||
|
|| strpos($value['column_name'], 'cover') !== false :
|
||
|
$value['form_type'] = 'image';
|
||
|
break;
|
||
|
|
||
|
case ($value['column_type'] == 'char' || $value['column_type'] == 'varchar')
|
||
|
&& $value['CHARACTER_MAXIMUM_LENGTH'] > 255:
|
||
|
$value['form_type'] = 'textarea';
|
||
|
break;
|
||
|
|
||
|
case $value['column_type'] == 'tinyint'
|
||
|
&& count(explode(',', strtr($value['column_comment'], [',' => ',']))) <= 3:
|
||
|
$value['form_type'] = 'radio';
|
||
|
break;
|
||
|
|
||
|
case isset($default[$value['column_type']]):
|
||
|
$value['form_type'] = $default[$value['column_type']];
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
$value['form_type'] = '';
|
||
|
}
|
||
|
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 关联表处理
|
||
|
* @param $value
|
||
|
* @return mixed
|
||
|
*/
|
||
|
protected function joinTable($value)
|
||
|
{
|
||
|
if (substr($value['column_name'], -3) !== '_id' && $value['column_name'] !== 'pid') return $value;
|
||
|
|
||
|
$table = $value['column_name'] === 'pid' ? '' : substr($value['column_name'], 0, -3);
|
||
|
|
||
|
$primary = $this->primary($table ?: $_GET['table_name']);
|
||
|
$labelData = $this->getTableInfo($table ?: $_GET['table_name']);
|
||
|
|
||
|
if (!$labelData) return $value;
|
||
|
|
||
|
foreach ($labelData as $item) {
|
||
|
if (in_array($item['column_type'], ['char', 'varchar'])) {
|
||
|
$showField = $item['column_name'];
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
$value['form_type'] = 'select';
|
||
|
$value['join'] = empty($showField) ? '' : "{$table}:{$primary}={$showField}";
|
||
|
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取主键
|
||
|
* @param $table
|
||
|
* @return mixed|string
|
||
|
*/
|
||
|
private function primary($table)
|
||
|
{
|
||
|
$sql = "SELECT
|
||
|
k.column_name
|
||
|
FROM
|
||
|
information_schema.table_constraints t
|
||
|
JOIN information_schema.key_column_usage k USING (
|
||
|
`constraint_name`,
|
||
|
table_schema,
|
||
|
`table_name`
|
||
|
)
|
||
|
WHERE
|
||
|
t.constraint_type = 'PRIMARY KEY'
|
||
|
AND t.table_schema = :schemas
|
||
|
AND t.table_name = :tables;
|
||
|
";
|
||
|
|
||
|
$result = $this->sqlExecute($sql, [
|
||
|
'schemas' => ConfigHelper::get('database_name'),
|
||
|
'tables' => ConfigHelper::get('database_prefix') . $table
|
||
|
]);
|
||
|
|
||
|
if ($result && count($result) == 1) {
|
||
|
return $result[0]['column_name'];
|
||
|
}
|
||
|
return '';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取表注释
|
||
|
* @param string $table
|
||
|
* @param bool $isPrefix
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function getTableComment($table = '', $isPrefix = false)
|
||
|
{
|
||
|
$sql = "SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = :table_names AND TABLE_SCHEMA = :schemas LIMIT 1";
|
||
|
|
||
|
$isPrefix or $table = ConfigHelper::get('database_prefix') . $table;
|
||
|
|
||
|
$result = $this->sqlExecute($sql, [
|
||
|
'table_names' => $table,
|
||
|
'schemas' => ConfigHelper::get('database_name')
|
||
|
]);
|
||
|
|
||
|
if ($result && !empty($result[0]) && is_array($result[0])) {
|
||
|
return $result[0]['TABLE_COMMENT'];
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @param $var
|
||
|
* @param bool $return
|
||
|
* @return mixed|string|string[]|null|void
|
||
|
*/
|
||
|
public static function varExport($var, $return = false)
|
||
|
{
|
||
|
$export = var_export($var, true);
|
||
|
$export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export);
|
||
|
$array = preg_split("/\r\n|\n|\r/", $export);
|
||
|
$array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [null, ']$1', ' => ['], $array);
|
||
|
$export = join(PHP_EOL, array_filter(["["] + $array));
|
||
|
if ((bool)$return) return $export; else echo $export;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 缩进换行
|
||
|
* @param int $number 缩进次数
|
||
|
* @param null $lineFeed 换行位置,默认不换行,1 缩进之前换行,2 缩进之后换行
|
||
|
* @return string
|
||
|
*/
|
||
|
public static function indentAndLineFeed($number, $lineFeed = null)
|
||
|
{
|
||
|
$str = str_repeat(self::INDENT, $number);
|
||
|
|
||
|
if ($lineFeed)
|
||
|
return $lineFeed == self::BEFORE ? PHP_EOL . $str : $str . PHP_EOL;
|
||
|
else
|
||
|
return $str;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 转数组合法代码
|
||
|
* @param array $array
|
||
|
* @param int $indent
|
||
|
* @param bool $notKey
|
||
|
* @return string|void
|
||
|
*/
|
||
|
public static function toLegalArrayCode(array $array, int $indent = 2, $notKey = false)
|
||
|
{
|
||
|
if ($indent === 0 && !$notKey) {
|
||
|
return self::toLegalArrayCodeNotLineFeed($array);
|
||
|
}
|
||
|
|
||
|
if ($notKey) {
|
||
|
return self::toLegalArrayCodeLineFeedNotKey($array, $indent);
|
||
|
}
|
||
|
|
||
|
$code = '[';
|
||
|
foreach ($array as $key => $value) {
|
||
|
$value = preg_match('/^\|.+|\n+\|$/', $value) ? strtr($value, ['|' => '']) : "'{$value}'";
|
||
|
$code .= self::indentAndLineFeed($indent, self::BEFORE) .
|
||
|
"'{$key}' => {$value},";
|
||
|
}
|
||
|
|
||
|
return $code . self::indentAndLineFeed($indent - 1, self::BEFORE) . ']';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 数组合法的字符串代码 @不换行
|
||
|
* @param array $array
|
||
|
* @return string
|
||
|
*/
|
||
|
private static function toLegalArrayCodeNotLineFeed(array $array)
|
||
|
{
|
||
|
return '[\'' . implode("','", $array) . '\']';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param array $array
|
||
|
* @param int $indent
|
||
|
* @return string
|
||
|
*/
|
||
|
private static function toLegalArrayCodeLineFeedNotKey(array $array, int $indent)
|
||
|
{
|
||
|
$code = '[%s\'' . implode("'," . self::indentAndLineFeed($indent, self::BEFORE) . "'", $array) . '\'%1$s]';
|
||
|
return sprintf($code, self::indentAndLineFeed($indent - 1, self::BEFORE));
|
||
|
}
|
||
|
}
|
||
|
|