Skip to content

Instantly share code, notes, and snippets.

@Hanson
Last active January 25, 2018 02:34
Show Gist options
  • Save Hanson/e816dff4f22c584db4fc66eaf389edc9 to your computer and use it in GitHub Desktop.
Save Hanson/e816dff4f22c584db4fc66eaf389edc9 to your computer and use it in GitHub Desktop.
laravel easyWeChat 微信公众号是否关注中间件
<?php
namespace App\Http\Middleware;
use App\Jobs\SaveWechatUser;
use Closure;
use WechatService;
class WechatSubscribe
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(session('wechatUser')){
//静默授权的用户信息
$user = session('wechatUser');
$request->openId = $user['id'];
if(!session('userInfo')){
//easyWechat Application实例
$app = WechatService::getApp();
$info = $app->user->get($request->openId)->toArray();
session(['userInfo' => $info]);
//自定义事件
dispatch(new SaveWechatUser($info));
}
if(session('userInfo')['subscribe'] == 1){
return $next($request);
}
}
return redirect('error/subscribe');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment