Login.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\kefuapi\http\middleware;
  20. use app\common\model\kefu\Kefu;
  21. use think\exception\ValidateException;
  22. use app\kefuapi\validate\TokenValidate;
  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. $token = $request->header('token');
  43. // 无需登录
  44. if (empty($token) && $this->isNotNeedLogin($request)) {
  45. return $next($request);
  46. }
  47. $validateError = '';
  48. try {
  49. validate(TokenValidate::class)->check(['token' => $token]);
  50. $kefu_info = (new Kefu())->alias('k')
  51. ->join('kefu_session ks', 'k.id = ks.kefu_id')
  52. ->where(['ks.token' => $token])
  53. ->field('k.*,ks.token,ks.client')
  54. ->hidden(['password'])
  55. ->findOrEmpty();
  56. $kefu_info = $kefu_info ? $kefu_info->toArray() : [];
  57. // 设置用户信息
  58. $request->kefu_info = $kefu_info;
  59. return $next($request);
  60. } catch (ValidateException $e) {
  61. $validateError = $e->getError();
  62. }
  63. //无需要登录,带token的情况
  64. if ($this->isNotNeedLogin($request) && $token) {
  65. return $next($request);
  66. }
  67. //登录失败
  68. $result = array(
  69. 'code' => -1,
  70. 'show' => 1,
  71. 'msg' => $validateError,
  72. 'data' => []
  73. );
  74. return json($result);
  75. }
  76. /**
  77. * @notes 是否需要登录
  78. * @param $request
  79. * @return bool // false-需要; true-不需要
  80. * @author 段誉
  81. * @date 2021/11/10 11:10
  82. */
  83. private function isNotNeedLogin($request)
  84. {
  85. $controllerObj = invoke('\\app\\kefuapi\\controller\\' . $request->controller());
  86. $data = $controllerObj->like_not_need_login;
  87. if (empty($data)) {
  88. return false;
  89. }
  90. return in_array($request->action(), $data);
  91. }
  92. }