SystemCrud.php 7.4 KB

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