Jssdk.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\common\plugin;
  3. /**
  4. * Created by PhpStorm.
  5. * User: shaoguo
  6. * Date: 2019-02-26
  7. * Time: 12:57
  8. */
  9. class Jssdk
  10. {
  11. private $app_Id;
  12. private $app_Secret;
  13. public function __construct($str='mp_wxconfig') {
  14. $wxconfig=config($str);
  15. $this->app_Id =$wxconfig['appid'];
  16. $this->app_Secret =$wxconfig['secret'];
  17. // dump($wxconfig);
  18. }
  19. public function getSignPackage($url="") {
  20. $jsapiTicket = $this->getJsApiTicket();
  21. // 注意 URL 一定要动态获取,不能 hardcode.
  22. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  23. $timestamp = time();
  24. $nonceStr = $this->createNonceStr();
  25. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  26. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  27. $signature = sha1($string);
  28. $signPackage = array(
  29. "appId" => $this->app_Id,
  30. "nonceStr" => $nonceStr,
  31. "timestamp" => $timestamp,
  32. "url" => $url,
  33. "signature" => $signature,
  34. "rawString" => $string,
  35. "ticket" =>$jsapiTicket
  36. );
  37. return $signPackage;
  38. }
  39. private function createNonceStr($length = 16) {
  40. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  41. $str = "";
  42. for ($i = 0; $i < $length; $i++) {
  43. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  44. }
  45. return $str;
  46. }
  47. private function getJsApiTicket() {
  48. // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
  49. $path=$this->app_Id."jsapi_ticket.json";
  50. if(!file_exists($path)){
  51. file_put_contents($path, '');
  52. chmod($path, 0777);
  53. }
  54. $data = json_decode(file_get_contents($path));
  55. $accessToken = $this->getAccessToken();
  56. // 如果是企业号用以下 URL 获取 ticket
  57. // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
  58. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
  59. if(!empty($data)){
  60. if ($data->expire_time < time()) {
  61. $res = json_decode($this->httpGet($url));
  62. $ticket = $res->ticket;
  63. if ($ticket) {
  64. $data->expire_time = time() + 7000;
  65. $data->jsapi_ticket = $ticket;
  66. $fp = fopen($path, "w");
  67. fwrite($fp, json_encode($data));
  68. fclose($fp);
  69. }
  70. } else {
  71. $ticket = $data->jsapi_ticket;
  72. }
  73. }else{
  74. $res = json_decode($this->httpGet($url));
  75. $ticket = $res->ticket;
  76. if ($ticket) {
  77. $res->expire_time = time() + 7000;
  78. $res->jsapi_ticket = $ticket;
  79. $fp = fopen($path, "w");
  80. fwrite($fp, json_encode($res));
  81. fclose($fp);
  82. }
  83. }
  84. return $ticket;
  85. }
  86. public function getAccessToken($refresh=false) {
  87. // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
  88. $path=$this->app_Id."access_token.json";
  89. if(!file_exists($path)){
  90. file_put_contents($path, '');
  91. chmod($path, 0777);
  92. }
  93. $data = json_decode(file_get_contents($path));
  94. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->app_Id&secret=$this->app_Secret";
  95. if(!is_null($data)){
  96. if ($data->expire_time < time()||$refresh) {
  97. // 如果是企业号用以下URL获取access_token
  98. // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->app_Id&corpsecret=$this->app_Secret";
  99. $res = json_decode($this->httpGet($url));
  100. $access_token = $res->access_token;
  101. $data->expire_time = time() + 7000;
  102. $data->access_token = $access_token;
  103. $fp = fopen($path, "w");
  104. fwrite($fp, json_encode($data));
  105. fclose($fp);
  106. } else {
  107. //检测是否失效
  108. $test_url='https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token='.$data->access_token;
  109. $res = json_decode($this->httpGet($test_url),true);
  110. if(isset($res['errcode'])){
  111. $res = json_decode($this->httpGet($url));
  112. $access_token = $res->access_token;
  113. $data->expire_time = time() + 7000;
  114. $data->access_token = $access_token;
  115. $fp = fopen($path, "w");
  116. fwrite($fp, json_encode($data));
  117. fclose($fp);
  118. }else{
  119. $access_token = $data->access_token;
  120. }
  121. }
  122. }else{
  123. $res = json_decode($this->httpGet($url));
  124. $access_token = $res->access_token;
  125. if ($access_token) {
  126. $res->expire_time = time() + 7000;
  127. $res->access_token = $access_token;
  128. $fp = fopen($path, "w");
  129. fwrite($fp, json_encode($res));
  130. fclose($fp);
  131. }
  132. }
  133. return $access_token;
  134. }
  135. private function httpGet($url) {
  136. $curl = curl_init();
  137. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  138. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  139. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  140. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  141. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  142. // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
  143. curl_setopt($curl, CURLOPT_URL, $url);
  144. $res = curl_exec($curl);
  145. curl_close($curl);
  146. return $res;
  147. }
  148. }