CheckWeinxin.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Model\WxEmployeeOfficial;
  4. use App\Service\EmployeeService;
  5. use Closure;
  6. use Illuminate\Http\Request;
  7. class CheckWeinxin
  8. {
  9. /**
  10. * Handle an incoming request.
  11. * 微信公众号中间件
  12. * @param Request $request
  13. * @param Closure $next
  14. * @return mixed
  15. */
  16. public function handle($request, Closure $next)
  17. {
  18. $token = $request->header('ciphertext');
  19. if (empty($token)) return response()->json(['code'=>401,'msg'=>'缺少openid','data'=>null]);
  20. $employee_id = $request->header('employee');
  21. if (empty($employee_id)) return response()->json(['code'=>401,'msg'=>'缺少人员id','data'=>null]);
  22. //校验openid是否绑定
  23. $bool = WxEmployeeOfficial::where('openid',$token)->where('employee_id',$employee_id)->exists();
  24. if (! $bool) return response()->json(['code'=>401,'msg'=>'用户信息错误!','data'=>null]);
  25. //校验用户
  26. $checkResult = EmployeeService::checkWxUser($employee_id);
  27. list($state, $data) = $checkResult;
  28. if(! $state) return response()->json(['code'=>401,'msg'=>$data,'data'=>null]);
  29. $request->userData = $data;
  30. return $next($request);
  31. }
  32. }