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