JSSDK3.php.bak 5.9 KB

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