SystemCrud.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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('缺少表名');
  94. }
  95. $this->services->createCrud($data);
  96. return app('json')->success('创建成功');
  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('缺少表名');
  113. }
  114. if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
  115. return app('json')->fail('不能生成系统自带数据表');
  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('缺少参数');
  161. }
  162. $info = $this->services->get($id);
  163. if (!$info) {
  164. return app('json')->fail('查看的生成的文件不存在');
  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 ($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], ['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('缺少参数');
  230. }
  231. $info = $this->services->get($id);
  232. if (!$info) {
  233. return app('json')->fail('删除的数据不存在');
  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('删除生成的菜单和信息成功,删除文件出错,详细错误:' . $e->getMessage());
  253. }
  254. }
  255. return app('json')->success('删除成功');
  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('缺少参数');
  269. }
  270. $info = $this->services->get($id);
  271. if (!$info) {
  272. return app('json')->fail('下载的文件不存在');
  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. $makePath = $info->make_path ?? [];
  277. foreach ($makePath as $key => $item) {
  278. if (in_array($key, ['pages', 'router', 'api'])) {
  279. $item = $zipPath . str_replace(dirname(app()->getRootPath()), '', Make::adminTemplatePath()) . $item;
  280. } else {
  281. $item = $zipPath . DS . $item;
  282. }
  283. $makePath[$key] = $item;
  284. }
  285. $routeName = 'crud/' . Str::snake($info->table_name);
  286. $column = $this->services->getColumnNamesList($info->table_name);
  287. $key = 'id';
  288. foreach ($column as $value) {
  289. if ($value['primaryKey']) {
  290. $key = $value['name'];
  291. break;
  292. }
  293. }
  294. $softDelete = false;
  295. foreach ((array)$info->field['tableField'] as $item) {
  296. if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
  297. $softDelete = true;
  298. break;
  299. }
  300. }
  301. $this->services->makeFile($info->table_name, $routeName, true, [
  302. 'menuName' => $info->name,
  303. 'key' => $key,
  304. 'softDelete' => $softDelete,
  305. 'fromField' => $info->field['fromField'] ?? [],
  306. 'columnField' => $info->field['columnField'] ?? [],
  307. ], $makePath);
  308. if (!extension_loaded('zip')) {
  309. return app('json')->fail('请安装zip扩展后在尝试下载');
  310. }
  311. $fileService = new FileService();
  312. $fileService->addZip($zipPath, $zipName, app()->getRootPath() . 'backup');
  313. $key = md5($zipName);
  314. CacheService::set($key, [
  315. 'path' => $zipName,
  316. 'fileName' => Str::camel($info->table_name) . '.zip'
  317. ]);
  318. return app()->success(['download_url' => sys_config('site_url') . '/adminapi/download/' . $key]);
  319. }
  320. /**
  321. * @return string
  322. * @author 等风来
  323. * @email 136327134@qq.com
  324. * @date 2023/4/14
  325. */
  326. public function npm()
  327. {
  328. $terminal = new Terminal();
  329. $adminPath = $terminal->adminTemplatePath();
  330. $adminPath = dirname($adminPath);
  331. try {
  332. $dir = $adminPath . DS . 'node_modules';
  333. if (!is_dir($dir)) {
  334. // $terminal->run('npm-install');
  335. }
  336. // $res = $terminal->run('npm-build');
  337. } catch (\Throwable $e) {
  338. $terminal->echoOutputFlag($e->getMessage());
  339. }
  340. if (!is_dir($adminPath . DS . 'dist')) {
  341. echo Response::create([
  342. 'message' => '打包失败',
  343. ], 'json')->getContent();
  344. }
  345. $build = public_path() . config('app.admin_prefix');
  346. // $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build);
  347. // echo $res;
  348. }
  349. }