时间:2023-05-31 01:21:01 | 来源:网站运营
时间:2023-05-31 01:21:01 来源:网站运营
【微信网页开发】关于微信JSSDK,如何自定义微信网页的分享内容:这个教程一直想做,但对于小白来说理解可能有些困难,但转念一想,我TM管小白干什么? //私有属性 private $appId; private $appSecret; private $s_url; //入口函数 public function __construct($appId, $appSecret,$s_url) { $this->appId = $appId; $this->appSecret = $appSecret; $this->s_url = $s_url; }
接受来自网页的公众号参数等private function getAccessToken() { // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 $data = json_decode(file_get_contents("access_token.json")); if ($data->expire_time < time()) { // 如果是企业号用以下URL获取access_token // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; $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) { $data->expire_time = time() + 7000; $data->access_token = $access_token; $fp = fopen("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->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; }
这里注意有一个access_token.json文件,是用来存储获取到的access_token参数的,一般在php中,这个参数可以存储在php的session中,也可以存在数据库中,但我们这是个单文件,没写框架也没写整套程序,所以我就简单的存在一个文件中了,所以你需要在你的jssdk.php同级目录,新建一个access_token.json空白文件。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; }
这里我们基础方法也就写完了,接下来是两个核心方法,首先按照官方的说法我们通过刚才的access_token去微信服务器获取jsapi_ticketprivate function getJsApiTicket() { // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 $data = json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); // 如果是企业号用以下 URL 获取 ticket // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; $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) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; }
对,根据上面的写法,我们需要再新建一个名为jsapi_ticket.json的空白文件,用来存储jsapi_ticketpublic function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); // 注意 URL 一定要动态获取,不能 hardcode. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = $this->s_url; $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; }
好了,是不是很懵啊,没关系,看看官方文档就可以了,这些字段就是我们前端页面调用jssdk接口所需的验签字段了,接下来还有一个问题,这些方法都是php的,但前端网页只能支持js脚本语言,通过js调用php的类好像行不通啊。$url = $_POST['url'];$jssdk = new JSSDK("你的appid", "你的appSecret",$url);$signPackage = $jssdk -> getSignPackage();echo json_encode($signPackage);
这里的appid和appSecret你也可以不写死,让js传过来,都是一样的<script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script> console.log(111) /**获取配置*/ function getConfig(){ $.post("你的域名/jssdk.php", { url: window.location.href, method: "ticket"}, function(data){ console.log(data) config = eval('('+data+')'); wx.config({ debug: false, appId: config.appId, timestamp: config.timestamp, nonceStr: config.nonceStr, signature: config.signature, jsApiList: [ // 所有要调用的 API 都要加到这个列表中 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'translateVoice', 'startRecord', 'stopRecord', 'onRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'uploadVoice', 'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard' ] }); wx.ready(function () { var shareData = { title: '自定义分享标题', desc: '自定义分享描述', link: '自定义分享链接', imgUrl: '自定义分享封面图' }; wx.onMenuShareAppMessage({ title: '自定义分享标题!', // 分享标题 desc: '自定义分享描述', // 分享描述 link: '自定义分享链接', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: '自定义分享封面图', // 分享图标 type: '', // 分享类型,music、video或link,不填默认为link dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 success: function () { // alert("成功") } }); wx.onMenuShareAppMessage(shareData);//分享给好友 wx.onMenuShareTimeline(shareData);//分享到朋友圈 wx.onMenuShareQQ(shareData);//分享给手机QQ wx.onMenuShareWeibo(shareData);//分享腾讯微博 wx.onMenuShareQZone(shareData);//分享到QQ空间 }); }); } </script>
最后上文中的这些文件我也都打包了,获取的话可以在原文底部获取关键词:定义,内容