SystemCrud.php 29 KB

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