You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

162 lines
3.5 KiB

  1. <?php
  2. namespace Kuxin\Weixin;
  3. use Kuxin\DI;
  4. use Kuxin\Helper\Http;
  5. use Kuxin\Helper\Url;
  6. use Kuxin\Response;
  7. /**
  8. * 微信 JSSDK.
  9. */
  10. class Js extends Weixin
  11. {
  12. /**
  13. * 当前URL.
  14. *
  15. * @var string
  16. */
  17. protected $url;
  18. const API_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi';
  19. /**
  20. * 获取JSSDK的配置数组.
  21. *
  22. * @param array $APIs
  23. * @param bool $debug
  24. * @param bool $json
  25. *
  26. * @return string|array
  27. */
  28. public function config(array $APIs, $debug = false, $json = true)
  29. {
  30. $signPackage = $this->getSignaturePackage($debug);
  31. $base = array(
  32. 'debug' => $debug,
  33. );
  34. $config = array_merge($base, $signPackage, array('jsApiList' => $APIs));
  35. return $json ? json_encode($config) : $config;
  36. }
  37. /**
  38. * 获取数组形式的配置.
  39. *
  40. * @param array $APIs
  41. * @param bool $debug
  42. *
  43. * @return array
  44. */
  45. public function getConfigArray(array $APIs, $debug = false)
  46. {
  47. return $this->config($APIs, $debug, false);
  48. }
  49. /**
  50. * 获取jsticket.
  51. *
  52. * @return string
  53. */
  54. public function getTicket()
  55. {
  56. $key = 'kuxin.weixin.jsapi_ticket.' . $this->appId;
  57. $data = DI::Cache()->get($key);
  58. if (!$data || empty($data['ticket'])) {
  59. $token = $this->getToken();
  60. $data = $this->parseJSON(Http::get(self::API_TICKET, ['access_token' => $token]));
  61. if ($data && !empty($data['ticket'])) {
  62. DI::Cache()->set($key, $data, $data['expires_in']/10);
  63. } else {
  64. $data['ticket'] = null;
  65. }
  66. }
  67. return $data['ticket'];
  68. }
  69. /**
  70. * 签名.
  71. *
  72. * @param bool $debug
  73. * @param string $url
  74. * @param string $nonce
  75. * @param int $timestamp
  76. *
  77. * @return array
  78. */
  79. public function getSignaturePackage($debug=false,$url = null, $nonce = null, $timestamp = null)
  80. {
  81. $url = $url ? $url : $this->getUrl();
  82. $nonce = $nonce ? $nonce : $this->getNonce();
  83. $timestamp = $timestamp ? $timestamp : time();
  84. $ticket = $this->getTicket();
  85. $sign = [
  86. 'appId' => $this->appId,
  87. 'nonceStr' => $nonce,
  88. 'timestamp' => $timestamp,
  89. 'signature' => $this->getSignature($ticket, $nonce, $timestamp, $url),
  90. ];
  91. if($debug){
  92. $sign['url']=$url;
  93. $sign['ticket']=$ticket;
  94. }
  95. return $sign;
  96. }
  97. /**
  98. * 生成签名.
  99. *
  100. * @param string $ticket
  101. * @param string $nonce
  102. * @param int $timestamp
  103. * @param string $url
  104. *
  105. * @return string
  106. */
  107. public function getSignature($ticket, $nonce, $timestamp, $url)
  108. {
  109. return sha1("jsapi_ticket={$ticket}&noncestr={$nonce}&timestamp={$timestamp}&url={$url}");
  110. }
  111. /**
  112. * 设置当前URL.
  113. *
  114. * @param string $url
  115. *
  116. * @return Js
  117. */
  118. public function setUrl($url)
  119. {
  120. $this->url = $url;
  121. return $this;
  122. }
  123. /**
  124. * 获取当前URL.
  125. *
  126. * @return string
  127. */
  128. public function getUrl()
  129. {
  130. if ($this->url) {
  131. return $this->url;
  132. }
  133. return Url::weixin();
  134. }
  135. /**
  136. * 获取随机字符串.
  137. *
  138. * @return string
  139. */
  140. public function getNonce()
  141. {
  142. return uniqid('kxcms_');
  143. }
  144. }