Adminapi.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\api\controller;
  3. use think\exception\HttpResponseException;
  4. use think\Request;
  5. use think\Response;
  6. use think\Db;
  7. use app\admin\model\Admin;
  8. use think\Config;
  9. use think\Validate;
  10. /**
  11. * API控制器基类
  12. */
  13. class Adminapi
  14. {
  15. /**
  16. * @var Request Request 实例
  17. */
  18. protected $request;
  19. /**
  20. * 默认响应输出类型,支持json/xml
  21. * @var string
  22. */
  23. protected $responseType = 'json';
  24. public $page = 1;
  25. public $listrow = 10;
  26. protected $_error = '';
  27. protected $admin_uid = false;
  28. /**
  29. * 构造方法
  30. * @access public
  31. * @param Request $request Request 对象
  32. */
  33. public function __construct(Request $request = null)
  34. {
  35. $this->request = is_null($request) ? Request::instance() : $request;
  36. $this->page = input('page',1);
  37. $this->listrow= input('listrow',10);
  38. // 控制器初始化
  39. $this->_initialize();
  40. //验证后台用户和密码
  41. $this->admin_login();
  42. }
  43. /**
  44. * 初始化操作
  45. * @access protected
  46. */
  47. protected function _initialize()
  48. {
  49. //移除HTML标签
  50. $this->request->filter('trim,strip_tags,htmlspecialchars');
  51. }
  52. /**
  53. * 管理员登录
  54. *
  55. * @param string $username 用户名
  56. * @param string $password 密码
  57. * @param int $keeptime 有效时长
  58. * @return boolean
  59. */
  60. public function auth_login($username, $password, $keeptime = 0)
  61. {
  62. $admin = Admin::get(['username' => $username]);
  63. if (!$admin) {
  64. $this->setError('Username is incorrect');
  65. return false;
  66. }
  67. if ($admin['status'] == 'hidden') {
  68. $this->setError('Admin is forbidden');
  69. return false;
  70. }
  71. if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) {
  72. $this->setError('Please try again after 1 day');
  73. return false;
  74. }
  75. if ($admin->password != md5(md5($password) . $admin->salt)) {
  76. $admin->loginfailure++;
  77. $admin->save();
  78. $this->setError('Password is incorrect');
  79. return false;
  80. }
  81. $admin->loginfailure = 0;
  82. $admin->logintime = time();
  83. $admin->loginip = request()->ip();
  84. $admin->save();
  85. //登录关键
  86. $this->admin_uid = $admin->id;
  87. return true;
  88. }
  89. /**
  90. * 设置错误信息
  91. *
  92. * @param string $error 错误信息
  93. * @return Auth
  94. */
  95. public function setError($error)
  96. {
  97. $this->_error = $error;
  98. return $this;
  99. }
  100. /**
  101. * 获取错误信息
  102. * @return string
  103. */
  104. public function getError()
  105. {
  106. return $this->_error ? __($this->_error) : '';
  107. }
  108. public function admin_login()
  109. {
  110. if ($this->request->isPost()) {
  111. $username = $this->request->post('username');
  112. $password = $this->request->post('password');
  113. $rule = [
  114. 'username' => 'require|length:3,30',
  115. 'password' => 'require|length:3,30',
  116. ];
  117. $data = [
  118. 'username' => $username,
  119. 'password' => $password,
  120. ];
  121. $validate = new Validate($rule, [], ['username' => '用户名', 'password' => '密码']);
  122. $result = $validate->check($data);
  123. if (!$result) {
  124. $this->error($validate->getError());
  125. }
  126. $result = $this->auth_login($username, $password, 0);
  127. if ($result === true) {
  128. return $this->admin_uid;
  129. } else {
  130. $msg = $this->getError();
  131. $msg = $msg ? $msg : __('Username or password is incorrect');
  132. $this->error($msg);
  133. }
  134. }else{
  135. $this->error('no post');
  136. }
  137. }
  138. //接口
  139. public function index(){
  140. $admin_uid = $this->admin_uid;
  141. $this->success('success',$admin_uid);
  142. }
  143. /**
  144. * 操作成功返回的数据
  145. * @param string $msg 提示信息
  146. * @param mixed $data 要返回的数据
  147. * @param int $code 错误码,默认为1
  148. * @param string $type 输出类型
  149. * @param array $header 发送的 Header 信息
  150. */
  151. protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
  152. {
  153. if(empty($msg)){
  154. $msg = '操作成功';
  155. }
  156. $this->result($msg, $data, $code, $type, $header);
  157. }
  158. //find查询出来的结果如果为空数组,强制转换object
  159. protected function success_find($msg = '', $data = null, $code = 1, $type = null, array $header = [])
  160. {
  161. if(empty($msg)){
  162. $msg = '操作成功';
  163. }
  164. if(is_null($data) || $data === []){
  165. $data = (object)[];
  166. }
  167. $this->result($msg, $data, $code, $type, $header);
  168. }
  169. /**
  170. * 操作失败返回的数据
  171. * @param string $msg 提示信息
  172. * @param mixed $data 要返回的数据
  173. * @param int $code 错误码,默认为0
  174. * @param string $type 输出类型
  175. * @param array $header 发送的 Header 信息
  176. */
  177. protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
  178. {
  179. if(empty($msg)){
  180. $msg = __('Invalid parameters');
  181. }
  182. $this->result($msg, $data, $code, $type, $header);
  183. }
  184. /**
  185. * 返回封装后的 API 数据到客户端
  186. * @access protected
  187. * @param mixed $msg 提示信息
  188. * @param mixed $data 要返回的数据
  189. * @param int $code 错误码,默认为0
  190. * @param string $type 输出类型,支持json/xml/jsonp
  191. * @param array $header 发送的 Header 信息
  192. * @return void
  193. * @throws HttpResponseException
  194. */
  195. protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
  196. {
  197. $result = [
  198. 'code' => $code,
  199. 'msg' => $msg,
  200. 'time' => Request::instance()->server('REQUEST_TIME'),
  201. 'data' => $data,
  202. ];
  203. // 如果未设置类型则自动判断
  204. $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
  205. if (isset($header['statuscode'])) {
  206. $code = $header['statuscode'];
  207. unset($header['statuscode']);
  208. } else {
  209. //未设置状态码,根据code值判断
  210. $code = $code >= 1000 || $code < 200 ? 200 : $code;
  211. }
  212. $response = Response::create($result, $type, $code)->header($header);
  213. throw new HttpResponseException($response);
  214. }
  215. }