diff --git a/config/config.php b/config/config.php index f5956b7..6db1e82 100644 --- a/config/config.php +++ b/config/config.php @@ -15,7 +15,7 @@ return [ 'client_secret' => env('WASH_CAR_SECRET', ''),// 就是 client_secret 'apiVersion' => 'v2', 'grant_type' => 'client_credentials', - 'tokenUri' => 'https://open.cyzl.com/beta/api/auth/oauth/token', + 'tokenUri' => 'https://open.cyzl.com/beta/auth/oauth/token', 'baseUri' => 'https://open.cyzl.com/beta/api', 'urlKey' => env('WASH_CAR_URLKEY', ''),//免登陆跳转 ], diff --git a/database/migrations/0000_00_00_000000_create_washcar_logs_table.php b/database/migrations/0000_00_00_000000_create_washcar_logs_table.php new file mode 100644 index 0000000..c4470fa --- /dev/null +++ b/database/migrations/0000_00_00_000000_create_washcar_logs_table.php @@ -0,0 +1,35 @@ +bigIncrements('id'); + $table->string('url')->comment('地址'); + $table->text('in_source')->comment('入参'); + $table->text('out_source')->comment('出参')->nullable(); + $table->boolean('status')->default(1); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * @return void + */ + public function down() + { + Schema::dropIfExists('washcar_logs'); + } + +} diff --git a/src/Action/CouponAction.php b/src/Action/CouponAction.php index 2631afd..0ece0d5 100644 --- a/src/Action/CouponAction.php +++ b/src/Action/CouponAction.php @@ -5,6 +5,26 @@ namespace XuanChen\WashCar\Action; class CouponAction extends Init { + /** + * Notes: 跳转到下单url + * @Author: 玄尘 + * @Date : 2021/6/4 14:11 + */ + public function getOrderUrl() + { + $url = $this->config['baseUri'] . '/v2/h5/redirect/autoLogin/returnUrl'; + + $this->params = array_merge($this->params, [ + 'urlKey' => $this->config['urlKey'], + ]); + + $params = $this->getParams(); + + $this->http($url, $params); + + return $this->getResponse(); + } + /** * Notes: 发券 * @Author: 玄尘 @@ -14,12 +34,14 @@ class CouponAction extends Init { $url = $this->config['baseUri'] . '/v2/rights/coupon/getCoupon'; - $res = $this->http($url, $this->params); - dd($res); + $params = $this->getParams(); + $this->http($url, $params); + + return $this->getResponse(); } /** - * Notes: 查询 + * Notes: 查询优惠券状态 * @Author: 玄尘 * @Date : 2021/6/4 14:28 */ @@ -27,12 +49,31 @@ class CouponAction extends Init { $url = $this->config['baseUri'] . '/v2/rights/coupon/getCouponStatus'; - $this->http($url, $this->params); + $params = $this->getParams(); + + $this->http($url, $params); return $this->getResponse(); } + /** + * Notes: 查询优惠券信息 + * @Author: 玄尘 + * @Date : 2021/6/9 15:22 + * @return array + */ + public function info() + { + $url = $this->config['baseUri'] . '/v2/rights/coupon/getCouponInfo'; + + $params = $this->getParams(); + + $this->http($url, $params); + + return $this->getResponse(); + } + /** * Notes: 取消优惠券 * @Author: 玄尘 @@ -42,7 +83,9 @@ class CouponAction extends Init { $url = $this->config['baseUri'] . '/v2/rights/coupon/recedeCoupon'; - $this->http($url, $this->params); + $params = $this->getParams(); + + $this->http($url, $params); return $this->getResponse(); diff --git a/src/Action/Init.php b/src/Action/Init.php index ecd2de2..d63de69 100644 --- a/src/Action/Init.php +++ b/src/Action/Init.php @@ -5,6 +5,7 @@ namespace XuanChen\WashCar\Action; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; use Illuminate\Support\Facades\Cache; +use XuanChen\WashCar\Models\WashcarLog; class Init { @@ -39,6 +40,18 @@ class Init */ protected $message; + /** + * @var 返回的数据 + */ + protected $data; + + /** + * @var 日志 + */ + protected $log; + + protected $url; + public function __construct() { $this->this_type = config('washcar.this_type'); @@ -51,15 +64,10 @@ class Init */ public function getToken() { - $this_type = $this->config['this_type']; - //从缓存里找token - $token = Cache::get('token_' . $this_type); - - if ($token) { - $token = collect($token); - $this->access_token = $token->access_token; - + $access_token = Cache::get('token_' . $this->config['grant_type']); + if ($access_token) { + $this->access_token = $access_token; } else { $this->getAjaxToken(); } @@ -75,20 +83,31 @@ class Init 'client_id' => $this->config['client_id'], 'grant_type' => $this->config['grant_type'], 'client_secret' => $this->config['client_secret'], + 'scope' => 'all', ]; - $tokenUrl = $this->config['tokenUri']; + $tokenUrl = $this->config['tokenUri'] . '?' . http_build_query($params); try { - $this->http($tokenUrl, $params, 'POST'); + + $client = new Client(); + + $response = $client->request('GET', $tokenUrl); + + $body = $response->getBody(); + $content = $body->getContents(); + $result = json_decode($content, true); if ($this->error > 0) { return new \Exception($this->message); } - Cache::put('token_' . $this->grant_type, $this->data['expires_in']); + $this->data = $result['data']; - $this->access_token = $data['access_token']; + Cache::put('token_' . $this->config['grant_type'], $this->data['access_token'], $this->data['expires_in']); + + $this->access_token = $this->data['access_token']; + $this->error = 0; } catch (RequestException $e) { $this->error = 9999; @@ -191,13 +210,16 @@ class Init throw new \Exception('获取校验数据失败,缺少数据.'); } - if (isset($params['sign'])) { - unset($params['sign']); - } - ksort($params); - return http_build_query($params) . $this->config['client_secret']; + $str = ''; + foreach ($params as $key => $param) { + if ($param != '' || $param != null || $key != 'sign') { + $str .= $key . $param; + } + } + + return $str . $this->config['client_secret']; } /** @@ -210,13 +232,28 @@ class Init */ public function http($url, $query, $method = 'POST') { + $this->getToken(); + + if (!$this->access_token) { + $this->error = 9999; + $this->message = '缺少 access_token'; + + return; + } + + $this->url = $url . '?access_token=' . $this->access_token; + try { + + $this->addLog($query); + $headers = [ 'Content-Type' => 'application/json', ]; - $client = new Client(); - $response = $client->request($method, $url, [ + $client = new Client(); + + $response = $client->request($method, $this->url, [ 'json' => $query, 'headers' => $headers, ]); @@ -225,21 +262,53 @@ class Init $content = $body->getContents(); $result = json_decode($content, true); - if ($result['ret'] > 0) { - $this->error = $result['ret']; + $this->updateLog($result); + + if ($result['code'] > 0) { + $this->error = $result['code']; $this->message = $result['message']; return false; } else { - $retData = $result['data']; - $this->data = $retData; + $this->data = $result['data']; + + return true; } } catch (RequestException $e) { - return ['ret' => '99999', $e->getMessage()]; + $this->error = 9999; + $this->message = $e->getMessage(); + $this->updateLog($e->getMessage()); + } } + /** + * Notes: 插入日志 + * @Author: 玄尘 + * @Date : 2021/6/9 16:36 + */ + public function addLog($query) + { + $this->log = WashcarLog::create([ + 'url' => $this->url, + 'in_source' => $query, + ]); + } + + /** + * Notes: 更新log + * @Author: 玄尘 + * @Date : 2021/6/9 16:40 + * @param $data + */ + public function updateLog($data) + { + $this->log->out_source = $data; + $this->log->status = $this->error == 0 ? WashcarLog::STATUS_SUCCESS : WashcarLog::STATUS_ERROR; + $this->log->save(); + } + /** * Notes: 返回 * @Author: 玄尘 @@ -247,10 +316,19 @@ class Init */ public function getResponse() { + $rt = microtime(true) - LARAVEL_START; + + $header = [ + 'rt' => round($rt * 1000, 2) . 'ms', + 'qps' => round(1 / $rt, 1), + 'endMemory' => round(memory_get_usage() / 1024 / 1024, 2), + ]; + return [ 'error' => $this->error, 'message' => $this->message, 'data' => $this->data, + 'header' => $header, ]; } diff --git a/src/Action/UrlAction.php b/src/Action/UrlAction.php index beadb35..19032b7 100644 --- a/src/Action/UrlAction.php +++ b/src/Action/UrlAction.php @@ -14,10 +14,12 @@ class UrlAction extends Init { $url = $this->config['baseUri'] . '/v2/h5/redirect/autoLogin/returnUrl'; - $params = array_merge($this->params, [ + $this->params = array_merge($this->params, [ 'urlKey' => $this->config['urlKey'], ]); + $params = $this->getParams(); + $this->http($url, $params); return $this->getResponse(); diff --git a/src/Models/WashcarLog.php b/src/Models/WashcarLog.php new file mode 100644 index 0000000..554e64c --- /dev/null +++ b/src/Models/WashcarLog.php @@ -0,0 +1,36 @@ + 'json', + 'out_source' => 'json', + ]; + + const STATUS_INIT = 1; + const STATUS_SUCCESS = 2; + const STATUS_ERROR = 3; + + const STATUS = [ + self::STATUS_INIT => '初始', + self::STATUS_SUCCESS => '成功', + self::STATUS_ERROR => '失败', + ]; + + protected function serializeDate(\DateTimeInterface $date) + { + if (version_compare(app()->version(), '7.0.0') < 0) { + return parent::serializeDate($date); + } + + return $date->format(Carbon::DEFAULT_TO_STRING_FORMAT); + } + +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 7eab525..4cc0ae1 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -14,7 +14,9 @@ class ServiceProvider extends LaravelServiceProvider public function register() { if ($this->app->runningInConsole()) { - $this->publishes([__DIR__ . '/../config/config.php' => config_path('pingan.php')]); + $this->publishes([__DIR__ . '/../config/config.php' => config_path('washcar.php')]); + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations/'); + } $this->app->bind('xuanchen.washcar', function ($app) { @@ -28,7 +30,7 @@ class ServiceProvider extends LaravelServiceProvider */ public function boot() { - $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'pingan'); + $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'washcar'); } } diff --git a/src/WashCar.php b/src/WashCar.php index 6648002..4eea372 100644 --- a/src/WashCar.php +++ b/src/WashCar.php @@ -3,6 +3,7 @@ namespace XuanChen\WashCar; use XuanChen\WashCar\Action\BaseAction; +use XuanChen\WashCar\Action\CouponAction; use XuanChen\WashCar\Action\UrlAction; class WashCar @@ -36,7 +37,7 @@ class WashCar */ public function coupon() { - return new Coupon + return new CouponAction(); } }