SystemCrud.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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\CacheService;
  18. use crmeb\services\crud\Make;
  19. use crmeb\services\FileService;
  20. use crmeb\utils\Terminal;
  21. use think\facade\App;
  22. use think\helper\Str;
  23. use think\Response;
  24. /**
  25. * Class SystemCrud
  26. * @author 等风来
  27. * @email 136327134@qq.com
  28. * @date 2023/4/6
  29. * @package app\adminapi\controller\v1\setting
  30. */
  31. class SystemCrud extends AuthController
  32. {
  33. /**
  34. * SystemCrud constructor.
  35. * @param App $app
  36. * @param SystemCrudServices $services
  37. */
  38. public function __construct(App $app, SystemCrudServices $services)
  39. {
  40. parent::__construct($app);
  41. $this->services = $services;
  42. }
  43. /**
  44. * @return \think\Response
  45. * @author 等风来
  46. * @email 136327134@qq.com
  47. * @date 2023/4/11
  48. */
  49. public function index()
  50. {
  51. return app('json')->success($this->services->getList());
  52. }
  53. /**
  54. * @return \think\Response
  55. * @author 等风来
  56. * @email 136327134@qq.com
  57. * @date 2023/4/11
  58. */
  59. public function save()
  60. {
  61. $data = $this->request->postMore([
  62. ['pid', 0],
  63. ['menuName', ''],
  64. ['tableName', ''],
  65. ['modelName', ''],
  66. ['tableComment', ''],//表备注
  67. ['tableField', []],//表字段
  68. ['tableIndex', []],//索引
  69. ['filePath', []],
  70. ['isTable', 0],
  71. ]);
  72. $fromField = $columnField = [];
  73. foreach ($data['tableField'] as $item) {
  74. if ($item['is_table'] && !in_array($item['field_type'], ['addSoftDelete', 'addSoftDelete'])) {
  75. $columnField[] = [
  76. 'field' => $item['field'],
  77. 'name' => $item['table_name'],
  78. 'type' => $item['from_type'],
  79. ];
  80. }
  81. if ($item['from_type']) {
  82. $name = $item['table_name'] ?: $item['comment'];
  83. if (!$name) {
  84. return app('json')->fail($item['field'] . '字段的表单标题必须填写');
  85. }
  86. $fromField[] = [
  87. 'field' => $item['field'],
  88. 'type' => $item['from_type'],
  89. 'name' => $name,
  90. 'required' => $item['required'],
  91. 'option' => $item['option'] ?? [],
  92. ];
  93. }
  94. }
  95. if (!$fromField) {
  96. return app('json')->fail('至少选择一个字段作为表单项');
  97. }
  98. if (!$columnField) {
  99. return app('json')->fail('至少选择一个字段作为列展示在列表中');
  100. }
  101. $data['fromField'] = $fromField;
  102. $data['columnField'] = $columnField;
  103. if (!$data['tableName']) {
  104. return app('json')->fail(500045);
  105. }
  106. $this->services->createCrud($data);
  107. return app('json')->success(500046);
  108. }
  109. /**
  110. * 获取创建文件的目录存放位置
  111. * @return \think\Response
  112. * @author 等风来
  113. * @email 136327134@qq.com
  114. * @date 2023/4/11
  115. */
  116. public function getFilePath()
  117. {
  118. [$tableName, $isTable] = $this->request->postMore([
  119. ['tableName', ''],
  120. ['isTable', 0],
  121. ], true);
  122. if (!$tableName) {
  123. return app('json')->fail(500045);
  124. }
  125. if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
  126. return app('json')->fail(500044);
  127. }
  128. $routeName = 'crud/' . Str::snake($tableName);
  129. $key = 'id';
  130. $tableField = [];
  131. if ($isTable) {
  132. $field = $this->services->getColumnNamesList($tableName);
  133. if (!$field) {
  134. return app('json')->fail('表不存在');
  135. }
  136. foreach ($field as $item) {
  137. if ($item['primaryKey']) {
  138. $key = $item['name'];
  139. }
  140. $tableField[] = [
  141. 'field' => $item['name'],
  142. 'field_type' => $item['type'],
  143. 'primaryKey' => (bool)$item['primaryKey'],
  144. 'default' => $item['default'],
  145. 'limit' => $item['limit'],
  146. 'comment' => $item['comment'],
  147. 'required' => false,
  148. 'is_table' => false,
  149. 'table_name' => '',
  150. 'from_type' => '',
  151. ];
  152. }
  153. }
  154. $make = $this->services->makeFile($tableName, $routeName, false, [
  155. 'menuName' => '',
  156. 'key' => $key,
  157. 'fromField' => [],
  158. 'columnField' => [],
  159. ]);
  160. $makePath = [];
  161. foreach ($make as $k => $item) {
  162. $makePath[$k] = $item['path'];
  163. }
  164. return app('json')->success(compact('makePath', 'tableField'));
  165. }
  166. /**
  167. * @param $id
  168. * @return \think\Response
  169. * @author 等风来
  170. * @email 136327134@qq.com
  171. * @date 2023/4/12
  172. */
  173. public function read($id)
  174. {
  175. if (!$id) {
  176. return app('json')->fail(500035);
  177. }
  178. $info = $this->services->get($id);
  179. if (!$info) {
  180. return app('json')->fail(500043);
  181. }
  182. $routeName = 'crud/' . Str::snake($info->table_name);
  183. $column = $this->services->getColumnNamesList($info->table_name);
  184. $key = 'id';
  185. foreach ($column as $value) {
  186. if ($value['primaryKey']) {
  187. $key = $value['name'];
  188. break;
  189. }
  190. }
  191. $softDelete = false;
  192. foreach ((array)$info->field['tableField'] as $item) {
  193. if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
  194. $softDelete = true;
  195. break;
  196. }
  197. }
  198. $make = $this->services->makeFile($info->table_name, $routeName, false, [
  199. 'menuName' => $info->name,
  200. 'modelName' => $info->model_name,
  201. 'key' => $key,
  202. 'softDelete' => $softDelete,
  203. 'fromField' => $info->field['fromField'] ?? [],
  204. 'columnField' => $info->field['columnField'] ?? [],
  205. ]);
  206. $data = [];
  207. foreach ($make as $item) {
  208. $item['name'] = $item['path'];
  209. $data[] = $item;
  210. }
  211. return app('json')->success($data);
  212. }
  213. /**
  214. * 获取tree菜单
  215. * @return \think\Response
  216. * @author 等风来
  217. * @email 136327134@qq.com
  218. * @date 2023/4/11
  219. */
  220. public function getMenus()
  221. {
  222. return app('json')->success(app()->make(SystemMenusServices::class)
  223. ->getList(['auth_type' => 1, 'is_show' => 1], ['auth_type', 'pid', 'id', 'menu_name as label', 'id as value']));
  224. }
  225. /**
  226. * 获取创建表数据类型
  227. * @return \think\Response
  228. * @author 等风来
  229. * @email 136327134@qq.com
  230. * @date 2023/4/11
  231. */
  232. public function columnType()
  233. {
  234. return app('json')->success($this->services->getTabelRule());
  235. }
  236. /**
  237. * @param SystemMenusServices $services
  238. * @param $id
  239. * @return \think\Response
  240. * @author 等风来
  241. * @email 136327134@qq.com
  242. * @date 2023/4/11
  243. */
  244. public function delete(SystemMenusServices $services, $id)
  245. {
  246. if (!$id) {
  247. return app('json')->fail(500035);
  248. }
  249. $info = $this->services->get($id);
  250. if (!$info) {
  251. return app('json')->fail(500042);
  252. }
  253. $services->transaction(function () use ($services, $info) {
  254. if ($info->menu_ids) {
  255. $services->deleteMenus($info->menu_ids);
  256. }
  257. $info->delete();
  258. });
  259. if ($info->make_path) {
  260. try {
  261. foreach ($info->make_path as $key => $item) {
  262. if (in_array($key, ['pages', 'router', 'api'])) {
  263. $item = Make::adminTemplatePath() . $item;
  264. } else {
  265. $item = app()->getRootPath() . $item;
  266. }
  267. unlink($item);
  268. }
  269. } catch (\Throwable $e) {
  270. return app('json')->success(500041, [], ['message' => $e->getMessage()]);
  271. }
  272. }
  273. return app('json')->success(100002);
  274. }
  275. /**
  276. * 下载文件
  277. * @param $id
  278. * @return Response
  279. * @author 等风来
  280. * @email 136327134@qq.com
  281. * @date 2023/4/15
  282. */
  283. public function download($id)
  284. {
  285. if (!$id) {
  286. return app('json')->fail(500035);
  287. }
  288. $info = $this->services->get($id);
  289. if (!$info) {
  290. return app('json')->fail(500039);
  291. }
  292. $zipPath = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name);
  293. $zipName = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name) . '.zip';
  294. if (is_file($zipName)) {
  295. unlink($zipName);
  296. }
  297. $makePath = $info->make_path ?? [];
  298. foreach ($makePath as $key => $item) {
  299. if (in_array($key, ['pages', 'router', 'api'])) {
  300. $item = $zipPath . str_replace(dirname(app()->getRootPath()), '', Make::adminTemplatePath()) . $item;
  301. } else {
  302. $item = $zipPath . DS . $item;
  303. }
  304. $makePath[$key] = $item;
  305. }
  306. $routeName = 'crud/' . Str::snake($info->table_name);
  307. $column = $this->services->getColumnNamesList($info->table_name);
  308. $key = 'id';
  309. foreach ($column as $value) {
  310. if ($value['primaryKey']) {
  311. $key = $value['name'];
  312. break;
  313. }
  314. }
  315. $softDelete = false;
  316. foreach ((array)$info->field['tableField'] as $item) {
  317. if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
  318. $softDelete = true;
  319. break;
  320. }
  321. }
  322. $this->services->makeFile($info->table_name, $routeName, true, [
  323. 'menuName' => $info->name,
  324. 'key' => $key,
  325. 'softDelete' => $softDelete,
  326. 'fromField' => $info->field['fromField'] ?? [],
  327. 'columnField' => $info->field['columnField'] ?? [],
  328. ], $makePath, $zipPath);
  329. if (!extension_loaded('zip')) {
  330. return app('json')->fail(500040);
  331. }
  332. $fileService = new FileService();
  333. $fileService->addZip($zipPath, $zipName, app()->getRootPath() . 'backup');
  334. $key = md5($zipName);
  335. CacheService::set($key, [
  336. 'path' => $zipName,
  337. 'fileName' => Str::camel($info->table_name) . '.zip',
  338. ], 300);
  339. return app('json')->success(['download_url' => sys_config('site_url') . '/adminapi/download/' . $key]);
  340. }
  341. /**
  342. * 获取权限路由
  343. * @param $tableName
  344. * @return Response
  345. * @author 等风来
  346. * @email 136327134@qq.com
  347. * @date 2023/4/20
  348. */
  349. public function getRouteList($tableName)
  350. {
  351. $info = $this->services->get(['table_name' => $tableName]);
  352. if (!$info) {
  353. return app('json')->fail('crud详情查询失败');
  354. }
  355. $routeList = app()->make(SystemMenusServices::class)->getColumn([
  356. ['id', 'in', $info->menu_ids],
  357. ['auth_type', '=', 2]
  358. ], 'methods,api_url');
  359. $newRoute = [];
  360. foreach ($routeList as $item) {
  361. if ($item['methods'] == 'GET') {
  362. if (strstr($item['api_url'], 'create')) {
  363. $newRoute['create'] = $item['api_url'];
  364. } else if (strstr($item['api_url'], 'edit')) {
  365. $newRoute['edit'] = $item['api_url'];
  366. } else {
  367. $newRoute['index'] = $item['api_url'];
  368. }
  369. } else if ($item['methods'] == 'DELETE') {
  370. $newRoute['delete'] = $item['api_url'];
  371. }
  372. }
  373. $column = $this->services->getColumnNamesList($info->table_name);
  374. $key = 'id';
  375. foreach ($column as $value) {
  376. if ($value['primaryKey']) {
  377. $key = $value['name'];
  378. break;
  379. }
  380. }
  381. $columns = [
  382. [
  383. 'title' => 'ID',
  384. 'key' => $key,
  385. 'from_type' => '',
  386. ]
  387. ];
  388. foreach ((array)$info->field['tableField'] as $item) {
  389. if (isset($item['is_table']) && $item['is_table']) {
  390. $columns[] = [
  391. 'title' => $item['table_name'] ?: $item['comment'],
  392. 'key' => $item['field'],
  393. 'from_type' => $item['from_type'],
  394. ];
  395. }
  396. }
  397. $route = $newRoute;
  398. return app('json')->success(compact('key', 'route', 'columns'));
  399. }
  400. /**
  401. * @return string
  402. * @author 等风来
  403. * @email 136327134@qq.com
  404. * @date 2023/4/14
  405. */
  406. public function npm()
  407. {
  408. $terminal = new Terminal();
  409. $adminPath = $terminal->adminTemplatePath();
  410. $adminPath = dirname($adminPath);
  411. try {
  412. $dir = $adminPath . DS . 'node_modules';
  413. if (!is_dir($dir)) {
  414. // $terminal->run('npm-install');
  415. }
  416. // $res = $terminal->run('npm-build');
  417. } catch (\Throwable $e) {
  418. $terminal->echoOutputFlag($e->getMessage());
  419. }
  420. if (!is_dir($adminPath . DS . 'dist')) {
  421. echo Response::create([
  422. 'message' => '打包失败',
  423. ], 'json')->getContent();
  424. }
  425. $build = public_path() . config('app.admin_prefix');
  426. // $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build);
  427. // echo $res;
  428. }
  429. }