BaseController.php 5.1 KB

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