SystemCrud.php 8.2 KB

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