<?php namespace app\wechat\service; use think\Config; class Signature { protected $appId ; protected $appSecret; protected $path; public function __construct($type='service') { $this->appId = Config::get('wx_'.$type.'.appId'); $this->appSecret = Config::get('wx_'.$type.'.appSecret'); $this->path = Config::get('wx_'.$type.'.fileUrl'); } public function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); $url = urldecode($_GET['sign_url']); $timestamp = time(); $nonceStr = get_rand_str(16, 0, 1, 0); // 这里参数的顺序要按照 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 getJsApiTicket() { $JsApiTicket = $this->path . "JsApiTicket_pssh.json";//缓存文件名 !is_file($JsApiTicket) && touch($JsApiTicket, '777'); $data = json_decode(file_get_contents($JsApiTicket), true); if ($data['expire_time'] < time() or !$data['expire_time']) { $accessToken = $this->access_Token(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode(httpRequest($url, 'GET'), true); $ticket = $res['ticket']; if ($ticket) { $data['expire_time'] = time() + 7000; $data['jsapi_ticket'] = $ticket; $fp = fopen($JsApiTicket, "w+"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data['jsapi_ticket']; } return $ticket; } public function access_Token() { $tokenFile = $this->path . "accessToken_pssh.json";//缓存文件名 !is_file($tokenFile) && touch($tokenFile, '777'); $data = json_decode(file_get_contents($tokenFile), true); if ($data['expire_time'] < time() or !$data['expire_time']) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appId=".$this->appId."&secret=".$this->appSecret; $res = httpRequest($url, 'GET'); $res = json_decode($res,true); $access_token = $res['access_token']; if($access_token) { $data['expire_time'] = time() + 7000; $data['access_token'] = $access_token; $fp = fopen($tokenFile, "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data['access_token']; } return $access_token; } }
使用 (上面的配置文件根据自己的需要写)
public function getSignPackage() { $sign = new SignatureService('service'); $result = $sign->getSignPackage(); return json($result); }
这是基于TP5写的 自己记录用 要是能看懂就拿走
SignatureService()方法里面的参数是区分不同公众号 这样也要有不同的配置文件
function share_disable(){ $.ajax({ type: 'GET', url: localurl + "/index.php/……/getSignPackage", data: {sign_url:encodeURIComponent(location.href.split('#')[0])}, dataType: 'JSON', success: function(data){ wx.config({ // debug :true, appId: data.appId, timestamp: data.timestamp, nonceStr: data.nonceStr, signature: data.signature, jsApiList: [ "hideMenuItems" ] }); wx.ready(function () { wx.hideMenuItems({ menuList: [ "menuItem:share:qq", "menuItem:share:timeline", "menuItem:share:appMessage", "menuItem:share:QZone", "menuItem:share:weiboApp", "menuItem:favorite", "menuItem:share:facebook", "menuItem:copyUrl", "menuItem:openWithQQBrowser", "menuItem:share:email", "menuItem:openWithSafari", "menuItem:originPage" ] // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3 }); }); } }); }
这是隐藏分享按钮的示例