SystemCrud.php 14 KB

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