<html > <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" /> <title>{headTitle}</title> <script type="text/javascript" src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js" ></script> </head> <body> {bodyHtml} <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script type="text/javascript"> var json_str = ‘<?php echo json_encode($signPackage); ?>‘; var signPackage = jQuery.parseJSON(json_str); $(function() { var urlpath = ‘http://‘ + window.location.hostname + window.location.pathname; if (urlpath.slice(-10) == "index.html") { urlpath = urlpath.slice(0, -10); } var appUrl = urlpath; var imgUrl = ‘http://‘ + window.location.hostname + ‘/static/lumi/share.jpg‘; var dataForWeixin = { title: "lumi", desc: " lumi", link: appUrl, imgUrl: imgUrl }; wx.config({ debug: false, appId: signPackage.appId, timestamp: signPackage.timestamp, nonceStr: signPackage.nonceStr, signature: signPackage.signature, jsApiList: [ ‘onMenuShareTimeline‘, ‘onMenuShareAppMessage‘ ] }); wx.ready(function() { // 2. 分享接口 // 2.1 监听“分享给朋友”,按钮点击、自定义分享内容及分享结果接口 wx.onMenuShareAppMessage({ title: dataForWeixin.title, desc: dataForWeixin.desc, link: dataForWeixin.link, imgUrl: dataForWeixin.imgUrl, trigger: function(res) { // alert(‘用户点击发送给朋友‘); }, success: function(res) { _hmt.push([‘_trackEvent‘, ‘chocolate_friend‘, ‘share_friend‘, ‘wechat‘, ‘friend‘]); }, cancel: function(res) { // alert(‘已取消‘); }, fail: function(res) { // alert(JSON.stringify(res)); } }); // 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口 wx.onMenuShareTimeline({ title: dataForWeixin.title, desc: dataForWeixin.desc, link: dataForWeixin.link, imgUrl: dataForWeixin.imgUrl, trigger: function(res) { // alert(‘用户点击分享到朋友圈‘); }, success: function(res) { _hmt.push([‘_trackEvent‘, ‘chocolate_friends‘, ‘share_friends‘, ‘wechat‘, ‘friends‘]); }, cancel: function(res) { //alert(‘已取消‘); }, fail: function(res) { // alert(JSON.stringify(res)); } }); }); wx.error(function(res) { alert(res.errMsg); }); }); </script> </body> </html>
上面是首页,view文件
基于CI框架
下面是 控制器
<?php if ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘); /** * @微信分享,发送好友圈后段代码 * @需要创建lumi_token表存储 token和ticket * @文件包中已提供表结构 */ class Lumi extends CI_Controller { var $appUrl=‘‘; var $appName = ‘lumi‘; var $appTitle = ‘lumi‘; var $appId = ‘wx0f82c5fa6e943f4c‘; var $appSecret = ‘411c63951104c3dab1f26e2f95fb3300‘; function __construct() { header("Content-Type: text/html; charset=UTF-8"); parent::__construct(); $this->appUrl = ‘http://‘.$_SERVER[‘HTTP_HOST‘].‘/lumi‘; } /** * */ public function index(){ $data = array( ‘headTitle‘=>$this->appTitle, ‘appUrl‘=>$this->appUrl ); $signPackage = $this->GetSignPackage(); $data[‘signPackage‘] = $signPackage; $data[‘bodyHtml‘] = $this->parser->parse($this->appName . ‘/index‘, $data); } /* * 获取微信信息 */ public function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); $protocol = (!empty($_SERVER[‘HTTPS‘]) && $_SERVER[‘HTTPS‘] !== ‘off‘ || $_SERVER[‘SERVER_PORT‘] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = $this->createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } private 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; } //获取Ticket private function getJsApiTicket() { $data = $this->db->query("select * from lumi_token order by id DESC limit 1")->result_array(); $data = $data[0]; if (count($data) <= 0) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $this->db->update(‘lumi_token‘, array(‘jsapi_ticket‘ => $ticket), array(‘access_token‘ => $accessToken)); } } else { if (time() - intval($data[‘lasttime‘]) > 6000) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $this->db->update(‘lumi_token‘, array(‘jsapi_ticket‘ => $ticket), array(‘access_token‘ => $accessToken)); } } else { $ticket = $data[‘jsapi_ticket‘]; } } return $ticket; } //获取token private function getAccessToken() { $tokendata = $this->db->query("select * from lumi_token order by id DESC limit 1")->result_array(); $tokendata = $tokendata[0]; if (count($tokendata) <= 0) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { $this->db->insert(‘lumi_token‘, array(‘access_token‘ => $access_token, ‘lasttime‘ => time())); } } else { if (time() - intval($tokendata[‘lasttime‘]) > 6000) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { $this->db->update(‘lumi_token‘, array(‘access_token‘ => $access_token, ‘lasttime‘ => time())); } } else { $access_token = $tokendata[‘access_token‘]; } } return $access_token; } private 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; } } ?>
还要配合一个数据表 表明为 lumi_token
百度云盘:1gdenMLH
时间: 2024-10-28 23:16:09