SystemCrud.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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\Request;
  16. use app\services\system\log\SystemFileServices;
  17. use app\services\system\SystemCrudDataService;
  18. use app\services\system\SystemCrudServices;
  19. use app\services\system\SystemMenusServices;
  20. use app\services\system\SystemRouteServices;
  21. use crmeb\services\CacheService;
  22. use crmeb\services\crud\enum\FormTypeEnum;
  23. use crmeb\services\crud\Make;
  24. use crmeb\services\crud\Service;
  25. use crmeb\services\FileService;
  26. use think\facade\App;
  27. use think\facade\Db;
  28. use think\facade\Env;
  29. use think\helper\Str;
  30. use think\Response;
  31. /**
  32. * Class SystemCrud
  33. * @author 等风来
  34. * @email 136327134@qq.com
  35. * @date 2023/4/6
  36. * @package app\adminapi\controller\v1\setting
  37. */
  38. class SystemCrud extends AuthController
  39. {
  40. /**
  41. * SystemCrud constructor.
  42. * @param App $app
  43. * @param SystemCrudServices $services
  44. */
  45. public function __construct(App $app, SystemCrudServices $services)
  46. {
  47. parent::__construct($app);
  48. $this->services = $services;
  49. }
  50. /**
  51. * @return Response
  52. * @author 等风来
  53. * @email 136327134@qq.com
  54. * @date 2023/4/11
  55. */
  56. public function index()
  57. {
  58. return app('json')->success($this->services->getList());
  59. }
  60. /**
  61. * 验证路径
  62. * @param $data
  63. * @return bool
  64. * @author wuhaotian
  65. * @email 442384644@qq.com
  66. * @date 2024/2/19
  67. */
  68. public function crudVerifyPath($data)
  69. {
  70. if (strpos($data['controller'], 'app/adminapi/controller/crud/') !== 0) return false;
  71. if (strpos($data['validate'], 'app/adminapi/validate/crud/') !== 0) return false;
  72. if (strpos($data['service'], 'app/services/crud/') !== 0) return false;
  73. if (strpos($data['dao'], 'app/dao/crud/') !== 0) return false;
  74. if (strpos($data['model'], 'app/model/crud/') !== 0) return false;
  75. if (strpos($data['route'], 'app/adminapi/route/crud/') !== 0) return false;
  76. if (strpos($data['router'], 'router/modules/crud/') !== 0) return false;
  77. if (strpos($data['api'], 'api/crud/') !== 0) return false;
  78. if (strpos($data['pages'], 'pages/crud/') !== 0) return false;
  79. return true;
  80. }
  81. /**
  82. * @return Response
  83. * @author 等风来
  84. * @email 136327134@qq.com
  85. * @date 2023/4/11
  86. */
  87. public function save(SystemCrudDataService $service, $id = 0)
  88. {
  89. $data = $this->request->postMore([
  90. ['pid', 0],//上级菜单id
  91. ['menuName', ''],//菜单名
  92. ['tableName', ''],//表名
  93. ['modelName', ''],//模块名称
  94. ['tableComment', ''],//表备注
  95. ['tableField', []],//表字段
  96. ['tableIndex', []],//索引
  97. ['filePath', []],//生成文件位置
  98. ['isTable', 0],//是否生成表
  99. ['deleteField', []],//删除的表字段
  100. ]);
  101. if (!preg_match('/^[\x{4e00}-\x{9fa5}a-zA-Z]+$/u', $data['menuName'])) return app('json')->fail('菜单名称只能是中文或者英文');
  102. if (!preg_match('/^[\x{4e00}-\x{9fa5}a-zA-Z]+$/u', $data['modelName'])) return app('json')->fail('模块名称只能是中文或者英文');
  103. if (!preg_match('/^[a-zA-Z_]+$/u', $data['tableName'])) return app('json')->fail('表名称只能是英文和下划线组成');
  104. if (!$this->crudVerifyPath($data['filePath'])) return app('json')->fail('生成的文件位置有误,请检查后重新生成');
  105. $fromField = $searchField = $hasOneField = $columnField = $tableIndex = [];
  106. $dictionaryids = array_column($data['tableField'], 'dictionary_id');
  107. if ($dictionaryids) {
  108. $dictionaryList = $service->getColumn([['id', 'in', $dictionaryids]], 'value', 'id');
  109. foreach ($dictionaryList as &$value) {
  110. $value = is_string($value) ? json_decode($value, true) : $value;
  111. }
  112. } else {
  113. $dictionaryList = [];
  114. }
  115. foreach ($data['tableField'] as $item) {
  116. //判断字段长度
  117. if (in_array($item['field_type'], [FormTypeEnum::DATE_TIME, 'timestamp', 'time', 'date', 'year']) && $item['limit'] > 6) {
  118. return app('json')->fail('字段' . $item['field'] . '长度不能大于6');
  119. }
  120. if ($item['field_type'] == 'enum' && !is_array($item['limit'])) {
  121. return app('json')->fail('数据类型为枚举时,长度为数组类型');
  122. }
  123. //收集列表展示数据
  124. if ($item['is_table'] && !in_array($item['field_type'], ['primaryKey', 'addSoftDelete'])) {
  125. if (isset($item['primaryKey']) && !$item['primaryKey']) {
  126. $columnField[] = [
  127. 'field' => $item['field'],
  128. 'name' => $item['table_name'] ?: $item['comment'],
  129. 'type' => $item['from_type'],
  130. ];
  131. }
  132. }
  133. $name = $item['table_name'] ?: $item['comment'];
  134. $option = $item['options'] ?? (isset($item['dictionary_id']) ? ($dictionaryList[$item['dictionary_id']] ?? []) : []);
  135. //收集表单展示数据
  136. if ($item['from_type']) {
  137. if (!$name) {
  138. return app('json')->fail(500048, [], ['field' => $item['field']]);
  139. }
  140. if (!$option && in_array($item['from_type'], [FormTypeEnum::RADIO, FormTypeEnum::SELECT])) {
  141. return app('json')->fail('表单类型为radio或select时,options字段不能为空');
  142. }
  143. $fromField[] = [
  144. 'field' => $item['field'],
  145. 'type' => $item['from_type'],
  146. 'name' => $name,
  147. 'required' => $item['required'],
  148. 'option' => $option
  149. ];
  150. }
  151. //搜索
  152. if (!empty($item['search'])) {
  153. $searchField[] = [
  154. 'field' => $item['field'],
  155. 'type' => $item['from_type'],
  156. 'name' => $name,
  157. 'search' => $item['search'],
  158. 'options' => $option
  159. ];
  160. }
  161. //关联
  162. if (!empty($item['hasOne'])) {
  163. $hasOneField[] = [
  164. 'field' => $item['field'],
  165. 'hasOne' => $item['hasOne'] ?? '',
  166. 'name' => $name,
  167. ];
  168. }
  169. //索引
  170. if (!empty($item['index'])) {
  171. $tableIndex[] = $item['field'];
  172. }
  173. }
  174. if (!$fromField) {
  175. return app('json')->fail(500046);
  176. }
  177. if (!$columnField) {
  178. return app('json')->fail(500047);
  179. }
  180. $data['fromField'] = $fromField;
  181. $data['tableIndex'] = $tableIndex;
  182. $data['columnField'] = $columnField;
  183. $data['searchField'] = $searchField;
  184. $data['hasOneField'] = $hasOneField;
  185. if (!$data['tableName']) {
  186. return app('json')->fail(500042);
  187. }
  188. $this->services->createCrud($id, $data);
  189. return app('json')->success(500043);
  190. }
  191. /**
  192. * 获取创建文件的目录存放位置
  193. * @return Response
  194. * @author 等风来
  195. * @email 136327134@qq.com
  196. * @date 2023/4/11
  197. */
  198. public function getFilePath()
  199. {
  200. [$tableName] = $this->request->postMore([
  201. ['tableName', ''],
  202. ], true);
  203. if (!$tableName) {
  204. return app('json')->fail(500042);
  205. }
  206. if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
  207. return app('json')->fail(500041);
  208. }
  209. $routeName = 'crud/' . Str::snake($tableName);
  210. $key = 'id';
  211. $tableField = [];
  212. $field = $this->services->getColumnNamesList($tableName);
  213. foreach ($field as $item) {
  214. if ($item['primaryKey']) {
  215. $key = $item['name'];
  216. }
  217. $tableField[] = [
  218. 'field' => $item['name'],
  219. 'field_type' => $item['type'],
  220. 'primaryKey' => (bool)$item['primaryKey'],
  221. 'default' => $item['default'],
  222. 'limit' => $item['limit'],
  223. 'comment' => $item['comment'],
  224. 'required' => false,
  225. 'is_table' => false,
  226. 'table_name' => '',
  227. 'from_type' => '',
  228. ];
  229. }
  230. $make = $this->services->makeFile($tableName, $routeName, false, [
  231. 'menuName' => '',
  232. 'key' => $key,
  233. 'fromField' => [],
  234. 'columnField' => [],
  235. ]);
  236. $makePath = [];
  237. foreach ($make as $k => $item) {
  238. $makePath[$k] = $item['path'];
  239. }
  240. return app('json')->success(compact('makePath', 'tableField'));
  241. }
  242. /**
  243. * @param $id
  244. * @return Response
  245. * @author 等风来
  246. * @email 136327134@qq.com
  247. * @date 2023/4/12
  248. */
  249. public function read($id)
  250. {
  251. if (!$id) {
  252. return app('json')->fail(500035);
  253. }
  254. $info = $this->services->get($id);
  255. if (!$info) {
  256. return app('json')->fail(100026);
  257. }
  258. $routeName = 'crud/' . Str::snake($info->table_name);
  259. $column = $this->services->getColumnNamesList($info->table_name);
  260. $key = 'id';
  261. foreach ($column as $value) {
  262. if ($value['primaryKey']) {
  263. $key = $value['name'];
  264. break;
  265. }
  266. }
  267. $softDelete = false;
  268. foreach ((array)$info->field['tableField'] as $item) {
  269. if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
  270. $softDelete = true;
  271. break;
  272. }
  273. }
  274. $make = $this->services->makeFile($info->table_name, $routeName, false, [
  275. 'menuName' => $info->name,
  276. 'modelName' => $info->model_name,
  277. 'tableField' => $info->field['tableField'] ?? [],
  278. 'key' => $key,
  279. 'softDelete' => $softDelete,
  280. 'fromField' => $info->field['fromField'] ?? [],
  281. 'columnField' => $info->field['columnField'] ?? [],
  282. 'searchField' => $info->field['searchField'] ?? [],
  283. 'hasOneField' => $info->field['hasOneField'] ?? [],
  284. ]);
  285. $data = [];
  286. foreach ($make as $key => $item) {
  287. if (in_array($key, ['pages', 'router', 'api'])) {
  288. $path = Make::adminTemplatePath() . $item['path'];
  289. } else {
  290. $path = app()->getRootPath() . $item['path'];
  291. }
  292. $item['name'] = $item['path'];
  293. try {
  294. $item['content'] = file_get_contents($path, LOCK_EX);
  295. $data[$key] = $item;
  296. } catch (\Throwable $e) {
  297. }
  298. }
  299. //调整排序
  300. $makeData = [];
  301. $names = [
  302. 'controller' => '控制器',
  303. 'validate' => '验证器',
  304. 'service' => '逻辑层',
  305. 'dao' => '数据库操作',
  306. 'model' => '模型层',
  307. 'route' => '后端路由',
  308. 'router' => '前端路由',
  309. 'api' => '前端接口',
  310. 'pages' => '前端页面'
  311. ];
  312. foreach ($names as $name => $value) {
  313. if (isset($data[$name])) {
  314. $data[$name]['file_name'] = $value;
  315. $makeData[] = $data[$name];
  316. }
  317. }
  318. $data = $makeData;
  319. $info = $info->toArray();
  320. //记录没有修改之前的数据
  321. foreach ((array)$info['field']['tableField'] as $key => $item) {
  322. $item['default_field'] = $item['field'];
  323. $item['default_limit'] = $item['limit'];
  324. $item['default_field_type'] = $item['field_type'];
  325. $item['default_comment'] = $item['comment'];
  326. $item['default_default'] = $item['default'];
  327. $item['default_default_type'] = $item['default_type'] ?? '1';
  328. $item['default_type'] = $item['default_type'] ?? '1';
  329. $item['is_table'] = !!$item['is_table'];
  330. $item['required'] = !!$item['required'];
  331. $item['index'] = isset($item['index']) && !!$item['index'];
  332. $item['primaryKey'] = isset($item['primaryKey']) ? (int)$item['primaryKey'] : 0;
  333. if (!isset($item['dictionary_id'])) {
  334. $item['dictionary_id'] = 0;
  335. }
  336. $info['field']['tableField'][$key] = $item;
  337. }
  338. //对比数据库,是否有新增字段
  339. $newColumn = [];
  340. $fieldAll = array_column($info['field']['tableField'], 'field');
  341. foreach ($column as $value) {
  342. if (!in_array($value['name'], $fieldAll)) {
  343. $newColumn[] = [
  344. 'field' => $value['name'],
  345. 'field_type' => $value['type'],
  346. 'primaryKey' => $value['primaryKey'] ? 1 : 0,
  347. 'default' => $value['default'],
  348. 'limit' => $value['limit'],
  349. 'comment' => $value['comment'],
  350. 'required' => '',
  351. 'is_table' => '',
  352. 'table_name' => '',
  353. 'from_type' => '',
  354. 'default_field' => $value['name'],
  355. 'default_limit' => $value['limit'],
  356. 'default_field_type' => $value['type'],
  357. 'default_comment' => $value['comment'],
  358. 'default_default' => $value['default'],
  359. ];
  360. }
  361. }
  362. if ($newColumn) {
  363. $info['field']['tableField'] = array_merge($newColumn, $info['field']['tableField']);
  364. }
  365. $keyInfo = $deleteInfo = $createInfo = $updateInfo = [];
  366. $tableField = [];
  367. foreach ($info['field']['tableField'] as $item) {
  368. if ($item['primaryKey']) {
  369. $keyInfo = $item;
  370. continue;
  371. }
  372. if ($item['field_type'] == 'timestamp' && $item['field'] === 'delete_time') {
  373. $deleteInfo = $item;
  374. continue;
  375. }
  376. if ($item['field_type'] == 'timestamp' && $item['field'] === 'create_time') {
  377. $createInfo = $item;
  378. continue;
  379. }
  380. if ($item['field_type'] == 'timestamp' && $item['field'] === 'update_time') {
  381. $updateInfo = $item;
  382. continue;
  383. }
  384. $tableField[] = $item;
  385. }
  386. if ($keyInfo) {
  387. array_unshift($tableField, $keyInfo);
  388. }
  389. if ($createInfo) {
  390. array_push($tableField, $createInfo);
  391. }
  392. if ($updateInfo) {
  393. array_push($tableField, $updateInfo);
  394. }
  395. if ($deleteInfo) {
  396. array_push($tableField, $deleteInfo);
  397. }
  398. $info['field']['tableField'] = $tableField;
  399. $info['field']['pid'] = (int)$info['field']['pid'];
  400. return app('json')->success(['file' => $data, 'crudInfo' => $info]);
  401. }
  402. /**
  403. * @param Request $request
  404. * @param SystemFileServices $service
  405. * @param $id
  406. * @return Response
  407. * @author 等风来
  408. * @email 136327134@qq.com
  409. * @date 2023/4/24
  410. */
  411. public function savefile(Request $request, SystemFileServices $service, $id)
  412. {
  413. $comment = $request->param('comment');
  414. $filepath = $request->param('filepath');
  415. $pwd = $request->param('pwd');
  416. if ($pwd == '') {
  417. return app('json')->fail('请输入文件管理密码');
  418. }
  419. if (config('filesystem.password') != $pwd) {
  420. return app('json')->fail('文件管理密码错误');
  421. }
  422. if (empty($filepath) || !$id) {
  423. return app('json')->fail(410087);
  424. }
  425. $crudInfo = $this->services->get($id, ['make_path']);
  426. if (!$crudInfo) {
  427. return app('json')->fail('修改的CRUD文件不存在');
  428. }
  429. $makeFilepath = '';
  430. foreach ($crudInfo->make_path as $key => $item) {
  431. $path = $item;
  432. if (in_array($key, ['pages', 'router', 'api'])) {
  433. $item = Make::adminTemplatePath() . $item;
  434. } else {
  435. $item = app()->getRootPath() . $item;
  436. }
  437. if ($filepath == $path) {
  438. $makeFilepath = $item;
  439. break;
  440. }
  441. }
  442. if (!$makeFilepath || !in_array($filepath, $crudInfo->make_path)) {
  443. return app('json')->fail('您没有权限修改此文件');
  444. }
  445. $res = $service->savefile($makeFilepath, $comment);
  446. if ($res) {
  447. return app('json')->success(100000);
  448. } else {
  449. return app('json')->fail(100006);
  450. }
  451. }
  452. /**
  453. * 获取tree菜单
  454. * @return Response
  455. * @author 等风来
  456. * @email 136327134@qq.com
  457. * @date 2023/4/11
  458. */
  459. public function getMenus()
  460. {
  461. return app('json')->success(app()->make(SystemMenusServices::class)
  462. ->getList(['auth_type' => 1, 'is_show' => 1], ['auth_type', 'pid', 'id', 'menu_name as label', 'id as value']));
  463. }
  464. /**
  465. * 获取可以进行关联的表名
  466. * @return Response
  467. * @author 等风来
  468. * @email 136327134@qq.com
  469. * @date 2023/8/2
  470. */
  471. public function getAssociationTable()
  472. {
  473. return app('json')->success($this->services->getTableAll());
  474. }
  475. /**
  476. * 获取表的详细信息
  477. * @param string $tableName
  478. * @return Response
  479. * @author 等风来
  480. * @email 136327134@qq.com
  481. * @date 2023/8/2
  482. */
  483. public function getAssociationTableInfo(string $tableName)
  484. {
  485. if (!$tableName) {
  486. return app('json')->fail('缺少表名');
  487. }
  488. // if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
  489. // return app('json')->fail('不允许查看当前表明细');
  490. // }
  491. $tableInfo = $this->services->getColumnNamesList($tableName);
  492. $data = [];
  493. foreach ($tableInfo as $key => $item) {
  494. $data[] = [
  495. 'label' => $item['comment'] ?: $key,
  496. 'value' => $key,
  497. 'leaf' => true
  498. ];
  499. }
  500. return app('json')->success($data);
  501. }
  502. /**
  503. * 获取创建表数据类型
  504. * @return Response
  505. * @author 等风来
  506. * @email 136327134@qq.com
  507. * @date 2023/4/11
  508. */
  509. public function columnType()
  510. {
  511. return app('json')->success($this->services->getTabelRule());
  512. }
  513. /**
  514. * @param SystemMenusServices $services
  515. * @param $id
  516. * @return Response
  517. * @author 等风来
  518. * @email 136327134@qq.com
  519. * @date 2023/4/11
  520. */
  521. public function delete(SystemMenusServices $services, $id)
  522. {
  523. if (!$id) {
  524. return app('json')->fail(500035);
  525. }
  526. $info = $this->services->get($id);
  527. if (!$info) {
  528. return app('json')->fail(100026);
  529. }
  530. $menusServices = app()->make(SystemMenusServices::class);
  531. if ($info->menu_ids) {
  532. $menusServices->deleteMenus($info->menu_ids);
  533. }
  534. if ($info->menu_id) {
  535. $menusServices->deleteMenus([$info->menu_id]);
  536. }
  537. $routeServices = app()->make(SystemRouteServices::class);
  538. if ($info->route_ids) {
  539. $routeServices->deleteRoutes($info->route_ids);
  540. }
  541. Db::query("DROP TABLE `" . Env::get('database.prefix', 'eb_') . $info->table_name . "`");
  542. if ($info->make_path) {
  543. $errorFile = [];
  544. foreach ($info->make_path as $key => $item) {
  545. if (in_array($key, ['pages', 'router', 'api'])) {
  546. $item = Make::adminTemplatePath() . $item;
  547. } else {
  548. $item = app()->getRootPath() . $item;
  549. }
  550. try {
  551. unlink($item);
  552. } catch (\Throwable $e) {
  553. $errorFile[] = $item;
  554. }
  555. }
  556. if ($errorFile) {
  557. return app('json')->success(500040, [], [
  558. 'message' => '文件:' . implode("\n", $errorFile) . ';无法被删除!'
  559. ]);
  560. }
  561. }
  562. $info->delete();
  563. return app('json')->success(100002);
  564. }
  565. /**
  566. * 下载文件
  567. * @param $id
  568. * @return Response
  569. * @author 等风来
  570. * @email 136327134@qq.com
  571. * @date 2023/4/15
  572. */
  573. public function download($id)
  574. {
  575. if (!$id) {
  576. return app('json')->fail(500035);
  577. }
  578. $info = $this->services->get($id);
  579. if (!$info) {
  580. return app('json')->fail(100026);
  581. }
  582. $zipPath = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name);
  583. $zipName = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name) . '.zip';
  584. if (is_file($zipName)) {
  585. unlink($zipName);
  586. }
  587. $makePath = $info->make_path ?? [];
  588. foreach ($makePath as $key => $item) {
  589. if (in_array($key, ['pages', 'router', 'api'])) {
  590. $item = $zipPath . str_replace(dirname(app()->getRootPath()), '', Make::adminTemplatePath()) . $item;
  591. } else {
  592. $item = $zipPath . DS . 'crmeb' . DS . $item;
  593. }
  594. $makePath[$key] = $item;
  595. }
  596. $routeName = 'crud/' . Str::snake($info->table_name);
  597. $column = $this->services->getColumnNamesList($info->table_name);
  598. $key = 'id';
  599. foreach ($column as $value) {
  600. if ($value['primaryKey']) {
  601. $key = $value['name'];
  602. break;
  603. }
  604. }
  605. $softDelete = false;
  606. foreach ((array)$info->field['tableField'] as $item) {
  607. if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
  608. $softDelete = true;
  609. break;
  610. }
  611. }
  612. $this->services->makeFile($info->table_name, $routeName, true, [
  613. 'menuName' => $info->name,
  614. 'tableFields' => $info->field['tableField'] ?? [],
  615. 'key' => $key,
  616. 'softDelete' => $softDelete,
  617. 'fromField' => $info->field['fromField'] ?? [],
  618. 'columnField' => $info->field['columnField'] ?? [],
  619. 'searchField' => $info->field['searchField'] ?? [],
  620. 'hasOneField' => $info->field['hasOneField'] ?? [],
  621. ], $makePath, $zipPath);
  622. if (!extension_loaded('zip')) {
  623. return app('json')->fail(500039);
  624. }
  625. $fileService = new FileService();
  626. $fileService->addZip($zipPath, $zipName, app()->getRootPath() . 'backup');
  627. $key = md5($zipName);
  628. CacheService::set($key, [
  629. 'path' => $zipName,
  630. 'fileName' => Str::camel($info->table_name) . '.zip',
  631. ], 300);
  632. return app('json')->success(['download_url' => sys_config('site_url') . '/adminapi/download/' . $key]);
  633. }
  634. /**
  635. * 获取权限路由
  636. * @param $tableName
  637. * @return Response
  638. * @author 等风来
  639. * @email 136327134@qq.com
  640. * @date 2023/4/20
  641. */
  642. public function getRouteList($tableName)
  643. {
  644. $info = $this->services->get(['table_name' => $tableName]);
  645. if (!$info) {
  646. return app('json')->fail('crud详情查询失败');
  647. }
  648. $routeList = app()->make(SystemMenusServices::class)->getColumn([
  649. ['id', 'in', $info->menu_ids],
  650. ['auth_type', '=', 2]
  651. ], 'methods,api_url');
  652. $newRoute = [];
  653. foreach ($routeList as $item) {
  654. if ($item['methods'] == 'GET') {
  655. if (strstr($item['api_url'], 'create')) {
  656. $newRoute['create'] = $item['api_url'];
  657. } else if (strstr($item['api_url'], 'edit')) {
  658. $newRoute['edit'] = $item['api_url'];
  659. } else if (strstr($item['api_url'], 'status')) {
  660. $newRoute['status'] = $item['api_url'];
  661. } else {
  662. if (strstr($item['api_url'], '<id>')) {
  663. $newRoute['read'] = $item['api_url'];
  664. } else {
  665. $newRoute['index'] = $item['api_url'];
  666. }
  667. }
  668. } else if ($item['methods'] == 'DELETE') {
  669. $newRoute['delete'] = $item['api_url'];
  670. } else if ($item['methods'] == 'PUT' && strstr($item['api_url'], 'status')) {
  671. $newRoute['status'] = $item['api_url'];
  672. }
  673. }
  674. $column = $this->services->getColumnNamesList($info->table_name);
  675. $key = 'id';
  676. foreach ($column as $value) {
  677. if ($value['primaryKey']) {
  678. $key = $value['name'];
  679. break;
  680. }
  681. }
  682. $columns = [
  683. [
  684. 'title' => 'ID',
  685. 'key' => $key,
  686. 'from_type' => '',
  687. ]
  688. ];
  689. $readFields = [
  690. 'name' => $info->field['modelName'] ?: $info->field['menuName'],
  691. 'all' => [],
  692. ];
  693. foreach ((array)$info->field['tableField'] as $item) {
  694. if (isset($item['primaryKey']) && $item['primaryKey']) {
  695. continue;
  696. }
  697. $prefix = app()->make(Service::class)->getAttrPrefix();
  698. $readFields['all'][] = [
  699. 'field' => in_array($item['from_type'], [FormTypeEnum::FRAME_IMAGES,
  700. FormTypeEnum::DATE_TIME_RANGE,
  701. FormTypeEnum::RADIO,
  702. FormTypeEnum::SELECT,
  703. FormTypeEnum::CHECKBOX]) ? $item['field'] . $prefix : $item['field'],
  704. 'comment' => $item['comment'],
  705. 'from_type' => $item['from_type'],
  706. ];
  707. if (isset($item['is_table']) && $item['is_table']) {
  708. $label = '';
  709. if (in_array($item['from_type'], [FormTypeEnum::SWITCH, FormTypeEnum::DATE_TIME_RANGE, FormTypeEnum::FRAME_IMAGE_ONE, FormTypeEnum::FRAME_IMAGES])) {
  710. $keyName = 'slot';
  711. if ($item['from_type'] == FormTypeEnum::FRAME_IMAGES) {
  712. $label = $prefix;
  713. } else if ($item['from_type'] == FormTypeEnum::DATE_TIME_RANGE) {
  714. $label = $prefix;
  715. }
  716. } elseif (in_array($item['from_type'], [FormTypeEnum::RADIO, FormTypeEnum::SELECT, FormTypeEnum::CHECKBOX])) {
  717. $label = $prefix;
  718. $keyName = 'key';
  719. } else {
  720. $keyName = 'key';
  721. }
  722. $columns[] = [
  723. 'title' => $item['table_name'] ?: $item['comment'],
  724. $keyName => $item['field'] . $label,
  725. 'from_type' => $item['from_type'],
  726. ];
  727. }
  728. }
  729. $searchField = $info->field['searchField'] ?? [];
  730. $search = [];
  731. foreach ((array)$searchField as $item) {
  732. if (!$item['type']) {
  733. $item['type'] = FormTypeEnum::INPUT;
  734. }
  735. if ($item['search'] == 'BETWEEN') {
  736. $item['type'] = 'date-picker';
  737. } else {
  738. if (in_array($item['type'], [FormTypeEnum::CHECKBOX, FormTypeEnum::RADIO, FormTypeEnum::SELECT])) {
  739. $item['type'] = FormTypeEnum::SELECT;
  740. } else {
  741. $item['type'] = FormTypeEnum::INPUT;
  742. }
  743. }
  744. $search[] = [
  745. 'field' => $item['field'],
  746. 'type' => $item['type'],
  747. 'name' => $item['name'],
  748. 'option' => $item['options'] ?? [],
  749. ];
  750. }
  751. $route = $newRoute;
  752. return app('json')->success(compact('key', 'route', 'columns', 'readFields', 'search'));
  753. }
  754. /**
  755. * 修改或者保存字典数据
  756. * @param SystemCrudDataService $service
  757. * @param int $id
  758. * @return Response
  759. * @author 等风来
  760. * @email 136327134@qq.com
  761. * @date 2023/8/1
  762. */
  763. public function saveDataDictionary(SystemCrudDataService $service, $id = 0)
  764. {
  765. $data = $this->request->postMore([
  766. ['name', ''],
  767. ['value', []],
  768. ]);
  769. if (!$data['name']) {
  770. return app('json')->fail('数据字段名不能为空');
  771. }
  772. if (!$data['value']) {
  773. return app('json')->fail('数据字段内容不能为空');
  774. }
  775. $data['value'] = json_encode($data['value']);
  776. if ($id) {
  777. $service->update($id, $data);
  778. } else {
  779. $service->save($data);
  780. }
  781. return app('json')->success($id ? '修改成功' : '添加成功');
  782. }
  783. /**
  784. * 查看数据字典
  785. * @param SystemCrudDataService $service
  786. * @param $id
  787. * @return Response
  788. * @author 等风来
  789. * @email 136327134@qq.com
  790. * @date 2023/8/7
  791. */
  792. public function getDataDictionaryOne(SystemCrudDataService $service, $id)
  793. {
  794. if (!$id) {
  795. return app('json')->fail('缺少参数');
  796. }
  797. $info = $service->get($id);
  798. if (!$info) {
  799. return app('json')->fail('没有查询到数据');
  800. }
  801. return app('json')->success($info->toArray());
  802. }
  803. /**
  804. * 获取数据字典列表
  805. * @param SystemCrudDataService $service
  806. * @return Response
  807. * @throws \ReflectionException
  808. * @throws \think\db\exception\DataNotFoundException
  809. * @throws \think\db\exception\DbException
  810. * @throws \think\db\exception\ModelNotFoundException
  811. * @author 等风来
  812. * @email 136327134@qq.com
  813. * @date 2023/8/1
  814. */
  815. public function getDataDictionary(SystemCrudDataService $service)
  816. {
  817. $name = $this->request->get('name', '');
  818. $data = $service->getlistAll($name);
  819. return app('json')->success($data);
  820. }
  821. /**
  822. * 删除数据字典
  823. * @param SystemCrudDataService $service
  824. * @param $id
  825. * @return Response
  826. * @author 等风来
  827. * @email 136327134@qq.com
  828. * @date 2023/8/4
  829. */
  830. public function deleteDataDictionary(SystemCrudDataService $service, $id)
  831. {
  832. if (!$id) {
  833. return app('json')->fail('缺少参数');
  834. }
  835. if ($service->delete($id)) {
  836. return app('json')->success('删除成功');
  837. } else {
  838. return app('json')->fail('删除失败');
  839. }
  840. }
  841. }