FieldController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\service\sys_manage\FieldService;
  4. use app\admin\validate\sys_manage\FieldValidate;
  5. use app\controller\Curd;
  6. use app\model\Content;
  7. use app\model\SysField;
  8. use Illuminate\Support\Facades\Schema;
  9. use support\Request;
  10. use support\Response;
  11. class FieldController extends Curd
  12. {
  13. public function __construct()
  14. {
  15. $this->model = new SysField();
  16. }
  17. /**
  18. * @Desc 字段列表
  19. * @Author Gorden
  20. * @Date 2024/2/22 14:49
  21. *
  22. * @param Request $request
  23. * @return \support\Response
  24. */
  25. public function fieldList(Request $request)
  26. {
  27. return FieldService::fieldList($request);
  28. }
  29. public function selectList(Request $request): Response
  30. {
  31. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  32. $query = $this->doSelect($where, $field, $order);
  33. return $this->doFormat($query, $format, 1000);
  34. }
  35. public function afterQuery($items)
  36. {
  37. foreach ($items as &$item) {
  38. if (!empty($item->field_refer_json)) {
  39. $fieldReferJson = json_decode($item->field_refer_json, true);
  40. $item->field_refer_json_default = $fieldReferJson['default'] ?? '';
  41. }
  42. }
  43. return $items;
  44. }
  45. /**
  46. * @Desc 字段详情
  47. * @Author Gorden
  48. * @Date 2024/2/22 14:54
  49. *
  50. * @param $id
  51. * @return \support\Response
  52. */
  53. public function fieldInfo($id)
  54. {
  55. return FieldService::fieldInfo($id);
  56. }
  57. /**
  58. * @Desc 新建字段
  59. * @Author Gorden
  60. * @Date 2024/2/22 14:32
  61. *
  62. * @param Request $request
  63. * @return \support\Response
  64. */
  65. public function addField(Request $request)
  66. {
  67. // $validate = new FieldValidate();
  68. // if (!$validate->scene('add')->check($request->post())) {
  69. // return json_fail($validate->getError());
  70. // }
  71. // 验证数据表是否存在
  72. if (!FieldService::checkTableExist($request->post('field_table'))) {
  73. return json_fail('数据表不存在');
  74. }
  75. // 验证字段重复
  76. if (FieldService::checkFieldExist($request->post('field_table'), $request->post('field_column_key'))) {
  77. return json_fail('字段已存在,请勿重复添加');
  78. }
  79. return FieldService::addField($request->post());
  80. }
  81. /**
  82. * @Desc 修改字段
  83. * @Author Gorden
  84. * @Date 2024/2/22 14:41
  85. *
  86. * @param $id
  87. * @param Request $request
  88. * @return \support\Response
  89. */
  90. public function updateFields($id, Request $request)
  91. {
  92. // $validate = new FieldValidate();
  93. // if (!$validate->scene('update')->check($request->post())) {
  94. // return json_fail($validate->getError());
  95. // }
  96. // 验证数据表是否存在
  97. if (!FieldService::checkTableExist($request->post('field_table'))) {
  98. return json_fail('数据表不存在');
  99. }
  100. return FieldService::updateField($id, $request->post());
  101. }
  102. /**
  103. * @Desc 修改字段状态
  104. * @Author Gorden
  105. * @Date 2024/2/22 15:05
  106. *
  107. * @param $id
  108. * @param Request $request
  109. * @return \support\Response
  110. */
  111. public function updateStatus($id, Request $request)
  112. {
  113. $validate = new FieldValidate();
  114. if (!$validate->scene('update_status')->check($request->post())) {
  115. return json_fail($validate->getError());
  116. }
  117. return FieldService::updateStatus($id, $request->post());
  118. }
  119. /**
  120. * @Desc 删除字段
  121. * @Author Gorden
  122. * @Date 2024/2/22 15:08
  123. *
  124. * @param $id
  125. * @return \support\Response
  126. */
  127. public function delField(Request $request)
  128. {
  129. $fieldId = $request->post('field_id');
  130. return FieldService::delField($fieldId);
  131. }
  132. }