ApiCommon.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: Api基础类,验证权限
  4. // +----------------------------------------------------------------------
  5. // | Author:
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use think\Request;
  9. use think\Db;
  10. use app\common\adapter\AuthAdapter;
  11. use app\common\controller\Common;
  12. class ApiCommon extends Common
  13. {
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. /*获取头部信息*/
  18. $header = Request::instance()->header();
  19. $request = Request::instance();
  20. $authKey = $header['authkey'];
  21. $sessionId = $header['sessionid'];
  22. $paramArr = $request->param();
  23. $platform = $paramArr['platform'] ? '_'.$paramArr['platform'] : ''; //请求平台(mobile,ding)
  24. $cache = cache('Auth_'.$authKey.$platform);
  25. // 校验sessionid和authKey
  26. if (empty($sessionId) || empty($authKey) || empty($cache)) {
  27. header('Content-Type:application/json; charset=utf-8');
  28. $dataTime=date('H:i',time());
  29. exit(json_encode(['code' => 302, 'data' => ['extra' => 1, 'extraTime' => $dataTime], 'msg' => '请先登录!']));
  30. }
  31. //登录有效时间
  32. $cacheConfig = config('cache');
  33. $loginExpire = $cacheConfig['expire'] ? : 86400*3;
  34. // 检查账号有效性
  35. $userInfo = $cache['userInfo'];
  36. $map['id'] = $userInfo['id'];
  37. $map['status'] = array('in',['1','2']);
  38. $userData = Db::name('admin_user')->where($map)->find();
  39. if (!$userData) {
  40. header('Content-Type:application/json; charset=utf-8');
  41. exit(json_encode(['code'=>103, 'data' => [], 'msg'=>'账号已被删除或禁用']));
  42. }
  43. session('user_id', $userInfo['id']);
  44. // 更新缓存
  45. cache('Auth_'.$authKey, $cache, $loginExpire);
  46. // $GLOBALS['userInfo'] = $userInfo;
  47. }
  48. }