AdminBaseController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /**
  42. * 初始化
  43. */
  44. public function __construct(Request $request)
  45. {
  46. parent::__construct($request);
  47. $this->initialize();
  48. }
  49. protected function initialize()
  50. {
  51. $this->adminId = $this->request->adminId();
  52. $this->adminInfo = $this->request->adminInfo();
  53. $this->auth = $this->adminInfo['rule'] ?? [];
  54. }
  55. public function index()
  56. {
  57. if (!$this->service) {
  58. throw new AdminException('接口不存在');
  59. }
  60. $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
  61. list($page, $limit) = $this->service->getPageValue();
  62. $list = $this->service->getList($where, '*', $page, $limit);
  63. $count = $this->service->getCount($where);
  64. return $this->success(compact('list', 'count'));
  65. }
  66. public function read($id)
  67. {
  68. if (!$this->service) {
  69. throw new AdminException('接口不存在');
  70. }
  71. $info = $this->service->get($id);
  72. if (!$info)
  73. return $this->error('数据不存在');
  74. return $this->success('ok', $info->toArray());
  75. }
  76. public function save()
  77. {
  78. if (!$this->service) {
  79. throw new AdminException('接口不存在');
  80. }
  81. $data = $this->request->postMore($this->createParams, false, $this->saveDeal);
  82. if ($this->validate) {
  83. $this->validate($data, $this->validate);
  84. }
  85. $res = $this->service->create($data);
  86. if ($res) return $this->success('添加成功');
  87. return $this->error('添加失败');
  88. }
  89. public function validate($data, $validate)
  90. {
  91. $res = $validate->check($data);
  92. if (!$res) throw new AdminException($validate->getError());
  93. }
  94. public function update($id)
  95. {
  96. if (!$this->service) {
  97. throw new AdminException('接口不存在');
  98. }
  99. $data = $this->service->get($id);
  100. if (!$data)
  101. return $this->error('数据不存在');
  102. $data = $this->request->postMore($this->createParams, false, $this->updateDeal);
  103. if ($this->validate) {
  104. $this->validate($data, $this->validate);
  105. }
  106. $res = $this->service->update($id, $data);
  107. if ($res) return $this->success('修改成功');
  108. return $this->error('修改失败');
  109. }
  110. public function delete($id)
  111. {
  112. if (!$this->service) {
  113. throw new AdminException('接口不存在');
  114. }
  115. $data = $this->service->get($id);
  116. if (!$data)
  117. return $this->error('数据不存在');
  118. $res = $this->service->delete($id);
  119. if ($res) return $this->success('已删除');
  120. return $this->error('删除失败');
  121. }
  122. public function export()
  123. {
  124. if (!$this->service) {
  125. throw new AdminException('接口不存在');
  126. }
  127. $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
  128. $export_type = sys_config('export_type', 1);
  129. return $this->success($this->service->export($where, $export_type));
  130. }
  131. }