Login.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\api\http\middleware;
  20. use app\api\cache\TokenCache;
  21. use think\exception\ValidateException;
  22. use app\api\validate\TokenValidate;
  23. use app\common\model\user\User;
  24. use app\common\server\JsonServer;
  25. class Login
  26. {
  27. /**
  28. * 登录验证
  29. * @param $request
  30. * @param \Closure $next
  31. * @return mixed|\think\response\Redirect
  32. */
  33. public function handle($request, \Closure $next)
  34. {
  35. //允许跨域调用
  36. header('Access-Control-Allow-Origin: *');
  37. header("Access-Control-Allow-Headers: Authorization, Sec-Fetch-Mode, DNT, X-Mx-ReqToken, Keep-Alive, User-Agent, If-Match, If-None-Match, If-Unmodified-Since, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Accept-Language, Origin, Accept-Encoding,Access-Token,token");
  38. header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
  39. header('Access-Control-Max-Age: 1728000');
  40. header('Access-Control-Allow-Credentials:true');
  41. if (strtoupper($request->method()) == "OPTIONS") {
  42. return response();
  43. }
  44. $token = $request->header('token');
  45. // 免登录
  46. if ($this->isNotNeedLogin($request) && empty($token)) {
  47. return $next($request);
  48. }
  49. // 根据token读取缓存
  50. $token_cache = new TokenCache($token, ['token' => $token]);
  51. if (!$token_cache->isEmpty()) {
  52. $request->user_info = $token_cache->get();
  53. return $next($request);
  54. }
  55. //token验证,并生成缓存
  56. $validateError = '';
  57. try{
  58. validate(TokenValidate::class)->check(['token' => $token]);
  59. $userInfo = User::alias('u')
  60. ->join('session s', 'u.id=s.user_id')
  61. ->where(['s.token' => $token])
  62. ->field('u.*,s.token,s.client')
  63. ->find();
  64. $userInfo = $userInfo ? $userInfo->toArray() : [];
  65. // 设置缓存
  66. $token_cache->set(600);
  67. // 设置用户信息
  68. $request->user_info = $userInfo;
  69. return $next($request);
  70. }catch(ValidateException $e) {
  71. $validateError = $e->getError();
  72. }
  73. //无需要登录,带token的情况
  74. if ($this->isNotNeedLogin($request) && $token) {
  75. return $next($request);
  76. }
  77. //登录失败
  78. $result = array(
  79. 'code' => -1,
  80. 'show' => 1,
  81. 'msg' => $validateError,
  82. 'data' => []
  83. );
  84. return json($result);
  85. }
  86. /**
  87. * 是否免登录验证
  88. * @param $request
  89. * @return bool
  90. */
  91. private function isNotNeedLogin($request)
  92. {
  93. // 提取当前请求控制器名称
  94. $baseUrl = $request->baseUrl(); // /api/goods/test
  95. $apperTwo = strpos($baseUrl, '/', 1);
  96. $apperThird = strpos($baseUrl, '/', $apperTwo + 1);
  97. $len = $apperThird - $apperTwo - 1;
  98. $controllerName = substr($baseUrl, $apperTwo + 1, $len);
  99. // 控制名称处理(兼容下划线的情况 例:goods_columns 处理成 GoodsColumns)
  100. $controllerNameArr = explode('_', $controllerName);
  101. $controllerNameArr = array_map('ucfirst', $controllerNameArr);
  102. $controllerName = implode($controllerNameArr);
  103. // 实例化当前请求的控制器
  104. $controllerObj = invoke('\\app\\api\\controller\\' . $controllerName);
  105. $data = $controllerObj->like_not_need_login;
  106. if (empty($data)) {
  107. return false;
  108. }
  109. // 提取操作名称
  110. $action = strtolower(substr($baseUrl, $apperThird + 1));
  111. $data = array_map('strtolower', $data);
  112. if (!in_array($action, $data)) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. }