CurdGenerate.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\service\curd\BuildCurd;
  4. use app\common\controller\AdminController;
  5. use app\admin\service\annotation\ControllerAnnotation;
  6. use app\admin\service\annotation\NodeAnnotation;
  7. use app\Request;
  8. use think\db\exception\PDOException;
  9. use think\exception\FileException;
  10. use think\facade\Console;
  11. use think\facade\Db;
  12. use think\helper\Str;
  13. use think\response\Json;
  14. #[ControllerAnnotation(title: 'CURD可视化管理')]
  15. class CurdGenerate extends AdminController
  16. {
  17. #[NodeAnnotation(title: '列表', auth: true)]
  18. public function index(Request $request): Json|string
  19. {
  20. return $this->fetch();
  21. }
  22. #[NodeAnnotation(title: '操作', auth: true)]
  23. public function save(Request $request, string $type = ''): ?Json
  24. {
  25. if (!$request->isAjax()) $this->error();
  26. switch ($type) {
  27. case "search":
  28. $tb_prefix = $request->param('tb_prefix/s', '');
  29. $tb_name = $request->param('tb_name/s', '');
  30. if (empty($tb_name)) $this->error('参数错误');
  31. try {
  32. $list = Db::query("SHOW FULL COLUMNS FROM {$tb_prefix}{$tb_name}");
  33. $data = [];
  34. foreach ($list as $value) {
  35. $data[] = [
  36. 'name' => $value['Field'],
  37. 'type' => $value['Type'],
  38. 'key' => $value['Key'],
  39. 'extra' => $value['Extra'],
  40. 'null' => $value['Null'],
  41. 'desc' => $value['Comment'],
  42. ];
  43. }
  44. $this->success('查询成功', compact('data', 'list'));
  45. }catch (PDOException $exception) {
  46. $this->error($exception->getMessage());
  47. }
  48. break;
  49. case "add":
  50. $tb_prefix = $request->param('tb_prefix/s', '');
  51. $tb_name = $request->param('tb_name/s', '');
  52. if (empty($tb_name)) $this->error('参数错误');
  53. $tb_fields = $request->param('tb_fields');
  54. $force = $request->post('force/d', 0);
  55. try {
  56. $build = (new BuildCurd())->setTablePrefix($tb_prefix)->setTable($tb_name);
  57. $build->setForce($force); // 强制覆盖
  58. // 新增字段类型
  59. if ($tb_fields) {
  60. foreach ($tb_fields as $tk => $tf) {
  61. if (empty($tf)) continue;
  62. $tf = array_values($tf);
  63. switch ($tk) {
  64. case 'ignore':
  65. $build->setIgnoreFields($tf, true);
  66. break;
  67. case 'select':
  68. $build->setSelectFields($tf, true);
  69. break;
  70. case 'radio':
  71. $build->setRadioFieldSuffix($tf, true);
  72. break;
  73. case 'checkbox':
  74. $build->setCheckboxFieldSuffix($tf, true);
  75. break;
  76. case 'image':
  77. $build->setImageFieldSuffix($tf, true);
  78. break;
  79. case 'images':
  80. $build->setImagesFieldSuffix($tf, true);
  81. break;
  82. case 'date':
  83. $build->setDateFieldSuffix($tf, true);
  84. break;
  85. case 'datetime':
  86. $build->setDatetimeFieldSuffix($tf, true);
  87. break;
  88. case 'editor':
  89. $build->setEditorFields($tf, true);
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. }
  96. $build = $build->render();
  97. $fileList = $build->getFileList();
  98. if (empty($fileList)) $this->error('这里什么都没有');
  99. $result = $build->create();
  100. $_file = $result[0] ?? '';
  101. $link = '';
  102. if (!empty($_file)) {
  103. $_fileExp = explode(DIRECTORY_SEPARATOR, $_file);
  104. $_fileExp_last = array_slice($_fileExp, -2);
  105. $_fileExp_last_0 = $_fileExp_last[0] . '.';
  106. if ($_fileExp_last[0] == 'controller') $_fileExp_last_0 = '';
  107. $link = '/' . config('admin.alias_name') . '/' . $_fileExp_last_0 . Str::snake(explode('.php', end($_fileExp_last))[0] ?? '') . '/index';
  108. }
  109. $this->success('生成成功', compact('result', 'link'));
  110. }catch (FileException $exception) {
  111. return json(['code' => -1, 'msg' => $exception->getMessage()]);
  112. }
  113. break;
  114. case "delete":
  115. $tb_prefix = $request->param('tb_prefix/s', '');
  116. $tb_name = $request->param('tb_name/s', '');
  117. if (empty($tb_name)) $this->error('参数错误');
  118. try {
  119. $build = (new BuildCurd())->setTablePrefix($tb_prefix)->setTable($tb_name);
  120. $build = $build->render();
  121. $fileList = $build->getFileList();
  122. if (empty($fileList)) $this->error('这里什么都没有');
  123. $result = $build->delete();
  124. $this->success('删除自动生成CURD文件成功', compact('result'));
  125. }catch (FileException $exception) {
  126. return json(['code' => -1, 'msg' => $exception->getMessage()]);
  127. }
  128. break;
  129. case 'console':
  130. $command = $request->post('command', '');
  131. if (empty($command)) $this->error('请输入命令');
  132. $commandExp = explode(' ', $command);
  133. $commandExp = array_values(array_filter($commandExp));
  134. try {
  135. $output = Console::call('curd', [...$commandExp]);
  136. }catch (\Throwable $exception) {
  137. $this->error($exception->getMessage() . $exception->getLine());
  138. }
  139. if (empty($output)) $this->error('设置错误');
  140. $this->success($output->fetch());
  141. break;
  142. default:
  143. $this->error('参数错误');
  144. break;
  145. }
  146. }
  147. }