Commit 5e32b215 authored by 朱招明's avatar 朱招明

update

parent 67c32cd0
第三方API包
===
## laravel 配置文件发布
`php artisan vendor:publish --tag=third-api`
## 微客API示例
~~~
#非laravel项目
$config = [
'app_key' => '',
'app_secret' => '',
];
$Power = new \SMG\ThirdApi\WeiKe\Rest\Power($config);
#laravel项目(需要发布配置文件后配置)
$Power = new \SMG\ThirdApi\WeiKe\Rest\Power();
$params = [];
#电费充值
$Power->pushOrder($params);
#电费订单查询
$Power->query($params);
#xxx
~~~
......@@ -8,6 +8,7 @@
namespace SMG\ThirdApi\WeiKe;
use GuzzleHttp\Client;
use SMG\ThirdApi\WeiKe\Rest\Power;
class Base
......@@ -31,6 +32,48 @@ class Base
}
/**
* 发送请求
* @desc
*
* @param $path
* @param $params
*
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @author [ZZM]
* @since 2023/10/14
* @modify
*/
protected function sendRequest($path,$params){
$url = $this->base_uri.$path;
$client = new Client();
$common_params = [
'app_key' => $this->config['app_key'],
'timestamp' => time(),
'client' => getClientIP(),
'v' => '1.0',
'format' => 'json',
];
$common_params['sign'] = $this->getSign(array_merge($common_params,$params));
$url = $url. '?' . http_build_query($common_params);
$response = $client->post($url, [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form_params' => $params,
]);
if ($response->getStatusCode() === 200) {
$body = $response->getBody()->getContents();
return json_decode($body);
} else {
throw new \Exception("请求失败,状态码:{$response->getStatusCode()}");
}
}
/**
* 签名
* @desc
*
......
......@@ -13,6 +13,11 @@ use SMG\ThirdApi\WeiKe\Base;
class Power extends Base
{
const PATH = [
'pushOrder' => 'rest/Power/pushOrder',
'query' => 'rest/Power/query',
];
/**
* 电费充值
* @desc
......@@ -27,31 +32,23 @@ class Power extends Base
*/
public function pushOrder($params)
{
$url = $this->base_uri.'rest/Power/pushOrder';
$client = new Client();
$common_params = [
'app_key' => $this->config['app_key'],
'timestamp' => time(),
'client' => getClientIP(),
'v' => '1.0',
'format' => 'json',
];
$common_params['sign'] = $this->getSign(array_merge($common_params,$params));
$url = $url. '?' . http_build_query($common_params);
$response = $client->post($url, [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form_params' => $params,
]);
if ($response->getStatusCode() === 200) {
$body = $response->getBody()->getContents();
return json_decode($body);
} else {
throw new \Exception("请求失败,状态码:{$response->getStatusCode()}");
return $this->sendRequest(self::PATH['pushOrder'], $params);
}
/**
* 电费订单查询
* @desc
*
* @param $params
*
* @return mixed
* @throws \Exception
* @since 2023/10/14
* @modify
* @author [ZZM]
*/
public function query($params)
{
return $this->sendRequest(self::PATH['query'], $params);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment