WeixinJsSDK.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Cas\Controller;
  3. use KIF\Cache\Memcached;
  4. use KIF\Core\Controller;
  5. use KIF\Core\Request;
  6. use Cas\Module\RefreshAccessToken;
  7. use KIF\Core\Config;
  8. /**
  9. * WeixinJsSDK 用于微信 js 调用 
  10. * 吐出相应数据
  11. * 可查看:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
  12. *
  13. * @author lihuanchun
  14. *
  15. */
  16. class WeixinJsSDK extends Controller {
  17. private $objMemache;
  18. public function __construct() {
  19. $this->objMemache = new Memcached ();
  20. $wechat_cfg = Config::getInstance ()->get ( 'wechat_cfg' );
  21. $this->appId = $wechat_cfg ['appId'];
  22. $this->appSecret = $wechat_cfg ['appSecret'];
  23. }
  24. /**
  25. * ajax 返回微信所需js配置信息
  26. * http://cas.lishuy.com/?c=WeixinJsSDK&a=AjaxConfigData
  27. */
  28. public function doAjaxConfigData() {
  29. $thisPageUrl = Request::g ( 'thisPageUrl' );
  30. $returnData = $this->getSignPackage ( $thisPageUrl );
  31. $this->ajax_success_exit ( $returnData );
  32. }
  33. /**
  34. * getSignPackage
  35. */
  36. private function getSignPackage($url = null) {
  37. $jsapiTicket = $this->getJsApiTicket ();
  38. if ($url == null) {
  39. $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  40. } else {
  41. $url = htmlspecialchars_decode ( $url );
  42. }
  43. //$url = explode('?', $url);
  44. //$url = $url[0];
  45. $timestamp = time ();
  46. $nonceStr = $this->createNonceStr ();
  47. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  48. $signature = sha1 ( $string );
  49. $signPackage = array (
  50. "appId" => $this->appId,
  51. "nonceStr" => $nonceStr,
  52. "timestamp" => $timestamp,
  53. "url" => $url,
  54. "signature" => $signature,
  55. "rawString" => $string
  56. );
  57. return $signPackage;
  58. }
  59. /**
  60. * createNonceStr
  61. */
  62. private function createNonceStr($length = 16) {
  63. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  64. $str = "";
  65. for($i = 0; $i < $length; $i ++) {
  66. $str .= substr ( $chars, mt_rand ( 0, strlen ( $chars ) - 1 ), 1 );
  67. }
  68. return $str;
  69. }
  70. /**
  71. * getJsApiTicket
  72. */
  73. private function getJsApiTicket() {
  74. $data = json_decode($this->objMemache->get('ticket'));
  75. if ($data->expire_time < time()) {
  76. $accessToken = $this->getAccessToken();
  77. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=' . $accessToken;
  78. //$url = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=' . $accessToken;
  79. $res = json_decode($this->httpGet($url));
  80. $ticket = $res->ticket;
  81. if ($ticket) {
  82. $data->expire_time = time() + 7000;
  83. $data->jsapi_ticket = $ticket;
  84. $this->objMemache->set('ticket', json_encode($data));
  85. }
  86. } else {
  87. $ticket = $data->jsapi_ticket;
  88. }
  89. return $ticket;
  90. }
  91. /*
  92. * (non-PHPdoc)
  93. * @see \KIF\Core\Controller::run()
  94. */
  95. public function run() {
  96. $action = $this->action;
  97. $this->$action ();
  98. }
  99. private function getAccessToken() {
  100. $data = json_decode($this->objMemache->get('token'));
  101. if ($data->expire_time < time()) {
  102. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appId.'&secret=' . $this->appSecret;
  103. //$url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $this->appId . '&corpsecret=' . $this->appSecret;
  104. $res = json_decode($this->httpGet($url));
  105. $access_token = $res->access_token;
  106. if ($access_token) {
  107. $data->expire_time = time() + 7000;
  108. $data->access_token = $access_token;
  109. $this->objMemache->set('token', json_encode($data));
  110. }
  111. } else {
  112. $access_token = $data->access_token;
  113. }
  114. return $access_token;
  115. }
  116. private function httpGet($url) {
  117. $curl = curl_init();
  118. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  119. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  120. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  121. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  122. curl_setopt($curl, CURLOPT_URL, $url);
  123. $res = curl_exec($curl);
  124. curl_close($curl);
  125. return $res;
  126. }
  127. }