new-admin-api/app/common/services/FaDaDaServices.php

294 lines
10 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 app\common\services;
use app\services\BaseServices;
use think\exception\ValidateException;
class FaDaDaServices extends BaseServices{
private $appid = '80001491';
private $appSecret = '84CDJF98RUP2EOBDYSF8CQTYTXEJIX1I';
private $apiLink = 'https://uat-api.fadada.com/api/v5/';// 生产https://api.fadada.com/api/v5/ 测试https://uat-api.fadada.com/api/v5/
// 发起请求
private function requestApi(array $bizContent = [],string $apiName = '',string $requestType = 'api'){
try {
// 通用header信息
$millisecond = '1719570699896';//(string)round(microtime(true) * 1000);
$signData = [
'X-FASC-App-Id' => $this->appid,
'X-FASC-Sign-Type' => 'HMAC-SHA256',
'X-FASC-Timestamp' => $millisecond,
'X-FASC-Nonce' => '1719570699896n667e910bdab4e',//uniqid($millisecond.'n'),
'X-FASC-Api-SubVersion' => '5.1',
// 'bizContent' => $bizContent ? json_encode($bizContent) : '',
];
$header = [
'Content-Type:application/x-www-form-urlencoded',
'Cache-Control:no-cache',
'Pragma:no-cache',
// 文档要求信息
'X-FASC-App-Id:'.$signData['X-FASC-App-Id'],
'X-FASC-Sign-Type:'.$signData['X-FASC-Sign-Type'],
'X-FASC-Timestamp:'.$signData['X-FASC-Timestamp'],
'X-FASC-Nonce:'.$signData['X-FASC-Nonce'],
'X-FASC-Api-SubVersion:'.$signData['X-FASC-Api-SubVersion']
];
// 根据请求类型 使用不同的header信息
if($requestType == 'access_token') {
$header[] = 'X-FASC-Grant-Type:client_credential';
$signData['X-FASC-Grant-Type'] = 'client_credential';
}
else {
$accessToken = $this->getAccessToken();
$header[] = 'X-FASC-AccessToken:'.$accessToken;
$signData['X-FASC-AccessToken'] = $accessToken;
}
// 获取签名
$header[] = 'X-FASC-Sign:'.$this->getSign($signData, $millisecond);
// 发起请求
$result = curlPost($this->apiLink.$apiName, [],30, $header);
debug($result);
return $result;
} catch (\Exception $e) {
// Log::info('法大大 - 错误: ' . $e->getMessage());
throw new ValidateException('法大大请求失败:' . $e->getMessage());
}
}
// 获取签名
private function getSign($data, $millisecond){
// 对排序后的参数字符串计算摘要sha256Hex
krsort($data);
$signStr = '';
foreach($data as $key => $value){
if(!empty($value) && $value != '') $signStr .= $key . '=' . $value . '&';
}
$signText = hash('sha256', $signStr);
debug([$signText,$data,$millisecond]);
$secretSigning = hash_hmac('sha256', $this->appSecret, $millisecond, true);
// 用时间戳计算临时签名密钥
// $secretSigning = hash_hmac('sha256', $millisecond, mb_convert_encoding($this->appSecret, 'UTF-8'));
// $signature = strtolower(bin2hex(hash_hmac('sha256', $signText, $secretSigning)));
return strtolower(bin2hex(hash_hmac('sha256', $secretSigning, $signText, TRUE)));
// // 对排序后的参数字符串计算摘要sha256Hex
// String signText = sha256Hex(paramToSignStr);
// // 用时间戳计算临时签名密钥
// byte[] secretSigning = hmac256((appSecret).getBytes(UTF8), timestamp);
// // 计算参数签名并统一转换成小写
// String signature = DatatypeConverter.printHexBinary(hmac256(secretSigning, signText)).toLowerCase();
return $signature;
}
// 获取访问凭证
private function getAccessToken(){
$result = $this->requestApi([],'/service/get-access-token', 'access_token');
debug(['access_token' => $result]);
// debug("获取访问凭证");
return '';
}
// 获取个人授权链接
public function getAuthUrl(){
$bizContent = [
// 个人用户在应用中的唯一标识由集成方自定义长度最大64个字符。
'clientUserId' => '',
];
$result = $this->requestApi($bizContent);
debug([
'获取个人授权链接',
'请求信息'=>$bizContent,
'返回结果'=>$result,
]);
}
// 创建签署任务(基于签署任务模板)
public function createWithTemplate(){
$bizContent = [
// 该签署任务的发起方(扣费主体),需检查授权
'initiator' => [
// 主体类型corp: 企业person: 个人
'idType' => '',
// 主体标识长度最大64个字符
// 如果idType为corp代表应用系统上的企业用户主体方是openCorpId所指定的企业
// 如果idType为person代表应用系统上的个人用户主体方是openUserId所指定的个人
'openId' => '',
],
// 签署任务主题。长度最大100个字符
'signTaskSubject' => '',
// 指定签署任务模板ID
'signTemplateId' => '',
// 是否自动提交签署任务
'autoStart' => true,
// 是否自动定稿填写内容(在有填写参与方时生效)
'autoFillFinalize' => true,
// 参与方列表 参与方信息
'actors' => [
[
'actor' => [
// 参与方标识。在同一个签署任务中,参与方标识唯一,不允许重复;例如:甲方、乙方、丙方
'actorId' => '',
// 参与方主体类型: corp: 企业person: 个人
'actorType' => '',
// 参与方具体名称。长度最大128个字符。
'actorName' => '',
// 参与方权限 fill填写sign签署cc抄送
'permissions' => ['fill','sign'],
// 参与方主体在应用上的OpenIdopenCorpId/openUserId
'actorOpenId' => '',
],
]
],
];
$result = $this->requestApi($bizContent);
debug([
'创建签署任务(基于签署任务模板)',
'请求信息'=>$bizContent,
'返回结果'=>$result,
]);
}
// 获取参与方签署链接
public function actorGetUrl(){
$bizContent = [
// 签署任务ID
'signTaskId' => '',
// 参与方标识。在同一个签署任务中,参与方标识唯一,不允许重复;例如:甲方、乙方、丙方
'actorId' => '',
// 应用系统中唯一确定登录用户身份的标识
'clientUserId' => '',
// 重定向地址,系统判断在非小程序环境下会跳转至该地址。
// 'redirectUrl' => '',
// 小程序的重定向地址(微信和支付宝)
'redirectMiniAppUrl' => '',
];
$result = $this->requestApi($bizContent);
debug([
'获取参与方签署链接',
'请求信息'=>$bizContent,
'返回结果'=>$result,
]);
}
// 查询签署任务列表
public function ownerGetList(){
$bizContent = [
// 签署任务发起方或参与方主体
'ownerId' => [
// 主体类型corp: 企业person: 个人
'idType' => '',
// 主体标识长度最大64个字符
// 如果idType为corp代表应用系统上的企业用户主体方是openCorpId所指定的企业
// 如果idType为person代表应用系统上的个人用户主体方是openUserId所指定的个人
'openId' => '',
],
// 指定第几页如果不传默从第一页返回。页码从1开始即首页为1。
'listPageNo' => '',
// 指定每页多少条数据如果不传默认为100单页最大100
'listPageSize' => '',
];
$result = $this->requestApi($bizContent);
debug([
'查询签署任务列表',
'请求信息'=>$bizContent,
'返回结果'=>$result,
]);
}
// 查询签署任务详情
public function ownerGetDetail(){
$bizContent = [
// 个人用户在应用中的唯一标识由集成方自定义长度最大64个字符。
'signTaskId' => '',
];
$result = $this->requestApi($bizContent);
debug([
'查询签署任务详情',
'请求信息'=>$bizContent,
'返回结果'=>$result,
]);
}
// 获取签署文档下载地址
public function ownerGetDownloadUrl(){
$bizContent = [
// 个人用户在应用中的唯一标识由集成方自定义长度最大64个字符。
'ownerId' => [
// 主体类型corp: 企业person: 个人
'idType' => '',
// 主体标识长度最大64个字符
// 如果idType为corp代表应用系统上的企业用户主体方是openCorpId所指定的企业
// 如果idType为person代表应用系统上的个人用户主体方是openUserId所指定的个人
'openId' => '',
],
// 签署任务ID。下载单个任务文档时传此参数直接返回下载链接。
'signTaskId' => '',
];
$result = $this->requestApi($bizContent);
debug([
'获取签署文档下载地址',
'请求信息'=>$bizContent,
'返回结果'=>$result,
]);
}
// 测试
public function test(){
$this->requestApi([]);
}
}