AlipayAuth.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Cookie;
  5. class AlipayAuth
  6. {
  7. /**
  8. * Handle an incoming request.
  9. *
  10. * @param \Illuminate\Http\Request $request
  11. * @param \Closure $next
  12. * @return mixed
  13. */
  14. public function handle($request, Closure $next)
  15. {
  16. $test = 0;
  17. if ($request->has('test') && $request->input('test') == 'bonan') {
  18. $test = 1;
  19. }
  20. $uid_cookie = Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  21. $h5_scheme = env('H5_SCHEME', 'https');
  22. $params = $request->except('_url');
  23. $url = str_replace('http://', $h5_scheme . '://', url()->current() . '?' . http_build_query($params));
  24. $send_order_id = $request->input('send_order_id');
  25. if($send_order_id){
  26. Cookie::queue('send_order_id',$send_order_id, env('U_COOKIE_EXPIRE'), null, null, false, false);
  27. }
  28. $distribution_channel_id = str_replace('site', '', explode('.', _domain())[0]);
  29. if(!is_numeric($distribution_channel_id)){
  30. $distribution_channel_id = decodeDistributionChannelId($distribution_channel_id);
  31. }
  32. if(!$distribution_channel_id){
  33. return response()->error('WAP_SYS_ERROR');
  34. }
  35. if (!$uid_cookie && $test == 0) {
  36. $uri = $request->input('_url');
  37. if(strpos($uri,'/yun/') !== false){
  38. $uri_send_order_id = (int)str_ireplace('/yun/','',$uri);
  39. Cookie::queue('send_order_id', $uri_send_order_id, env('U_COOKIE_EXPIRE'), null, null, false, false);
  40. }
  41. Cookie::queue('auth_redirect', urlencode($url), env('U_COOKIE_EXPIRE'));
  42. $redirect = $this->getAuthUrl();
  43. return redirect()->to($redirect);
  44. }
  45. $response = $next($request);
  46. if ($request->has('fromtype') && $request->input('fromtype')) {
  47. try {
  48. $from = $request->input('fromtype', 'main');
  49. Cookie::queue('from', $from);
  50. Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie);
  51. Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1);
  52. Redis::sadd('wap_from_stats'.date('Y-m-d'),$distribution_channel_id.'-'.$from);
  53. } catch (\Exception $e) {
  54. }
  55. }
  56. return $response;
  57. }
  58. /**
  59. * @return string
  60. */
  61. public function getAuthUrl():string{
  62. $url = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?';
  63. $data = [
  64. 'app_id'=>'2018081461071404',
  65. 'scope'=>'auth_base',
  66. 'redirect_uri'=>env('ALI_AUTH_URL')
  67. ];
  68. $url = $url.http_build_query($data);
  69. return $url;
  70. }
  71. }