SystemCrud.php 29 KB

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