From 9464d20af40b24bc06fd146509ad003c17240116 Mon Sep 17 00:00:00 2001
From: Anyon <zoujingli@qq.com>
Date: Thu, 16 May 2019 10:17:20 +0800
Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E8=8E=B7=E5=8F=96=E4=BA=8C?=
 =?UTF-8?q?=E8=BF=9B=E5=88=B6=E8=B5=84=E6=BA=90=E6=8E=A5=E5=8F=A3=E5=A2=9E?=
 =?UTF-8?q?=E5=8A=A0=E9=87=8D=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore                       |  3 ++-
 We.php                           |  2 +-
 WeChat/Contracts/BasicWeChat.php |  8 ++++----
 WeChat/Contracts/MyCurlFile.php  | 12 ------------
 WeChat/Custom.php                |  2 +-
 WeChat/Media.php                 | 12 ++++++++++--
 WeChat/Pay.php                   |  1 +
 WeMini/Qrcode.php                | 19 ++++++++++++++++---
 8 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/.gitignore b/.gitignore
index 3803ef2..7791a86 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,5 @@
 /Cache
 /Test/cert
 /nbproject
-/composer.lock
\ No newline at end of file
+/composer.lock
+/_test/cert
\ No newline at end of file
diff --git a/We.php b/We.php
index 919e35f..a4ffc59 100644
--- a/We.php
+++ b/We.php
@@ -74,7 +74,7 @@ class We
      * 定义当前版本
      * @var string
      */
-    const VERSION = '1.2.9';
+    const VERSION = '1.2.12';
 
     /**
      * 静态配置
diff --git a/WeChat/Contracts/BasicWeChat.php b/WeChat/Contracts/BasicWeChat.php
index da8e2f8..0f9f8b8 100644
--- a/WeChat/Contracts/BasicWeChat.php
+++ b/WeChat/Contracts/BasicWeChat.php
@@ -40,19 +40,19 @@ class BasicWeChat
      * 当前请求方法参数
      * @var array
      */
-    private $currentMethod = [];
+    protected $currentMethod = [];
 
     /**
      * 当前模式
      * @var bool
      */
-    private $isTry = false;
+    protected $isTry = false;
 
     /**
      * 注册代替函数
      * @var string
      */
-    private $GetAccessTokenCallback;
+    protected $GetAccessTokenCallback;
 
     /**
      * BasicWeChat constructor.
@@ -128,7 +128,7 @@ class BasicWeChat
     }
 
     /**
-     * 清理删除accessToken
+     * 清理删除 AccessToken
      * @return bool
      */
     public function delAccessToken()
diff --git a/WeChat/Contracts/MyCurlFile.php b/WeChat/Contracts/MyCurlFile.php
index ff812ab..e81e3da 100644
--- a/WeChat/Contracts/MyCurlFile.php
+++ b/WeChat/Contracts/MyCurlFile.php
@@ -1,17 +1,5 @@
 <?php
 
-// +----------------------------------------------------------------------
-// | WeChatDeveloper
-// +----------------------------------------------------------------------
-// | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
-// +----------------------------------------------------------------------
-// | 官方网站: http://think.ctolog.com
-// +----------------------------------------------------------------------
-// | 开源协议 ( https://mit-license.org )
-// +----------------------------------------------------------------------
-// | github开源项目:https://github.com/zoujingli/WeChatDeveloper
-// +----------------------------------------------------------------------
-
 namespace WeChat\Contracts;
 
 /**
diff --git a/WeChat/Custom.php b/WeChat/Custom.php
index 12e5051..6d03091 100644
--- a/WeChat/Custom.php
+++ b/WeChat/Custom.php
@@ -79,7 +79,7 @@ class Custom extends BasicWeChat
      * @throws Exceptions\InvalidResponseException
      * @throws Exceptions\LocalCacheException
      */
-    public function inviteworker($kf_account, $invite_wx)
+    public function inviteWorker($kf_account, $invite_wx)
     {
         $url = 'https://api.weixin.qq.com/customservice/kfaccount/inviteworker?access_token=ACCESS_TOKEN';
         $this->registerApi($url, __FUNCTION__, func_get_args());
diff --git a/WeChat/Media.php b/WeChat/Media.php
index 33f9a69..8c7ca02 100644
--- a/WeChat/Media.php
+++ b/WeChat/Media.php
@@ -56,7 +56,11 @@ class Media extends BasicWeChat
         $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id={$media_id}";
         $this->registerApi($url, __FUNCTION__, func_get_args());
         $result = Tools::get($url);
-        if (json_decode($result)) {
+        if (is_array($json = json_decode($result, true))) {
+            if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
+                [$this->delAccessToken(), $this->isTry = true];
+                return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
+            }
             return Tools::json2arr($result);
         }
         return is_null($outType) ? $result : $outType($result);
@@ -139,7 +143,11 @@ class Media extends BasicWeChat
         $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
         $this->registerApi($url, __FUNCTION__, func_get_args());
         $result = Tools::post($url, ['media_id' => $media_id]);
-        if (json_decode($result)) {
+        if (is_array($json = json_decode($result, true))) {
+            if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
+                [$this->delAccessToken(), $this->isTry = true];
+                return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
+            }
             return Tools::json2arr($result);
         }
         return is_null($outType) ? $result : $outType($result);
diff --git a/WeChat/Pay.php b/WeChat/Pay.php
index 9045c00..dbbd4a1 100644
--- a/WeChat/Pay.php
+++ b/WeChat/Pay.php
@@ -160,6 +160,7 @@ class Pay extends BasicWePay
      * @param array $options 静音参数
      * @param null|string $outType 输出类型
      * @return bool|string
+     * @throws Exceptions\LocalCacheException
      * @throws InvalidResponseException
      */
     public function billDownload(array $options, $outType = null)
diff --git a/WeMini/Qrcode.php b/WeMini/Qrcode.php
index 7bc351e..fe31299 100644
--- a/WeMini/Qrcode.php
+++ b/WeMini/Qrcode.php
@@ -16,6 +16,7 @@ namespace WeMini;
 
 use WeChat\Contracts\BasicWeChat;
 use WeChat\Contracts\Tools;
+use WeChat\Exceptions\InvalidResponseException;
 
 /**
  * 微信小程序二维码管理
@@ -44,7 +45,11 @@ class Qrcode extends BasicWeChat
         $this->registerApi($url, __FUNCTION__, func_get_args());
         $data = ['path' => $path, 'width' => $width, 'auto_color' => $auto_color, 'line_color' => $line_color, 'is_hyaline' => $is_hyaline];
         $result = Tools::post($url, Tools::arr2json($data));
-        if (json_decode($result)) {
+        if (is_array($json = json_decode($result, true))) {
+            if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
+                [$this->delAccessToken(), $this->isTry = true];
+                return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
+            }
             return Tools::json2arr($result);
         }
         return is_null($outType) ? $result : $outType($result);
@@ -70,7 +75,11 @@ class Qrcode extends BasicWeChat
         $data = ['scene' => $scene, 'width' => $width, 'auto_color' => $auto_color, 'page' => $page, 'line_color' => $line_color, 'is_hyaline' => $is_hyaline];
         $this->registerApi($url, __FUNCTION__, func_get_args());
         $result = Tools::post($url, Tools::arr2json($data));
-        if (json_decode($result)) {
+        if (is_array($json = json_decode($result, true))) {
+            if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
+                [$this->delAccessToken(), $this->isTry = true];
+                return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
+            }
             return Tools::json2arr($result);
         }
         return is_null($outType) ? $result : $outType($result);
@@ -91,7 +100,11 @@ 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());
         $result = Tools::post($url, Tools::arr2json(['path' => $path, 'width' => $width]));
-        if (json_decode($result)) {
+        if (is_array($json = json_decode($result, true))) {
+            if (!$this->isTry && isset($json['errcode']) && in_array($json['errcode'], ['40014', '40001', '41001', '42001'])) {
+                [$this->delAccessToken(), $this->isTry = true];
+                return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
+            }
             return Tools::json2arr($result);
         }
         return is_null($outType) ? $result : $outType($result);