QRCode.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * Part of Overtrue\WeChat.
  14. *
  15. * For the full copyright and license information, please view the LICENSE
  16. * file that was distributed with this source code.
  17. *
  18. * @author mingyoung <mingyoungcheung@gmail.com>
  19. * @copyright 2016
  20. *
  21. * @see https://github.com/overtrue
  22. * @see http://overtrue.me
  23. */
  24. namespace EasyWeChat\MiniProgram\QRCode;
  25. use EasyWeChat\MiniProgram\Core\AbstractMiniProgram;
  26. class QRCode extends AbstractMiniProgram
  27. {
  28. const API_GET_WXACODE = 'https://api.weixin.qq.com/wxa/getwxacode';
  29. const API_GET_WXACODE_UNLIMIT = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit';
  30. const API_CREATE_QRCODE = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode';
  31. /**
  32. * Get WXACode.
  33. *
  34. * @param string $path
  35. * @param int $width
  36. * @param bool $autoColor
  37. * @param array $lineColor
  38. *
  39. * @return \Psr\Http\Message\StreamInterface
  40. */
  41. public function getAppCode($path, $width = 430, $autoColor = false, $lineColor = ['r' => 0, 'g' => 0, 'b' => 0])
  42. {
  43. $params = [
  44. 'path' => $path,
  45. 'width' => $width,
  46. 'auto_color' => $autoColor,
  47. 'line_color' => $lineColor,
  48. ];
  49. return $this->getStream(self::API_GET_WXACODE, $params);
  50. }
  51. /**
  52. * Get WXACode unlimit.
  53. *
  54. * @param string $scene
  55. * @param int $width
  56. * @param bool $autoColor
  57. * @param array $lineColor
  58. *
  59. * @return \Psr\Http\Message\StreamInterface
  60. */
  61. public function getAppCodeUnlimit($scene, $width = 430, $autoColor = false, $lineColor = ['r' => 0, 'g' => 0, 'b' => 0])
  62. {
  63. $params = [
  64. 'scene' => $scene,
  65. 'width' => $width,
  66. 'auto_color' => $autoColor,
  67. 'line_color' => $lineColor,
  68. ];
  69. return $this->getStream(self::API_GET_WXACODE_UNLIMIT, $params);
  70. }
  71. /**
  72. * Get app code unlimit.
  73. *
  74. * @param string $scene
  75. * @param string $page
  76. * @param int $width
  77. * @param bool $autoColor
  78. * @param array $lineColor
  79. *
  80. * @return \Psr\Http\Message\StreamInterface
  81. */
  82. public function appCodeUnlimit($scene, $page = null, $width = null, $autoColor = null, $lineColor = null)
  83. {
  84. $params = [
  85. 'scene' => $scene,
  86. 'page' => $page,
  87. 'width' => $width,
  88. 'auto_color' => $autoColor,
  89. 'line_color' => $lineColor,
  90. ];
  91. return $this->getStream(self::API_GET_WXACODE_UNLIMIT, $params);
  92. }
  93. /**
  94. * Create QRCode.
  95. *
  96. * @param string $path
  97. * @param int $width
  98. *
  99. * @return \Psr\Http\Message\StreamInterface
  100. */
  101. public function createQRCode($path, $width = 430)
  102. {
  103. return $this->getStream(self::API_CREATE_QRCODE, compact('path', 'width'));
  104. }
  105. /**
  106. * Get stream.
  107. *
  108. * @param string $endpoint
  109. * @param array $params
  110. *
  111. * @return \Psr\Http\Message\StreamInterface
  112. */
  113. protected function getStream($endpoint, $params)
  114. {
  115. return $this->getHttp()->json($endpoint, $params)->getBody();
  116. }
  117. }