SystemCrud.php 14 KB

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