| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?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 crmeb\services\crud\Make;
- use crmeb\services\FileService;
- use crmeb\utils\Terminal;
- use think\facade\App;
- use think\helper\Str;
- use think\Response;
- /**
- * 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'] && !in_array($item['field_type'], ['addSoftDelete', 'addSoftDelete'])) {
- $columnField[] = [
- 'field' => $item['field'],
- 'name' => $item['table_name'],
- 'type' => $item['from_type'],
- ];
- }
- if ($item['from_type']) {
- $fromField[] = [
- 'field' => $item['field'],
- 'type' => $item['from_type'],
- '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'],
- 'field_type' => $item['type'],
- 'primaryKey' => (bool)$item['primaryKey'],
- '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;
- }
- }
- $softDelete = false;
- foreach ((array)$info->field['tableField'] as $item) {
- if ($item['field_type'] === 'addSoftDelete') {
- $softDelete = true;
- break;
- }
- }
- $make = $this->services->makeFile($info->table_name, $routeName, false, [
- 'menuName' => $info->name,
- 'key' => $key,
- 'softDelete' => $softDelete,
- 'fromField' => $info->field['fromField'] ?? [],
- 'columnField' => $info->field['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(SystemMenusServices $services, $id)
- {
- if (!$id) {
- return app('json')->fail('缺少参数');
- }
- $info = $this->services->get($id);
- if (!$info) {
- return app('json')->fail('删除的数据不存在');
- }
- $services->transaction(function () use ($services, $info) {
- if ($info->menu_ids) {
- $services->deleteMenus($info->menu_ids);
- }
- $info->delete();
- });
- if ($info->make_path) {
- try {
- foreach ($info->make_path as $key => $item) {
- if (in_array($key, ['pages', 'router', 'api'])) {
- $item = Make::adminTemplatePath() . $item;
- } else {
- $item = app()->getRootPath() . $item;
- }
- unlink($item);
- }
- } catch (\Throwable $e) {
- return app('json')->success('删除生成的菜单和信息成功,删除文件出错,详细错误:' . $e->getMessage());
- }
- }
- return app('json')->success('删除成功');
- }
- /**
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/14
- */
- public function npm()
- {
- $terminal = new Terminal();
- $adminPath = $terminal->adminTemplatePath();
- $adminPath = dirname($adminPath);
- $dir = $adminPath . DS . 'node_modules';
- if (!is_dir($dir)) {
- $terminal->run('npm-install');
- }
- $terminal->run('npm-build');
- if (!is_dir($adminPath . DS . 'dist')) {
- return Response::create([
- 'message' => '打包失败',
- ], 'json')->getContent();
- }
- $build = public_path() . config('app.admin_prefix');
- $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build);
- return Response::create([
- 'message' => '打包成功',
- 'success' => 'ok'
- ], 'json')->getContent();
- }
- }
|