SystemCrud.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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\crud\Make;
  18. use crmeb\services\FileService;
  19. use crmeb\utils\Terminal;
  20. use think\facade\App;
  21. use think\helper\Str;
  22. use think\Response;
  23. /**
  24. * Class SystemCrud
  25. * @author 等风来
  26. * @email 136327134@qq.com
  27. * @date 2023/4/6
  28. * @package app\adminapi\controller\v1\setting
  29. */
  30. class SystemCrud extends AuthController
  31. {
  32. /**
  33. * SystemCrud constructor.
  34. * @param App $app
  35. * @param SystemCrudServices $services
  36. */
  37. public function __construct(App $app, SystemCrudServices $services)
  38. {
  39. parent::__construct($app);
  40. $this->services = $services;
  41. }
  42. /**
  43. * @return \think\Response
  44. * @author 等风来
  45. * @email 136327134@qq.com
  46. * @date 2023/4/11
  47. */
  48. public function index()
  49. {
  50. return app('json')->success($this->services->getList());
  51. }
  52. /**
  53. * @return \think\Response
  54. * @author 等风来
  55. * @email 136327134@qq.com
  56. * @date 2023/4/11
  57. */
  58. public function save()
  59. {
  60. $data = $this->request->postMore([
  61. ['pid', 0],
  62. ['menuName', ''],
  63. ['tableName', ''],
  64. ['tableComment', ''],//表备注
  65. ['tableField', []],//表字段
  66. ['tableIndex', []],//索引
  67. ['filePath', []],
  68. ['isTable', 0],
  69. ]);
  70. $fromField = $columnField = [];
  71. foreach ($data['tableField'] as $item) {
  72. if ($item['is_table'] && !in_array($item['field_type'], ['addSoftDelete', 'addSoftDelete'])) {
  73. $columnField[] = [
  74. 'field' => $item['field'],
  75. 'name' => $item['table_name'],
  76. 'type' => $item['from_type'],
  77. ];
  78. }
  79. if ($item['from_type']) {
  80. $fromField[] = [
  81. 'field' => $item['field'],
  82. 'type' => $item['from_type'],
  83. 'name' => $item['table_name'],
  84. 'required' => $item['required'],
  85. 'option' => $item['option'] ?? [],
  86. ];
  87. }
  88. }
  89. $data['fromField'] = $fromField;
  90. $data['columnField'] = $columnField;
  91. if (!$data['tableName']) {
  92. return app('json')->fail('缺少表名');
  93. }
  94. $this->services->createCrud($data);
  95. return app('json')->success('创建成功');
  96. }
  97. /**
  98. * 获取创建文件的目录存放位置
  99. * @return \think\Response
  100. * @author 等风来
  101. * @email 136327134@qq.com
  102. * @date 2023/4/11
  103. */
  104. public function getFilePath()
  105. {
  106. [$tableName, $isTable] = $this->request->postMore([
  107. ['tableName', ''],
  108. ['isTable', 0],
  109. ], true);
  110. if (!$tableName) {
  111. return app('json')->fail('缺少表名');
  112. }
  113. if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
  114. return app('json')->fail('不能生成系统自带数据表');
  115. }
  116. $routeName = 'crud/' . Str::snake($tableName);
  117. $make = $this->services->makeFile($tableName, $routeName, false, [
  118. 'menuName' => '',
  119. 'fromField' => [],
  120. 'columnField' => [],
  121. ]);
  122. $makePath = [];
  123. foreach ($make as $key => $item) {
  124. $makePath[$key] = $item['path'];
  125. }
  126. $tableField = [];
  127. if ($isTable) {
  128. $field = $this->services->getColumnNamesList($tableName);
  129. if (!$field) {
  130. return app('json')->fail('表不存在');
  131. }
  132. foreach ($field as $item) {
  133. $tableField[] = [
  134. 'field' => $item['name'],
  135. 'field_type' => $item['type'],
  136. 'primaryKey' => (bool)$item['primaryKey'],
  137. 'default' => $item['default'],
  138. 'limit' => $item['limit'],
  139. 'comment' => $item['comment'],
  140. 'required' => false,
  141. 'is_table' => false,
  142. 'table_name' => '',
  143. 'from_type' => '',
  144. ];
  145. }
  146. }
  147. return app('json')->success(compact('makePath', 'tableField'));
  148. }
  149. /**
  150. * @param $id
  151. * @return \think\Response
  152. * @author 等风来
  153. * @email 136327134@qq.com
  154. * @date 2023/4/12
  155. */
  156. public function read($id)
  157. {
  158. if (!$id) {
  159. return app('json')->fail('缺少参数');
  160. }
  161. $info = $this->services->get($id);
  162. if (!$info) {
  163. return app('json')->fail('查看的生成的文件不存在');
  164. }
  165. $routeName = 'crud/' . Str::snake($info->table_name);
  166. $column = $this->services->getColumnNamesList($info->table_name);
  167. $key = 'id';
  168. foreach ($column as $value) {
  169. if ($value['primaryKey']) {
  170. $key = $value['name'];
  171. break;
  172. }
  173. }
  174. $softDelete = false;
  175. foreach ((array)$info->field['tableField'] as $item) {
  176. if ($item['field_type'] === 'addSoftDelete') {
  177. $softDelete = true;
  178. break;
  179. }
  180. }
  181. $make = $this->services->makeFile($info->table_name, $routeName, false, [
  182. 'menuName' => $info->name,
  183. 'key' => $key,
  184. 'softDelete' => $softDelete,
  185. 'fromField' => $info->field['fromField'] ?? [],
  186. 'columnField' => $info->field['columnField'] ?? [],
  187. ]);
  188. $data = [];
  189. foreach ($make as $item) {
  190. $item['name'] = $item['path'];
  191. $data[] = $item;
  192. }
  193. return app('json')->success($data);
  194. }
  195. /**
  196. * 获取tree菜单
  197. * @return \think\Response
  198. * @author 等风来
  199. * @email 136327134@qq.com
  200. * @date 2023/4/11
  201. */
  202. public function getMenus()
  203. {
  204. return app('json')->success(app()->make(SystemMenusServices::class)
  205. ->getList(['auth_type' => 1], ['pid', 'id', 'menu_name as label', 'id as value']));
  206. }
  207. /**
  208. * 获取创建表数据类型
  209. * @return \think\Response
  210. * @author 等风来
  211. * @email 136327134@qq.com
  212. * @date 2023/4/11
  213. */
  214. public function columnType()
  215. {
  216. return app('json')->success($this->services->getTabelRule());
  217. }
  218. /**
  219. * @param $id
  220. * @return \think\Response
  221. * @author 等风来
  222. * @email 136327134@qq.com
  223. * @date 2023/4/11
  224. */
  225. public function delete(SystemMenusServices $services, $id)
  226. {
  227. if (!$id) {
  228. return app('json')->fail('缺少参数');
  229. }
  230. $info = $this->services->get($id);
  231. if (!$info) {
  232. return app('json')->fail('删除的数据不存在');
  233. }
  234. $services->transaction(function () use ($services, $info) {
  235. if ($info->menu_ids) {
  236. $services->deleteMenus($info->menu_ids);
  237. }
  238. $info->delete();
  239. });
  240. if ($info->make_path) {
  241. try {
  242. foreach ($info->make_path as $key => $item) {
  243. if (in_array($key, ['pages', 'router', 'api'])) {
  244. $item = Make::adminTemplatePath() . $item;
  245. } else {
  246. $item = app()->getRootPath() . $item;
  247. }
  248. unlink($item);
  249. }
  250. } catch (\Throwable $e) {
  251. return app('json')->success('删除生成的菜单和信息成功,删除文件出错,详细错误:' . $e->getMessage());
  252. }
  253. }
  254. return app('json')->success('删除成功');
  255. }
  256. /**
  257. * @return string
  258. * @author 等风来
  259. * @email 136327134@qq.com
  260. * @date 2023/4/14
  261. */
  262. public function npm()
  263. {
  264. $terminal = new Terminal();
  265. $adminPath = $terminal->adminTemplatePath();
  266. $adminPath = dirname($adminPath);
  267. $dir = $adminPath . DS . 'node_modules';
  268. if (!is_dir($dir)) {
  269. $terminal->run('npm-install');
  270. }
  271. $terminal->run('npm-build');
  272. if (!is_dir($adminPath . DS . 'dist')) {
  273. return Response::create([
  274. 'message' => '打包失败',
  275. ], 'json')->getContent();
  276. }
  277. $build = public_path() . config('app.admin_prefix');
  278. $this->app->make(FileService::class)->copyDir($adminPath . DS . 'dist', $build);
  279. return Response::create([
  280. 'message' => '打包成功',
  281. 'success' => 'ok'
  282. ], 'json')->getContent();
  283. }
  284. }