AdminBaseController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/20
  6. * @time: 11:24
  7. */
  8. namespace app\common;
  9. use app\Request;
  10. use qiniu\basic\BaseController;
  11. use qiniu\exceptions\AdminException;
  12. abstract class AdminBaseController extends BaseController
  13. {
  14. /**
  15. * 当前登陆管理员信息
  16. * @var
  17. */
  18. protected $adminInfo;
  19. /**
  20. * 当前登陆管理员ID
  21. * @var
  22. */
  23. protected $adminId;
  24. /**
  25. * 当前管理员权限
  26. * @var array
  27. */
  28. protected $auth = [];
  29. // 搜索条件
  30. protected $searchable = [];
  31. // 搜索处理
  32. protected $searchDeal = null;
  33. // 创建参数
  34. protected $createParams = [];
  35. // 新增处理
  36. protected $saveDeal = null;
  37. // 更新处理
  38. protected $updateDeal = null;
  39. protected $validate = null;
  40. protected $service = null;
  41. protected $with = [];
  42. /**
  43. * 初始化
  44. */
  45. public function __construct(Request $request)
  46. {
  47. parent::__construct($request);
  48. $this->initialize();
  49. }
  50. protected function initialize()
  51. {
  52. $this->adminId = $this->request->adminId();
  53. $this->adminInfo = $this->request->adminInfo();
  54. $this->auth = $this->adminInfo['rule'] ?? [];
  55. }
  56. public function index()
  57. {
  58. if (!$this->service) {
  59. throw new AdminException('接口不存在');
  60. }
  61. $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
  62. list($page, $limit) = $this->service->getPageValue();
  63. $list = $this->service->getList($where, '*', $page, $limit, $this->with);
  64. $count = $this->service->getCount($where);
  65. return $this->success(compact('list', 'count'));
  66. }
  67. public function read($id)
  68. {
  69. if (!$this->service) {
  70. throw new AdminException('接口不存在');
  71. }
  72. $info = $this->service->get($id, '*', $this->with);
  73. if (!$info)
  74. return $this->error('数据不存在');
  75. return $this->success('ok', $info->toArray());
  76. }
  77. public function save()
  78. {
  79. if (!$this->service) {
  80. throw new AdminException('接口不存在');
  81. }
  82. $data = $this->request->postMore($this->createParams, false, $this->saveDeal);
  83. if ($this->validate) {
  84. $this->validate($data, $this->validate, 'save');
  85. }
  86. $res = $this->service->create($data);
  87. if ($res) return $this->success('添加成功');
  88. return $this->error('添加失败');
  89. }
  90. public function validate($data, $validate, $s = '')
  91. {
  92. $scene = method_exists($validate, 'allScene') ? ($validate->allScene() ?? []) : [];
  93. if (in_array($s, $scene))
  94. $res = $validate->scene($s)->check($data);
  95. else
  96. $res = $validate->check($data);
  97. if (!$res) throw new AdminException($validate->getError());
  98. }
  99. public function update($id)
  100. {
  101. if (!$this->service) {
  102. throw new AdminException('接口不存在');
  103. }
  104. $data = $this->service->get($id);
  105. if (!$data)
  106. return $this->error('数据不存在');
  107. $data = $this->request->postMore($this->createParams, false, $this->updateDeal, $id);
  108. if ($this->validate) {
  109. $this->validate($data, $this->validate, 'update');
  110. }
  111. $res = $this->service->update($id, $data);
  112. if ($res) return $this->success('修改成功');
  113. return $this->error('修改失败');
  114. }
  115. public function delete($id)
  116. {
  117. if (!$this->service) {
  118. throw new AdminException('接口不存在');
  119. }
  120. $data = $this->service->get($id);
  121. if (!$data)
  122. return $this->error('数据不存在');
  123. $res = $this->service->delete($id);
  124. if ($res) return $this->success('已删除');
  125. return $this->error('删除失败');
  126. }
  127. public function export()
  128. {
  129. if (!$this->service) {
  130. throw new AdminException('接口不存在');
  131. }
  132. $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
  133. $export_type = sys_config('export_type', 1);
  134. return $this->success($this->service->export($where, $export_type));
  135. }
  136. }