SystemCrud.php 30 KB

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