1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Http\Middleware;
- use App\Exceptions\MemberAuthException;
- use App\Models\Member;
- use App\Servers\RedisDataServer;
- use Closure;
- class MemberAuthMiddleware
- {
- private $noAuth=[
- 'member.register',
- 'member.index',
- 'member.login',
- 'common.location',
- 'common.file',
- 'common.js',
- 'common.index',
- 'common.send',
- 'news.banner',
- 'news.notices',
- 'news.notice',
- 'news.about_us',
- 'common.start',
- 'common.app_version',
- 'common.privacy_info',//获取隐私协议
- ];
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
- public function handle($request, Closure $next)
- {
- //设置系统语言包
- app()->setLocale(request()->input('lang', 'zh-Hans'));
- //验证会员授权信息
- $clientRoute = request()->route()->getName();
- $token=request()->header('Authorization');
- if(empty($token)){
- $token = request()->input('token','');
- }
- if (!in_array($clientRoute, $this->noAuth)) {
- if ( empty($token) ) {
- throw new MemberAuthException('auth.no_key', 401);
- }
- $member = RedisDataServer::creatServer()->getData( 'login_' . $token, 'json');
- if (empty($member)) {
- $member = Member::where('token_str', $token)->select(['id', 'address', 'status', 'token_str', 'level_id'])->first();
- if(empty($member)){
- throw new MemberAuthException('auth.login_error', 401);
- }
- }
- //用户信息缓存30秒
- RedisDataServer::creatServer()->setData($token . '_' . $token, $member, 'json', 300);
- $request->member = $member;
- }else{
- if( $token){
- $member = RedisDataServer::creatServer()->getData( 'login_' . $token, 'json');
- if (empty($member)) {
- $member = Member::where('token_str', $token)->where('status',1)->select(['id', 'address', 'status', 'token_str', 'level_id'])->first();
- }
- //用户信息缓存30秒
- if($member){
- RedisDataServer::creatServer()->setData('login_' . $token, $member, 'json', 300);
- $request->member = $member;
- }else{
- $request->member = ['id'=>0];
- }
- }else{
- $request->member = ['id'=>0];
- }
- }
- return $next($request);
- }
- }
|