BaseController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\api;
  15. use think\exception\HttpResponseException;
  16. use think\facade\Request;
  17. use think\facade\Session;
  18. use think\facade\View;
  19. use think\facade\Db;
  20. use think\Response;
  21. /**
  22. * 控制器基础类
  23. */
  24. abstract class BaseController
  25. {
  26. /**
  27. * 是否批量验证
  28. * @var bool
  29. */
  30. protected $batchValidate = false;
  31. /**
  32. * 控制器中间件
  33. * @var array
  34. */
  35. protected $middleware = [];
  36. /**
  37. * 分页数量
  38. * @var string
  39. */
  40. protected $pageSize = 20;
  41. /**
  42. * jwt配置
  43. * @var string
  44. */
  45. protected $jwt_conf = [
  46. 'secrect' => 'gouguoa',
  47. 'iss' => 'www.gougucms.com', //签发者 可选
  48. 'aud' => 'gouguoa', //接收该JWT的一方,可选
  49. 'exptime' => 7200, //过期时间,这里设置2个小时
  50. ];
  51. protected $module;
  52. protected $controller;
  53. protected $action;
  54. protected $uid;
  55. protected $did;
  56. protected $pid;
  57. /**
  58. * 构造方法
  59. * @access public
  60. * @param App $app 应用对象
  61. */
  62. public function __construct()
  63. {
  64. $this->module = strtolower(app('http')->getName());
  65. $this->controller = strtolower(Request::controller());
  66. $this->action = strtolower(Request::action());
  67. $this->uid = 0;
  68. $this->did = 0;
  69. $this->pid = 0;
  70. $this->jwt_conf = get_system_config('token');
  71. // 控制器初始化
  72. $this->initialize();
  73. }
  74. // 初始化
  75. protected function initialize()
  76. {
  77. // 检测权限
  78. $this->checkLogin();
  79. //每页显示数据量
  80. $this->pageSize = Request::param('limit', \think\facade\Config::get('app.page_size'));
  81. }
  82. /**
  83. *验证用户登录
  84. */
  85. protected function checkLogin()
  86. {
  87. $session_admin = get_config('app.session_admin');
  88. if (!Session::has($session_admin)) {
  89. $this->apiError('请先登录');
  90. }
  91. else{
  92. $this->uid = Session::get($session_admin);
  93. $login_admin = get_admin($this->uid);
  94. $this->did = $login_admin['did'];
  95. $this->pid = $login_admin['pid'];
  96. View::assign('login_admin', $login_admin);
  97. }
  98. }
  99. /**
  100. * Api处理成功结果返回方法
  101. * @param $message
  102. * @param null $redirect
  103. * @param null $extra
  104. * @return mixed
  105. * @throws ReturnException
  106. */
  107. protected function apiSuccess($msg = 'success', $data = [])
  108. {
  109. return $this->apiReturn($data, 0, $msg);
  110. }
  111. /**
  112. * Api处理结果失败返回方法
  113. * @param $error_code
  114. * @param $message
  115. * @param null $redirect
  116. * @param null $extra
  117. * @return mixed
  118. * @throws ReturnException
  119. */
  120. protected function apiError($msg = 'fail', $data = [], $code = 1)
  121. {
  122. return $this->apiReturn($data, $code, $msg);
  123. }
  124. /**
  125. * 返回封装后的API数据到客户端
  126. * @param mixed $data 要返回的数据
  127. * @param integer $code 返回的code
  128. * @param mixed $msg 提示信息
  129. * @param string $type 返回数据格式
  130. * @param array $header 发送的Header信息
  131. * @return Response
  132. */
  133. protected function apiReturn($data, int $code = 0, $msg = '', string $type = '', array $header = []): Response
  134. {
  135. $result = [
  136. 'code' => $code,
  137. 'msg' => $msg,
  138. 'time' => time(),
  139. 'data' => $data,
  140. ];
  141. $type = $type ?: 'json';
  142. $response = Response::create($result, $type)->header($header);
  143. throw new HttpResponseException($response);
  144. }
  145. }