diff --git a/Action/BaseAction.php b/Action/BaseAction.php new file mode 100644 index 0000000..1174624 --- /dev/null +++ b/Action/BaseAction.php @@ -0,0 +1,13 @@ +appId = config('pingan.appId'); + $this->appSecret = config('pingan.appSecret'); + } + + /** + * Notes: 设置数据 + * @Author: 玄尘 + * @Date : 2021/6/1 14:37 + * @param $data + * @return $this + */ + public function setParams($data) + { + //判断是否有sign,有的话是传来的值 + if (isset($data['sign'])) { + $this->sign = $data['sign']; + } else { + $data = array_merge($data, [ + 'appId' => $this->appId, + 'apiVersion' => $this->apiVersion, + 'timestamp' => $this->getMillisecond(), + ]); + } + + $this->params = $data; + + return $this; + } + + /** + * Notes: 获取传入值 + * @Author: 玄尘 + * @Date : 2021/6/1 15:31 + * @return mixed + */ + public function getParams() + { + $params = $this->params; + $params['sign'] = $this->getSign(); + + return $params; + } + + /** + * Notes: 获取签名 + * @Author: 玄尘 + * @Date : 2021/6/1 14:32 + */ + public function getSign() + { + $str = $this->getSignString(); + + return md5($str); + } + + /** + * Notes: 检验 + * @Author: 玄尘 + * @Date : 2021/6/1 15:32 + */ + public function checkSign() + { + $sign = $this->getSign(); + if ($sign == $this->sign) { + return true; + } + + return false; + } + + /** + * Notes: 封装跳转链接 + * @Author: 玄尘 + * @Date : 2021/6/1 15:25 + * @return string + */ + public function getUrl() + { + $params = $this->params; + $params['sign'] = $this->getSign(); + + return $this->url . http_build_query($this->params); + } + + /** + * Notes: 获取时间戳 + * @Author: 玄尘 + * @Date : 2021/6/1 14:32 + * @return float + */ + private function getMillisecond() + { + [$msec, $sec] = explode(' ', microtime()); + $msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); + $msectime = explode('.', $msectime); + + return $msectime[0]; + } + + /** + * Notes: description + * @Author: 玄尘 + * @Date : 2021/6/1 14:32 + */ + public function getSignString() + { + $params = $this->params; + + if (empty($params)) { + throw new \Exception('获取校验数据失败,缺少数据.'); + } + + if (isset($params['sign'])) { + unset($params['sign']); + } + + ksort($params); + + return http_build_query($params) . $this->appSecret; + } + + /** + * 通用获取数据接口 + * @param [type] $url 请求地址 + * @param array $query 传递参数 + * @param array $json 需要传的json数据 + * @param string $method 方式 + * @return array|mixed [type] [description] + */ + public function http($url, $query, $method = 'POST') + { + try { + $client = new Client(); + $response = $client->request($method, $url, $query); + $body = $response->getBody(); + $content = $body->getContents(); + $result = json_decode($content, true); + + if ($result['ret'] > 0) { + $retData = $result['msg']; + } else { + $retData = $result['data']; + } + + return $retData; + } catch (RequestException $e) { + + return ['ret' => '99999', $e->getMessage()]; + } + } + +} diff --git a/Action/Sign.php b/Action/Sign.php new file mode 100644 index 0000000..8d16aeb --- /dev/null +++ b/Action/Sign.php @@ -0,0 +1,8 @@ + 122121, + 'appSecret' => '122121', + 'apiVersion' => 'v2', + ]; diff --git a/src/Action/Init.php b/src/Action/Init.php index 60f1b10..43ad377 100644 --- a/src/Action/Init.php +++ b/src/Action/Init.php @@ -35,8 +35,7 @@ class Init public function __construct() { - $this->this_type = config('pingan.this_type'); - $pingan = config('pingan.' . $this->this_type); + $appId = config('pingan.appId'); $this->baseUri = $pingan['Uri']; $this->tokenUri = $pingan['tokenUri']; diff --git a/src/PingAn.php b/src/PingAn.php index 5d5eb37..fbf1f65 100644 --- a/src/PingAn.php +++ b/src/PingAn.php @@ -2,12 +2,19 @@ namespace XuanChen\PingAnSdk; -use Carbon\Carbon; +use XuanChen\PingAnSdk\Action\BaseAction; -/** - * 平安SDK - */ class PingAn { + /** + * Notes: 基础 + * @Author: 玄尘 + * @Date : 2021/6/1 15:44 + */ + public function base() + { + return new BaseAction(); + } + } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 9bc188f..d1715bf 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -16,6 +16,10 @@ class ServiceProvider extends LaravelServiceProvider if ($this->app->runningInConsole()) { $this->publishes([__DIR__ . '/../config/config.php' => config_path('pingan.php')]); } + + $this->app->bind('xuanchen.pingan_washcar', function ($app) { + return new PingAn(); + }); } /**