SystemCrud.php 8.3 KB

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