This commit is contained in:
szdot 2024-06-04 14:47:32 +08:00
commit 93d359aa19
6 changed files with 38 additions and 120 deletions

View File

@ -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()
{

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()
{
// 调用父类的_initialize方法
parent::_initialize();
}
/**
* @description: 授权登录 未注册过 进行注册之后 登录

View File

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

View File

@ -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: 发起小程序支付
*/
@ -84,7 +59,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();

View File

@ -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)