|
@@ -165,9 +165,13 @@ class SystemCrudServices extends BaseServices
|
|
|
'value' => 'dateTime',
|
|
'value' => 'dateTime',
|
|
|
'label' => '单选日期时间',
|
|
'label' => '单选日期时间',
|
|
|
],
|
|
],
|
|
|
|
|
+// [
|
|
|
|
|
+// 'value' => 'dateTimeRange',
|
|
|
|
|
+// 'label' => '日期时间区间选择',
|
|
|
|
|
+// ],
|
|
|
[
|
|
[
|
|
|
- 'value' => 'dateTimeRange',
|
|
|
|
|
- 'label' => '日期时间区间选择',
|
|
|
|
|
|
|
+ 'value' => 'checkbox',
|
|
|
|
|
+ 'label' => '多选框',
|
|
|
],
|
|
],
|
|
|
[
|
|
[
|
|
|
'value' => 'radio',
|
|
'value' => 'radio',
|
|
@@ -303,7 +307,47 @@ class SystemCrudServices extends BaseServices
|
|
|
$changeFiled = addslashes($changeFiled);
|
|
$changeFiled = addslashes($changeFiled);
|
|
|
$type = addslashes($type);
|
|
$type = addslashes($type);
|
|
|
$default = addslashes($default);
|
|
$default = addslashes($default);
|
|
|
- $sql = "ALTER TABLE `$tableName` CHANGE `$field` `$changeFiled` $type($limit) NOT NULL DEFAULT '$default' COMMENT '$comment';";
|
|
|
|
|
|
|
+ if (in_array(strtolower($type), ['text', 'longtext', 'tinytext'])) {
|
|
|
|
|
+ $sql = "ALTER TABLE `$tableName` CHANGE `$field` `$changeFiled` $type CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '$comment';";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $sql = "ALTER TABLE `$tableName` CHANGE `$field` `$changeFiled` $type($limit) NOT NULL DEFAULT '$default' COMMENT '$comment';";
|
|
|
|
|
+ }
|
|
|
|
|
+ return Db::execute($sql);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 添加字段
|
|
|
|
|
+ * @param string $tableName
|
|
|
|
|
+ * @param string $field
|
|
|
|
|
+ * @param string $prevFiled
|
|
|
|
|
+ * @param string $type
|
|
|
|
|
+ * @param string $limit
|
|
|
|
|
+ * @param string $default
|
|
|
|
|
+ * @param string $comment
|
|
|
|
|
+ * @param array $options
|
|
|
|
|
+ * @return mixed
|
|
|
|
|
+ * @author 等风来
|
|
|
|
|
+ * @email 136327134@qq.com
|
|
|
|
|
+ * @date 2023/4/24
|
|
|
|
|
+ */
|
|
|
|
|
+ public function addAlter(string $tableName, string $field, string $prevFiled, string $type, $limit = '', string $default = '', string $comment = '', array $options = [])
|
|
|
|
|
+ {
|
|
|
|
|
+ $tableName = $this->getTableName($tableName);
|
|
|
|
|
+ $comment = addslashes($comment);
|
|
|
|
|
+ $field = addslashes($field);
|
|
|
|
|
+ $prevFiled = addslashes($prevFiled);
|
|
|
|
|
+ $type = addslashes($type);
|
|
|
|
|
+ $default = addslashes($default);
|
|
|
|
|
+ if ($prevFiled) {
|
|
|
|
|
+ $after = "AFTER `$prevFiled`";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $after = "";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (in_array(strtolower($type), ['text', 'longtext', 'tinytext'])) {
|
|
|
|
|
+ $sql = "ALTER TABLE `$tableName` ADD `$field` $type NULL COMMENT '$comment' $after;";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $sql = "ALTER TABLE `$tableName` ADD `$field` $type($limit) NOT NULL DEFAULT '$default' COMMENT '$comment' $after;";
|
|
|
|
|
+ }
|
|
|
return Db::execute($sql);
|
|
return Db::execute($sql);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -324,6 +368,23 @@ class SystemCrudServices extends BaseServices
|
|
|
return Db::execute($sql);
|
|
return Db::execute($sql);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 修改表备注
|
|
|
|
|
+ * @param string $tableName
|
|
|
|
|
+ * @param string $common
|
|
|
|
|
+ * @return mixed
|
|
|
|
|
+ * @author 等风来
|
|
|
|
|
+ * @email 136327134@qq.com
|
|
|
|
|
+ * @date 2023/4/24
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function updateFromCommon(string $tableName, string $common)
|
|
|
|
|
+ {
|
|
|
|
|
+ $tableName = $this->getTableName($tableName);
|
|
|
|
|
+ $common = addslashes($common);
|
|
|
|
|
+ $sql = "ALTER TABLE `$tableName` COMMENT = '$common';";
|
|
|
|
|
+ return Db::execute($sql);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 对比字段变动了更改
|
|
* 对比字段变动了更改
|
|
|
* @param string $tableName
|
|
* @param string $tableName
|
|
@@ -336,28 +397,57 @@ class SystemCrudServices extends BaseServices
|
|
|
protected function diffAlter(string $tableName, array $deleteField, array $tableField)
|
|
protected function diffAlter(string $tableName, array $deleteField, array $tableField)
|
|
|
{
|
|
{
|
|
|
$updateAlter = [];
|
|
$updateAlter = [];
|
|
|
|
|
+ $addAlter = [];
|
|
|
|
|
+
|
|
|
|
|
+ $columns = $this->getColumnNamesList($tableName);
|
|
|
|
|
+ $fieldAll = array_column($columns, 'name');
|
|
|
|
|
+ $prevFiled = $columns[count($columns) - 1]['name'] ?? '';
|
|
|
//对比数据库字段
|
|
//对比数据库字段
|
|
|
foreach ($tableField as $item) {
|
|
foreach ($tableField as $item) {
|
|
|
if ($item['primaryKey']) {
|
|
if ($item['primaryKey']) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ //前台新增的字段进行添加
|
|
|
if (!(isset($item['default_field']) &&
|
|
if (!(isset($item['default_field']) &&
|
|
|
isset($item['default_field_type']) &&
|
|
isset($item['default_field_type']) &&
|
|
|
isset($item['default_limit']) &&
|
|
isset($item['default_limit']) &&
|
|
|
isset($item['default_comment']) &&
|
|
isset($item['default_comment']) &&
|
|
|
isset($item['default_default']))
|
|
isset($item['default_default']))
|
|
|
) {
|
|
) {
|
|
|
|
|
+ if (!in_array($item['field'], $fieldAll)) {
|
|
|
|
|
+ $addAlter[] = [
|
|
|
|
|
+ 'prev_filed' => $prevFiled,
|
|
|
|
|
+ 'field' => $item['field'],
|
|
|
|
|
+ 'limit' => $item['limit'],
|
|
|
|
|
+ 'type' => $item['field_type'],
|
|
|
|
|
+ 'comment' => $item['comment'],
|
|
|
|
|
+ 'default' => $item['default'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
continue;
|
|
continue;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //默认字段没有在数据库中,需要添加字段
|
|
|
|
|
+ if (!in_array($item['default_field'], $fieldAll)) {
|
|
|
|
|
+ $addAlter[] = [
|
|
|
|
|
+ 'prev_filed' => $prevFiled,
|
|
|
|
|
+ 'field' => $item['field'],
|
|
|
|
|
+ 'limit' => $item['limit'],
|
|
|
|
|
+ 'type' => $item['field_type'],
|
|
|
|
|
+ 'comment' => $item['comment'],
|
|
|
|
|
+ 'default' => $item['default'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if ($item['default_field'] != $item['field'] && in_array($item['field_type'], ['addTimestamps', 'addSoftDelete'])) {
|
|
if ($item['default_field'] != $item['field'] && in_array($item['field_type'], ['addTimestamps', 'addSoftDelete'])) {
|
|
|
- throw new AdminException('伪删除字段不允许被更改');
|
|
|
|
|
|
|
+ throw new AdminException($item['field'] . '字段不允许被更改');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ //数据库表存在的,字段,并且被修改
|
|
|
if ($item['default_field'] != $item['field'] ||
|
|
if ($item['default_field'] != $item['field'] ||
|
|
|
- $item['default_field_type'] != $item['type'] ||
|
|
|
|
|
|
|
+ $item['default_field_type'] != $item['field_type'] ||
|
|
|
$item['default_limit'] != $item['limit'] ||
|
|
$item['default_limit'] != $item['limit'] ||
|
|
|
$item['default_comment'] != $item['comment'] ||
|
|
$item['default_comment'] != $item['comment'] ||
|
|
|
$item['default_default'] != $item['default']) {
|
|
$item['default_default'] != $item['default']) {
|
|
@@ -375,6 +465,10 @@ class SystemCrudServices extends BaseServices
|
|
|
foreach ($updateAlter as $item) {
|
|
foreach ($updateAlter as $item) {
|
|
|
$this->updateAlter($tableName, $item['default_field'], $item['field'], $item['type'], $item['limit'], $item['default'], $item['comment']);
|
|
$this->updateAlter($tableName, $item['default_field'], $item['field'], $item['type'], $item['limit'], $item['default'], $item['comment']);
|
|
|
}
|
|
}
|
|
|
|
|
+ //添加字段
|
|
|
|
|
+ foreach ($addAlter as $item) {
|
|
|
|
|
+ $this->addAlter($tableName, $item['field'], $item['prev_filed'], $item['type'], $item['limit'], $item['default'], $item['comment']);
|
|
|
|
|
+ }
|
|
|
//删除多余字段
|
|
//删除多余字段
|
|
|
foreach ($deleteField as $item) {
|
|
foreach ($deleteField as $item) {
|
|
|
$this->deleteAlter($tableName, $item);
|
|
$this->deleteAlter($tableName, $item);
|
|
@@ -393,14 +487,10 @@ class SystemCrudServices extends BaseServices
|
|
|
public function createCrud(int $id, array $data)
|
|
public function createCrud(int $id, array $data)
|
|
|
{
|
|
{
|
|
|
$tableName = $data['tableName'];
|
|
$tableName = $data['tableName'];
|
|
|
- $tableComment = $data['tableComment'] ?? $data['menuName'];
|
|
|
|
|
$tableField = $this->valueReplace($data['tableField']);
|
|
$tableField = $this->valueReplace($data['tableField']);
|
|
|
$filePath = $this->valueReplace($data['filePath']);
|
|
$filePath = $this->valueReplace($data['filePath']);
|
|
|
- $modelName = $data['modelName'] ?? $data['menuName'] ?? $tableName;
|
|
|
|
|
-
|
|
|
|
|
- if ($this->dao->value(['table_name' => $tableName])) {
|
|
|
|
|
- throw new AdminException(500048);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $modelName = !empty($data['modelName']) ? $data['modelName'] : $tableName;
|
|
|
|
|
+ $tableComment = !empty($data['tableComment']) ? $data['tableComment'] : $modelName;
|
|
|
|
|
|
|
|
//检测是否为系统表
|
|
//检测是否为系统表
|
|
|
if (in_array($tableName, self::NOT_CRUD_TABANAME)) {
|
|
if (in_array($tableName, self::NOT_CRUD_TABANAME)) {
|
|
@@ -411,6 +501,7 @@ class SystemCrudServices extends BaseServices
|
|
|
|
|
|
|
|
$tableInfo = null;
|
|
$tableInfo = null;
|
|
|
if ($id) {
|
|
if ($id) {
|
|
|
|
|
+ $this->updateFromCommon($tableName, $tableComment);
|
|
|
//删除数据库表
|
|
//删除数据库表
|
|
|
$tableInfo = $this->getTableInfo($tableName);
|
|
$tableInfo = $this->getTableInfo($tableName);
|
|
|
if ($tableInfo) {
|
|
if ($tableInfo) {
|
|
@@ -476,9 +567,16 @@ class SystemCrudServices extends BaseServices
|
|
|
|
|
|
|
|
$res = $this->transaction(function () use ($crudInfo, $tableInfo, $modelName, $filePath, $tableName, $routeName, $data, $dataMenu) {
|
|
$res = $this->transaction(function () use ($crudInfo, $tableInfo, $modelName, $filePath, $tableName, $routeName, $data, $dataMenu) {
|
|
|
$routeService = app()->make(SystemRouteServices::class);
|
|
$routeService = app()->make(SystemRouteServices::class);
|
|
|
|
|
+ $meunService = app()->make(SystemMenusServices::class);
|
|
|
//修改菜单名称
|
|
//修改菜单名称
|
|
|
if ($crudInfo) {
|
|
if ($crudInfo) {
|
|
|
- $menuInfo = app()->make(SystemMenusServices::class)->update($crudInfo->menu_id, $dataMenu);
|
|
|
|
|
|
|
+ //菜单存在的时候进行修改
|
|
|
|
|
+ if ($crudInfo->menu_id && $meunService->value(['id' => [$crudInfo->menu_id]], 'id')) {
|
|
|
|
|
+ $meunService->update($crudInfo->menu_id, $dataMenu);
|
|
|
|
|
+ $menuInfo = (object)['id' => $crudInfo->menu_id];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $menuInfo = $meunService->save($dataMenu);
|
|
|
|
|
+ }
|
|
|
//删除掉添加的路由权限
|
|
//删除掉添加的路由权限
|
|
|
if ($crudInfo->routes_id) {
|
|
if ($crudInfo->routes_id) {
|
|
|
$routeService->deleteRoutes($crudInfo->routes_id);
|
|
$routeService->deleteRoutes($crudInfo->routes_id);
|
|
@@ -488,7 +586,7 @@ class SystemCrudServices extends BaseServices
|
|
|
app()->make(SystemMenusServices::class)->deleteMenus($crudInfo->menu_ids);
|
|
app()->make(SystemMenusServices::class)->deleteMenus($crudInfo->menu_ids);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- $menuInfo = app()->make(SystemMenusServices::class)->save($dataMenu);
|
|
|
|
|
|
|
+ $menuInfo = $meunService->save($dataMenu);
|
|
|
}
|
|
}
|
|
|
//写入路由权限
|
|
//写入路由权限
|
|
|
$cateId = app()->make(SystemRouteServices::class)->topCateId('adminapi', 'CRUD');
|
|
$cateId = app()->make(SystemRouteServices::class)->topCateId('adminapi', 'CRUD');
|
|
@@ -557,7 +655,7 @@ class SystemCrudServices extends BaseServices
|
|
|
$menuData = [];
|
|
$menuData = [];
|
|
|
foreach ($ruleData as $item) {
|
|
foreach ($ruleData as $item) {
|
|
|
$menuData[] = [
|
|
$menuData[] = [
|
|
|
- 'pid' => $menuInfo->id,
|
|
|
|
|
|
|
+ 'pid' => $menuInfo->id ?: 0,
|
|
|
'methods' => $item['method'],
|
|
'methods' => $item['method'],
|
|
|
'api_url' => $item['path'],
|
|
'api_url' => $item['path'],
|
|
|
'unique_auth' => $item['unique_auth'],
|
|
'unique_auth' => $item['unique_auth'],
|
|
@@ -566,6 +664,7 @@ class SystemCrudServices extends BaseServices
|
|
|
'auth_type' => 2,
|
|
'auth_type' => 2,
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
$menus = app()->make(SystemMenusServices::class)->saveAll($menuData);
|
|
$menus = app()->make(SystemMenusServices::class)->saveAll($menuData);
|
|
|
$menuIds = array_column($menus->toArray(), 'id');
|
|
$menuIds = array_column($menus->toArray(), 'id');
|
|
|
//生成文件
|
|
//生成文件
|
|
@@ -584,6 +683,7 @@ class SystemCrudServices extends BaseServices
|
|
|
'table_collation' => $tableInfo['TABLE_COLLATION'] ?? '',
|
|
'table_collation' => $tableInfo['TABLE_COLLATION'] ?? '',
|
|
|
'field' => json_encode($data),//提交的数据
|
|
'field' => json_encode($data),//提交的数据
|
|
|
'menu_ids' => json_encode($menuIds),//生成的菜单id
|
|
'menu_ids' => json_encode($menuIds),//生成的菜单id
|
|
|
|
|
+ 'menu_id' => $menuInfo->id,//生成的菜单id
|
|
|
'make_path' => json_encode($makePath),
|
|
'make_path' => json_encode($makePath),
|
|
|
'routes_id' => json_encode($routeIds),
|
|
'routes_id' => json_encode($routeIds),
|
|
|
];
|
|
];
|
|
@@ -596,6 +696,8 @@ class SystemCrudServices extends BaseServices
|
|
|
$res = $this->dao->save($crudDate);
|
|
$res = $this->dao->save($crudDate);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// throw new ValidateException('测试中');
|
|
|
|
|
+
|
|
|
return $res;
|
|
return $res;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -692,6 +794,10 @@ class SystemCrudServices extends BaseServices
|
|
|
$timestamps = true;
|
|
$timestamps = true;
|
|
|
} else {
|
|
} else {
|
|
|
$option['comment'] = $item['comment'];
|
|
$option['comment'] = $item['comment'];
|
|
|
|
|
+ $fieldType = $this->changeTabelRule($item['field_type']);
|
|
|
|
|
+ if (in_array($fieldType, ['text', 'longtext', 'tinytext'])) {
|
|
|
|
|
+ unset($option['limit']);
|
|
|
|
|
+ }
|
|
|
$table->addColumn($item['field'], $this->changeTabelRule($item['field_type']), $option);
|
|
$table->addColumn($item['field'], $this->changeTabelRule($item['field_type']), $option);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|