JSSDK2.php 5.8 KB

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