composer 安装 easywechat

官方文档:https://www.easywechat.com/

1.在app\Http\Kernel.php 路由中间件$routeMiddleware 里面添加中间件

   'wechat.oauth' => \Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate::class,

2.路由中使用中间件

   Route::any('register', 'IndexController@register')->name('m.register')->middleware('wechat.oauth');

3.在中间件VerifyCsrfToken 排除csrf验证路由

protected $except = [    
            'wechat_notify',
            'ali_notify',
            'wechat',
            'wechat/*',
            'payments/*',
    ];

4.获取用户信息 session('wechat.oauth_user.default')

    public function register(Request $request){    
              $current_name = '注册';
              $menu = 'member';
              if($request->isMethod('post'))
              {
                   $openid = trim($request->get('openid'));
                   $name = trim($request->get('name'));
                   $email = trim($request->get('email'));
                   $password = trim($request->get('password'));
                   $password2 = trim($request->get('repassword'));
                   $phone = trim($request->get('phone'));
                   $code = trim($request->get('code'));
                   $company_name = trim($request->get('company_name'));
                   $identity = 'buyer,seller';
                   if($email){
                        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                             return Y::error('邮箱验证失败!',["code"=>"ERROR_FORMAT"]);
                        }
                        //判断邮箱是否已经注册
                        if(Member::where('email','=',$email)->first()){
                             return Y::error('邮箱已被注册!' ,["code"=>"EMAIL_ERROR"]);
                        }
                   }
                   if($password !=$password2){
                        return Y::error('两次密码不一致!',["code"=>"ERROR_PASSWORD"]);
                   }
                   //判断手机号码是否注册
                   if($phone){
                        if(Member::where('phone','=',$phone)->first()){
                             return Y::error('手机号码已被注册!' ,["code"=>"PHONE_ERROR"]);
                        }
                   }
                   //判断手机验证码是否正确
                   $phoneCode = SmsCode::where(['phone' => $phone])->where(['code' => $code])->where(['type' => 'register'])->where(['status' => 1])->where('created_at', '>', date("Y-m-d H:i:s", strtotime("-10 minute")))->first();
                   if (!$phoneCode) {
                        return Y::error('手机短信验证码不正确!', ["code" => "VERIFY_ERROR"]);
                   }
                   //关联用户和公司,先判断公司是否存在,允许一家公司多个账号存在
                   $company = Company::where("name_cn", "=", $company_name)->first();
                   if(!$company){
                        $companyArr = array(
                             'name_cn'=> $company_name,
                             'from'=>'m'
                        );
                        $company = Company::create($companyArr);
                        $company_id = $company->id;
                   }else{
                        $company_id = $company->id;
                   }
                   $user = array(
                        'identity'=>$identity,
                        'username'=>Common::createUserName(),
                        'name'=>$name,
                        'email'=>$email,
                        'phone'=>$phone,
                        'password'=>Hash::make($password),
                        'company_name'=>$company_name,
                        'company_id'=>$company_id,
                        'from'=>'m'
                   );
                   if ($member = Member::create($user)){
                        $this->guard()->login($member);
                        //更改短信验证码状态
                        $phoneCode->status = 0;
                        $phoneCode->save();
                        //绑定微信公众号openid
                        if($openid){
                             MemberConnect::where('openid', '=', $openid)->update(
                                  array(
                                       'member_id'=>$member->id
                                  )
                             );
                        }
                   }
                   return Y::success('注册成功!' ,["code"=>"SUCCESS"]);
              }
              else
              {
                   $openid = '';
                   try{
                        //拿到授权用户资料
                        $wechat = session('wechat.oauth_user.default');
                        $wechat_info = $wechat->original;
                        $openid = isset($wechat_info['openid'])?$wechat_info['openid'] : '';
                        //没有connect 先新建
                        $memberConnect = MemberConnect::where('openid', '=', $openid)->first();
                        if(!$memberConnect && $openid !='') {
                             Log::info('original', $wechat_info);
                             $user_info = array(
                                  'openid'=>$wechat_info['openid'],
                                  'nickname'=>$wechat_info['nickname'],
                                  'headimgurl'=>$wechat_info['headimgurl'],
                                  'sex'=>$wechat_info['sex'],
                                  'country'=>$wechat_info['country'],
                                  'province'=>$wechat_info['province'],
                                  'city'=>$wechat_info['city'],
                             );
                             Log::info('user_info', $user_info);
                             //没保存先保存memberConnect
                             Common::createMemberConnect($user_info, 3);
                        }
                   }catch (Exception $e){
                        Log::info('reg_exception', [$e]);
                   }
                   return view('new_m.index.register', compact('current_name', 'menu', 'openid'));
              }
         }