Jssdk.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Org\Util;
  3. use Common\Cache\RedisCache;
  4. /*
  5. * 微信jssdk
  6. */
  7. class Jssdk {
  8. private $appId;
  9. private $appSecret;
  10. public function __construct($appId, $appSecret) {
  11. $this->appId = $appId;
  12. $this->appSecret = $appSecret;
  13. }
  14. public function getSignPackage($url) {
  15. $jsapiTicket = $this->getJsApiTicket();
  16. // 注意 URL 一定要动态获取,不能 hardcode.
  17. //$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  18. //$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  19. $timestamp = time();
  20. $nonceStr = $this->createNonceStr();
  21. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  22. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  23. $signature = sha1($string);
  24. $signPackage = array(
  25. "appId" => $this->appId,
  26. "nonceStr" => $nonceStr,
  27. "timestamp" => $timestamp,
  28. "url" => $url,
  29. "signature" => $signature,
  30. "rawString" => $string
  31. );
  32. return $signPackage;
  33. }
  34. private function createNonceStr($length = 16) {
  35. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  36. $str = "";
  37. for ($i = 0; $i < $length; $i++) {
  38. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  39. }
  40. return $str;
  41. }
  42. private function getJsApiTicket() {
  43. $authname = 'wechat_jsapi_ticket'.$this->appId;
  44. if ($ticket = $this->getCache($authname)) {
  45. return $ticket;
  46. }
  47. $accessToken = $this->getAccessToken();
  48. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
  49. $result = $this->httpGet($url);
  50. if ($result) {
  51. $json = json_decode($result,true);
  52. if (!$json || !empty($json['errcode'])) {
  53. $this->errCode = $json['errcode'];
  54. $this->errMsg = $json['errmsg'];
  55. return false;
  56. }
  57. $ticket = $json['ticket'];
  58. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  59. $this->setCache($authname,$ticket,$expire);
  60. return $ticket;
  61. }
  62. }
  63. private function getAccessToken() {
  64. $authname = 'wechat_access_token'.$this->appId;
  65. if ($access_token = $this->getCache($authname)) {
  66. return $access_token;
  67. }
  68. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
  69. $result = $this->httpGet($url);
  70. if ($result) {
  71. $json = json_decode($result,true);
  72. if (!$json || isset($json['errcode'])) {
  73. $this->errCode = $json['errcode'];
  74. $this->errMsg = $json['errmsg'];
  75. return false;
  76. }
  77. $access_token = $json['access_token'];
  78. $expire = $json['expires_in'] ? intval($json['expires_in'])-100 : 3600;
  79. $this->setCache($authname,$access_token,$expire);
  80. return $access_token;
  81. }
  82. }
  83. private function httpGet($url) {
  84. $curl = curl_init();
  85. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  86. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  87. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  88. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
  89. curl_setopt($curl, CURLOPT_URL, $url);
  90. $res = curl_exec($curl);
  91. curl_close($curl);
  92. return $res;
  93. }
  94. /**
  95. * 设置缓存
  96. * @param string $cachename
  97. * @param mixed $value
  98. * @param int $expired
  99. * @return boolean
  100. */
  101. private function setCache($cachename,$value,$expired){
  102. $redis_cache = new RedisCache();
  103. $redis_cache->set($cachename,$value,$expired);
  104. }
  105. /**
  106. * 获取缓存
  107. * @param string $cachename
  108. * @return mixed
  109. */
  110. private function getCache($cachename){
  111. $redis_cache = new RedisCache();
  112. return $redis_cache->get($cachename);
  113. }
  114. }