1. 程式人生 > >微信自帶分享功能實現

微信自帶分享功能實現

最近做的微信公眾號開發,有個頁面是需要分享推廣,微信自帶的也有轉發可是轉發出去的效果是網址,這個效果顯然不是我們需要的

然後建立方法呼叫jssdk類

function jssdk($info){include_once("Wechat/JSSDK.class.php");
    $jssdk = new JSSDK ($info ['appid'], $info ['appsecret']);
    $jsapiParams = $jssdk->GetSignPackage();
    return $jsapiParams;
}

此處需要注意的是傳進來的是個陣列,此陣列是微信的配置檔案微信的appid和呼叫高階功能的金鑰

最後直接在控制器中呼叫此方法

public function getStore(Request $request)
{
    $info = WemallWxconfig::where(array('id' => 1))->first();//讀取資料庫微信配置內容
    $jsapiParams=jssdk($info);
    $username=$request->session()->get('username');
    $shop = DB::table('shop')->where('username',$username)->first
(); $data = std_class_object_to_array($shop); $img = explode('||',$data['image']); unset($img[count($img) - 1]); $data['image'] = $img ; return view('Index/dpxq')->with('data',$data)->with('jsapiParams',$jsapiParams); }

最後輸出在頁面上

<script src="{{ asset('Sys/js/jquery.js') }}"
></script> <script src="{{ asset('Sys/js/weixin.js') }}"></script> <script> wx.config({ debug: false, appId: "{{$jsapiParams['appId']}}", timestamp: "{{$jsapiParams['timestamp']}}", nonceStr: "{{$jsapiParams['nonceStr']}}", signature: "{{$jsapiParams['signature']}}", jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] }); wx.ready(function() { wx.onMenuShareTimeline({ title: '{{ $data["title"] }}', link: 'http://www.qiaowei360.com/huogou/public/store/{{ $data["id"] }}', imgUrl: "{{ asset('') }}/{{ $data['imgurl'] }}thumb/{{ $data['img'] }}", success: function() { alert('分享成功'); }, cancel: function() { alert('分享失敗'); } }); wx.onMenuShareAppMessage({ title: '{{ $data["title"] }}', desc: '主營:{{ $data["shopinfo"] }}', link: 'http://www.qiaowei360.com/huogou/public/store/{{ $data["id"] }}', imgUrl: "{{ asset('') }}/{{ $data['imgurl'] }}thumb/{{ $data['img'] }}", type: '', dataUrl: '', success: function() { alert('分享成功'); }, cancel: function() { alert('分享失敗'); } }); }); </script>
然後該頁面分享出去的效果是