ThinkPHP5怎麼整合JS-SDK實作微信自訂分享功能
Jssdk類別庫
1、檔案名稱及位置
名字:Jssdk.php
位置:extend\util\Jssdk.php
# 2、代碼
<?php namespace util; class Jssdk { protected $appid = 'xxxx'; protected $secret = 'xxxx'; /** * 获取access_token方法 */ public function getAccessToken(){ //定义文件名称 $name = 'token_' . md5($this->appid . $this->secret); //定义存储文件路径 // $filename = __DIR__ . '/cache/' . $name . '.php'; $filename = '../runtime/temp/' . $name . '.php'; //判断文件是否存在,如果存在,就取出文件中的数据值,如果不存在,就向微信端请求 if (is_file($filename) && filemtime($filename) + 7100 > time()){ $result = include $filename; //定义需要返回的内容$data $data = $result['access_token']; }else{ // https请求方式: GET // https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET // 调用curl方法完成请求 $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret=' . $this->secret; $result = $this->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->appid . $this->secret); //定义存储文件路径 //$filename = __DIR__ . '/cache/' . $name . '.php'; $filename = '../runtime/temp/' . $name . '.php'; //判断是否存在临时票据的文件,如果存在,就直接取值,如果不存在,就发送请求获取并保存 if (is_file($filename) && filemtime($filename) + 7100 > time()){ $result = include $filename; }else{ //定义请求地址 $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this ->getAccessToken().'&type=jsapi'; //使用curl方法发送请求,获取临时票据 $result = $this->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->makeStr(); $ticket = $this->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('&',$arr); //将字符串加密生成签名 $sign = sha1($string); //由于调用签名方法的时候不只需要签名,还需要生成签名的时候的随机数,时间戳,所以我们应该返回由这些内容组成的一个数组 $reArr = [ 'appId' => $this->appid, 'timestamp' => $time, 'nonceStr' => $nonceStr, 'signature' => $sign, 'url' => $url ]; //将数组返回 return $reArr; } /** * * 生成随机数 * * @return string */ protected function makeStr(){ //定义字符串组成的种子 $seed = 'www512wayanbao1qasxianrendong5tgblaochaguan8ik9500net'; //通过循环来组成一个16位的随机字符串 //定义一个空字符串 用来接收组合成的字符串内容 $str = ''; for ($i = 0;$i < 16; $i++){ //定义一个随机数 $num = rand(0,strlen($seed) - 1); //循环连接随机生成的字符串 $str .= $seed[$num]; } //将随机数返回 return $str; } /** * * 服务器之间请求的curl方法 * * @param $url 请求地址 * @param array $field post参数 * @return string */ public function curl($url,$field = []){ //初始化curl $ch = curl_init(); //设置请求的地址 curl_setopt($ch,CURLOPT_URL,$url); //设置接收返回的数据,不直接展示在页面 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //设置禁止证书校验 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); //判断是否为post请求方式,如果传递了第二个参数,就代表是post请求,如果么有传递,第二个参数为空,就是get请求 if (!empty($field)){ //设置请求超时时间 curl_setopt($ch,CURLOPT_TIMEOUT,30); //设置开启post curl_setopt($ch,CURLOPT_POST,1); //传递post数据 curl_setopt($ch,CURLOPT_POSTFIELDS,$field); } //定义一个空字符串,用来接收请求的结果 $data = ''; if (curl_exec($ch)){ $data = curl_multi_getcontent($ch); } //关闭curl curl_close($ch); //将得到的结果返回 return $data; } } //测试获取access_token值的方法 //$obj = new Wx(); //$data = $obj->getAccessToken(); //echo $data; //测试获取jsapiticket方法 //$obj = new Wx(); //$data = $obj->getJsapiTicket(); //echo $data; //测试生成签名方法 //$obj = new Wx(); //$data = $obj->sign(); //echo '<pre class="brush:php;toolbar:false">'; //print_r($data); ?>
後台控制器處理
<?php namespace app\index\controller; use think\Controller; use think\Db; use app\admin\model\Menu; use util\Jssdk; class Index extends Controller { public function demo(){ $id = input('id',0);//ID $catid = input('catid',0);//分类ID $modelInfo = getModInfoById($catid); $info = Db::name($modelInfo['tablename'])->where('id',$id)->find(); $catinfo = getCatInfoById($catid); $p_catname = getCatInfoById($catinfo['parentid'],'catname'); $obj = new Jssdk(); $data = $obj->sign(); $this->assign('infos',$info); $this->assign('catids',$catid); $this->assign('catnames',$catinfo['catname']); $this->assign('p_catnames',$p_catname); $this->assign('data',$data); return view('../application/index/view/default/index/' . $modelInfo['show_template']); } } ?>
微信事件回應
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <script type="text/javascript"> // 通过config接口注入权限验证配置 wx.config({ debug: false, appId: '{$data.appId}', timestamp: '{$data.timestamp}', nonceStr: '{$data.nonceStr}', signature: '{$data.signature}', jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage' ] }); // 通过ready接口处理成功验证 wx.ready(function(){ // 分享到朋友圈 wx.onMenuShareTimeline({ title: '{$info.title}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用户点击了分享后执行的回调函数 } }); // 分享给朋友 wx.onMenuShareAppMessage({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', type: 'link', // 分享类型,music、video或link,不填默认为link dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 success: function () { // 用户点击了分享后执行的回调函数 } }); }); </script>
全部分享介面
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <script type="text/javascript"> // 通过config接口注入权限验证配置 wx.config({ debug: true, appId: '{$data.appId}', timestamp: '{$data.timestamp}', nonceStr: '{$data.nonceStr}', signature: '{$data.signature}', jsApiList: [ 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone' ] }); // 通过ready接口处理成功验证 wx.ready(function(){ // 分享到朋友圈 wx.onMenuShareTimeline({ title: '{$info.title}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用户点击了分享后执行的回调函数 } }); // 分享给朋友 wx.onMenuShareAppMessage({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', type: 'link', // 分享类型,music、video或link,不填默认为link dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 success: function () { // 用户点击了分享后执行的回调函数 } }); // 分享到QQ wx.onMenuShareQQ({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享后执行的回调函数 } }); // 分享到腾讯微博 wx.onMenuShareWeibo({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享后执行的回调函数 } }); // 分享到QQ空间 wx.onMenuShareQZone({ title: '{$info.title}', desc: '{$info.description}', link: '{$data.url}', imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}', success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享后执行的回调函数 } }); }); </script>
以上是ThinkPHP5怎麼整合JS-SDK實作微信自訂分享功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

執行 ThinkPHP 專案需要:安裝 Composer;使用 Composer 建立專案;進入專案目錄,執行 php bin/console serve;造訪 http://localhost:8000 查看歡迎頁面。

ThinkPHP 擁有多個版本,針對不同 PHP 版本而設計。主要版本包括 3.2、5.0、5.1 和 6.0,而次要版本用於修復 bug 和提供新功能。目前最新穩定版本為 ThinkPHP 6.0.16。在選擇版本時,需考慮 PHP 版本、功能需求和社群支援。建議使用最新穩定版本以獲得最佳性能和支援。

ThinkPHP Framework 的本機運作步驟:下載並解壓縮 ThinkPHP Framework 到本機目錄。建立虛擬主機(可選),指向 ThinkPHP 根目錄。配置資料庫連線參數。啟動 Web 伺服器。初始化 ThinkPHP 應用程式。存取 ThinkPHP 應用程式 URL 運行。

Laravel 和 ThinkPHP 框架的效能比較:ThinkPHP 效能通常優於 Laravel,專注於最佳化和快取。 Laravel 性能良好,但對於複雜應用程序,ThinkPHP 可能更適合。

《開發建議:如何利用ThinkPHP框架實現非同步任務》隨著網路技術的快速發展,Web應用程式對於處理大量並發請求和複雜業務邏輯的需求也越來越高。為了提高系統的效能和使用者體驗,開發人員常常會考慮利用非同步任務來執行一些耗時操作,例如發送郵件、處理文件上傳、產生報表等。在PHP領域,ThinkPHP框架作為一個流行的開發框架,提供了一些便捷的方式來實現非同步任務。

ThinkPHP 安裝步驟:準備 PHP、Composer、MySQL 環境。使用 Composer 建立專案。安裝 ThinkPHP 框架及相依性。配置資料庫連線。產生應用程式碼。啟動應用程式並造訪 http://localhost:8000。

ThinkPHP 是一款高效能的 PHP 框架,具備快取機制、程式碼最佳化、平行處理和資料庫最佳化等優勢。官方性能測試顯示,它每秒可處理超過 10,000 個請求,實際應用中被廣泛用於京東商城、攜程網等大型網站和企業系統。

開發建議:如何利用ThinkPHP框架進行API開發隨著網際網路的不斷發展,API(ApplicationProgrammingInterface)的重要性也日益凸顯。 API是不同應用程式之間進行通訊的橋樑,它可以實現資料共享、功能呼叫等操作,為開發者提供了相對簡單且快速的開發方式。而ThinkPHP框架作為一款優秀的PHP開發框架,具有高效能、可擴展且易用
