diff --git a/Test/mini-qrc.php b/Test/mini-qrc.php new file mode 100644 index 0000000..55c43dd --- /dev/null +++ b/Test/mini-qrc.php @@ -0,0 +1,20 @@ + 'wx6bb7b70258da09c6', + 'appsecret' => '78b7b8d65bd67b078babf951d4342b42', +]; + +$mini = new WeMini\Qrcode($config); + +echo '
'; +try { +// var_dump($mini->getCode('pages/index?query=1')); +// var_dump($mini->getCodeUnlimit('432432', 'pages/index/index')); +// var_dump($mini->createQrcode('pages/index?query=1')); +} catch (Exception $e) { + var_dump($e->getMessage()); +} diff --git a/WeChat/Contracts/BasicWeChat.php b/WeChat/Contracts/BasicWeChat.php index 2ec029f..1af8ceb 100644 --- a/WeChat/Contracts/BasicWeChat.php +++ b/WeChat/Contracts/BasicWeChat.php @@ -122,6 +122,7 @@ class BasicWeChat * 以GET获取接口数据并转为数组 * @param string $url 接口地址 * @return array + * @throws \WeChat\Exceptions\InvalidResponseException */ protected function httpGetForJson($url) { @@ -133,6 +134,7 @@ class BasicWeChat $this->isTry = true; return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']); } + throw new InvalidResponseException($e->getMessage(), $e->getCode()); } } @@ -142,6 +144,7 @@ class BasicWeChat * @param array $data 请求数据 * @param bool $buildToJson * @return array + * @throws \WeChat\Exceptions\InvalidResponseException */ protected function httpPostForJson($url, array $data, $buildToJson = true) { @@ -149,10 +152,10 @@ class BasicWeChat return Tools::json2arr(Tools::post($url, $buildToJson ? Tools::arr2json($data) : $data)); } catch (InvalidResponseException $e) { if (!$this->isTry && in_array($e->getCode(), ['40014', '40001', '41001', '42001'])) { - $this->delAccessToken(); - $this->isTry = true; + [$this->delAccessToken(), $this->isTry = true]; return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']); } + throw new InvalidResponseException($e->getMessage(), $e->getCode()); } } diff --git a/WeMini/Qrcode.php b/WeMini/Qrcode.php index 217d4d5..7fec035 100644 --- a/WeMini/Qrcode.php +++ b/WeMini/Qrcode.php @@ -15,6 +15,7 @@ namespace WeMini; use WeChat\Contracts\BasicWeChat; +use WeChat\Contracts\Tools; /** * 微信小程序二维码管理 @@ -31,16 +32,17 @@ class Qrcode extends BasicWeChat * @param integer $width 二维码的宽度 * @param bool $auto_color 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 * @param array $line_color auto_color 为 false 时生效 - * @return array + * @return array|string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException */ public function getCode($path, $width = 430, $auto_color = false, $line_color = ["r" => "0", "g" => "0", "b" => "0"]) { - $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN'; + $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN'; $this->registerApi($url, __FUNCTION__, func_get_args()); - $data = ['path' => $path, 'width' => $width, 'auto_color' => false, 'line_color' => $line_color]; - return $this->callPostApi($url, $data, true); + $data = ['path' => $path, 'width' => $width, 'auto_color' => $auto_color, 'line_color' => $line_color]; + $result = Tools::post($url, Tools::arr2json($data)); + return strlen($result) > 256 ? $result : Tools::json2arr($result); } /** @@ -51,16 +53,17 @@ class Qrcode extends BasicWeChat * @param integer $width 二维码的宽度 * @param bool $auto_color 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 * @param array $line_color auto_color 为 false 时生效 - * @return array + * @return array|string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException */ public function getCodeUnlimit($scene, $page, $width = 430, $auto_color = false, $line_color = ["r" => "0", "g" => "0", "b" => "0"]) { $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN'; + $data = ['scene' => $scene, 'width' => $width, 'auto_color' => $auto_color, 'page' => $page, 'line_color' => $line_color]; $this->registerApi($url, __FUNCTION__, func_get_args()); - $data = ['scene' => $scene, 'width' => $width, 'auto_color' => false, 'line_color' => $line_color]; - return $this->callPostApi($url, $data, true); + $result = Tools::post($url, Tools::arr2json($data)); + return strlen($result) > 256 ? $result : Tools::json2arr($result); } /** @@ -68,7 +71,7 @@ class Qrcode extends BasicWeChat * 接口C:适用于需要的码数量较少的业务场景 * @param string $path 不能为空,最大长度 128 字节 * @param integer $width 二维码的宽度 - * @return array + * @return array|string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException */ @@ -76,7 +79,8 @@ class Qrcode extends BasicWeChat { $url = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN'; $this->registerApi($url, __FUNCTION__, func_get_args()); - return $this->callPostApi($url, ['path' => $path, 'width' => $width], true); + $result = Tools::post($url, Tools::arr2json(['path' => urlencode($path), 'width' => $width])); + return strlen($result) > 256 ? $result : Tools::json2arr($result); } } \ No newline at end of file