初始化-安装依赖
This commit is contained in:
25
server/plugin/saiadmin/app/logic/tool/CrontabLogLogic.php
Normal file
25
server/plugin/saiadmin/app/logic/tool/CrontabLogLogic.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\app\logic\tool;
|
||||
|
||||
use plugin\saiadmin\app\model\tool\CrontabLog;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
|
||||
/**
|
||||
* 定时任务日志逻辑层
|
||||
*/
|
||||
class CrontabLogLogic extends BaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new CrontabLog();
|
||||
}
|
||||
|
||||
}
|
||||
247
server/plugin/saiadmin/app/logic/tool/CrontabLogic.php
Normal file
247
server/plugin/saiadmin/app/logic/tool/CrontabLogic.php
Normal file
@@ -0,0 +1,247 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\app\logic\tool;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Webman\Channel\Client as ChannelClient;
|
||||
use plugin\saiadmin\app\model\tool\Crontab;
|
||||
use plugin\saiadmin\app\model\tool\CrontabLog;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
|
||||
/**
|
||||
* 定时任务逻辑层
|
||||
*/
|
||||
class CrontabLogic extends BaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new Crontab();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加任务
|
||||
*/
|
||||
public function add($data): bool
|
||||
{
|
||||
$second = $data['second'];
|
||||
$minute = $data['minute'];
|
||||
$hour = $data['hour'];
|
||||
$week = $data['week'];
|
||||
$day = $data['day'];
|
||||
$month = $data['month'];
|
||||
|
||||
// 规则处理
|
||||
$rule = match ($data['task_style']) {
|
||||
1 => "0 {$minute} {$hour} * * *",
|
||||
2 => "0 {$minute} * * * *",
|
||||
3 => "0 {$minute} */{$hour} * * *",
|
||||
4 => "0 */{$minute} * * * *",
|
||||
5 => "*/{$second} * * * * *",
|
||||
6 => "0 {$minute} {$hour} * * {$week}",
|
||||
7 => "0 {$minute} {$hour} {$day} * *",
|
||||
8 => "0 {$minute} {$hour} {$day} {$month} *",
|
||||
default => throw new ApiException("任务类型异常"),
|
||||
};
|
||||
|
||||
// 定时任务模型新增
|
||||
$model = Crontab::create([
|
||||
'name' => $data['name'],
|
||||
'type' => $data['type'],
|
||||
'task_style' => $data['task_style'],
|
||||
'rule' => $rule,
|
||||
'target' => $data['target'],
|
||||
'parameter' => $data['parameter'],
|
||||
'status' => $data['status'],
|
||||
'remark' => $data['remark'],
|
||||
]);
|
||||
|
||||
$id = $model->getKey();
|
||||
// 连接到Channel服务
|
||||
ChannelClient::connect();
|
||||
ChannelClient::publish('crontab', ['args' => $id]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改任务
|
||||
*/
|
||||
public function edit($id, $data): bool
|
||||
{
|
||||
$second = $data['second'];
|
||||
$minute = $data['minute'];
|
||||
$hour = $data['hour'];
|
||||
$week = $data['week'];
|
||||
$day = $data['day'];
|
||||
$month = $data['month'];
|
||||
|
||||
// 规则处理
|
||||
$rule = match ($data['task_style']) {
|
||||
1 => "0 {$minute} {$hour} * * *",
|
||||
2 => "0 {$minute} * * * *",
|
||||
3 => "0 {$minute} */{$hour} * * *",
|
||||
4 => "0 */{$minute} * * * *",
|
||||
5 => "*/{$second} * * * * *",
|
||||
6 => "0 {$minute} {$hour} * * {$week}",
|
||||
7 => "0 {$minute} {$hour} {$day} * *",
|
||||
8 => "0 {$minute} {$hour} {$day} {$month} *",
|
||||
default => throw new ApiException("任务类型异常"),
|
||||
};
|
||||
|
||||
// 查询任务数据
|
||||
$model = $this->model->findOrEmpty($id);
|
||||
if ($model->isEmpty()) {
|
||||
throw new ApiException('数据不存在');
|
||||
}
|
||||
|
||||
$result = $model->save([
|
||||
'name' => $data['name'],
|
||||
'type' => $data['type'],
|
||||
'task_style' => $data['task_style'],
|
||||
'rule' => $rule,
|
||||
'target' => $data['target'],
|
||||
'parameter' => $data['parameter'],
|
||||
'status' => $data['status'],
|
||||
'remark' => $data['remark'],
|
||||
]);
|
||||
if ($result) {
|
||||
// 连接到Channel服务
|
||||
ChannelClient::connect();
|
||||
ChannelClient::publish('crontab', ['args' => $id]);
|
||||
}
|
||||
|
||||
// 修改任务数据
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除定时任务
|
||||
* @param $ids
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy($ids): bool
|
||||
{
|
||||
if (is_array($ids)) {
|
||||
if (count($ids) > 1) {
|
||||
throw new ApiException('禁止批量删除操作');
|
||||
}
|
||||
$ids = $ids[0];
|
||||
}
|
||||
$result = parent::destroy($ids);
|
||||
if ($result) {
|
||||
// 连接到Channel服务
|
||||
ChannelClient::connect();
|
||||
ChannelClient::publish('crontab', ['args' => $ids]);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改状态
|
||||
* @param $id
|
||||
* @param $status
|
||||
* @return bool
|
||||
*/
|
||||
public function changeStatus($id, $status): bool
|
||||
{
|
||||
$model = $this->model->findOrEmpty($id);
|
||||
if ($model->isEmpty()) {
|
||||
throw new ApiException('数据不存在');
|
||||
}
|
||||
$result = $model->save(['status' => $status]);
|
||||
if ($result) {
|
||||
// 连接到Channel服务
|
||||
ChannelClient::connect();
|
||||
ChannelClient::publish('crontab', ['args' => $id]);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行定时任务
|
||||
* @param $id
|
||||
* @return bool
|
||||
*/
|
||||
public function run($id): bool
|
||||
{
|
||||
$info = $this->model->where('status', 1)->findOrEmpty($id);
|
||||
if ($info->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
$data['crontab_id'] = $info->id;
|
||||
$data['name'] = $info->name;
|
||||
$data['target'] = $info->target;
|
||||
$data['parameter'] = $info->parameter;
|
||||
switch ($info->type) {
|
||||
case 1:
|
||||
// URL任务GET
|
||||
$httpClient = new Client([
|
||||
'timeout' => 5,
|
||||
'verify' => false,
|
||||
]);
|
||||
try {
|
||||
$httpClient->request('GET', $info->target);
|
||||
$data['status'] = 1;
|
||||
CrontabLog::create($data);
|
||||
return true;
|
||||
} catch (GuzzleException $e) {
|
||||
$data['status'] = 2;
|
||||
$data['exception_info'] = $e->getMessage();
|
||||
CrontabLog::create($data);
|
||||
return false;
|
||||
}
|
||||
case 2:
|
||||
// URL任务POST
|
||||
$httpClient = new Client([
|
||||
'timeout' => 5,
|
||||
'verify' => false,
|
||||
]);
|
||||
try {
|
||||
$res = $httpClient->request('POST', $info->target, [
|
||||
'form_params' => json_decode($info->parameter ?? '', true)
|
||||
]);
|
||||
$data['status'] = 1;
|
||||
$data['exception_info'] = $res->getBody();
|
||||
CrontabLog::create($data);
|
||||
return true;
|
||||
} catch (GuzzleException $e) {
|
||||
$data['status'] = 2;
|
||||
$data['exception_info'] = $e->getMessage();
|
||||
CrontabLog::create($data);
|
||||
return false;
|
||||
}
|
||||
case 3:
|
||||
// 类任务
|
||||
$class_name = $info->target;
|
||||
$method_name = 'run';
|
||||
$class = new $class_name;
|
||||
if (method_exists($class, $method_name)) {
|
||||
$return = $class->$method_name($info->parameter);
|
||||
$data['status'] = 1;
|
||||
$data['exception_info'] = $return;
|
||||
CrontabLog::create($data);
|
||||
return true;
|
||||
} else {
|
||||
$data['status'] = 2;
|
||||
$data['exception_info'] = '类:' . $class_name . ',方法:run,未找到';
|
||||
CrontabLog::create($data);
|
||||
return false;
|
||||
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
213
server/plugin/saiadmin/app/logic/tool/GenerateColumnsLogic.php
Normal file
213
server/plugin/saiadmin/app/logic/tool/GenerateColumnsLogic.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\app\logic\tool;
|
||||
|
||||
use plugin\saiadmin\app\model\tool\GenerateColumns;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
|
||||
/**
|
||||
* 代码生成业务字段逻辑层
|
||||
*/
|
||||
class GenerateColumnsLogic extends BaseLogic
|
||||
{
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new GenerateColumns();
|
||||
}
|
||||
|
||||
public function saveExtra($data)
|
||||
{
|
||||
$default_column = ['create_time', 'update_time', 'created_by', 'updated_by', 'delete_time', 'remark'];
|
||||
// 组装数据
|
||||
foreach ($data as $k => $item) {
|
||||
|
||||
if ($item['column_name'] == 'delete_time') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$column = [
|
||||
'table_id' => $item['table_id'],
|
||||
'column_name' => $item['column_name'],
|
||||
'column_comment' => $item['column_comment'],
|
||||
'column_type' => $item['column_type'],
|
||||
'default_value' => $item['default_value'],
|
||||
'is_pk' => ($item['column_key'] == 'PRI') ? 2 : 1,
|
||||
'is_required' => $item['is_nullable'] == 'NO' ? 2 : 1,
|
||||
'query_type' => 'eq',
|
||||
'view_type' => 'input',
|
||||
'sort' => count($data) - $k,
|
||||
'options' => $item['options'] ?? null
|
||||
];
|
||||
|
||||
// 设置默认选项
|
||||
if (!in_array($item['column_name'], $default_column) && empty($item['column_key'])) {
|
||||
$column = array_merge(
|
||||
$column,
|
||||
[
|
||||
'is_insert' => 2,
|
||||
'is_edit' => 2,
|
||||
'is_list' => 2,
|
||||
'is_query' => 1,
|
||||
'is_sort' => 1,
|
||||
]
|
||||
);
|
||||
}
|
||||
$keyList = [
|
||||
'column_comment',
|
||||
'column_type',
|
||||
'default_value',
|
||||
'is_pk',
|
||||
'is_required',
|
||||
'is_insert',
|
||||
'is_edit',
|
||||
'is_list',
|
||||
'is_query',
|
||||
'is_sort',
|
||||
'query_type',
|
||||
'view_type',
|
||||
'dict_type',
|
||||
'options',
|
||||
'sort',
|
||||
'is_cover'
|
||||
];
|
||||
foreach ($keyList as $key) {
|
||||
if (isset($item[$key]))
|
||||
$column[$key] = $item[$key];
|
||||
}
|
||||
GenerateColumns::create($this->fieldDispose($column));
|
||||
}
|
||||
}
|
||||
|
||||
public function update($data, $where)
|
||||
{
|
||||
$data['is_insert'] = $data['is_insert'] ? 2 : 1;
|
||||
$data['is_edit'] = $data['is_edit'] ? 2 : 1;
|
||||
$data['is_list'] = $data['is_list'] ? 2 : 1;
|
||||
$data['is_query'] = $data['is_query'] ? 2 : 1;
|
||||
$data['is_sort'] = $data['is_sort'] ? 2 : 1;
|
||||
$data['is_required'] = $data['is_required'] ? 2 : 1;
|
||||
$this->model->update($data, $where);
|
||||
}
|
||||
|
||||
private function fieldDispose(array $column): array
|
||||
{
|
||||
$object = new class {
|
||||
public function viewTypeDispose(&$column): void
|
||||
{
|
||||
switch ($column['column_type']) {
|
||||
case 'varchar':
|
||||
$column['view_type'] = 'input';
|
||||
break;
|
||||
// 富文本
|
||||
case 'text':
|
||||
case 'longtext':
|
||||
$column['is_list'] = 1;
|
||||
$column['is_query'] = 1;
|
||||
$column['view_type'] = 'editor';
|
||||
$options = [
|
||||
'height' => 400,
|
||||
];
|
||||
$column['options'] = $options;
|
||||
break;
|
||||
// 日期字段
|
||||
case 'datetime':
|
||||
$column['view_type'] = 'date';
|
||||
$options = [
|
||||
'mode' => 'datetime'
|
||||
];
|
||||
$column['options'] = $options;
|
||||
$column['query_type'] = 'between';
|
||||
break;
|
||||
case 'date':
|
||||
$column['view_type'] = 'date';
|
||||
$options = [
|
||||
'mode' => 'date'
|
||||
];
|
||||
$column['options'] = $options;
|
||||
$column['query_type'] = 'between';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function columnName(&$column): void
|
||||
{
|
||||
if (stristr($column['column_name'], 'name')) {
|
||||
$column['is_query'] = 2;
|
||||
$column['is_required'] = 2;
|
||||
$column['query_type'] = 'like';
|
||||
}
|
||||
|
||||
if (stristr($column['column_name'], 'title')) {
|
||||
$column['is_query'] = 2;
|
||||
$column['is_required'] = 2;
|
||||
$column['query_type'] = 'like';
|
||||
}
|
||||
|
||||
if (stristr($column['column_name'], 'type')) {
|
||||
$column['is_query'] = 2;
|
||||
$column['is_required'] = 2;
|
||||
$column['query_type'] = 'eq';
|
||||
}
|
||||
|
||||
if (stristr($column['column_name'], 'image')) {
|
||||
$column['is_query'] = 1;
|
||||
$column['view_type'] = 'uploadImage';
|
||||
$options = [
|
||||
'multiple' => false,
|
||||
'limit' => 1,
|
||||
];
|
||||
$column['options'] = $options;
|
||||
}
|
||||
|
||||
if (stristr($column['column_name'], 'file')) {
|
||||
$column['is_query'] = 1;
|
||||
$column['view_type'] = 'uploadFile';
|
||||
$options = [
|
||||
'multiple' => false,
|
||||
'limit' => 1,
|
||||
];
|
||||
$column['options'] = $options;
|
||||
}
|
||||
|
||||
if (stristr($column['column_name'], 'attach')) {
|
||||
$column['is_query'] = 1;
|
||||
$column['view_type'] = 'uploadFile';
|
||||
$options = [
|
||||
'multiple' => false,
|
||||
'limit' => 1,
|
||||
];
|
||||
$column['options'] = $options;
|
||||
}
|
||||
|
||||
if ($column['column_name'] === 'sort') {
|
||||
$column['view_type'] = 'inputNumber';
|
||||
}
|
||||
|
||||
if ($column['column_name'] === 'status') {
|
||||
$column['view_type'] = 'radio';
|
||||
$column['dict_type'] = 'data_status';
|
||||
}
|
||||
|
||||
if (stristr($column['column_name'], 'is_')) {
|
||||
$column['view_type'] = 'radio';
|
||||
$column['dict_type'] = 'yes_or_no';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!$column['is_cover']) {
|
||||
$object->viewTypeDispose($column);
|
||||
$object->columnName($column);
|
||||
}
|
||||
$column['options'] = json_encode($column['options'], JSON_UNESCAPED_UNICODE);
|
||||
return $column;
|
||||
}
|
||||
}
|
||||
478
server/plugin/saiadmin/app/logic/tool/GenerateTablesLogic.php
Normal file
478
server/plugin/saiadmin/app/logic/tool/GenerateTablesLogic.php
Normal file
@@ -0,0 +1,478 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\app\logic\tool;
|
||||
|
||||
use plugin\saiadmin\app\cache\UserMenuCache;
|
||||
use plugin\saiadmin\app\logic\system\DatabaseLogic;
|
||||
use plugin\saiadmin\app\model\system\SystemMenu;
|
||||
use plugin\saiadmin\app\model\tool\GenerateTables;
|
||||
use plugin\saiadmin\app\model\tool\GenerateColumns;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use plugin\saiadmin\utils\code\CodeZip;
|
||||
use plugin\saiadmin\utils\code\CodeEngine;
|
||||
|
||||
/**
|
||||
* 代码生成业务逻辑层
|
||||
*/
|
||||
class GenerateTablesLogic extends BaseLogic
|
||||
{
|
||||
protected $columnLogic = null;
|
||||
|
||||
protected $dataLogic = null;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->model = new GenerateTables();
|
||||
$this->columnLogic = new GenerateColumnsLogic();
|
||||
$this->dataLogic = new DatabaseLogic();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除表和字段信息
|
||||
* @param $ids
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($ids): bool
|
||||
{
|
||||
return $this->transaction(function () use ($ids) {
|
||||
parent::destroy($ids);
|
||||
GenerateColumns::destroy(function ($query) use ($ids) {
|
||||
$query->where('table_id', 'in', $ids);
|
||||
});
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 装载表信息
|
||||
* @param $names
|
||||
* @param $source
|
||||
* @return void
|
||||
*/
|
||||
public function loadTable($names, $source): void
|
||||
{
|
||||
$data = config('think-orm.connections');
|
||||
$config = $data[$source];
|
||||
if (!$config) {
|
||||
throw new ApiException('数据库配置读取失败');
|
||||
}
|
||||
|
||||
$prefix = $config['prefix'] ?? '';
|
||||
foreach ($names as $item) {
|
||||
$class_name = $item['name'];
|
||||
if (!empty($prefix)) {
|
||||
$class_name = Helper::str_replace_once($prefix, '', $class_name);
|
||||
}
|
||||
$class_name = Helper::camel($class_name);
|
||||
$tableInfo = [
|
||||
'table_name' => $item['name'],
|
||||
'table_comment' => $item['comment'],
|
||||
'class_name' => $class_name,
|
||||
'business_name' => Helper::get_business($item['name']),
|
||||
'belong_menu_id' => 80,
|
||||
'menu_name' => $item['comment'],
|
||||
'tpl_category' => 'single',
|
||||
'template' => 'app',
|
||||
'stub' => 'think',
|
||||
'namespace' => '',
|
||||
'package_name' => '',
|
||||
'source' => $source,
|
||||
'generate_menus' => 'index,save,update,read,destroy',
|
||||
];
|
||||
$model = GenerateTables::create($tableInfo);
|
||||
$columns = $this->dataLogic->getColumnList($item['name'], $source);
|
||||
foreach ($columns as &$column) {
|
||||
$column['table_id'] = $model->id;
|
||||
$column['is_cover'] = false;
|
||||
}
|
||||
$this->columnLogic->saveExtra($columns);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步表字段信息
|
||||
* @param $id
|
||||
* @return void
|
||||
*/
|
||||
public function sync($id)
|
||||
{
|
||||
$model = $this->model->findOrEmpty($id);
|
||||
// 拉取已有数据表信息
|
||||
$queryModel = $this->columnLogic->model->where('table_id', $id);
|
||||
$columnLogicData = $this->columnLogic->getAll($queryModel);
|
||||
$columnLogicList = [];
|
||||
foreach ($columnLogicData as $item) {
|
||||
$columnLogicList[$item['column_name']] = $item;
|
||||
}
|
||||
GenerateColumns::destroy(function ($query) use ($id) {
|
||||
$query->where('table_id', $id);
|
||||
});
|
||||
$columns = $this->dataLogic->getColumnList($model->table_name, $model->source ?? '');
|
||||
foreach ($columns as &$column) {
|
||||
$column['table_id'] = $model->id;
|
||||
$column['is_cover'] = false;
|
||||
if (isset($columnLogicList[$column['column_name']])) {
|
||||
// 存在历史信息的情况
|
||||
$getcolumnLogicItem = $columnLogicList[$column['column_name']];
|
||||
if ($getcolumnLogicItem['column_type'] == $column['column_type']) {
|
||||
$column['is_cover'] = true;
|
||||
foreach ($getcolumnLogicItem as $key => $item) {
|
||||
$array = [
|
||||
'column_comment',
|
||||
'column_type',
|
||||
'default_value',
|
||||
'is_pk',
|
||||
'is_required',
|
||||
'is_insert',
|
||||
'is_edit',
|
||||
'is_list',
|
||||
'is_query',
|
||||
'is_sort',
|
||||
'query_type',
|
||||
'view_type',
|
||||
'dict_type',
|
||||
'options',
|
||||
'sort',
|
||||
'is_cover'
|
||||
];
|
||||
if (in_array($key, $array)) {
|
||||
$column[$key] = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->columnLogic->saveExtra($columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* 代码预览
|
||||
* @param $id
|
||||
* @return array
|
||||
*/
|
||||
public function preview($id): array
|
||||
{
|
||||
$data = $this->renderData($id);
|
||||
|
||||
$codeEngine = new CodeEngine($data);
|
||||
$controllerContent = $codeEngine->renderContent('php', 'controller.stub');
|
||||
$logicContent = $codeEngine->renderContent('php', 'logic.stub');
|
||||
$modelContent = $codeEngine->renderContent('php', 'model.stub');
|
||||
$validateContent = $codeEngine->renderContent('php', 'validate.stub');
|
||||
$sqlContent = $codeEngine->renderContent('sql', 'sql.stub');
|
||||
$indexContent = $codeEngine->renderContent('vue', 'index.stub');
|
||||
$editContent = $codeEngine->renderContent('vue', 'edit-dialog.stub');
|
||||
$searchContent = $codeEngine->renderContent('vue', 'table-search.stub');
|
||||
$apiContent = $codeEngine->renderContent('ts', 'api.stub');
|
||||
|
||||
// 返回生成内容
|
||||
return [
|
||||
[
|
||||
'tab_name' => 'controller.php',
|
||||
'name' => 'controller',
|
||||
'lang' => 'php',
|
||||
'code' => $controllerContent
|
||||
],
|
||||
[
|
||||
'tab_name' => 'logic.php',
|
||||
'name' => 'logic',
|
||||
'lang' => 'php',
|
||||
'code' => $logicContent
|
||||
],
|
||||
[
|
||||
'tab_name' => 'model.php',
|
||||
'name' => 'model',
|
||||
'lang' => 'php',
|
||||
'code' => $modelContent
|
||||
],
|
||||
[
|
||||
'tab_name' => 'validate.php',
|
||||
'name' => 'validate',
|
||||
'lang' => 'php',
|
||||
'code' => $validateContent
|
||||
],
|
||||
[
|
||||
'tab_name' => 'sql.sql',
|
||||
'name' => 'sql',
|
||||
'lang' => 'sql',
|
||||
'code' => $sqlContent
|
||||
],
|
||||
[
|
||||
'tab_name' => 'index.vue',
|
||||
'name' => 'index',
|
||||
'lang' => 'html',
|
||||
'code' => $indexContent
|
||||
],
|
||||
[
|
||||
'tab_name' => 'edit-dialog.vue',
|
||||
'name' => 'edit-dialog',
|
||||
'lang' => 'html',
|
||||
'code' => $editContent
|
||||
],
|
||||
[
|
||||
'tab_name' => 'table-search.vue',
|
||||
'name' => 'table-search',
|
||||
'lang' => 'html',
|
||||
'code' => $searchContent
|
||||
],
|
||||
[
|
||||
'tab_name' => 'api.ts',
|
||||
'name' => 'api',
|
||||
'lang' => 'javascript',
|
||||
'code' => $apiContent
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成到模块
|
||||
* @param $id
|
||||
*/
|
||||
public function genModule($id)
|
||||
{
|
||||
$data = $this->renderData($id);
|
||||
|
||||
// 生成文件到模块
|
||||
$codeEngine = new CodeEngine($data);
|
||||
$codeEngine->generateBackend('controller', $codeEngine->renderContent('php', 'controller.stub'));
|
||||
$codeEngine->generateBackend('logic', $codeEngine->renderContent('php', 'logic.stub'));
|
||||
$codeEngine->generateBackend('model', $codeEngine->renderContent('php', 'model.stub'));
|
||||
$codeEngine->generateBackend('validate', $codeEngine->renderContent('php', 'validate.stub'));
|
||||
$codeEngine->generateFrontend('index', $codeEngine->renderContent('vue', 'index.stub'));
|
||||
$codeEngine->generateFrontend('edit-dialog', $codeEngine->renderContent('vue', 'edit-dialog.stub'));
|
||||
$codeEngine->generateFrontend('table-search', $codeEngine->renderContent('vue', 'table-search.stub'));
|
||||
$codeEngine->generateFrontend('api', $codeEngine->renderContent('ts', 'api.stub'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理数据
|
||||
* @param $id
|
||||
* @return array
|
||||
*/
|
||||
protected function renderData($id): array
|
||||
{
|
||||
$table = $this->model->findOrEmpty($id);
|
||||
if (!in_array($table['template'], ["plugin", "app"])) {
|
||||
throw new ApiException('应用类型必须为plugin或者app');
|
||||
}
|
||||
if (empty($table['namespace'])) {
|
||||
throw new ApiException('请先设置应用名称');
|
||||
}
|
||||
|
||||
$columns = $this->columnLogic->where('table_id', $id)
|
||||
->order('sort', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
$pk = 'id';
|
||||
foreach ($columns as &$column) {
|
||||
if ($column['is_pk'] == 2) {
|
||||
$pk = $column['column_name'];
|
||||
}
|
||||
if ($column['column_name'] == 'delete_time') {
|
||||
unset($column['column_name']);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理特殊变量
|
||||
if ($table['template'] == 'plugin') {
|
||||
$namespace_start = "plugin\\" . $table['namespace'] . "\\app\\admin\\";
|
||||
$namespace_start_model = "plugin\\" . $table['namespace'] . "\\app\\";
|
||||
$namespace_end = "\\" . $table['package_name'];
|
||||
$url_path = 'app/' . $table['namespace'] . '/admin/' . $table['package_name'] . '/' . $table['class_name'];
|
||||
$route = 'app/';
|
||||
} else {
|
||||
$namespace_start = "app\\" . $table['namespace'] . "\\";
|
||||
$namespace_start_model = "app\\" . $table['namespace'] . "\\";
|
||||
$namespace_end = "\\" . $table['package_name'];
|
||||
$url_path = $table['namespace'] . '/' . $table['package_name'] . '/' . $table['class_name'];
|
||||
$route = '';
|
||||
}
|
||||
|
||||
$config = config('think-orm');
|
||||
|
||||
$data = $table->toArray();
|
||||
$data['pk'] = $pk;
|
||||
$data['namespace_start'] = $namespace_start;
|
||||
$data['namespace_start_model'] = $namespace_start_model;
|
||||
$data['namespace_end'] = $namespace_end;
|
||||
$data['url_path'] = $url_path;
|
||||
$data['route'] = $route;
|
||||
$data['tables'] = [$data];
|
||||
$data['columns'] = $columns;
|
||||
$data['db_source'] = $config['default'] ?? 'mysql';
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成到模块
|
||||
*/
|
||||
public function generateFile($id)
|
||||
{
|
||||
$table = $this->model->where('id', $id)->findOrEmpty();
|
||||
if ($table->isEmpty()) {
|
||||
throw new ApiException('请选择要生成的表');
|
||||
}
|
||||
$debug = config('app.debug', true);
|
||||
if (!$debug) {
|
||||
throw new ApiException('非调试模式下,不允许生成文件');
|
||||
}
|
||||
$this->updateMenu($table);
|
||||
$this->genModule($id);
|
||||
UserMenuCache::clearMenuCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* 代码生成下载
|
||||
*/
|
||||
public function generate($idsArr): array
|
||||
{
|
||||
$zip = new CodeZip();
|
||||
$tables = $this->model->where('id', 'in', $idsArr)->select()->toArray();
|
||||
foreach ($idsArr as $table_id) {
|
||||
$data = $this->renderData($table_id);
|
||||
$data['tables'] = $tables;
|
||||
$codeEngine = new CodeEngine($data);
|
||||
$codeEngine->generateTemp();
|
||||
}
|
||||
|
||||
$filename = 'saiadmin.zip';
|
||||
$download = $zip->compress();
|
||||
|
||||
return compact('filename', 'download');
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理菜单列表
|
||||
* @param $tables
|
||||
*/
|
||||
public function updateMenu($tables)
|
||||
{
|
||||
/*不存在的情况下进行新建操作*/
|
||||
$url_path = $tables['namespace'] . ":" . $tables['package_name'] . ':' . $tables['business_name'];
|
||||
$code = $tables['namespace'] . "/" . $tables['package_name'] . '/' . $tables['business_name'];
|
||||
$path = $tables['package_name'] . '/' . $tables['business_name'];
|
||||
$component = $tables['namespace'] . "/" . $tables['package_name'] . '/' . $tables['business_name'];
|
||||
|
||||
/*先获取一下已有的路由中是否包含当前ID的路由的核心信息*/
|
||||
$model = new SystemMenu();
|
||||
$tableMenu = $model->where('generate_id', $tables['id'])->findOrEmpty();
|
||||
$fistMenu = [
|
||||
'parent_id' => $tables['belong_menu_id'],
|
||||
'name' => $tables['menu_name'],
|
||||
'code' => $code,
|
||||
'path' => $path,
|
||||
'icon' => 'ri:home-2-line',
|
||||
'component' => "/plugin/$component/index",
|
||||
'type' => 2,
|
||||
'sort' => 100,
|
||||
'is_iframe' => 2,
|
||||
'is_keep_alive' => 2,
|
||||
'is_hidden' => 2,
|
||||
'is_fixed_tab' => 2,
|
||||
'is_full_page' => 2,
|
||||
'generate_id' => $tables['id']
|
||||
];
|
||||
if ($tableMenu->isEmpty()) {
|
||||
$temp = SystemMenu::create($fistMenu);
|
||||
$fistMenuId = $temp->id;
|
||||
} else {
|
||||
$fistMenu['id'] = $tableMenu['id'];
|
||||
$tableMenu->save($fistMenu);
|
||||
$fistMenuId = $tableMenu['id'];
|
||||
}
|
||||
/*开始进行子权限的判定操作*/
|
||||
$childNodes = [
|
||||
['name' => '列表', 'key' => 'index'],
|
||||
['name' => '保存', 'key' => 'save'],
|
||||
['name' => '更新', 'key' => 'update'],
|
||||
['name' => '读取', 'key' => 'read'],
|
||||
['name' => '删除', 'key' => 'destroy'],
|
||||
];
|
||||
|
||||
foreach ($childNodes as $node) {
|
||||
$nodeData = $model->where('parent_id', $fistMenuId)->where('generate_key', $node['key'])->findOrEmpty();
|
||||
$childNodeData = [
|
||||
'parent_id' => $fistMenuId,
|
||||
'name' => $node['name'],
|
||||
'slug' => "$url_path:{$node['key']}",
|
||||
'type' => 3,
|
||||
'sort' => 100,
|
||||
'is_iframe' => 2,
|
||||
'is_keep_alive' => 2,
|
||||
'is_hidden' => 2,
|
||||
'is_fixed_tab' => 2,
|
||||
'is_full_page' => 2,
|
||||
'generate_key' => $node['key']
|
||||
];
|
||||
if (!empty($nodeData)) {
|
||||
$childNodeData['id'] = $nodeData['id'];
|
||||
$nodeData->save($childNodeData);
|
||||
} else {
|
||||
$menuModel = new SystemMenu();
|
||||
$menuModel->save($childNodeData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据表字段信息
|
||||
* @param $table_id
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTableColumns($table_id): mixed
|
||||
{
|
||||
$query = $this->columnLogic->where('table_id', $table_id);
|
||||
return $this->columnLogic->getAll($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑数据
|
||||
* @param $id
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id, $data): mixed
|
||||
{
|
||||
$columns = $data['columns'];
|
||||
|
||||
unset($data['columns']);
|
||||
|
||||
if (!empty($data['belong_menu_id'])) {
|
||||
$data['belong_menu_id'] = is_array($data['belong_menu_id']) ? array_pop($data['belong_menu_id']) : $data['belong_menu_id'];
|
||||
} else {
|
||||
$data['belong_menu_id'] = 0;
|
||||
}
|
||||
|
||||
$data['generate_menus'] = implode(',', $data['generate_menus']);
|
||||
|
||||
if (empty($data['options'])) {
|
||||
unset($data['options']);
|
||||
}
|
||||
|
||||
$data['options'] = json_encode($data['options'], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
// 更新业务表
|
||||
$this->update($data, ['id' => $id]);
|
||||
|
||||
// 更新业务字段表
|
||||
foreach ($columns as $column) {
|
||||
if ($column['options']) {
|
||||
$column['options'] = json_encode($column['options'], JSON_NUMERIC_CHECK);
|
||||
}
|
||||
$this->columnLogic->update($column, ['id' => $column['id']]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user