SystemCrud.php 36 KB

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