PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/class/jssdk.php

https://gitlab.com/0072016/0072016-wechat
PHP | 94 lines | 61 code | 13 blank | 20 comment | 7 complexity | d1f713ea1af853c9c8774b900613dbc9 MD5 | raw file
  1. <?php
  2. $mypath=$_SERVER['DOCUMENT_ROOT'] . '/wechat';
  3. require_once $mypath.'/class/interfaceHandler.php';
  4. class JSSDK {
  5. private $appId;
  6. private $appSecret;
  7. private $mInterfaceHander=null;
  8. public $weixinId;
  9. public function __construct($weixinId) {
  10. $this->weixinId=$weixinId;
  11. $temp=new interfaceHandler($weixinId);
  12. $this->mInterfaceHander= $temp;
  13. $this->appId = appID;
  14. $this->appSecret = appsecret;
  15. }
  16. public function getSignPackage() {
  17. $jsapiTicket = $this->getJsApiTicket();
  18. // 注意 URL 一定要动态获取,不能 hardcode.
  19. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  20. $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  21. $timestamp = time();
  22. $nonceStr = $this->createNonceStr();
  23. // 这里参数的顺序要按照 key 值 ASCII 码升序排序
  24. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
  25. $signature = sha1($string);
  26. $signPackage = array(
  27. "appId" => $this->appId,
  28. "nonceStr" => $nonceStr,
  29. "timestamp" => $timestamp,
  30. "url" => $url,
  31. "signature" => $signature,
  32. "rawString" => $string
  33. );
  34. return $signPackage;
  35. }
  36. private function createNonceStr($length = 16) {
  37. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  38. $str = "";
  39. for ($i = 0; $i < $length; $i++) {
  40. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  41. }
  42. return $str;
  43. }
  44. private function getJsApiTicket() {
  45. // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
  46. $data = json_decode(file_get_contents($GLOBALS['mypath'].'/tokens/'.$this->weixinId.'_jsapi_ticket.dat'));
  47. if ($data->expire_time < time()) {
  48. // wxlog('ticket timeout: '.(time()-$data->expire_time));
  49. // 如果是企业号用以下 URL 获取 ticket
  50. // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
  51. $this->mInterfaceHander->reflashAccessToken();
  52. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->mInterfaceHander->currentToken;
  53. $res = json_decode($this->mInterfaceHander->getByCurl($url));
  54. $ticket = $res->ticket;
  55. if ($ticket) {
  56. $data->expire_time = time() + 7000;
  57. $data->jsapi_ticket = $ticket;
  58. $data=json_encode($data);
  59. file_put_contents($GLOBALS['mypath'].'/tokens/'.$this->weixinId.'_jsapi_ticket.dat',$data);
  60. }
  61. wxlog('get jsapiTicketOnLine');
  62. } else {
  63. $ticket = $data->jsapi_ticket;
  64. // wxlog('get jsapiTicket from file');
  65. }
  66. return $ticket;
  67. }
  68. // private function httpGet($url) {
  69. // $curl = curl_init();
  70. // curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  71. // curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  72. // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  73. // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  74. // curl_setopt($curl, CURLOPT_URL, $url);
  75. //
  76. // $res = curl_exec($curl);
  77. // curl_close($curl);
  78. //
  79. // return $res;
  80. // }
  81. }