SystemCrud.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. ['tableTime', 0],//表是否增加修改和添加时间
  64. ['tableDelete', 0],//表是否增加伪删除
  65. ['fromField', []],
  66. ['columnField', []],
  67. ['filePath', []],
  68. ['isTable', 0],
  69. ]);
  70. if (!$data['tableName']) {
  71. return app('json')->fail('缺少表名');
  72. }
  73. $this->services->createCrud($data);
  74. return app('json')->success('创建成功');
  75. }
  76. /**
  77. * 获取创建文件的目录存放位置
  78. * @return \think\Response
  79. * @author 等风来
  80. * @email 136327134@qq.com
  81. * @date 2023/4/11
  82. */
  83. public function getFilePath()
  84. {
  85. [$menuName, $tableName, $isTable, $fromField, $columnField] = $this->request->postMore([
  86. ['menuName', ''],
  87. ['tableName', ''],
  88. ['isTable', 0],
  89. ['fromField', []],
  90. ['columnField', []],
  91. ], true);
  92. if (!$tableName) {
  93. return app('json')->fail('缺少表名');
  94. }
  95. if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
  96. return app('json')->fail('不能生成系统自带数据表');
  97. }
  98. $routeName = 'crud/' . Str::snake($tableName);
  99. $make = $this->services->makeFile($tableName, $routeName, false, [
  100. 'menuName' => $menuName,
  101. 'fromField' => $fromField,
  102. 'columnField' => $columnField,
  103. ]);
  104. $makePath = [];
  105. foreach ($make as $key => $item) {
  106. $makePath[$key] = $item['path'];
  107. }
  108. $tableField = [];
  109. if ($isTable) {
  110. $field = $this->services->getColumnNamesList($tableName);
  111. if (!$field) {
  112. return app('json')->fail('表不存在');
  113. }
  114. foreach ($field as $item) {
  115. $tableField[] = ['value' => $item['name'], 'comment' => $item['comment'], 'label' => $item['name']];
  116. }
  117. }
  118. return app('json')->success(compact('makePath', 'tableField'));
  119. }
  120. /**
  121. * @param $id
  122. * @return \think\Response
  123. * @author 等风来
  124. * @email 136327134@qq.com
  125. * @date 2023/4/12
  126. */
  127. public function read($id)
  128. {
  129. if (!$id) {
  130. return app('json')->fail('缺少参数');
  131. }
  132. $info = $this->services->get($id);
  133. if (!$info) {
  134. return app('json')->fail('查看的生成的文件不存在');
  135. }
  136. $routeName = 'crud/' . Str::snake($info->table_name);
  137. $make = $this->services->makeFile($info->table_name, $routeName, false, [
  138. 'menuName' => $info->name,
  139. 'fromField' => $info->field['fromField'] ?? [],
  140. 'columnField' => $info->field['columnField'] ?? [],
  141. ]);
  142. $data = [];
  143. foreach ($make as $item) {
  144. $item['name'] = pathinfo($item['path'])['basename'] ?? '';
  145. $data[] = $item;
  146. }
  147. return app('json')->success($data);
  148. }
  149. /**
  150. * 获取tree菜单
  151. * @return \think\Response
  152. * @author 等风来
  153. * @email 136327134@qq.com
  154. * @date 2023/4/11
  155. */
  156. public function getMenus()
  157. {
  158. return app('json')->success(app()->make(SystemMenusServices::class)
  159. ->getList(['auth_type' => 1], ['pid', 'id', 'menu_name as label', 'id as value']));
  160. }
  161. /**
  162. * 获取创建表数据类型
  163. * @return \think\Response
  164. * @author 等风来
  165. * @email 136327134@qq.com
  166. * @date 2023/4/11
  167. */
  168. public function columnType()
  169. {
  170. return app('json')->success($this->services->getTabelRule());
  171. }
  172. /**
  173. * @param $id
  174. * @return \think\Response
  175. * @author 等风来
  176. * @email 136327134@qq.com
  177. * @date 2023/4/11
  178. */
  179. public function delete($id)
  180. {
  181. if (!$id) {
  182. return app('json')->fail('缺少参数');
  183. }
  184. $this->services->delete($id);
  185. return app('json')->success('删除成功');
  186. }
  187. }