From 12f5f7181781c92d42a8f79682e8f32a82334c72 Mon Sep 17 00:00:00 2001 From: xuanchen <122383162@qq.com> Date: Tue, 1 Jun 2021 16:28:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Action/Info.php | 8 - Action/Init.php | 176 ------------------- Action/Sign.php | 8 - composer.json | 6 +- {Action => src/Action}/BaseAction.php | 4 +- src/Action/Init.php | 232 ++++++++++++-------------- src/Action/Query.php | 43 ----- src/PingAn.php | 4 +- src/ServiceProvider.php | 2 +- 9 files changed, 111 insertions(+), 372 deletions(-) delete mode 100644 Action/Info.php delete mode 100644 Action/Init.php delete mode 100644 Action/Sign.php rename {Action => src/Action}/BaseAction.php (56%) delete mode 100644 src/Action/Query.php diff --git a/Action/Info.php b/Action/Info.php deleted file mode 100644 index 9d65568..0000000 --- a/Action/Info.php +++ /dev/null @@ -1,8 +0,0 @@ -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 deleted file mode 100644 index 8d16aeb..0000000 --- a/Action/Sign.php +++ /dev/null @@ -1,8 +0,0 @@ -baseUri = $pingan['Uri']; - $this->tokenUri = $pingan['tokenUri']; - $this->client_id = $pingan['client_id']; - $this->grant_type = $pingan['grant_type']; - $this->userName = $pingan['userName']; - $this->client_secret = $pingan['client_secret']; - $this->aes_code = $pingan['AES_CODE']; + $this->appId = config('pingan.appId'); + $this->appSecret = config('pingan.appSecret'); } /** - * 获取access_token - * @return void [type] [description] + * Notes: 设置数据 + * @Author: 玄尘 + * @Date : 2021/6/1 14:37 + * @param $data + * @return $this */ - public function getToken() + public function setParams($data) { - //从数据库里找token - $token = PinganToken::where('type', $this->this_type)->orderBy('id', 'desc')->first(); - - if ($token) { - $access_token = $token->access_token; - $expires_in = $token->expires_in; - $get_token_time = $token->get_token_time; - $diffMinutes = $get_token_time->diffInMinutes(now(), false); - if ($diffMinutes < $expires_in) { - $this->access_token = $access_token; - } else { - $this->getAjaxToken(); - } + //判断是否有sign,有的话是传来的值 + if (isset($data['sign'])) { + $this->sign = $data['sign']; } else { - $this->getAjaxToken(); + $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 getMsecTime() + 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); @@ -83,48 +122,25 @@ class Init } /** - * 请求平台 access_token - * @return void [type] [description] + * Notes: description + * @Author: 玄尘 + * @Date : 2021/6/1 14:32 */ - public function getAjaxToken() + public function getSignString() { - $params = [ - 'client_id' => $this->client_id, - 'grant_type' => $this->grant_type, - 'client_secret' => $this->client_secret, - ]; + $params = $this->params; - try { - $log = $this->createLog($this->tokenUri, 'POST', $params, 'pingan'); - - $client = new Client(); - $response = $client->request('POST', $this->tokenUri, [ - 'form_params' => $params, - ]); - $body = $response->getBody(); - $content = $body->getContents(); - $result = json_decode($content, true); - - $this->updateLog($log, $result); //更新日志 - - if ($result['ret'] > 0) { - $this->error = $result['msg']; - } else { - $data = $result['data']; - PinganToken::create([ - 'type' => $this->this_type, - 'access_token' => $data['access_token'], - 'expires_in' => $data['expires_in'], - 'get_token_time' => now(), - ]); - $this->access_token = $data['access_token']; - $this->error = ''; - } - } catch (RequestException $e) { - $this->error = $e->getMessage(); - $this->updateLog($log, [$this->error]); //更新日志 + if (empty($params)) { + throw new \Exception('获取校验数据失败,缺少数据.'); } + if (isset($params['sign'])) { + unset($params['sign']); + } + + ksort($params); + + return http_build_query($params) . $this->appSecret; } /** @@ -135,32 +151,11 @@ class Init * @param string $method 方式 * @return array|mixed [type] [description] */ - public function getPingAnData($url, $query = [], $json = [], $method = 'POST') + public function http($url, $query, $method = 'POST') { - $this->getToken(); - - if ($this->error) { - return $this->error; - } - - $postData = [ - 'query' => array_merge([ - 'access_token' => $this->access_token, - 'request_id' => $this->getMsecTime(), - 'userName' => $this->userName, - ], $query), - 'json' => $json, - 'headers' => [ - 'Content-Type' => 'application/json;charset=utf-8', - 'accept' => 'application/json;charset=utf-8', - ], - ]; - - $log = $this->createLog($url, $method, $postData, 'pingan'); //日志 - try { $client = new Client(); - $response = $client->request($method, $url, $postData); + $response = $client->request($method, $url, $query); $body = $response->getBody(); $content = $body->getContents(); $result = json_decode($content, true); @@ -170,33 +165,12 @@ class Init } else { $retData = $result['data']; } - $this->updateLog($log, $retData);//更新日志 return $retData; } catch (RequestException $e) { - $this->updateLog($log, [$e->getMessage()]);//更新日志 return ['ret' => '99999', $e->getMessage()]; } } - //加密 - public function encrypt($str) - { - if (is_array($str)) { - $str = json_encode($str); - } - $data = openssl_encrypt($str, 'aes-128-ecb', $this->aes_code, OPENSSL_RAW_DATA); - - return base64_encode($data); - } - - //解密 - public function decrypt($str) - { - $encrypted = base64_decode($str); - - return openssl_decrypt($encrypted, 'aes-128-ecb', $this->aes_code, OPENSSL_RAW_DATA); - } - } diff --git a/src/Action/Query.php b/src/Action/Query.php deleted file mode 100644 index 58e74d1..0000000 --- a/src/Action/Query.php +++ /dev/null @@ -1,43 +0,0 @@ -outletId)->first(); - - if (!$outlet) { - throw new \Exception('网点编号错误,未查询到网点信息'); - } - - $url = $this->baseUri . 'partner/v2/coupondetail'; - $params = [ - 'redemptionCode' => $this->redemptionCode, - 'outletNo' => $outlet->PaOutletId, - 'thirdOutletNo' => $outlet->outlet_id, - ]; - - $res = $this->getPingAnData($url, $params); - - if (!is_array($res)) { - throw new \Exception($res); - } - - if ($res['code'] != 200) { - throw new \Exception($res['message']); - } - - return collect($res['data']); - } catch (\Exception $e) { - return $e->getMessage(); - } - - } - -} \ No newline at end of file diff --git a/src/PingAn.php b/src/PingAn.php index fbf1f65..45708b3 100644 --- a/src/PingAn.php +++ b/src/PingAn.php @@ -1,8 +1,8 @@