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.
 
 
 
 
 
 

145 lines
3.5 KiB

  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * QRCode.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\QRCode;
  20. use EasyWeChat\Core\AbstractAPI;
  21. /**
  22. * Class QRCode.
  23. */
  24. class QRCode extends AbstractAPI
  25. {
  26. const DAY = 86400;
  27. const SCENE_MAX_VALUE = 100000;
  28. const SCENE_QR_CARD = 'QR_CARD';
  29. const SCENE_QR_TEMPORARY = 'QR_SCENE';
  30. const SCENE_QR_TEMPORARY_STR = 'QR_STR_SCENE';
  31. const SCENE_QR_FOREVER = 'QR_LIMIT_SCENE';
  32. const SCENE_QR_FOREVER_STR = 'QR_LIMIT_STR_SCENE';
  33. const API_CREATE = 'https://api.weixin.qq.com/cgi-bin/qrcode/create';
  34. const API_SHOW = 'https://mp.weixin.qq.com/cgi-bin/showqrcode';
  35. /**
  36. * Create forever.
  37. *
  38. * @param int $sceneValue
  39. *
  40. * @return \EasyWeChat\Support\Collection
  41. */
  42. public function forever($sceneValue)
  43. {
  44. if (is_int($sceneValue) && $sceneValue > 0 && $sceneValue < self::SCENE_MAX_VALUE) {
  45. $type = self::SCENE_QR_FOREVER;
  46. $sceneKey = 'scene_id';
  47. } else {
  48. $type = self::SCENE_QR_FOREVER_STR;
  49. $sceneKey = 'scene_str';
  50. }
  51. $scene = [$sceneKey => $sceneValue];
  52. return $this->create($type, $scene, false);
  53. }
  54. /**
  55. * Create temporary.
  56. *
  57. * @param string $sceneValue
  58. * @param null $expireSeconds
  59. *
  60. * @return \EasyWeChat\Support\Collection
  61. */
  62. public function temporary($sceneValue, $expireSeconds = null)
  63. {
  64. if (is_int($sceneValue) && $sceneValue > 0) {
  65. $type = self::SCENE_QR_TEMPORARY;
  66. $sceneKey = 'scene_id';
  67. } else {
  68. $type = self::SCENE_QR_TEMPORARY_STR;
  69. $sceneKey = 'scene_str';
  70. }
  71. $scene = [$sceneKey => $sceneValue];
  72. return $this->create($type, $scene, true, $expireSeconds);
  73. }
  74. /**
  75. * Create QRCode for card.
  76. *
  77. * @param array $card
  78. *
  79. * {
  80. * "card_id": "pFS7Fjg8kV1IdDz01r4SQwMkuCKc",
  81. * "code": "198374613512",
  82. * "openid": "oFS7Fjl0WsZ9AMZqrI80nbIq8xrA",
  83. * "expire_seconds": "1800",
  84. * "is_unique_code": false , "outer_id" : 1
  85. * }
  86. *
  87. * @return \EasyWeChat\Support\Collection
  88. */
  89. public function card($card)
  90. {
  91. return $this->create(self::SCENE_QR_CARD, ['card' => $card]);
  92. }
  93. /**
  94. * Return url for ticket.
  95. *
  96. * @param string $ticket
  97. *
  98. * @return string
  99. */
  100. public function url($ticket)
  101. {
  102. return self::API_SHOW."?ticket={$ticket}";
  103. }
  104. /**
  105. * Create a QRCode.
  106. *
  107. * @param string $actionName
  108. * @param array $actionInfo
  109. * @param bool $temporary
  110. * @param int $expireSeconds
  111. *
  112. * @return \EasyWeChat\Support\Collection
  113. */
  114. protected function create($actionName, $actionInfo, $temporary = true, $expireSeconds = null)
  115. {
  116. null !== $expireSeconds || $expireSeconds = 7 * self::DAY;
  117. $params = [
  118. 'action_name' => $actionName,
  119. 'action_info' => ['scene' => $actionInfo],
  120. ];
  121. if ($temporary) {
  122. $params['expire_seconds'] = min($expireSeconds, 30 * self::DAY);
  123. }
  124. return $this->parseJSON('json', [self::API_CREATE, $params]);
  125. }
  126. }