| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- /**
- * +----------------------------------------------------------------------
- * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- * +----------------------------------------------------------------------
- * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- * +----------------------------------------------------------------------
- * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- * +----------------------------------------------------------------------
- * | Author: CRMEB Team <admin@crmeb.com>
- * +----------------------------------------------------------------------
- */
- namespace app\adminapi\controller\v1\setting;
- use app\adminapi\controller\AuthController;
- use app\services\system\SystemCrudServices;
- use app\services\system\SystemMenusServices;
- use think\facade\App;
- use think\helper\Str;
- /**
- * Class SystemCrud
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/6
- * @package app\adminapi\controller\v1\setting
- */
- class SystemCrud extends AuthController
- {
- /**
- * SystemCrud constructor.
- * @param App $app
- * @param SystemCrudServices $services
- */
- public function __construct(App $app, SystemCrudServices $services)
- {
- parent::__construct($app);
- $this->services = $services;
- }
- /**
- * @return \think\Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function index()
- {
- return app('json')->success($this->services->getList());
- }
- /**
- * @return \think\Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function save()
- {
- $data = $this->request->postMore([
- ['pid', 0],
- ['menuName', ''],
- ['tableName', ''],
- ['tableComment', ''],//表备注
- ['tableField', []],//表字段
- ['tableIndex', []],//索引
- ['filePath', []],
- ['isTable', 0],
- ]);
- $fromField = $columnField = [];
- foreach ($data['tableField'] as $item) {
- if ($item['is_table']) {
- $columnField[] = [
- 'field' => $item['field'],
- 'name' => $item['table_name'],
- ];
- }
- if ($item['from_type']) {
- $fromField[] = [
- 'field' => $item['field'],
- 'name' => $item['table_name'],
- 'required' => $item['required'],
- 'option' => $item['option'] ?? [],
- ];
- }
- }
- $data['fromField '] = $fromField;
- $data['columnField '] = $columnField;
- if (!$data['tableName']) {
- return app('json')->fail('缺少表名');
- }
- $this->services->createCrud($data);
- return app('json')->success('创建成功');
- }
- /**
- * 获取创建文件的目录存放位置
- * @return \think\Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function getFilePath()
- {
- [$tableName, $isTable] = $this->request->postMore([
- ['tableName', ''],
- ['isTable', 0],
- ], true);
- if (!$tableName) {
- return app('json')->fail('缺少表名');
- }
- if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
- return app('json')->fail('不能生成系统自带数据表');
- }
- $routeName = 'crud/' . Str::snake($tableName);
- $make = $this->services->makeFile($tableName, $routeName, false, [
- 'menuName' => '',
- 'fromField' => [],
- 'columnField' => [],
- ]);
- $makePath = [];
- foreach ($make as $key => $item) {
- $makePath[$key] = $item['path'];
- }
- $tableField = [];
- if ($isTable) {
- $field = $this->services->getColumnNamesList($tableName);
- if (!$field) {
- return app('json')->fail('表不存在');
- }
- foreach ($field as $item) {
- $tableField[] = [
- 'field' => $item['name'],
- 'file_type' => $item['type'],
- 'default' => $item['default'],
- 'limit' => $item['limit'],
- 'comment' => $item['comment'],
- 'required' => false,
- 'is_table' => false,
- 'table_name' => '',
- 'from_type' => '',
- ];
- }
- }
- return app('json')->success(compact('makePath', 'tableField'));
- }
- /**
- * @param $id
- * @return \think\Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/12
- */
- public function read($id)
- {
- if (!$id) {
- return app('json')->fail('缺少参数');
- }
- $info = $this->services->get($id);
- if (!$info) {
- return app('json')->fail('查看的生成的文件不存在');
- }
- $routeName = 'crud/' . Str::snake($info->table_name);
- $column = $this->services->getColumnNamesList($info->table_name);
- $key = 'id';
- foreach ($column as $value) {
- if ($value['primaryKey']) {
- $key = $value['name'];
- break;
- }
- }
- $fromField = $columnField = [];
- foreach ($info->tableField as $item) {
- if ($item['is_table']) {
- $columnField[] = [
- 'field' => $item['field'],
- 'name' => $item['table_name'],
- ];
- }
- if ($item['from_type']) {
- $fromField[] = [
- 'field' => $item['field'],
- 'name' => $item['table_name'],
- 'required' => $item['required'],
- 'option' => $item['option'] ?? [],
- ];
- }
- }
- $make = $this->services->makeFile($info->table_name, $routeName, false, [
- 'menuName' => $info->name,
- 'key' => $key,
- 'fromField' => $fromField,
- 'columnField' => $columnField,
- ]);
- $data = [];
- foreach ($make as $item) {
- $item['name'] = $item['path'];
- $data[] = $item;
- }
- return app('json')->success($data);
- }
- /**
- * 获取tree菜单
- * @return \think\Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function getMenus()
- {
- return app('json')->success(app()->make(SystemMenusServices::class)
- ->getList(['auth_type' => 1], ['pid', 'id', 'menu_name as label', 'id as value']));
- }
- /**
- * 获取创建表数据类型
- * @return \think\Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function columnType()
- {
- return app('json')->success($this->services->getTabelRule());
- }
- /**
- * @param $id
- * @return \think\Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function delete($id)
- {
- if (!$id) {
- return app('json')->fail('缺少参数');
- }
- $this->services->delete($id);
- return app('json')->success('删除成功');
- }
- }
|