From 51f61ef9e6f5a6040621460be1f772f3db2d2a9c Mon Sep 17 00:00:00 2001 From: tk Date: Tue, 4 Jun 2024 13:44:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90=E7=B1=BB=09=E5=9E=8B=E3=80=91?= =?UTF-8?q?=EF=BC=9Afix=20=E3=80=90=E4=B8=BB=09=E9=A2=98=E3=80=91=EF=BC=9A?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA=E9=94=99=E8=AF=AF=20?= =?UTF-8?q?=E3=80=90=E6=8F=8F=09=E8=BF=B0=E3=80=91=EF=BC=9A=20=09[?= =?UTF-8?q?=E5=8E=9F=E5=9B=A0]=EF=BC=9A=E4=B8=8D=E7=94=A8Logger::debug=20?= =?UTF-8?q?=E6=9C=89logger=E5=8F=82=E6=95=B0=E5=8D=B3=E5=8F=AF=20=09[?= =?UTF-8?q?=E8=BF=87=E7=A8=8B]=EF=BC=9A=E5=88=A0=E9=99=A4Logger::debug?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=B0=83=E7=94=A8=20=09[=E5=BD=B1=E5=93=8D]?= =?UTF-8?q?=EF=BC=9A=20=E3=80=90=E7=BB=93=09=E6=9D=9F=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动 --- FlyCube/Api/Controller/PayController.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/FlyCube/Api/Controller/PayController.class.php b/FlyCube/Api/Controller/PayController.class.php index b2b9301..bc97ffe 100644 --- a/FlyCube/Api/Controller/PayController.class.php +++ b/FlyCube/Api/Controller/PayController.class.php @@ -84,7 +84,6 @@ class PayController extends PublicController try { $pay = Pay::wechat($this->config)->mini($order); - Logger::debug('Paying...', $order->all()); //打印日志 echo json_encode($pay); } catch (\Exception $e) { echo 'Error: ' . $e->getMessage(); From 4f946f268a61e0473c7e90d3bf425af22e866721 Mon Sep 17 00:00:00 2001 From: tk Date: Tue, 4 Jun 2024 14:43:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E3=80=90=E7=B1=BB=09=E5=9E=8B=E3=80=91?= =?UTF-8?q?=EF=BC=9Arefactor=20=E3=80=90=E4=B8=BB=09=E9=A2=98=E3=80=91?= =?UTF-8?q?=EF=BC=9A=E5=86=8Dpublic=E6=8E=A7=E5=88=B6=E5=99=A8=E9=87=8C=20?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=85=AC=E7=94=A8=E7=9A=84=20=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E6=96=B9=E6=B3=95=20=E3=80=90=E6=8F=8F=09=E8=BF=B0?= =?UTF-8?q?=E3=80=91=EF=BC=9A=20=09[=E5=8E=9F=E5=9B=A0]=EF=BC=9A=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E8=B0=83=E7=94=A8=E5=85=AC=E7=94=A8=E5=85=AC=E7=94=A8?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E6=96=B9=E6=B3=95=20=09[=E8=BF=87=E7=A8=8B]?= =?UTF-8?q?=EF=BC=9A=20=09[=E5=BD=B1=E5=93=8D]=EF=BC=9A=20=E3=80=90?= =?UTF-8?q?=E7=BB=93=09=E6=9D=9F=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动 --- .../Api/Controller/CheckController.class.php | 33 ------------ .../Api/Controller/DownController.class.php | 50 ------------------- .../Api/Controller/LoginController.class.php | 2 - .../Api/Controller/NormalController.class.php | 2 - .../Api/Controller/PayController.class.php | 39 +++------------ .../Api/Controller/PublicController.class.php | 31 ++++++++++++ 6 files changed, 38 insertions(+), 119 deletions(-) delete mode 100644 FlyCube/Api/Controller/DownController.class.php diff --git a/FlyCube/Api/Controller/CheckController.class.php b/FlyCube/Api/Controller/CheckController.class.php index 09d2a4b..f6e12c6 100644 --- a/FlyCube/Api/Controller/CheckController.class.php +++ b/FlyCube/Api/Controller/CheckController.class.php @@ -2,13 +2,8 @@ namespace Api\Controller; -use Firebase\JWT\JWT; -use Firebase\JWT\Key; - class CheckController extends PublicController { - private $openid; //用户id - private $session_key; //用户session_key /** * @description: 子类的初始化 */ @@ -16,34 +11,6 @@ class CheckController extends PublicController { // 调用父类的_initialize方法 parent::_initialize(); - //解构文件头里面的token - $server = isset($_SERVER) ? $_SERVER : ""; - $token = isset($server['HTTP_TOKEN']) ? $server['HTTP_TOKEN'] : null; - $jwtKey = C('jwtKey'); // jwt密钥 - try { - $jwt = JWT::decode($token, new Key($jwtKey, 'HS256')); // 使用密钥和 HS256 算法对 JWT 进行解码 - $res_token = (array) $jwt; // 将解码后的对象转换为数组 - // token过期 - if (empty($res_token)) { - echo json_encode(array('status' => -1, 'msg' => '帐号认证过期!')); - exit(); - } - // token检测通过 获取用户id - $this->openid = $res_token['openid']; - $this->session_key = $res_token['session_key']; - } catch (\UnexpectedValueException $e) { - // 捕获JWT解码错误 - echo json_encode(array('status' => -1, 'msg' => 'Token 无效: ' . $e->getMessage())); - exit(); - } catch (\DomainException $e) { - // 捕获JWT解码错误 - echo json_encode(array('status' => -1, 'msg' => 'Token 解码失败: ' . $e->getMessage())); - exit(); - } catch (\Exception $e) { - // 捕获其他可能的错误 - echo json_encode(array('status' => -1, 'msg' => '未知错误: ' . $e->getMessage())); - exit(); - } } public function index() { diff --git a/FlyCube/Api/Controller/DownController.class.php b/FlyCube/Api/Controller/DownController.class.php deleted file mode 100644 index a1647c5..0000000 --- a/FlyCube/Api/Controller/DownController.class.php +++ /dev/null @@ -1,50 +0,0 @@ - [ - 'default' => [ - 'mch_id' => '1625070753', // 必填-商户号 - 'mch_secret_key' => 'qwertyuiopasdfghjklzxcvbnm123456', // 必填-v3商户秘钥 - 'mch_secret_cert' => 'C:/phpstudy_pro/WWW/cart/apiclient_key.pem', // 必填-商户私钥路径 - 'mch_public_cert_path' => 'C:/phpstudy_pro/WWW/cart/apiclient_cert.pem', // 必填-商户公钥证书路径 - 'notify_url' => 'https://szdot.top', // 回调地址 - 'mini_app_id' => 'wx8347571d6893a383', // 小程序 app_id - ], - ], - ]; - - // 配置 Pay - Pay::config($config); - - // 定义参数 - $params = [ - '_config' => 'default', // 使用默认配置 - ]; - - // 保存证书的目录 - $certPath = 'C:/phpstudy_pro/WWW/cart/Cert'; - - // 创建保存证书的目录,如果不存在 - if (!is_dir($certPath)) { - mkdir($certPath, 0777, true); - } - - // 获取微信平台公钥证书并保存到指定目录 - try { - \Yansongda\Pay\get_wechat_public_certs($params, $certPath); - echo "微信平台公钥证书已成功保存到 $certPath\n"; - } catch (Exception $e) { - echo '获取微信平台公钥证书时发生错误: ' . $e->getMessage() . "\n"; - } - } -} diff --git a/FlyCube/Api/Controller/LoginController.class.php b/FlyCube/Api/Controller/LoginController.class.php index 91d53a7..f2d09f4 100644 --- a/FlyCube/Api/Controller/LoginController.class.php +++ b/FlyCube/Api/Controller/LoginController.class.php @@ -9,8 +9,6 @@ class LoginController extends PublicController */ public function _initialize() { - // 调用父类的_initialize方法 - parent::_initialize(); } /** * @description: 授权登录 未注册过 进行注册之后 登录 diff --git a/FlyCube/Api/Controller/NormalController.class.php b/FlyCube/Api/Controller/NormalController.class.php index 81357ee..b6af9ff 100644 --- a/FlyCube/Api/Controller/NormalController.class.php +++ b/FlyCube/Api/Controller/NormalController.class.php @@ -9,8 +9,6 @@ class NormalController extends PublicController */ public function _initialize() { - // 调用父类的_initialize方法 - parent::_initialize(); } public function index() { diff --git a/FlyCube/Api/Controller/PayController.class.php b/FlyCube/Api/Controller/PayController.class.php index bc97ffe..3dda0f7 100644 --- a/FlyCube/Api/Controller/PayController.class.php +++ b/FlyCube/Api/Controller/PayController.class.php @@ -2,15 +2,17 @@ namespace Api\Controller; -use Firebase\JWT\JWT; -use Firebase\JWT\Key; use Yansongda\Pay\Pay; -use Yansongda\Pay\Logger; class PayController extends PublicController { - private $openid; // 用户openid - private $session_key; // 用户session_key + /** + * @description: 子类的初始化 + */ + public function _initialize() + { + parent::_initialize(); + } protected $config = [ 'wechat' => [ 'default' => [ @@ -38,33 +40,6 @@ class PayController extends PublicController 'connect_timeout' => 5.0, ], ]; - - /** - * @description: 子类的初始化 - */ - public function _initialize() - { - parent::_initialize(); - - // 解析HTTP请求头中的token - if ($_REQUEST['order_sn']) { - //解构文件头里面的token - $server = isset($_SERVER) ? $_SERVER : ""; - $token = $server['HTTP_TOKEN']; - $jwtKey = C('jwtKey'); // jwt密钥 - $jwt = JWT::decode($token, new Key($jwtKey, 'HS256')); // 使用密钥和 HS256 算法对 JWT 进行解码 - $res_token = (array) $jwt; // 将解码后的对象转换为数组 - //token过期 - if (empty($res_token)) { - echo json_encode(array('status' => -1, 'msg' => '帐号认证过期!')); - exit(); - } - //token检测通过 获取用户id - $this->openid = $res_token['openid']; - $this->session_key = $res_token['session_key']; - } - } - /** * @description: 发起小程序支付 */ diff --git a/FlyCube/Api/Controller/PublicController.class.php b/FlyCube/Api/Controller/PublicController.class.php index b68f7a8..b41bcb5 100644 --- a/FlyCube/Api/Controller/PublicController.class.php +++ b/FlyCube/Api/Controller/PublicController.class.php @@ -5,12 +5,43 @@ namespace Api\Controller; use Think\Controller; use Firebase\JWT\JWT; +use Firebase\JWT\Key; class PublicController extends Controller { + protected $openid; // 用户openid + protected $session_key; // 用户session_key //初始化 public function _initialize() { + //解构文件头里面的token + $server = isset($_SERVER) ? $_SERVER : ""; + $token = isset($server['HTTP_TOKEN']) ? $server['HTTP_TOKEN'] : null; + $jwtKey = C('jwtKey'); // jwt密钥 + try { + $jwt = JWT::decode($token, new Key($jwtKey, 'HS256')); // 使用密钥和 HS256 算法对 JWT 进行解码 + $res_token = (array) $jwt; // 将解码后的对象转换为数组 + // token过期 + if (empty($res_token)) { + echo json_encode(array('status' => -1, 'msg' => '帐号认证过期!')); + exit(); + } + // token检测通过 获取用户id + $this->openid = $res_token['openid']; + $this->session_key = $res_token['session_key']; + } catch (\UnexpectedValueException $e) { + // 捕获JWT解码错误 + echo json_encode(array('status' => -1, 'msg' => 'Token 无效: ' . $e->getMessage())); + exit(); + } catch (\DomainException $e) { + // 捕获JWT解码错误 + echo json_encode(array('status' => -1, 'msg' => 'Token 解码失败: ' . $e->getMessage())); + exit(); + } catch (\Exception $e) { + // 捕获其他可能的错误 + echo json_encode(array('status' => -1, 'msg' => '未知错误: ' . $e->getMessage())); + exit(); + } } //远程调取页面 访问API protected function apiUrl($url)