SystemCrud.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\adminapi\controller\v1\setting;
  14. use app\adminapi\controller\AuthController;
  15. use app\services\system\SystemCrudServices;
  16. use app\services\system\SystemMenusServices;
  17. use crmeb\services\crud\Make;
  18. use think\facade\App;
  19. use think\helper\Str;
  20. /**
  21. * Class SystemCrud
  22. * @author 等风来
  23. * @email 136327134@qq.com
  24. * @date 2023/4/6
  25. * @package app\adminapi\controller\v1\setting
  26. */
  27. class SystemCrud extends AuthController
  28. {
  29. /**
  30. * SystemCrud constructor.
  31. * @param App $app
  32. * @param SystemCrudServices $services
  33. */
  34. public function __construct(App $app, SystemCrudServices $services)
  35. {
  36. parent::__construct($app);
  37. $this->services = $services;
  38. }
  39. /**
  40. * @return \think\Response
  41. * @author 等风来
  42. * @email 136327134@qq.com
  43. * @date 2023/4/11
  44. */
  45. public function index()
  46. {
  47. return app('json')->success($this->services->getList());
  48. }
  49. /**
  50. * @return \think\Response
  51. * @author 等风来
  52. * @email 136327134@qq.com
  53. * @date 2023/4/11
  54. */
  55. public function save()
  56. {
  57. $data = $this->request->postMore([
  58. ['pid', 0],
  59. ['menuName', ''],
  60. ['tableName', ''],
  61. ['tableComment', ''],//表备注
  62. ['tableField', []],//表字段
  63. ['tableIndex', []],//索引
  64. ['filePath', []],
  65. ['isTable', 0],
  66. ]);
  67. $fromField = $columnField = [];
  68. foreach ($data['tableField'] as $item) {
  69. if ($item['is_table']) {
  70. $columnField[] = [
  71. 'field' => $item['field'],
  72. 'name' => $item['table_name'],
  73. ];
  74. }
  75. if ($item['from_type']) {
  76. $fromField[] = [
  77. 'field' => $item['field'],
  78. 'type' => $item['from_type'],
  79. 'name' => $item['table_name'],
  80. 'required' => $item['required'],
  81. 'option' => $item['option'] ?? [],
  82. ];
  83. }
  84. }
  85. $data['fromField'] = $fromField;
  86. $data['columnField'] = $columnField;
  87. if (!$data['tableName']) {
  88. return app('json')->fail('缺少表名');
  89. }
  90. $this->services->createCrud($data);
  91. return app('json')->success('创建成功');
  92. }
  93. /**
  94. * 获取创建文件的目录存放位置
  95. * @return \think\Response
  96. * @author 等风来
  97. * @email 136327134@qq.com
  98. * @date 2023/4/11
  99. */
  100. public function getFilePath()
  101. {
  102. [$tableName, $isTable] = $this->request->postMore([
  103. ['tableName', ''],
  104. ['isTable', 0],
  105. ], true);
  106. if (!$tableName) {
  107. return app('json')->fail('缺少表名');
  108. }
  109. if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
  110. return app('json')->fail('不能生成系统自带数据表');
  111. }
  112. $routeName = 'crud/' . Str::snake($tableName);
  113. $make = $this->services->makeFile($tableName, $routeName, false, [
  114. 'menuName' => '',
  115. 'fromField' => [],
  116. 'columnField' => [],
  117. ]);
  118. $makePath = [];
  119. foreach ($make as $key => $item) {
  120. $makePath[$key] = $item['path'];
  121. }
  122. $tableField = [];
  123. if ($isTable) {
  124. $field = $this->services->getColumnNamesList($tableName);
  125. if (!$field) {
  126. return app('json')->fail('表不存在');
  127. }
  128. foreach ($field as $item) {
  129. $tableField[] = [
  130. 'field' => $item['name'],
  131. 'file_type' => $item['type'],
  132. 'default' => $item['default'],
  133. 'limit' => $item['limit'],
  134. 'comment' => $item['comment'],
  135. 'required' => false,
  136. 'is_table' => false,
  137. 'table_name' => '',
  138. 'from_type' => '',
  139. ];
  140. }
  141. }
  142. return app('json')->success(compact('makePath', 'tableField'));
  143. }
  144. /**
  145. * @param $id
  146. * @return \think\Response
  147. * @author 等风来
  148. * @email 136327134@qq.com
  149. * @date 2023/4/12
  150. */
  151. public function read($id)
  152. {
  153. if (!$id) {
  154. return app('json')->fail('缺少参数');
  155. }
  156. $info = $this->services->get($id);
  157. if (!$info) {
  158. return app('json')->fail('查看的生成的文件不存在');
  159. }
  160. $routeName = 'crud/' . Str::snake($info->table_name);
  161. $column = $this->services->getColumnNamesList($info->table_name);
  162. $key = 'id';
  163. foreach ($column as $value) {
  164. if ($value['primaryKey']) {
  165. $key = $value['name'];
  166. break;
  167. }
  168. }
  169. $make = $this->services->makeFile($info->table_name, $routeName, false, [
  170. 'menuName' => $info->name,
  171. 'key' => $key,
  172. 'fromField' => $info->field['fromField'] ?? [],
  173. 'columnField' => $info->field['columnField'] ?? [],
  174. ]);
  175. $data = [];
  176. foreach ($make as $item) {
  177. $item['name'] = $item['path'];
  178. $data[] = $item;
  179. }
  180. return app('json')->success($data);
  181. }
  182. /**
  183. * 获取tree菜单
  184. * @return \think\Response
  185. * @author 等风来
  186. * @email 136327134@qq.com
  187. * @date 2023/4/11
  188. */
  189. public function getMenus()
  190. {
  191. return app('json')->success(app()->make(SystemMenusServices::class)
  192. ->getList(['auth_type' => 1], ['pid', 'id', 'menu_name as label', 'id as value']));
  193. }
  194. /**
  195. * 获取创建表数据类型
  196. * @return \think\Response
  197. * @author 等风来
  198. * @email 136327134@qq.com
  199. * @date 2023/4/11
  200. */
  201. public function columnType()
  202. {
  203. return app('json')->success($this->services->getTabelRule());
  204. }
  205. /**
  206. * @param $id
  207. * @return \think\Response
  208. * @author 等风来
  209. * @email 136327134@qq.com
  210. * @date 2023/4/11
  211. */
  212. public function delete(SystemMenusServices $services, $id)
  213. {
  214. if (!$id) {
  215. return app('json')->fail('缺少参数');
  216. }
  217. $info = $this->services->get($id);
  218. if (!$info) {
  219. return app('json')->fail('删除的数据不存在');
  220. }
  221. $services->transaction(function () use ($services, $info) {
  222. if ($info->menu_ids) {
  223. $services->deleteMenus($info->menu_ids);
  224. }
  225. $info->delete();
  226. });
  227. if ($info->make_path) {
  228. try {
  229. foreach ($info->make_path as $key => $item) {
  230. if (in_array($key, ['pages', 'router', 'api'])) {
  231. $item = Make::adminTemplatePath() . $item;
  232. } else {
  233. $item = app()->getRootPath() . $item;
  234. }
  235. unlink($item);
  236. }
  237. } catch (\Throwable $e) {
  238. return app('json')->success('删除生成的菜单和信息成功,删除文件出错,详细错误:' . $e->getMessage());
  239. }
  240. }
  241. return app('json')->success('删除成功');
  242. }
  243. }