diff --git a/README.md b/README.md index 4e97570..57bcd35 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,23 @@ $youzan = $youzan->oauth->createAuthorization($token['token']); $result = $youzan->request('youzan.shop.get'); ``` +### 消息推送 + +``` +// 消息结构体 +$data = $youzan->push->parse(); + +$response = $youzan->push->response(); + +// $response 为 `Symfony\Component\HttpFoundation\Response` 实例 +// 对于需要直接输出响应的框架,或者原生 PHP 环境下 +$response->send(); + +// 而 laravel 中直接返回即可: + +return $response; +``` + ## Help QQ 群: 570769430 diff --git a/src/Push.php b/src/Push.php index be16de3..394ed76 100644 --- a/src/Push.php +++ b/src/Push.php @@ -5,6 +5,7 @@ use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; class Push { @@ -24,7 +25,7 @@ public function __construct($clientId, $secret, Request $request) } /** - * @return array + * @return Response|array * @throws YouzanException */ public function parse() @@ -32,11 +33,9 @@ public function parse() $data = $this->request->getContent(); $data = json_decode($data, true); - - echo json_encode(['code' => 0, 'msg' => 'success']); - if ($data['test'] === true) { - return; + if ($this->checkTest($data)) { + return false; } $this->checkSign($data); @@ -46,6 +45,11 @@ public function parse() return $data; } + public function checkTest($data) + { + return $data['test'] === true; + } + public function checkSign($data) { $sign = md5($this->clientId.$data['msg'].$this->secret); @@ -55,4 +59,9 @@ public function checkSign($data) } } + public function response() + { + return Response::create(json_encode(['code' => 0, 'msg' => 'success'])); + } + }