Qrcode.php 5.8 KB

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