Laravel6.0+easywechat4.1获取用户授权例子与分享示例

//1.新建立2个路由.
Route::get('/buy','index\IndexController@buy')->name('buy'); //要访问的
Route::get('/profit','index\IndexController@profit')->name('profit'); //要跳转的

//2.建立控制器:
public function __construct()
    {
        $config = [
        'app_id' => '11111',
        'secret' => 'a93ba7629',
        'token' => 'psrlJRr3eZrg7ser1GcNTZ11rFSIC1TP',
        'response_type' => 'array',
        'oauth' => [
            'scopes'   => ['snsapi_userinfo'],
            'callback' => '/profit', //这个就是告诉授权要跳转到这个页面
        ],
    ];

        $this->app = \EasyWeChat\Factory::officialAccount($config);

    }

    public function buy(Request $request){


        if(empty(session('wechat_user'))){
            $oauth = $this->app->oauth;
            session(['target_url'=>'/buy']);
            return $oauth->redirect();
        }
        $user = session('wechat_user');
        //dd($user);
        $skd = $this->app->jssdk->buildConfig(['updateAppMessageShareData', 'updateTimelineShareData'],$debug = false, $beta = false, $json = true);
        $url = "box.7wh.com/buy";
        $this->app->jssdk->setUrl($url);

        return view('share',compact('skd','user'));
    }


public function  profit(){
        $oauth = $this->app->oauth;
        $user = $oauth->user();
        session(['wechat_user'=>$user->toArray()]);
        $target_url = empty(session('target_url'))?'/':session('target_url');
        header('Location:'.$target_url);
    }


//以下为分享示例share.blade.php内容,对应buy方法下的模板


  

你可能感兴趣的:(Laravel6.0+easywechat4.1获取用户授权例子与分享示例)