SystemCrud.php 13 KB

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