Login.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\shopapi\http\middleware;
  20. use app\common\model\shop\ShopAdmin;
  21. use app\shopapi\validate\TokenValidate;
  22. use think\exception\ValidateException;
  23. class Login
  24. {
  25. /**
  26. * 登录验证
  27. * @param $request
  28. * @param \Closure $next
  29. * @return mixed|\think\response\Redirect
  30. */
  31. public function handle($request, \Closure $next)
  32. {
  33. //允许跨域调用
  34. header('Access-Control-Allow-Origin: *');
  35. 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");
  36. header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
  37. header('Access-Control-Max-Age: 1728000');
  38. header('Access-Control-Allow-Credentials:true');
  39. if (strtoupper($request->method()) == "OPTIONS") {
  40. return response();
  41. }
  42. // 过滤前后空格
  43. $request->filter(['trim']);
  44. $token = $request->header('token');
  45. // 无需登录
  46. if (empty($token) && $this->isNotNeedLogin($request)) {
  47. return $next($request);
  48. }
  49. //token验证,并生成缓存
  50. $validateError = '';
  51. try {
  52. validate(TokenValidate::class)->check(['token' => $token]);
  53. $adminInfo = (new ShopAdmin())->alias('a')
  54. ->join('shop_session ss', 'a.id=ss.admin_id')
  55. ->join('shop s', 's.id = a.shop_id')
  56. ->where(['ss.token' => $token])
  57. ->field('a.*,ss.token,ss.client,s.name as shop_name')
  58. ->hidden(['password'])
  59. ->findOrEmpty();
  60. $adminInfo = $adminInfo ? $adminInfo->toArray() : [];
  61. // 设置缓存
  62. cache($token, $adminInfo);
  63. // 设置用户信息
  64. $request->admin_info = $adminInfo;
  65. return $next($request);
  66. } catch (ValidateException $e) {
  67. $validateError = $e->getError();
  68. }
  69. //无需要登录,带token的情况
  70. if ($this->isNotNeedLogin($request) && $token) {
  71. return $next($request);
  72. }
  73. //登录失败
  74. $result = array(
  75. 'code' => -1,
  76. 'show' => 1,
  77. 'msg' => $validateError,
  78. 'data' => []
  79. );
  80. return json($result);
  81. }
  82. /**
  83. * @notes 是否需要登录
  84. * @param $request
  85. * @return bool // false-需要; true-不需要
  86. * @author 段誉
  87. * @date 2021/11/10 11:10
  88. */
  89. private function isNotNeedLogin($request)
  90. {
  91. $controllerObj = invoke('\\app\\shopapi\\controller\\' . $request->controller());
  92. $data = $controllerObj->like_not_need_login;
  93. if (empty($data)) {
  94. return false;
  95. }
  96. return in_array($request->action(), $data);
  97. }
  98. }