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


    //1.新建立2个路由.
    Route::get('/buy','indexIndexController@buy')->name('buy'); //要访问的
    Route::get('/profit','indexIndexController@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 = EasyWeChatFactory::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方法下的模板
    <script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" charset="utf-8">
        wx.config(<?php echo $skd; ?>);// 不用ECHO就会报APPID不合法
    
        wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
    
            wx.updateAppMessageShareData({
                title: "{{$user['name']}}"+'正在分享', // 分享标题
                desc: '这是分享的描述', // 分享描述
                link: 'http://box.3wh.com/buy', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: "{{$user['avatar']}}", // 分享图标
                success: function (res) {
                }
            })
        });
    </script>
    

      

  • 相关阅读:
    CF1295D Same GCDs
    欧拉函数
    CF1509C The Sports Festival
    莫队学习笔记
    CF271D Good Substrings
    字符串
    P2704 [NOI2001] 炮兵阵地
    【洛谷 3399】丝绸之路
    【洛谷 3379】最近公共祖先
    【洛谷 2939】Revamping Trails G
  • 原文地址:https://www.cnblogs.com/bing2017/p/12371088.html
Copyright © 2020-2023  润新知