【类 型】:refactor

【主	题】:再public控制器里 调用公用的 构造方法
【描	述】:
	[原因]:统一调用公用公用构造方法
	[过程]:
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
tk 2024-06-04 14:43:23 +08:00
parent 51f61ef9e6
commit 4f946f268a
6 changed files with 38 additions and 119 deletions

View File

@ -2,13 +2,8 @@
namespace Api\Controller; namespace Api\Controller;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class CheckController extends PublicController class CheckController extends PublicController
{ {
private $openid; //用户id
private $session_key; //用户session_key
/** /**
* @description: 子类的初始化 * @description: 子类的初始化
*/ */
@ -16,34 +11,6 @@ class CheckController extends PublicController
{ {
// 调用父类的_initialize方法 // 调用父类的_initialize方法
parent::_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() public function index()
{ {

View File

@ -1,50 +0,0 @@
<?php
namespace Api\Controller;
use Think\Controller;
use Yansongda\Pay\Pay;
class DownController extends Controller
{
public function index()
{
// 配置微信支付参数
$config = [
'wechat' => [
'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";
}
}
}

View File

@ -9,8 +9,6 @@ class LoginController extends PublicController
*/ */
public function _initialize() public function _initialize()
{ {
// 调用父类的_initialize方法
parent::_initialize();
} }
/** /**
* @description: 授权登录 未注册过 进行注册之后 登录 * @description: 授权登录 未注册过 进行注册之后 登录

View File

@ -9,8 +9,6 @@ class NormalController extends PublicController
*/ */
public function _initialize() public function _initialize()
{ {
// 调用父类的_initialize方法
parent::_initialize();
} }
public function index() public function index()
{ {

View File

@ -2,15 +2,17 @@
namespace Api\Controller; namespace Api\Controller;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Yansongda\Pay\Pay; use Yansongda\Pay\Pay;
use Yansongda\Pay\Logger;
class PayController extends PublicController class PayController extends PublicController
{ {
private $openid; // 用户openid /**
private $session_key; // 用户session_key * @description: 子类的初始化
*/
public function _initialize()
{
parent::_initialize();
}
protected $config = [ protected $config = [
'wechat' => [ 'wechat' => [
'default' => [ 'default' => [
@ -38,33 +40,6 @@ class PayController extends PublicController
'connect_timeout' => 5.0, '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: 发起小程序支付 * @description: 发起小程序支付
*/ */

View File

@ -5,12 +5,43 @@ namespace Api\Controller;
use Think\Controller; use Think\Controller;
use Firebase\JWT\JWT; use Firebase\JWT\JWT;
use Firebase\JWT\Key;
class PublicController extends Controller class PublicController extends Controller
{ {
protected $openid; // 用户openid
protected $session_key; // 用户session_key
//初始化 //初始化
public function _initialize() 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 //远程调取页面 访问API
protected function apiUrl($url) protected function apiUrl($url)