当前位置:首页 > CMS教程 > Thinkphp > 列表

ThinkPHP5集成JS-SDK实现微信自定义分享功能

发布:smiling 来源: PHP粉丝网  添加日期:2023-06-27 13:35:50 浏览: 评论:0 

这篇文章主要介绍了ThinkPHP5集成JS-SDK实现微信自定义分享功能的相关资料,需要的朋友可以参考下。

微信链接分享给好友时能够自定义标题、简介和logo,现将ThinkPHP5集成JS-SDK实现微信自定义分享功能的过程整理成文。

Jssdk类库

1、文件名及位置

名字:Jssdk.php

位置:extend\util\Jssdk.php

2、代码

  1. <?php 
  2. namespace util; 
  3.  
  4. class Jssdk { 
  5.  
  6.     protected $appid = 'xxxx'
  7.     protected $secret = 'xxxx'
  8.  
  9.     /** 
  10.      * 获取access_token方法 
  11.      */ 
  12.     public function getAccessToken(){ 
  13.         //定义文件名称 
  14.         $name = 'token_' . md5($this->appid . $this->secret); 
  15.         //定义存储文件路径 
  16.         // $filename = __DIR__ . '/cache/' . $name . '.php'; 
  17.         $filename = '../runtime/temp/' . $name . '.php'
  18.         //判断文件是否存在,如果存在,就取出文件中的数据值,如果不存在,就向微信端请求 
  19.         if (is_file($filename) && filemtime($filename) + 7100 > time()){ 
  20.             $result = include $filename
  21.             //定义需要返回的内容$data 
  22.             $data = $result['access_token']; 
  23.         }else
  24.             // https请求方式: GET 
  25.             // https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET 
  26.             // 调用curl方法完成请求 
  27.             $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret=' . $this->secret; 
  28.             $result = $this->curl($url); 
  29.             //将返回得到的json数据转成php数组 
  30.             $result = json_decode($result,true); 
  31.             //将内容写入文件中 
  32.             file_put_contents($filename,"<?php\nreturn " . var_export($result,true) . ";\n?>"); 
  33.             //定义需要返回的内容 
  34.             $data = $result['access_token']; 
  35.         } 
  36.  
  37.         //将得到的access_token的值返回 
  38.         return $data
  39.  
  40.     } 
  41.  
  42.     /** 
  43.      * 
  44.      * 获取临时票据方法 
  45.      * 
  46.      * @return mixed 
  47.      */ 
  48.     public function getJsapiTicket(){ 
  49.         //存入文件中,定义文件的名称和路径 
  50.         $name = 'ticket_' . md5($this->appid . $this->secret); 
  51.         //定义存储文件路径 
  52.         //$filename = __DIR__ . '/cache/' . $name . '.php'; 
  53.         $filename = '../runtime/temp/' . $name . '.php'
  54.         //判断是否存在临时票据的文件,如果存在,就直接取值,如果不存在,就发送请求获取并保存 
  55.         if (is_file($filename) && filemtime($filename) + 7100 > time()){ 
  56.             $result = include $filename
  57.         }else
  58.             //定义请求地址 
  59.             $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$this 
  60.                     ->getAccessToken().'&type=jsapi'
  61.             //使用curl方法发送请求,获取临时票据 
  62.             $result = $this->curl($url); 
  63.             //转换成php数组 
  64.             $result = json_decode($result,true); 
  65.             //将获取到的值存入文件中 
  66.             file_put_contents($filename,"<?php\nreturn " . var_export($result,true) . ";\n?>"); 
  67.  
  68.         } 
  69.         //定义返回的数据 
  70.         $data = $result['ticket']; 
  71.         //将得到的临时票据结果返回 
  72.         return $data
  73.     } 
  74.  
  75.     /** 
  76.      * 获取签名方法 
  77.      */ 
  78.     public function sign(){ 
  79.         //需要定义4个参数,分别包括随机数,临时票据,时间戳和当前url地址 
  80.         $nonceStr = $this->makeStr(); 
  81.         $ticket = $this->getJsapiTicket(); 
  82.         $time = time(); 
  83.         //组合url 
  84.         //$url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 
  85.         $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; 
  86.         //将4个参数放入一个数组中 
  87.         $arr = [ 
  88.             'noncestr=' . $nonceStr
  89.             'jsapi_ticket=' . $ticket
  90.             'timestamp=' . $time
  91.             'url=' . $url 
  92.         ]; 
  93.         //对数组进行字段化排序 
  94.         sort($arr,SORT_STRING); 
  95.         //对数组进行组合成字符串 
  96.         $string = implode('&',$arr); 
  97.         //将字符串加密生成签名 
  98.         $sign = sha1($string); 
  99.         //由于调用签名方法的时候不只需要签名,还需要生成签名的时候的随机数,时间戳,所以我们应该返回由这些内容组成的一个数组 
  100.         $reArr = [ 
  101.             'appId' => $this->appid, 
  102.             'timestamp' => $time
  103.             'nonceStr' => $nonceStr
  104.             'signature' => $sign
  105.             'url' => $url 
  106.         ]; 
  107.         //将数组返回 
  108.         return $reArr
  109.     } 
  110.  
  111.     /** 
  112.      * 
  113.      * 生成随机数 
  114.      * 
  115.      * @return string 
  116.      */ 
  117.     protected function makeStr(){ 
  118.         //定义字符串组成的种子 
  119.         $seed = 'www512wayanbao1qasxianrendong5tgblaochaguan8ik9500net'
  120.         //通过循环来组成一个16位的随机字符串 
  121.         //定义一个空字符串 用来接收组合成的字符串内容 
  122.         $str = ''
  123.         for ($i = 0;$i < 16; $i++){ 
  124.             //定义一个随机数 
  125.             $num = rand(0,strlen($seed) - 1); 
  126.             //循环连接随机生成的字符串 
  127.             $str .= $seed[$num]; 
  128.         } 
  129.         //将随机数返回 
  130.         return $str
  131.     } 
  132.  
  133.  
  134.     /** 
  135.      * 
  136.      * 服务器之间请求的curl方法 
  137.      * 
  138.      * @param $url 请求地址 
  139.      * @param array $field post参数 
  140.      * @return string 
  141.      */ 
  142.     public function curl($url,$field = []){ 
  143.         //初始化curl 
  144.         $ch = curl_init(); 
  145.         //设置请求的地址 
  146.         curl_setopt($ch,CURLOPT_URL,$url); 
  147.         //设置接收返回的数据,不直接展示在页面 
  148.         curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  149.         //设置禁止证书校验 
  150.         curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); 
  151.         curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); 
  152.         //判断是否为post请求方式,如果传递了第二个参数,就代表是post请求,如果么有传递,第二个参数为空,就是get请求 
  153.         if (!emptyempty($field)){ 
  154.             //设置请求超时时间 
  155.             curl_setopt($ch,CURLOPT_TIMEOUT,30); 
  156.             //设置开启post 
  157.             curl_setopt($ch,CURLOPT_POST,1); 
  158.             //传递post数据 
  159.             curl_setopt($ch,CURLOPT_POSTFIELDS,$field); 
  160.         } 
  161.         //定义一个空字符串,用来接收请求的结果 
  162.         $data = ''
  163.         if (curl_exec($ch)){ 
  164.             $data = curl_multi_getcontent($ch); 
  165.         } 
  166.         //关闭curl 
  167.         curl_close($ch); 
  168.         //将得到的结果返回 
  169.         return $data
  170.     } 
  171.  
  172. //测试获取access_token值的方法 
  173. //$obj = new Wx(); 
  174. //$data = $obj->getAccessToken(); 
  175. //echo $data; 
  176.  
  177. //测试获取jsapiticket方法 
  178. //$obj = new Wx(); 
  179. //$data = $obj->getJsapiTicket(); 
  180. //echo $data; 
  181.  
  182. //测试生成签名方法 
  183. //$obj = new Wx(); 
  184. //$data = $obj->sign(); 
  185. //echo '<pre>'; 
  186. //print_r($data); 
  187.  
  188. ?> 

后台控制器处理

  1. <?php 
  2. namespace app\index\controller; 
  3. use think\Controller; 
  4. use think\Db; 
  5. use app\admin\model\Menu; 
  6. use util\Jssdk; 
  7.  
  8. class Index extends Controller { 
  9.     public function demo(){ 
  10.         $id = input('id',0);//ID 
  11.         $catid = input('catid',0);//分类ID 
  12.  
  13.         $modelInfo = getModInfoById($catid); 
  14.  
  15.         $info = Db::name($modelInfo['tablename'])->where('id',$id)->find(); 
  16.         $catinfo = getCatInfoById($catid); 
  17.         $p_catname = getCatInfoById($catinfo['parentid'],'catname'); 
  18.  
  19.         $obj = new Jssdk(); 
  20.         $data = $obj->sign(); 
  21.  
  22.         $this->assign('infos',$info); 
  23.         $this->assign('catids',$catid); 
  24.         $this->assign('catnames',$catinfo['catname']); 
  25.         $this->assign('p_catnames',$p_catname); 
  26.         $this->assign('data',$data); 
  27.  
  28.         return view('../application/index/view/default/index/' . $modelInfo['show_template']); 
  29.     } 
  30. ?> 

微信事件响应

  1. <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> 
  2. <script type="text/javascript"
  3.     // 通过config接口注入权限验证配置 
  4.     wx.config({ 
  5.         debug: false,  
  6.         appId: '{$data.appId}'
  7.         timestamp: '{$data.timestamp}'
  8.         nonceStr: '{$data.nonceStr}',  
  9.         signature: '{$data.signature}'
  10.         jsApiList: [ 
  11.             'onMenuShareTimeline'
  12.             'onMenuShareAppMessage' 
  13.         ] 
  14.     }); 
  15.     // 通过ready接口处理成功验证 
  16.     wx.ready(function(){ 
  17.         // 分享到朋友圈 
  18.         wx.onMenuShareTimeline({ 
  19.             title: '{$info.title}'
  20.             link: '{$data.url}',  
  21.             imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',  
  22.             success: function () { 
  23.                 // 用户点击了分享后执行的回调函数 
  24.             } 
  25.         }); 
  26.         // 分享给朋友 
  27.         wx.onMenuShareAppMessage({ 
  28.             title: '{$info.title}',  
  29.             desc: '{$info.description}',  
  30.             link: '{$data.url}',  
  31.             imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',  
  32.             type: 'link'// 分享类型,music、video或link,不填默认为link 
  33.             dataUrl: ''// 如果type是music或video,则要提供数据链接,默认为空 
  34.             success: function () { 
  35.                 // 用户点击了分享后执行的回调函数 
  36.             } 
  37.         }); 
  38.     }); 
  39. </script> 

全部分享接口

  1. <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> 
  2. <script type="text/javascript"
  3.     // 通过config接口注入权限验证配置 
  4.     wx.config({ 
  5.         debug: true,  
  6.         appId: '{$data.appId}'
  7.         timestamp: '{$data.timestamp}'
  8.         nonceStr: '{$data.nonceStr}',  
  9.         signature: '{$data.signature}'
  10.         jsApiList: [ 
  11.             'onMenuShareTimeline'
  12.             'onMenuShareAppMessage'
  13.             'onMenuShareQQ'
  14.             'onMenuShareWeibo'
  15.             'onMenuShareQZone' 
  16.         ] 
  17.     }); 
  18.     // 通过ready接口处理成功验证 
  19.     wx.ready(function(){ 
  20.         // 分享到朋友圈 
  21.         wx.onMenuShareTimeline({ 
  22.             title: '{$info.title}'
  23.             link: '{$data.url}',  
  24.             imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',  
  25.             success: function () { 
  26.                 // 用户点击了分享后执行的回调函数 
  27.             } 
  28.         }); 
  29.         // 分享给朋友 
  30.         wx.onMenuShareAppMessage({ 
  31.             title: '{$info.title}',  
  32.             desc: '{$info.description}',  
  33.             link: '{$data.url}',  
  34.             imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',  
  35.             type: 'link'// 分享类型,music、video或link,不填默认为link 
  36.             dataUrl: ''// 如果type是music或video,则要提供数据链接,默认为空 
  37.             success: function () { 
  38.                 // 用户点击了分享后执行的回调函数 
  39.             } 
  40.         }); 
  41.         // 分享到QQ 
  42.         wx.onMenuShareQQ({ 
  43.             title: '{$info.title}',  
  44.             desc: '{$info.description}',  
  45.             link: '{$data.url}',  
  46.             imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',  
  47.             success: function () { 
  48.                 // 用户确认分享后执行的回调函数 
  49.             }, 
  50.             cancel: function () { 
  51.                 // 用户取消分享后执行的回调函数 
  52.             } 
  53.         }); 
  54.         // 分享到腾讯微博 
  55.         wx.onMenuShareWeibo({ 
  56.             title: '{$info.title}'
  57.             desc: '{$info.description}',  
  58.             link: '{$data.url}',  
  59.             imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',  
  60.             success: function () { 
  61.                 // 用户确认分享后执行的回调函数 
  62.             }, 
  63.             cancel: function () { 
  64.                 // 用户取消分享后执行的回调函数 
  65.             } 
  66.         }); 
  67.         // 分享到QQ空间 
  68.         wx.onMenuShareQZone({ 
  69.             title: '{$info.title}',  
  70.             desc: '{$info.description}',  
  71.             link: '{$data.url}',  
  72.             imgUrl: 'http://m.psnav.com/uploads/image/{$info.thumb}',  
  73.             success: function () { 
  74.                 // 用户确认分享后执行的回调函数 
  75.             }, 
  76.             cancel: function () { 
  77.                 // 用户取消分享后执行的回调函数 
  78.             } 
  79.         }); 
  80.     }); 
  81. </script>

Tags: ThinkPHP5集成JS-SDK ThinkPHP5微信自定义分享

分享到: