SystemCrud.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. if (!$item['table_name']) {
  83. return app('json')->fail('表单标题必须填写');
  84. }
  85. $fromField[] = [
  86. 'field' => $item['field'],
  87. 'type' => $item['from_type'],
  88. 'name' => $item['table_name'],
  89. 'required' => $item['required'],
  90. 'option' => $item['option'] ?? [],
  91. ];
  92. }
  93. }
  94. if (!$fromField) {
  95. return app('json')->fail('至少选择一个字段作为表单项');
  96. }
  97. if (!$columnField) {
  98. return app('json')->fail('至少选择一个字段作为列展示在列表中');
  99. }
  100. $data['fromField'] = $fromField;
  101. $data['columnField'] = $columnField;
  102. if (!$data['tableName']) {
  103. return app('json')->fail(500045);
  104. }
  105. $this->services->createCrud($data);
  106. return app('json')->success(500046);
  107. }
  108. /**
  109. * 获取创建文件的目录存放位置
  110. * @return \think\Response
  111. * @author 等风来
  112. * @email 136327134@qq.com
  113. * @date 2023/4/11
  114. */
  115. public function getFilePath()
  116. {
  117. [$tableName, $isTable] = $this->request->postMore([
  118. ['tableName', ''],
  119. ['isTable', 0],
  120. ], true);
  121. if (!$tableName) {
  122. return app('json')->fail(500045);
  123. }
  124. if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
  125. return app('json')->fail(500044);
  126. }
  127. $routeName = 'crud/' . Str::snake($tableName);
  128. $key = 'id';
  129. $tableField = [];
  130. if ($isTable) {
  131. $field = $this->services->getColumnNamesList($tableName);
  132. if (!$field) {
  133. return app('json')->fail('表不存在');
  134. }
  135. foreach ($field as $item) {
  136. if ($item['primaryKey']) {
  137. $key = $item['name'];
  138. }
  139. $tableField[] = [
  140. 'field' => $item['name'],
  141. 'field_type' => $item['type'],
  142. 'primaryKey' => (bool)$item['primaryKey'],
  143. 'default' => $item['default'],
  144. 'limit' => $item['limit'],
  145. 'comment' => $item['comment'],
  146. 'required' => false,
  147. 'is_table' => false,
  148. 'table_name' => '',
  149. 'from_type' => '',
  150. ];
  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. return app('json')->success($data);
  211. }
  212. /**
  213. * 获取tree菜单
  214. * @return \think\Response
  215. * @author 等风来
  216. * @email 136327134@qq.com
  217. * @date 2023/4/11
  218. */
  219. public function getMenus()
  220. {
  221. return app('json')->success(app()->make(SystemMenusServices::class)
  222. ->getList(['auth_type' => 1, 'is_show' => 1], ['auth_type', 'pid', 'id', 'menu_name as label', 'id as value']));
  223. }
  224. /**
  225. * 获取创建表数据类型
  226. * @return \think\Response
  227. * @author 等风来
  228. * @email 136327134@qq.com
  229. * @date 2023/4/11
  230. */
  231. public function columnType()
  232. {
  233. return app('json')->success($this->services->getTabelRule());
  234. }
  235. /**
  236. * @param SystemMenusServices $services
  237. * @param $id
  238. * @return \think\Response
  239. * @author 等风来
  240. * @email 136327134@qq.com
  241. * @date 2023/4/11
  242. */
  243. public function delete(SystemMenusServices $services, $id)
  244. {
  245. if (!$id) {
  246. return app('json')->fail(500035);
  247. }
  248. $info = $this->services->get($id);
  249. if (!$info) {
  250. return app('json')->fail(500042);
  251. }
  252. $services->transaction(function () use ($services, $info) {
  253. if ($info->menu_ids) {
  254. $services->deleteMenus($info->menu_ids);
  255. }
  256. $info->delete();
  257. });
  258. if ($info->make_path) {
  259. try {
  260. foreach ($info->make_path as $key => $item) {
  261. if (in_array($key, ['pages', 'router', 'api'])) {
  262. $item = Make::adminTemplatePath() . $item;
  263. } else {
  264. $item = app()->getRootPath() . $item;
  265. }
  266. unlink($item);
  267. }
  268. } catch (\Throwable $e) {
  269. return app('json')->success(500041, [], ['message' => $e->getMessage()]);
  270. }
  271. }
  272. return app('json')->success(100002);
  273. }
  274. /**
  275. * 下载文件
  276. * @param $id
  277. * @return Response
  278. * @author 等风来
  279. * @email 136327134@qq.com
  280. * @date 2023/4/15
  281. */
  282. public function download($id)
  283. {
  284. if (!$id) {
  285. return app('json')->fail(500035);
  286. }
  287. $info = $this->services->get($id);
  288. if (!$info) {
  289. return app('json')->fail(500039);
  290. }
  291. $zipPath = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name);
  292. $zipName = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name) . '.zip';
  293. if (is_file($zipName)) {
  294. unlink($zipName);
  295. }
  296. $makePath = $info->make_path ?? [];
  297. foreach ($makePath as $key => $item) {
  298. if (in_array($key, ['pages', 'router', 'api'])) {
  299. $item = $zipPath . str_replace(dirname(app()->getRootPath()), '', Make::adminTemplatePath()) . $item;
  300. } else {
  301. $item = $zipPath . DS . $item;
  302. }
  303. $makePath[$key] = $item;
  304. }
  305. $routeName = 'crud/' . Str::snake($info->table_name);
  306. $column = $this->services->getColumnNamesList($info->table_name);
  307. $key = 'id';
  308. foreach ($column as $value) {
  309. if ($value['primaryKey']) {
  310. $key = $value['name'];
  311. break;
  312. }
  313. }
  314. $softDelete = false;
  315. foreach ((array)$info->field['tableField'] as $item) {
  316. if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
  317. $softDelete = true;
  318. break;
  319. }
  320. }
  321. $this->services->makeFile($info->table_name, $routeName, true, [
  322. 'menuName' => $info->name,
  323. 'key' => $key,
  324. 'softDelete' => $softDelete,
  325. 'fromField' => $info->field['fromField'] ?? [],
  326. 'columnField' => $info->field['columnField'] ?? [],
  327. ], $makePath, $zipPath);
  328. if (!extension_loaded('zip')) {
  329. return app('json')->fail(500040);
  330. }
  331. $fileService = new FileService();
  332. $fileService->addZip($zipPath, $zipName, app()->getRootPath() . 'backup');
  333. $key = md5($zipName);
  334. CacheService::set($key, [
  335. 'path' => $zipName,
  336. 'fileName' => Str::camel($info->table_name) . '.zip',
  337. ], 300);
  338. return app('json')->success(['download_url' => sys_config('site_url') . '/adminapi/download/' . $key]);
  339. }
  340. /**
  341. * 获取权限路由
  342. * @param $tableName
  343. * @return Response
  344. * @author 等风来
  345. * @email 136327134@qq.com
  346. * @date 2023/4/20
  347. */
  348. public function getRouteList($tableName)
  349. {
  350. $info = $this->services->get(['table_name' => $tableName]);
  351. if (!$info) {
  352. return app('json')->fail('crud详情查询失败');
  353. }
  354. $routeList = app()->make(SystemMenusServices::class)->getColumn([
  355. ['id', 'in', $info->menu_ids],
  356. ['auth_type', '=', 2]
  357. ], 'methods,api_url');
  358. $newRoute = [];
  359. foreach ($routeList as $item) {
  360. if ($item['methods'] == 'GET') {
  361. if (strstr($item['api_url'], 'create')) {
  362. $newRoute['create'] = $item['api_url'];
  363. } else if (strstr($item['api_url'], 'edit')) {
  364. $newRoute['edit'] = $item['api_url'];
  365. } else {
  366. $newRoute['index'] = $item['api_url'];
  367. }
  368. } else if ($item['methods'] == 'DELETE') {
  369. $newRoute['delete'] = $item['api_url'];
  370. }
  371. }
  372. $column = $this->services->getColumnNamesList($info->table_name);
  373. $key = 'id';
  374. foreach ($column as $value) {
  375. if ($value['primaryKey']) {
  376. $key = $value['name'];
  377. break;
  378. }
  379. }
  380. $columns = [
  381. [
  382. 'title' => 'ID',
  383. 'key' => $key,
  384. 'from_type' => '',
  385. ]
  386. ];
  387. foreach ((array)$info->field['tableField'] as $item) {
  388. if (isset($item['is_table']) && $item['is_table']) {
  389. $columns[] = [
  390. 'title' => $item['table_name'] ?: $item['comment'],
  391. 'key' => $item['field'],
  392. 'from_type' => $item['from_type'],
  393. ];
  394. }
  395. }
  396. $route = $newRoute;
  397. return app('json')->success(compact('key', 'route', 'columns'));
  398. }
  399. /**
  400. * @return string
  401. * @author 等风来
  402. * @email 136327134@qq.com
  403. * @date 2023/4/14
  404. */
  405. public function npm()
  406. {
  407. $terminal = new Terminal();
  408. $adminPath = $terminal->adminTemplatePath();
  409. $adminPath = dirname($adminPath);
  410. try {
  411. $dir = $adminPath . DS . 'node_modules';
  412. if (!is_dir($dir)) {
  413. // $terminal->run('npm-install');
  414. }
  415. // $res = $terminal->run('npm-build');
  416. } catch (\Throwable $e) {
  417. $terminal->echoOutputFlag($e->getMessage());
  418. }
  419. if (!is_dir($adminPath . DS . 'dist')) {
  420. echo Response::create([
  421. 'message' => '打包失败',
  422. ], 'json')->getContent();
  423. }
  424. $build = public_path() . config('app.admin_prefix');
  425. // $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build);
  426. // echo $res;
  427. }
  428. }