SystemCrud.php 15 KB

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