123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace Org\Util;
- use Common\Cache\RedisCache;
- /*
- * 微信jssdk
- */
- class Jssdk {
- private $appId;
- private $appSecret;
- public function __construct($appId, $appSecret) {
- $this->appId = $appId;
- $this->appSecret = $appSecret;
- }
- public function getSignPackage($url) {
- $jsapiTicket = $this->getJsApiTicket();
- // 注意 URL 一定要动态获取,不能 hardcode.
- //$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;
- }
- private function getJsApiTicket() {
- $authname = 'wechat_jsapi_ticket'.$this->appId;
- if ($ticket = $this->getCache($authname)) {
- return $ticket;
- }
- $accessToken = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
- $result = $this->httpGet($url);
- if ($result) {
- $json = json_decode($result,true);
- if (!$json || !empty($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- $ticket = $json['ticket'];
- $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
- $this->setCache($authname,$ticket,$expire);
- return $ticket;
- }
- }
- private function getAccessToken() {
- $authname = 'wechat_access_token'.$this->appId;
- if ($access_token = $this->getCache($authname)) {
- return $access_token;
- }
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
- $result = $this->httpGet($url);
- if ($result) {
- $json = json_decode($result,true);
- if (!$json || isset($json['errcode'])) {
- $this->errCode = $json['errcode'];
- $this->errMsg = $json['errmsg'];
- return false;
- }
- $access_token = $json['access_token'];
- $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
- $this->setCache($authname,$access_token,$expire);
- 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, true);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
- curl_setopt($curl, CURLOPT_URL, $url);
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
- /**
- * 设置缓存
- * @param string $cachename
- * @param mixed $value
- * @param int $expired
- * @return boolean
- */
- private function setCache($cachename,$value,$expired){
- $redis_cache = new RedisCache();
- $redis_cache->set($cachename,$value,$expired);
- }
- /**
- * 获取缓存
- * @param string $cachename
- * @return mixed
- */
- private function getCache($cachename){
- $redis_cache = new RedisCache();
- return $redis_cache->get($cachename);
- }
- }
|