123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace app\api\controller;
- use think\exception\HttpResponseException;
- use think\Request;
- use think\Response;
- use think\Db;
- use app\admin\model\Admin;
- use think\Config;
- use think\Validate;
- class Adminapi
- {
-
- protected $request;
-
- protected $responseType = 'json';
- public $page = 1;
- public $listrow = 10;
- protected $_error = '';
- protected $admin_uid = false;
-
- public function __construct(Request $request = null)
- {
- $this->request = is_null($request) ? Request::instance() : $request;
- $this->page = input('page',1);
- $this->listrow= input('listrow',10);
-
- $this->_initialize();
-
- $this->admin_login();
- }
-
- protected function _initialize()
- {
-
- $this->request->filter('trim,strip_tags,htmlspecialchars');
- }
-
- public function auth_login($username, $password, $keeptime = 0)
- {
- $admin = Admin::get(['username' => $username]);
- if (!$admin) {
- $this->setError('Username is incorrect');
- return false;
- }
- if ($admin['status'] == 'hidden') {
- $this->setError('Admin is forbidden');
- return false;
- }
- if (Config::get('fastadmin.login_failure_retry') && $admin->loginfailure >= 10 && time() - $admin->updatetime < 86400) {
- $this->setError('Please try again after 1 day');
- return false;
- }
- if ($admin->password != md5(md5($password) . $admin->salt)) {
- $admin->loginfailure++;
- $admin->save();
- $this->setError('Password is incorrect');
- return false;
- }
- $admin->loginfailure = 0;
- $admin->logintime = time();
- $admin->loginip = request()->ip();
- $admin->save();
-
- $this->admin_uid = $admin->id;
- return true;
- }
-
- public function setError($error)
- {
- $this->_error = $error;
- return $this;
- }
-
- public function getError()
- {
- return $this->_error ? __($this->_error) : '';
- }
- public function admin_login()
- {
- if ($this->request->isPost()) {
- $username = $this->request->post('username');
- $password = $this->request->post('password');
- $rule = [
- 'username' => 'require|length:3,30',
- 'password' => 'require|length:3,30',
- ];
- $data = [
- 'username' => $username,
- 'password' => $password,
- ];
- $validate = new Validate($rule, [], ['username' => '用户名', 'password' => '密码']);
- $result = $validate->check($data);
- if (!$result) {
- $this->error($validate->getError());
- }
- $result = $this->auth_login($username, $password, 0);
- if ($result === true) {
- return $this->admin_uid;
- } else {
- $msg = $this->getError();
- $msg = $msg ? $msg : __('Username or password is incorrect');
- $this->error($msg);
- }
- }else{
- $this->error('no post');
- }
- }
-
- public function index(){
- $admin_uid = $this->admin_uid;
- $this->success('success',$admin_uid);
- }
-
- protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
- {
- if(empty($msg)){
- $msg = '操作成功';
- }
- $this->result($msg, $data, $code, $type, $header);
- }
-
- protected function success_find($msg = '', $data = null, $code = 1, $type = null, array $header = [])
- {
- if(empty($msg)){
- $msg = '操作成功';
- }
- if(is_null($data) || $data === []){
- $data = (object)[];
- }
- $this->result($msg, $data, $code, $type, $header);
- }
-
- protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
- {
- if(empty($msg)){
- $msg = __('Invalid parameters');
- }
- $this->result($msg, $data, $code, $type, $header);
- }
-
- protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => Request::instance()->server('REQUEST_TIME'),
- 'data' => $data,
- ];
-
- $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
- if (isset($header['statuscode'])) {
- $code = $header['statuscode'];
- unset($header['statuscode']);
- } else {
-
- $code = $code >= 1000 || $code < 200 ? 200 : $code;
- }
- $response = Response::create($result, $type, $code)->header($header);
- throw new HttpResponseException($response);
- }
- }
|