jsSDK.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace common\components;
  3. use common\services\xhMerchantService;
  4. use linslin\yii2\curl;
  5. use yii\helpers\Json;
  6. class jsSDK
  7. {
  8. public function getMiniSignPackage()
  9. {
  10. $jsapiTicket = $this->getJsApiTicket($merchant);
  11. $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  12. $timestamp = time();
  13. $nonceStr = $this->createNonceStr();
  14. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";// 这里参数的顺序要按照 key 值 ASCII 码升序排序
  15. $signature = sha1($string);
  16. $appId = $merchant['miniAppId'];
  17. $signPackage = array(
  18. "appId" => $appId,
  19. "nonceStr" => $nonceStr,
  20. "timestamp" => $timestamp,
  21. "url" => $url,
  22. "signature" => $signature,
  23. "rawString" => $string
  24. );
  25. return $signPackage;
  26. }
  27. public function getSignPackage($merchant, $url)
  28. {
  29. $jsapiTicket = $this->getJsApiTicket($merchant);
  30. //$url 当前网页的URL,不包含#及其后面部分
  31. $pos = strpos($url, '#');
  32. if ($pos !== false) {
  33. $url = substr($url, 0, $pos);
  34. }
  35. $timestamp = time();
  36. $nonceStr = $this->createNonceStr();
  37. $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";// 这里参数的顺序要按照 key 值 ASCII 码升序排序
  38. $signature = sha1($string);
  39. $appId = !empty($merchant) && !empty($merchant['wxAppId']) ? $merchant['wxAppId'] : '';
  40. $signPackage = array(
  41. "appId" => $appId,
  42. "nonceStr" => $nonceStr,
  43. "timestamp" => $timestamp,
  44. "url" => $url,
  45. "signature" => $signature,
  46. "rawString" => $string
  47. );
  48. return $signPackage;
  49. }
  50. private function createNonceStr($length = 16)
  51. {
  52. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  53. $str = "";
  54. for ($i = 0; $i < $length; $i++) {
  55. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  56. }
  57. return $str;
  58. }
  59. private function getJsApiTicket($merchant)
  60. {
  61. $now = time();
  62. $sjId = $merchant['id'];
  63. if (!empty($merchant) && !empty($merchant['wxJsApiTicket']) && ($now < strtotime($merchant['wxJsApiTicketExpire']))) {
  64. return $merchant['wxJsApiTicket'];
  65. } else {
  66. $accessToken = wxUtil::getAuthorizerAccessToken($merchant);
  67. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
  68. $curl = new curl\Curl();
  69. $result = $curl->get($url);
  70. $res = Json::decode($result);
  71. if (isset($res['ticket'])) {
  72. $expireTime = (int)($now + $res['expires_in']);
  73. $uData['wxJsApiTicket'] = $res['ticket'];
  74. $uData['wxJsApiTicketExpire'] = date("Y-m-d H:i:s", $expireTime);
  75. xhMerchantService::updateById($sjId, $uData);
  76. return $res['ticket'];
  77. }
  78. return '';
  79. }
  80. }
  81. }