ThinkPHP5怎么集成JS-SDK實現微信自定義分享功能

jssdk類庫

1、文件名及位置

名字:Jssdk.php
位置:extendutilJssdk.php

2、代碼

<?php namespace util;  class Jssdk {      protected $appid = &#39;xxxx&#39;;     protected $secret = &#39;xxxx&#39;;      /**      * 獲取access_token方法      */     public function getAccessToken(){         //定義文件名稱         $name = &#39;token_&#39; . md5($this->appid?.?$this-&gt;secret); ????????//定義存儲文件路徑 ????????//?$filename?=?__DIR__?.?'/cache/'?.?$name?.?'.php'; 		$filename?=?'../runtime/temp/'?.?$name?.?'.php'; ????????//判斷文件是否存在,如果存在,就取出文件中的數據值,如果不存在,就向微信端請求 ????????if?(is_file($filename)?&amp;&amp;?filemtime($filename)?+?7100?&gt;?time()){ ????????????$result?=?include?$filename; ????????????//定義需要返回的內容$data ????????????$data?=?$result['access_token']; ????????}else{ ????????????//?https請求方式:?GET 			//?https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=APPID&amp;secret=APPSECRET ????????????//?調用curl方法完成請求 ????????????$url?=?'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid='.$this-&gt;appid.'&amp;secret='?.?$this-&gt;secret; ????????????$result?=?$this-&gt;curl($url); ????????????//將返回得到的json數據轉成php數組 ????????????$result?=?json_decode($result,true); ????????????//將內容寫入文件中 ????????????file_put_contents($filename,"<?php nreturn " . var_export($result,true) . ";n?>"); ????????????//定義需要返回的內容 ????????????$data?=?$result['access_token']; ????????}  ????????//將得到的access_token的值返回 ????????return?$data;  ????}  ????/** ?????* ?????*?獲取臨時票據方法 ?????* ?????*?@return?mixed ?????*/ ????public?function?getJsapiTicket(){ ????????//存入文件中,定義文件的名稱和路徑 ????????$name?=?'ticket_'?.?md5($this-&gt;appid?.?$this-&gt;secret); ????????//定義存儲文件路徑 ????????//$filename?=?__DIR__?.?'/cache/'?.?$name?.?'.php'; 		$filename?=?'../runtime/temp/'?.?$name?.?'.php'; ????????//判斷是否存在臨時票據的文件,如果存在,就直接取值,如果不存在,就發送請求獲取并保存 ????????if?(is_file($filename)?&amp;&amp;?filemtime($filename)?+?7100?&gt;?time()){ ????????????$result?=?include?$filename; ????????}else{ ????????????//定義請求地址 ????????????$url?=?'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this ????????????????????-&gt;getAccessToken().'&amp;type=jsapi'; ????????????//使用curl方法發送請求,獲取臨時票據 ????????????$result?=?$this-&gt;curl($url); ????????????//轉換成php數組 ????????????$result?=?json_decode($result,true); ????????????//將獲取到的值存入文件中 ????????????file_put_contents($filename,"<?php nreturn " . var_export($result,true) . ";n?>");  ????????} ????????//定義返回的數據 ????????$data?=?$result['ticket']; ????????//將得到的臨時票據結果返回 ????????return?$data; ????}  ????/** ?????*?獲取簽名方法 ?????*/ ????public?function?sign(){ ????????//需要定義4個參數,分別包括隨機數,臨時票據,時間戳和當前url地址 ????????$nonceStr?=?$this-&gt;makeStr(); ????????$ticket?=?$this-&gt;getJsapiTicket(); ????????$time?=?time(); ????????//組合url 		//$url?=?$_SERVER['REQUEST_SCHEME']?.?'://'?.?$_SERVER['SERVER_NAME']?.?$_SERVER['REQUEST_URI']; ????????$url?=?'http://'?.?$_SERVER['SERVER_NAME']?.?$_SERVER['REQUEST_URI']; ????????//將4個參數放入一個數組中 ????????$arr?=?[ ????????????'noncestr='?.?$nonceStr, ????????????'jsapi_ticket='?.?$ticket, ????????????'timestamp='?.?$time, ????????????'url='?.?$url ????????]; ????????//對數組進行字段化排序 ????????sort($arr,SORT_STRING); ????????//對數組進行組合成字符串 ????????$string?=?implode('&amp;',$arr); ????????//將字符串加密生成簽名 ????????$sign?=?sha1($string); ????????//由于調用簽名方法的時候不只需要簽名,還需要生成簽名的時候的隨機數,時間戳,所以我們應該返回由這些內容組成的一個數組 ????????$reArr?=?[ ????????????'appId'?=&gt;?$this-&gt;appid, ????????????'timestamp'?=&gt;?$time, ????????????'nonceStr'?=&gt;?$nonceStr, ????????????'signature'?=&gt;?$sign, ????????????'url'?=&gt;?$url ????????]; ????????//將數組返回 ????????return?$reArr; ????}  ????/** ?????* ?????*?生成隨機數 ?????* ?????*?@return?string ?????*/ ????protected?function?makeStr(){ ????????//定義字符串組成的種子 ????????$seed?=?'www512wayanbao1qasxianrendong5tgblaochaguan8ik9500net'; ????????//通過循環來組成一個16位的隨機字符串 ????????//定義一個空字符串?用來接收組合成的字符串內容 ????????$str?=?''; ????????for?($i?=?0;$i?getAccessToken(); //echo?$data;  //測試獲取jsapiticket方法 //$obj?=?new?Wx(); //$data?=?$obj-&gt;getJsapiTicket(); //echo?$data;  //測試生成簽名方法 //$obj?=?new?Wx(); //$data?=?$obj-&gt;sign(); //echo?'<pre class="brush:php;toolbar:false">'; //print_r($data);  ?>

后臺控制器處理

<?php namespace appindexcontroller; use thinkController; use thinkDb; use appadminmodelMenu; use utilJssdk;  class Index extends Controller {     public function demo(){         $id = input(&#39;id&#39;,0);//ID         $catid = input(&#39;catid&#39;,0);//分類ID          $modelInfo = getModInfoById($catid);          $info = Db::name($modelInfo[&#39;tablename&#39;])->where('id',$id)-&gt;find(); ????????$catinfo?=?getCatInfoById($catid); ????????$p_catname?=?getCatInfoById($catinfo['parentid'],'catname');  		$obj?=?new?Jssdk(); 		$data?=?$obj-&gt;sign();  ????????$this-&gt;assign('infos',$info); ????????$this-&gt;assign('catids',$catid); ????????$this-&gt;assign('catnames',$catinfo['catname']); ????????$this-&gt;assign('p_catnames',$p_catname); 		$this-&gt;assign('data',$data);  ????????return?view('../application/index/view/default/index/'?.?$modelInfo['show_template']); ????} } ?&gt;

微信事件響應

<script></script><script> 	// 通過config接口注入權限驗證配置 	wx.config({ 		debug: false,  		appId: &#39;{$data.appId}&#39;, 		timestamp: &#39;{$data.timestamp}&#39;, 		nonceStr: &#39;{$data.nonceStr}&#39;,  		signature: &#39;{$data.signature}&#39;, 		jsApiList: [ 			&#39;onMenuShareTimeline&#39;, 			&#39;onMenuShareAppMessage&#39; 		] 	}); 	// 通過ready接口處理成功驗證 	wx.ready(function(){ 		// 分享到朋友圈 		wx.onMenuShareTimeline({ 			title: &#39;{$info.title}&#39;, 			link: &#39;{$data.url}&#39;,  			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,  			success: function () { 				// 用戶點擊了分享后執行的回調函數 			} 		}); 		// 分享給朋友 		wx.onMenuShareAppMessage({ 			title: &#39;{$info.title}&#39;,  			desc: &#39;{$info.description}&#39;,  			link: &#39;{$data.url}&#39;,  			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,  			type: &#39;link&#39;, // 分享類型,music、video或link,不填默認為link 			dataUrl: &#39;&#39;, // 如果type是music或video,則要提供數據鏈接,默認為空 			success: function () { 				// 用戶點擊了分享后執行的回調函數 			} 		}); 	}); </script>

?全部分享接口

<script></script><script> 	// 通過config接口注入權限驗證配置 	wx.config({ 		debug: true,  		appId: &#39;{$data.appId}&#39;, 		timestamp: &#39;{$data.timestamp}&#39;, 		nonceStr: &#39;{$data.nonceStr}&#39;,  		signature: &#39;{$data.signature}&#39;, 		jsApiList: [ 			&#39;onMenuShareTimeline&#39;, 			&#39;onMenuShareAppMessage&#39;, 			&#39;onMenuShareQQ&#39;, 			&#39;onMenuShareWeibo&#39;, 			&#39;onMenuShareQZone&#39; 		] 	}); 	// 通過ready接口處理成功驗證 	wx.ready(function(){ 		// 分享到朋友圈 		wx.onMenuShareTimeline({ 			title: &#39;{$info.title}&#39;, 			link: &#39;{$data.url}&#39;,  			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,  			success: function () { 				// 用戶點擊了分享后執行的回調函數 			} 		}); 		// 分享給朋友 		wx.onMenuShareAppMessage({ 			title: &#39;{$info.title}&#39;,  			desc: &#39;{$info.description}&#39;,  			link: &#39;{$data.url}&#39;,  			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,  			type: &#39;link&#39;, // 分享類型,music、video或link,不填默認為link 			dataUrl: &#39;&#39;, // 如果type是music或video,則要提供數據鏈接,默認為空 			success: function () { 				// 用戶點擊了分享后執行的回調函數 			} 		}); 		// 分享到QQ 		wx.onMenuShareQQ({ 			title: &#39;{$info.title}&#39;,  			desc: &#39;{$info.description}&#39;,  			link: &#39;{$data.url}&#39;,  			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,  			success: function () { 				// 用戶確認分享后執行的回調函數 			}, 			cancel: function () { 				// 用戶取消分享后執行的回調函數 			} 		}); 		// 分享到騰訊微博 		wx.onMenuShareWeibo({ 			title: &#39;{$info.title}&#39;, 			desc: &#39;{$info.description}&#39;,  			link: &#39;{$data.url}&#39;,  			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,  			success: function () { 				// 用戶確認分享后執行的回調函數 			}, 			cancel: function () { 				// 用戶取消分享后執行的回調函數 			} 		}); 		// 分享到QQ空間 		wx.onMenuShareQZone({ 			title: &#39;{$info.title}&#39;,  			desc: &#39;{$info.description}&#39;,  			link: &#39;{$data.url}&#39;,  			imgUrl: &#39;http://m.psnav.com/uploads/image/{$info.thumb}&#39;,  			success: function () { 				// 用戶確認分享后執行的回調函數 			}, 			cancel: function () { 				// 用戶取消分享后執行的回調函數 			} 		}); 	}); </script>

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享