代码分三部分进行,第一部分是C层(Controller),第二部分是M层(Model),第三部分是V层(View)
1.控制器层WeixinController
$data = array( 'pic'=>'',//图片地址 'title'=>'',//标题 'description'=>''//描述 ); $weixinmodel = new WeixinshareModel(); $weixindata = array(); $weixindata['appId'] = 'xxxxxxxxxxxxxxxxxxxx';//appid $weixindata['nonceStr'] = $weixinmodel->createNonceStr(); $weixindata['timestamp'] = time(); $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $weixindata['pic'] = $data['pic']; $weixindata['link'] = $url; $weixindata['title'] = $data['title']; $weixindata['description'] = mb_substr($data['description'], 0, 30, 'UTF-8'); $weixindata['signature'] = $weixinmodel->signature($weixindata['nonceStr'],$weixindata['timestamp'],$url); $this->assign("weixindata", $weixindata);
2.模型层WeixinModel
<?php class WeixinshareModel extends baseModel { const APPID = 'xxxxxxxxxxxxxxx'; const APPSECRET = 'xxxxxxxxxxxxxxxxxxxxxxxx'; function __construct() { parent::__construct(); } //获取access_token public function accessToken() { $access_token = $this->redis->get("weixin_access_token");//存入redis,这里要结合自己的项目,对redis或者memcahe进行设置 if(!$access_token){ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".self::APPID."&secret=".self::APPSECRET; $data = json_decode($this->httpGet($url),true); if(isset($data['access_token']) && $data['access_token'] != ''){ $access_token = $this->redis->set("weixin_access_token",$data['access_token'],7200); }else{ return false; } } return $access_token; } //用第一步拿到的access_token 采用http GET方式请求获得jsapi_ticket public function jsapiTicket() { $jsapi_ticket = $this->redis->get("weixin_jsapi_ticket"); if(!$jsapi_ticket){ $access_token = $this->accessToken(); if($access_token){ $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi"; $data = json_decode($this->httpGet($url),true); if(isset($data['errcode']) && $data['errcode']== 0){//请求成功 $jsapi_ticket = $this->redis->set("weixin_jsapi_ticket",$data['ticket'],7200); }else{ return false; } }else{ return false; } } return $jsapi_ticket; } //获取signature public function signature($nonceStr,$timestamp,$url) { $jsapi_ticket = $this->jsapiTicket(); $signature = ''; if($jsapi_ticket) { $string = "jsapi_ticket=$jsapi_ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string);//对string1进行sha1签名,得到signature }else{ return false; } return $signature; } //生成随机字符串 public function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } //远程请求,可以调用自己项目的远程请求 public function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }
3.视图层weixin.phtml
<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> <meta content="width=device-width,minimum-scale=1.0,maximum-scale=1.0" name="viewport"> <!--这个js必须引入--> <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> </head> <body> <script type="text/javascript"> wx.config({ //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: '<?=$weixindata['appId']?>', // 必填,公众号的唯一标识 timestamp: <?=$weixindata['timestamp']?>, // 必填,生成签名的时间戳 nonceStr: '<?=$weixindata['nonceStr']?>', // 必填,生成签名的随机串 signature: '<?=$weixindata['signature']?>',// 必填,签名,见附录1 jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填,需要使用的JS接口列表 }); wx.ready(function(){ wx.onMenuShareTimeline({//分享到朋友圈 title: '<?=$weixindata['title']?>', // 分享标题 link: '<?=$weixindata['link']?>', // 分享链接 imgUrl: '<?=$weixindata['pic']?>' // 分享图标 }); wx.onMenuShareAppMessage({//分享给朋友 title: '<?=$weixindata['title']?>', // 分享标题 desc: '<?=$weixindata['description']?>', // 分享描述 link: '<?=$weixindata['link']?>', // 分享链接 imgUrl: '<?=$weixindata['pic']?>', // 分享图标 type: '', // 分享类型,music、video或link,不填默认为link dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享后执行的回调函数 } }); wx.onMenuShareQQ({//分享到QQ title: '<?=$weixindata['title']?>', // 分享标题 desc: '<?=$weixindata['description']?>', // 分享描述 link: '<?=$weixindata['link']?>', // 分享链接 imgUrl: '<?=$weixindata['pic']?>' // 分享图标 }); wx.onMenuShareWeibo({//分享到腾讯微博 title: '<?=$weixindata['title']?>', // 分享标题 desc: '<?=$weixindata['description']?>', // 分享描述 link: '<?=$weixindata['link']?>', // 分享链接 imgUrl: '<?=$weixindata['pic']?>' // 分享图标 }); wx.onMenuShareQZone({//分享到QQ空间 title: '<?=$weixindata['title']?>', // 分享标题 desc: '<?=$weixindata['description']?>', // 分享描述 link: '<?=$weixindata['link']?>', // 分享链接 imgUrl: '<?=$weixindata['pic']?>' // 分享图标 }); // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。 }); </script> <div class="wrap"> </div> </body> </html>