JSSDK.php.bak 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: TianFeng
  5. * Date: 2019-03-25
  6. * Time: 13:18
  7. */
  8. class JSSDK{
  9. # - 公众号appid ,公众号开发配置处可查看
  10. public $appId;
  11. # - 公众号appi , 公众号开发配置处可查看
  12. private $appSecret;
  13. /**
  14. * @name 初始化参数
  15. * @author cq <just_leaf@foxmail.com>
  16. * @copyright zydbbt 2018-10-27
  17. */
  18. public function __construct( $appId , $appSecret ) {
  19. $this -> appId = $appId;
  20. $this -> appSecret = $appSecret;
  21. }
  22. /**
  23. * @name 获取accessToken
  24. * @author cq <just_leaf@foxmail.com>
  25. * @copyright zydbbt 2018-10-27
  26. */
  27. public function getAcc(){
  28. return $this -> getAccessToken();
  29. }
  30. /**
  31. * @name 获取config接口注入权限验证配置
  32. * @author cq <just_leaf@foxmail.com>
  33. * @copyright zydbbt 2018-10-27
  34. */
  35. public function getWxConfig() {
  36. # - 获取 jsapi_ticket
  37. $jsapiTicket = $this -> getJsApiTicket();
  38. # - 获取调用页面的url
  39. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  40. $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  41. # - 时间戳
  42. $timestamp = time();
  43. # - 获取随机字符串
  44. $nonceStr = $this -> createNonceStr();
  45. # - 这里参数的顺序要按照 key 值 ASCII 码升序排序
  46. # - 亦可把参数以数组存值,ksort() - 以升序对关联数组进行排序
  47. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  48. # - sha1获取签名
  49. $signature = sha1($string);
  50. # - 页面所需注入参数
  51. $WxConfig = array(
  52. "appId" => $this -> appId,
  53. "nonceStr" => $nonceStr,
  54. "timestamp" => $timestamp,
  55. "url" => $url,
  56. "signature" => $signature,
  57. "rawString" => $string
  58. );
  59. # - 返回
  60. return $WxConfig;
  61. }
  62. /**
  63. * @name 获取JsApiTicket
  64. * @author cq <just_leaf@foxmail.com>
  65. * @copyright zydbbt 2018-10-27
  66. */
  67. private function getJsApiTicket() {
  68. # - 判断缓存
  69. $ticket = S('ticket');
  70. if(!$ticket) {
  71. # - 获取
  72. $accessToken = $this->getAccessToken();
  73. # - 获取Ticket
  74. # - 如果是企业号用以下 URL 获取 ticket
  75. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
  76. # - get请求,转换数组
  77. $result = json_decode($this->httpGet($url),true);
  78. $ticket = $result['ticket'];
  79. # - 全局缓存
  80. if ($ticket) {
  81. # - 官方返回
  82. # - {
  83. # - "errcode":0,
  84. # - "errmsg":"ok",
  85. # - "ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
  86. # - "expires_in":7200
  87. # - }
  88. S('ticket',$ticket,$result['expires_in']);
  89. }
  90. }
  91. # - 返回
  92. return $ticket;
  93. }
  94. /**
  95. * @name 获取AccessToken
  96. * @author cq <just_leaf@foxmail.com>
  97. * @copyright zydbbt 2018-10-27
  98. */
  99. private function getAccessToken() {
  100. # - 判断缓存
  101. $access_token = S('accesToken');
  102. if(!$access_token) {
  103. # - 如果是企业号用以下URL获取access_token
  104. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
  105. # - get请求,转换数组
  106. $result = json_decode($this->httpGet($url),true);
  107. $access_token = $result['access_token'];
  108. # - 全局缓存
  109. if ($access_token) {
  110. S('accesToken',$result['access_token'],$result['expires_in']);
  111. }
  112. }
  113. # - 返回
  114. return $access_token;
  115. }
  116. /**
  117. * @name GET请求
  118. * @author cq <just_leaf@foxmail.com>
  119. * @copyright zydbbt 2018-10-27
  120. */
  121. private function httpGet($url) {
  122. # - 初始化
  123. $curl = curl_init();
  124. # - 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  125. # - 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  126. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
  127. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
  128. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  129. curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  130. curl_setopt($curl, CURLOPT_URL, $url);
  131. # - 请求
  132. $res = curl_exec($curl);
  133. # - 关闭
  134. curl_close($curl);
  135. # - 返回
  136. return $res;
  137. }
  138. /**
  139. * @name 产生随机字符串
  140. * @author cq <just_leaf@foxmail.com>
  141. * @copyright zydbbt 2018-10-27
  142. */
  143. private function createNonceStr($length = 16) {
  144. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  145. $str = "";
  146. for ($i = 0; $i < $length; $i++) {
  147. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  148. }
  149. return $str;
  150. }
  151. }