210 lines
6.7 KiB
PHP
210 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\Print;
|
|
|
|
use App\Cache\Redis\Common\CommonRedisKey;
|
|
use App\Cache\Redis\RedisCache;
|
|
use App\Exception\ErrException;
|
|
use App\Extend\StringUtil;
|
|
use App\Lib\Log;
|
|
use Exception;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Hyperf\Guzzle\ClientFactory;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use function Hyperf\Config\config;
|
|
|
|
class YlyBasicsLib
|
|
{
|
|
/**
|
|
* @var RedisCache
|
|
*/
|
|
#[Inject]
|
|
protected RedisCache $redisCache;
|
|
|
|
/**
|
|
* @var ClientFactory
|
|
*/
|
|
#[Inject]
|
|
protected ClientFactory $clientFactory;
|
|
|
|
/**
|
|
* @var Log
|
|
*/
|
|
#[Inject]
|
|
protected Log $log;
|
|
|
|
/**
|
|
* @return false|mixed|\Redis|string
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function selfAppAccredit(): mixed
|
|
{
|
|
$key = CommonRedisKey::yiLianYunSelfAppAccreditKey();
|
|
|
|
if ($this->redisCache->exists($key)) return $this->redisCache->get($key);
|
|
|
|
$signData = $requestData = [
|
|
'client_id' => config('print.yly.client_id'),
|
|
'timestamp' => time()
|
|
];
|
|
|
|
$requestData['sign'] = $this->sign($signData);
|
|
unset($signData);
|
|
$requestData['grant_type'] = config('print.yly.grant_type');
|
|
$requestData['scope'] = config('print.yly.scope');
|
|
$requestData['id'] = $this->getRequestId();
|
|
|
|
$url = config('print.yly.no_dns_request_url').config('print.yly.self_app_accredit_url');
|
|
|
|
try {
|
|
$ylyResponse = $this->clientFactory->create([
|
|
'base_uri' => $url,
|
|
'timeout' => 5
|
|
])->post($url,[
|
|
'headers' => [
|
|
'Content-Type' => 'application/json'
|
|
],
|
|
'body' => json_encode($requestData)
|
|
]);
|
|
|
|
$contents = $ylyResponse->getBody()->getContents();
|
|
|
|
$this->log->callbackLog(__class__.':易联云服务器返回token信息:'.json_encode($contents));
|
|
|
|
$res = json_decode($contents,true);
|
|
|
|
if ($res['error'] != 0 || $res['error_description'] != 'success' || empty($res['body'])) throw new Exception($res['error_description'] ?? '打印系统Token错误');
|
|
}catch (GuzzleException|Exception $e){
|
|
$this->log->debug(__CLASS__.':'.__FUNCTION__.':debug:'.$e->getMessage());
|
|
throw new ErrException($e->getMessage());
|
|
}
|
|
|
|
$this->redisCache->delete($key);
|
|
$this->redisCache->set($key,$res['body']['access_token']);
|
|
$this->redisCache->expire($key,config('print.yly.self_app_token_expired'));
|
|
|
|
return $res['body']['access_token'];
|
|
}
|
|
|
|
/**
|
|
* @param string $token
|
|
* @param string $machineCode
|
|
* @param string $content
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function printTextRequest(string $token,string $machineCode,string $content): void
|
|
{
|
|
$signData = $requestData = [
|
|
'client_id' => config('print.yly.client_id'),
|
|
'timestamp' => time()
|
|
];
|
|
|
|
$requestData['sign'] = $this->sign($signData);
|
|
unset($signData);
|
|
$requestData['id'] = $this->getRequestId();
|
|
$requestData['access_token'] = $token;
|
|
$requestData['machine_code'] = $machineCode;
|
|
$requestData['origin_id'] = StringUtil::randStr(16);
|
|
if (config('print.yly.idempotence') == 1) $requestData['idempotence'] = (int)config('print.yly.idempotence');
|
|
|
|
$requestData['content'] = $content;
|
|
|
|
$url = config('print.yly.no_dns_request_url').config('print.yly.print_text_url');
|
|
|
|
try {
|
|
$ylyResponse = $this->clientFactory->create([
|
|
'base_uri' => $url,
|
|
'timeout' => 5
|
|
])->post($url,[
|
|
'headers' => [
|
|
'Content-Type' => 'application/json'
|
|
],
|
|
'body' => json_encode($requestData)
|
|
]);
|
|
|
|
$contents = $ylyResponse->getBody()->getContents();
|
|
|
|
$this->log->callbackLog(__class__.':易联云打印服务器返回token信息:'.json_encode($contents));
|
|
|
|
$res = json_decode($contents,true);
|
|
|
|
if ($res['error'] != 0 || $res['error_description'] != 'success' || empty($res['body'])) throw new Exception($res['error_description'] ?? '打印错误');
|
|
}catch (GuzzleException|Exception $e){
|
|
$this->log->debug(__CLASS__.':'.__FUNCTION__.':debug:'.$e->getMessage());
|
|
throw new ErrException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $token
|
|
* @param string $machineCode
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function cancelRequest(string $token,string $machineCode): void
|
|
{
|
|
$signData = $requestData = [
|
|
'client_id' => config('print.yly.client_id'),
|
|
'timestamp' => time()
|
|
];
|
|
|
|
$requestData['sign'] = $this->sign($signData);
|
|
unset($signData);
|
|
$requestData['id'] = $this->getRequestId();
|
|
$requestData['access_token'] = $token;
|
|
$requestData['machine_code'] = $machineCode;
|
|
|
|
$url = config('print.yly.no_dns_request_url').config('print.yly.cancel_all_url');
|
|
|
|
try {
|
|
$ylyResponse = $this->clientFactory->create([
|
|
'base_uri' => $url,
|
|
'timeout' => 5
|
|
])->post($url,[
|
|
'headers' => [
|
|
'Content-Type' => 'application/json'
|
|
],
|
|
'body' => json_encode($requestData)
|
|
]);
|
|
|
|
$contents = $ylyResponse->getBody()->getContents();
|
|
|
|
$this->log->callbackLog(__class__.':易联云取消服务器返回token信息:'.json_encode($contents));
|
|
|
|
$res = json_decode($contents,true);
|
|
|
|
if ($res['error'] != 0 || $res['error_description'] != 'success' || empty($res['body'])) throw new Exception($res['error_description'] ?? '取消打印错误');
|
|
}catch (GuzzleException|Exception $e){
|
|
$this->log->debug(__CLASS__.':'.__FUNCTION__.':debug:'.$e->getMessage());
|
|
throw new ErrException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 返回签名 拼接字符串 acb 然后再 md5
|
|
* @param $signRequestData
|
|
* @return string
|
|
*/
|
|
private function sign($signRequestData): string
|
|
{
|
|
$signRequestData[] = config('print.yly.client_secret');
|
|
$signText = implode('', $signRequestData);
|
|
|
|
return md5($signText);
|
|
}
|
|
|
|
/**
|
|
* 获取请求唯一id uuid-v4
|
|
* @return string
|
|
*/
|
|
private function getRequestId(): string
|
|
{
|
|
return StringUtil::generateUUIDv4();
|
|
}
|
|
} |