BaseController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic;
  8. use support\Request;
  9. use support\Response;
  10. use plugin\saiadmin\app\cache\UserInfoCache;
  11. use plugin\saiadmin\exception\ApiException;
  12. /**
  13. * 基类 控制器继承此类
  14. */
  15. class BaseController extends OpenController
  16. {
  17. /**
  18. * 当前登陆管理员信息
  19. */
  20. protected $adminInfo;
  21. /**
  22. * 当前登陆管理员ID
  23. */
  24. protected $adminId;
  25. /**
  26. * 当前登陆管理员账号
  27. */
  28. protected $adminName;
  29. /**
  30. * 逻辑层注入
  31. */
  32. protected $logic;
  33. /**
  34. * 验证器注入
  35. */
  36. protected $validate;
  37. /**
  38. * 初始化
  39. */
  40. protected function init(): void
  41. {
  42. // 检查默认请求类型
  43. $this->checkDefaultMethod();
  44. // 登录模式赋值
  45. $isLogin = request()->header('check_login', false);
  46. if ($isLogin) {
  47. $result = request()->header('check_admin');
  48. $userInfoCache = new UserInfoCache($result['id']);
  49. $this->adminId = $result['id'];
  50. $this->adminName = $result['username'];
  51. $this->adminInfo = $userInfoCache->getUserInfo();
  52. // 用户数据传递给逻辑层
  53. $this->logic && $this->logic->init($this->adminInfo);
  54. }
  55. }
  56. /**
  57. * 检查默认方法
  58. * @return void
  59. */
  60. protected function checkDefaultMethod()
  61. {
  62. $functions = [
  63. 'index' => 'get',
  64. 'save' => 'post',
  65. 'update' => 'put',
  66. 'read' => 'get',
  67. 'changestatus' => 'post',
  68. 'destroy' => 'delete',
  69. 'import' => 'post',
  70. 'export' => 'post',
  71. ];
  72. $action = strtolower(request()->action);
  73. if (array_key_exists($action, $functions)) {
  74. $this->checkMethod($functions[$action]);
  75. }
  76. }
  77. /**
  78. * 验证请求方式
  79. * @param string $method
  80. * @return void
  81. */
  82. protected function checkMethod(string $method)
  83. {
  84. $m = strtolower(request()->method());
  85. if ($m !== strtolower($method)) {
  86. throw new ApiException('Not Found!', 404);
  87. }
  88. }
  89. /**
  90. * 添加数据
  91. * @param Request $request
  92. * @return Response
  93. */
  94. public function save(Request $request) : Response
  95. {
  96. $data = $request->post();
  97. if ($this->validate) {
  98. if (!$this->validate->scene('save')->check($data)) {
  99. return $this->fail($this->validate->getError());
  100. }
  101. }
  102. $key = $this->logic->add($data);
  103. if ($key > 0) {
  104. $this->afterChange('save', $key);
  105. return $this->success('操作成功');
  106. } else {
  107. return $this->fail('操作失败');
  108. }
  109. }
  110. /**
  111. * 修改数据
  112. * @param $id
  113. * @param Request $request
  114. * @return Response
  115. */
  116. public function update(Request $request, $id) : Response
  117. {
  118. $id = $request->input('id', $id);
  119. if (empty($id)) {
  120. return $this->fail('参数错误,请检查');
  121. }
  122. $data = $request->post();
  123. if ($this->validate) {
  124. if (!$this->validate->scene('update')->check($data)) {
  125. return $this->fail($this->validate->getError());
  126. }
  127. }
  128. $result = $this->logic->edit($id, $data);
  129. if ($result) {
  130. $this->afterChange('update', $result);
  131. return $this->success('操作成功');
  132. } else {
  133. return $this->fail('操作失败');
  134. }
  135. }
  136. /**
  137. * 删除数据
  138. * @param Request $request
  139. * @return Response
  140. */
  141. public function destroy(Request $request) : Response
  142. {
  143. $ids = $request->input('ids', '');
  144. if (!empty($ids)) {
  145. $this->logic->destroy($ids);
  146. $this->afterChange('destroy', $ids);
  147. return $this->success('操作成功');
  148. } else {
  149. return $this->fail('参数错误,请检查');
  150. }
  151. }
  152. /**
  153. * 读取数据
  154. * @param Request $request
  155. * @param $id
  156. * @return Response
  157. */
  158. public function read(Request $request, $id) : Response
  159. {
  160. $id = $request->input('id', $id);
  161. $model = $this->logic->read($id);
  162. if ($model) {
  163. $data = is_array($model) ? $model : $model->toArray();
  164. return $this->success($data);
  165. } else {
  166. return $this->fail('未查找到信息');
  167. }
  168. }
  169. /**
  170. * 数据改变后执行
  171. * @param string $type 类型
  172. * @param $args
  173. */
  174. protected function afterChange(string $type, $args): void
  175. {
  176. // todo
  177. }
  178. }