Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit 1d1d010

Browse files
author
justmd5
committed
处理参数,image 参数支持base_64和图片地址
1 parent 19fe97e commit 1d1d010

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
$config = [
3939
'appKey' => '1106944xxx',
4040
'appSecret' => 'dsgnbnWnX8Yxxxxxx',
41-
'debug' => 0,//1 show debug info
41+
'debug' => true,//true show debug info
4242
];
4343
$AI = new \Justmd5\TencentAi\Ai($config);
4444

src/Core/API.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
use Justmd5\TencentAi\Exception\NotFoundException;
1414
use Overtrue\Validation\Factory as ValidatorFactory;
1515
use Overtrue\Validation\Translator;
16+
use Justmd5\TencentAi\Core\Traits\ArgumentProcessingTrait;
1617

1718
class API extends AbstractAPI
1819
{
20+
use ArgumentProcessingTrait;
21+
1922
const BASE_API = 'https://api.ai.qq.com/fcgi-bin/';
2023

2124
/**
2225
* @var string
2326
*/
2427
protected $category;
2528
/**
26-
* @var \Justmd5\TencentAi\Core\Signature
29+
* @var Signature
2730
*/
2831
protected $signature;
2932
protected $classify;
@@ -40,13 +43,13 @@ public function __construct(Signature $signature, $classify, $filter)
4043
* 请求API.
4144
*
4245
* @param string $method
43-
* @param array $params
44-
* @param array $files
45-
*
46-
* @throws \Justmd5\TencentAi\Exception\NotFoundException
47-
* @throws \Justmd5\TencentAi\Exception\IllegalParameterException
46+
* @param array $params
47+
* @param array $files
4848
*
4949
* @return array
50+
* @throws IllegalParameterException
51+
*
52+
* @throws NotFoundException
5053
*/
5154
public function request($method, $params = [], $files = [])
5255
{
@@ -60,7 +63,7 @@ public function request($method, $params = [], $files = [])
6063
throw new IllegalParameterException(sprintf('参数错误:[%s]', json_encode($validator->errors(), JSON_UNESCAPED_UNICODE)));
6164
}
6265
$http = $this->getHttp();
63-
$params['sign'] = $this->signature->getReqSign($params);
66+
$params=$this->processParams($this->signature,$params);
6467
$response = $files ? $http->upload($url, $params, $files) : $http->post($url, $params);
6568
$result = json_decode(strval($response->getBody()), true);
6669
if (isset($result['ret'])) {

src/Core/ApplicationProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Justmd5\TencentAi\Core;
1010

11+
use Justmd5\TencentAi\Core\Traits\FilterTrait;
1112
use Pimple\Container;
1213
use Pimple\ServiceProviderInterface;
1314

src/Core/Signature.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ public function __construct($appId, $secret)
3939
public function getReqSign(&$params)
4040
{
4141
$params['app_id'] = $this->appId;
42-
if (empty($params['nonce_str'])) {
43-
$params['nonce_str'] = md5(uniqid("{$params['app_id']}_"));
44-
}
45-
if (empty($params['time_stamp'])) {
46-
$params['time_stamp'] = strval(time());
47-
}
4842
ksort($params);
4943
$str = '';
5044
array_walk($params, function ($item, $key) use (&$str) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Justmd5\TencentAi\Core\Traits;
4+
5+
use Justmd5\TencentAi\Core\Signature;
6+
7+
trait ArgumentProcessingTrait
8+
{
9+
public function processParams(Signature $signature, $params)
10+
{
11+
if (empty($params['nonce_str'])) {
12+
$params['nonce_str'] = md5(uniqid("TencentAi_"));
13+
}
14+
if (empty($params['time_stamp'])) {
15+
$params['time_stamp'] = strval(time());
16+
}
17+
if (isset($params['image']) && is_file($params['image'])) {
18+
$params['image'] = base64_encode(file_get_contents($params['image']));
19+
}
20+
$params['sign'] = $signature->getReqSign($params);
21+
22+
return $params;
23+
}
24+
}

src/Core/FilterTrait.php renamed to src/Core/Traits/FilterTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Time: 下午7:53.
77
*/
88

9-
namespace Justmd5\TencentAi\Core;
9+
namespace Justmd5\TencentAi\Core\Traits;
1010

1111
trait FilterTrait
1212
{

0 commit comments

Comments
 (0)