ApiCommon.php 940 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Titan 名字就是密码
  5. * Date: 2019/6/4
  6. * Time: 17:02
  7. */
  8. namespace app\index\controller;
  9. use think\Controller;
  10. use think\Request;
  11. class ApiCommon extends Controller
  12. {
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. /*获取头部信息*/
  17. $header = Request::instance()->header();
  18. if(!isset($header['authkey'])){
  19. header('Content-Type:application/json; charset=utf-8');
  20. exit(json_encode(['code'=>103, 'error'=>'非法操作'],JSON_UNESCAPED_UNICODE));
  21. }
  22. $authKey = $header['authkey'];
  23. $cache = cache('Auth_'.$authKey);
  24. if(empty($authKey) || empty($cache)){
  25. header('Content-Type:application/json; charset=utf-8');
  26. exit(json_encode(['code'=>103, 'error'=>'登录已失效'],JSON_UNESCAPED_UNICODE));
  27. }
  28. cache('Auth_'.$authKey, $cache, 7200);
  29. }
  30. }