首页> 实战笔录 >PHP开发笔记 >Hyperf Hyperf
Hyperf3.1使用PandaDoc插件包封装一个API服务类
作者:小萝卜 2025-07-05 【 Hyperf PandaDoc 】 浏览 16
简介Hyperf3.1使用PandaDoc插件包封装一个API服务类
GitHub地址:https://github.com/PandaDoc/pandadoc-api-php-client/tree/main
安装插件
composer require pandadoc/php-client
<?php
declare(strict_types=1);
/**
* PandaDocService
*
* @author Alen
* @date 2025/4/22 14:34
*/
namespace App\Common\Lib\PandaDoc;
use GuzzleHttp\Client;
use Hyperf\Collection\Arr;
use Hyperf\Coroutine\Coroutine;
use PandaDoc\Client\Configuration;
use PandaDoc\Client\Api\DocumentsApi;
use PandaDoc\Client\Api\TemplatesApi;
use PandaDoc\Client\Model\DocumentCreateLinkRequest;
class PandaDocService
{
/**
* 初始化SDK
* @return DocumentsApi
* @author Alen
* @date 2025/4/22 14:37
*/
protected function _initPandaSdk()
{
$config = Configuration::getDefaultConfiguration()->setApiKey('Authorization', config('pandadoc.api_key'));
$config = $config->setApiKeyPrefix('Authorization', 'API-Key');
return new DocumentsApi(new Client(), $config);
}
protected function _initPandaSdk11()
{
$config = Configuration::getDefaultConfiguration()->setApiKey('Authorization', config('pandadoc.api_key'));
$config = $config->setApiKeyPrefix('Authorization', 'API-Key');
return new TemplatesApi(new Client(), $config);
}
/**
* 合成并创建文档
* @param $data
* @return object|\PandaDoc\Client\Model\DocumentCreateResponse
* @throws \PandaDoc\Client\ApiException
* @author Alen
* @date 2025/4/22 14:41
*/
public function createDocument($data, $templateName):mixed
{
$apiInstance = $this->_initPandaSdk();
$templateId = config('pandadoc.templates.' . $templateName, '');
if(!$templateId){
throw newBusinessException(10020, crmTrans(':TEMP template not found',['TEMP'=>$templateName]));
}
Arr::set($data, 'template_uuid', $templateId);
return $apiInstance->createDocument($data, 'ev2');
}
/**
* 根据模板id生成文档
* @param $data
* @return object|\PandaDoc\Client\Model\DocumentCreateResponse
* @throws \PandaDoc\Client\ApiException
* @author Alen
* @date 2025/4/22 14:41
*/
public function createDocumentByTempId($data, $templateId):mixed
{
$apiInstance = $this->_initPandaSdk();
Arr::set($data, 'template_uuid', $templateId);
return $apiInstance->createDocument($data, 'ev2');
}
/**
* 获取文档状态
* @param $documentId
* @return object|\PandaDoc\Client\Model\DocumentStatusResponse
* @throws \PandaDoc\Client\ApiException
* @author Alen
* @date 2025/4/22 18:21
*/
public function getCreatedStatus($documentId){
$apiInstance = $this->_initPandaSdk();
return $apiInstance->statusDocument($documentId);
}
/**
* 下载文档
* @param $documentId
* @return object|\PandaDoc\Client\Model\DocumentCreateLinkResponse
* @throws \PandaDoc\Client\ApiException
* @author Alen
* @date 2025/4/22 16:53
*/
public function downloadDocument($documentId){
$apiInstance = $this->_initPandaSdk();
return $apiInstance->downloadDocument($documentId);
}
/**
* 同步下载文件
*
* @param string $documentId
* @return string
* @author Vito
* @date 2025-05-12
*/
public function syncDownloadDocument($documentId):string
{
$apiInstance = $this->_initPandaSdk();
$isCompleted = false;
for($i=0;$i<10;$i++){
// 查询当前文件状态
$info = $apiInstance->statusDocument($documentId);
if($info->getStatus() != 'document.completed'){
Coroutine::sleep(2);
continue;
}else{
$isCompleted = true;
break;
}
}
if(!$isCompleted){
throw newBusinessException(10000, crmTrans('The strategy lpoa does not complete'));
}
$splFileObj = $apiInstance->downloadDocument($documentId);
// 获取完整名
$tmpOrigin = $splFileObj->getPathname();
// 重命名
rename($tmpOrigin, $tmpOrigin . '.pdf');
// 返回
return $tmpOrigin . '.pdf';
}
/**
* 生成文档详情
*
* @param string $documentId
* @return mixed
* @author Vito
* @date 2025-05-12
*/
public function detailsDocument($documentId):mixed
{
$apiInstance = $this->_initPandaSdk();
return $apiInstance->detailsDocument($documentId);
}
/**
* 发送文档
* @param $data
* @return object|\PandaDoc\Client\Model\DocumentCreateResponse
* @throws \PandaDoc\Client\ApiException
* @author Alen
* @date 2025/4/22 14:41
*/
public function sendDocument($documentId):mixed
{
$apiInstance = $this->_initPandaSdk();
return $apiInstance->sendDocument(
$documentId,
new \PandaDoc\Client\Model\DocumentSendRequest(['silent' => true])
);
}
/**
* 创建文档的连接
*
* @param [type] $documentId
* @return mixed
* @author Vito
* @date 2025-06-12
*/
public function createDocumentLink($documentId, DocumentCreateLinkRequest $createLinkRequest):mixed
{
$apiInstance = $this->_initPandaSdk();
return $apiInstance->createDocumentLink($documentId, $createLinkRequest);
}
/**
* 获取模板详情
*
* @param [type] $documentId
* @return mixed
* @author Vito
* @date 2025-06-12
*/
public function detailsTemplate($templateId):mixed
{
$apiInstance = $this->_initPandaSdk11();
return $apiInstance->detailsTemplate($templateId);
}
/**
* @return mixed
* @throws \PandaDoc\Client\ApiException
* @author Luobo
* @date 2025/06/30 11:05
* @desc 获取模板列表(不做分页和赛选)
*/
public function listTemplates():mixed
{
$apiInstance = $this->_initPandaSdk11();
return $apiInstance->listTemplates();
}
}
很赞哦! (0)