JSSDK3.php 6.0 KB

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