多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 一、前端 ~~~ <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', 'onMenuShareQQ', 'onMenuShareQZone', 'onMenuShareWeibo' ] }); // 通过ready接口处理成功验证 wx.ready(function(){ wx.onMenuShareTimeline({ title: '{$info.title}', link: '{if condition='$static'}{$data.url}{else/}{:url('info',['id'=>$vo.id,'catid'=>$vo.catid])}{/if}', imgUrl: 'http://xxx/uploads/image/{$info.thumb}', success: function () {} }); wx.onMenuShareAppMessage({ title: '{$info.title}', desc: '{$info.description}', link: '{if condition='$static'}{$data.url}{else/}{:url('info',['id'=>$vo.id,'catid'=>$vo.catid])}{/if}', imgUrl: 'http://xxx/uploads/image/{$info.thumb}', type: 'link', dataUrl: '', success: function () {} }); wx.onMenuShareQQ({ title: '{$info.title}', desc: '{$info.description}', link: '{if condition='$static'}{$data.url}{else/}{:url('info',['id'=>$vo.id,'catid'=>$vo.catid])}{/if}', imgUrl: 'http://xxx/uploads/image/{$info.thumb}', success: function () {}, cancel: function () {} }); wx.onMenuShareQZone({ title: '{$info.title}', desc: '{$info.description}', link: '{if condition='$static'}{$data.url}{else/}{:url('info',['id'=>$vo.id,'catid'=>$vo.catid])}{/if}', imgUrl: 'http://xxx/uploads/image/{$info.thumb}', success: function () {}, cancel: function () {} }); wx.onMenuShareWeibo({ title: '{$info.title}', desc: '{$info.description}', link: '{if condition='$static'}{$data.url}{else/}{:url('info',['id'=>$vo.id,'catid'=>$vo.catid])}{/if}', imgUrl: 'http://xxx/uploads/image/{$info.thumb}', success: function () {}, cancel: function () {} }); }); </script> ~~~ ## 二、后端 ### 1、接口调用 ~~~ public function interfaceCall(){ $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($catid,$id); $this->assign('info',$info); $this->assign('catid',$catid); $this->assign('catname',$catinfo['catname']); $this->assign('p_catname',$p_catname); $this->assign('data',$data); return view('../application/index/view/default/index/' . $modelInfo['show_template']); } ~~~ ### 2、微信接口 ~~~ <?php namespace util; /** +------------------------------------------------ * ThinkPHP5集成JS-SDK实现微信自定义分享功能 +------------------------------------------------ +------------------------------------------------ * @date 2018年8月15 +------------------------------------------------ */ class Jssdk { protected $appid = '你的接口ID'; protected $secret = '你的接口密码'; /** * 获取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($cid,$id){ //需要定义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']; //$url = 'http://' . $_SERVER['SERVER_NAME'] . 'a/show_' . $cid . '_' . $id . '.html'; //将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 = 'wwwianrendong5tgblaochaguan8ik9500net'; //通过循环来组成一个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>'; //print_r($data); ?> ~~~ ## 三、静态页微信分享 参考网址:[html静态页面实现微信分享思路](https://www.cnblogs.com/phpper/p/6604903.html) 微信分享网页的时候,希望分享出来的链接是标题+描述+缩略图,微信开发代码示例里已提供了方法,但只适用于动态页面。由于有的是生成了静态文件,其实我想使用ajax获取jssdk参数也能也能实现微信分享功能了,在这里分享给大家。 **jssdk的步骤业务流程是这样滴**: 1:在微信公众号后台配置js 安全域名,即需要引入jssdk的页面域名。 需要在域名根目录下放置微信的检测文件。 2:配置出ip白名单(可参考:http://www.idcxx.com/wx-125-1-1.html) 3:后台开发人员生成签名传递给前台 4:前端页面引入script方式 jssdk文件 5:通过ajax获取config 配置,完成config 配置后即可使用jssdk的各项功能了。 再补充啰嗦一句: **确保你获取用来签名的url是动态获取的,动态页面可参见实例代码中php的实现方式。如果是html的静态页面在前端通过ajax将url传到后台签名,前端需要用js获取当前页面除去'#'hash部分的链接(可用location.href.split('#')[0]获取,而且需要encodeURIComponent,后台decodeURIComponent解码),因为页面一旦分享,微信客户端会在你的链接末尾加入其它参数,如果不是动态获取当前链接,将导致分享后的页面签名失败**。