Qrcode.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. namespace WeMini;
  14. use WeChat\Contracts\BasicWeChat;
  15. use WeChat\Contracts\Tools;
  16. /**
  17. * 微信小程序二维码管理
  18. * Class Qrcode
  19. * @package WeMini
  20. */
  21. class Qrcode extends BasicWeChat
  22. {
  23. /**
  24. * 获取小程序码(永久有效)
  25. * 接口A: 适用于需要的码数量较少的业务场景
  26. * @param string $path 不能为空,最大长度 128 字节
  27. * @param integer $width 二维码的宽度
  28. * @param bool $auto_color 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
  29. * @param array $line_color auto_color 为 false 时生效
  30. * @param boolean $is_hyaline 是否需要透明底色
  31. * @param null|string $outType 输出类型
  32. * @return array|string
  33. * @throws \WeChat\Exceptions\InvalidResponseException
  34. * @throws \WeChat\Exceptions\LocalCacheException
  35. */
  36. public function createMiniPath($path, $width = 430, $auto_color = false, $line_color = ["r" => "0", "g" => "0", "b" => "0"], $is_hyaline = true, $outType = null)
  37. {
  38. $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN';
  39. $this->registerApi($url, __FUNCTION__, func_get_args());
  40. $data = ['path' => $path, 'width' => $width, 'auto_color' => $auto_color, 'line_color' => $line_color, 'is_hyaline' => $is_hyaline];
  41. $result = Tools::post($url, Tools::arr2json($data));
  42. if (is_array($json = json_decode($result, true))) {
  43. if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
  44. [$this->delAccessToken(), $this->isTry = true];
  45. return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
  46. }
  47. return Tools::json2arr($result);
  48. }
  49. return is_null($outType) ? $result : $outType($result);
  50. }
  51. /**
  52. * 获取小程序码(永久有效)
  53. * 接口B:适用于需要的码数量极多的业务场景
  54. * @param string $scene 最大32个可见字符,只支持数字
  55. * @param string $page 必须是已经发布的小程序存在的页面
  56. * @param integer $width 二维码的宽度
  57. * @param bool $auto_color 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
  58. * @param array $line_color auto_color 为 false 时生效
  59. * @param boolean $is_hyaline 是否需要透明底色
  60. * @param null|string $outType 输出类型
  61. * @return array|string
  62. * @throws \WeChat\Exceptions\InvalidResponseException
  63. * @throws \WeChat\Exceptions\LocalCacheException
  64. */
  65. public function createMiniScene($scene, $page, $width = 430, $auto_color = false, $line_color = ["r" => "0", "g" => "0", "b" => "0"], $is_hyaline = true, $outType = null)
  66. {
  67. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN';
  68. $data = ['scene' => $scene, 'width' => $width, 'auto_color' => $auto_color, 'page' => $page, 'line_color' => $line_color, 'is_hyaline' => $is_hyaline];
  69. $this->registerApi($url, __FUNCTION__, func_get_args());
  70. $result = Tools::post($url, Tools::arr2json($data));
  71. if (is_array($json = json_decode($result, true))) {
  72. if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
  73. [$this->delAccessToken(), $this->isTry = true];
  74. return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
  75. }
  76. return Tools::json2arr($result);
  77. }
  78. return is_null($outType) ? $result : $outType($result);
  79. }
  80. /**
  81. * 获取小程序二维码(永久有效)
  82. * 接口C:适用于需要的码数量较少的业务场景
  83. * @param string $path 不能为空,最大长度 128 字节
  84. * @param integer $width 二维码的宽度
  85. * @param null|string $outType 输出类型
  86. * @return array|string
  87. * @throws \WeChat\Exceptions\InvalidResponseException
  88. * @throws \WeChat\Exceptions\LocalCacheException
  89. */
  90. public function createDefault($path, $width = 430, $outType = null)
  91. {
  92. $url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN';
  93. $this->registerApi($url, __FUNCTION__, func_get_args());
  94. $result = Tools::post($url, Tools::arr2json(['path' => $path, 'width' => $width]));
  95. if (is_array($json = json_decode($result, true))) {
  96. if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
  97. [$this->delAccessToken(), $this->isTry = true];
  98. return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
  99. }
  100. return Tools::json2arr($result);
  101. }
  102. return is_null($outType) ? $result : $outType($result);
  103. }
  104. }