webman_ad/generated/GRPC/Auth/AuthClient.php
2025-01-06 10:47:22 +08:00

85 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace GRPC\Auth;
use Grpc\BaseStub;
use Grpc\UnaryCall;
use Grpc\Channel;
use Grpc\ChannelCredentials;
use GRPC\Auth\ValidateJwtTokenReq;
use GRPC\Auth\ValidateJwtTokenResp;
/**
* AuthClient 是通过手动实现的 GRPC 客户端类
*/
class AuthClient extends BaseStub
{
/**
* AuthClient 构造函数
*
* @param string $hostname 服务端地址(如 "192.168.21.27:22101"
* @param array $opts 客户端选项(可选)
* @param mixed $channel GRPC 通道(可选,默认使用明文连接)
*/
public function __construct(string $hostname, array $opts = [], $channel = null)
{
if ($channel === null) {
// 使用明文连接
$channel = new Channel($hostname, [
'credentials' => ChannelCredentials::createInsecure()
]);
}
parent::__construct($hostname, $opts, $channel);
}
/**
* 调用 ValidateJwtToken 方法
*
* @param ValidateJwtTokenReq $request 请求数据
* @param array $metadata 元数据(可选)
* @param array $options 调用选项(可选)
*
* @return ValidateJwtTokenResp
*/
public function ValidateJwtToken(
ValidateJwtTokenReq $request,
array $metadata = [],
array $options = []
): ValidateJwtTokenResp
{
try {
// 调试:输出请求数据
// dump('aaa',$request->getJwtToken());
// 调用 GRPC 服务
$unaryCall = $this->_simpleRequest(
'/auth.Auth/ValidateJwtToken', // 方法的完整路径
$request,
['\GRPC\Auth\ValidateJwtTokenResp', 'decode'], // 解码器
$metadata,
$options
);
// 使用 wait() 获取返回值
list($response, $status) = $unaryCall->wait();
// 调试:输出 GRPC 状态码
dump($status);
// 检查请求是否成功
if ($status->code !== \Grpc\STATUS_OK) {
throw new \Exception('GRPC call failed with status: ' . $status->details, $status->code);
}
// 返回解析后的响应数据
return $response;
} catch (\Exception $e) {
// 捕获异常并打印
dump($e->getMessage());
throw $e; // 继续抛出异常
}
}
}