wugg 2 years ago
parent
commit
1fc8f7bfe1
1 changed files with 39 additions and 6 deletions
  1. 39 6
      app/youzan/controller/Push.php

+ 39 - 6
app/youzan/controller/Push.php

@@ -4,15 +4,48 @@ declare (strict_types = 1);
 namespace app\youzan\controller;
 namespace app\youzan\controller;
 
 
 use app\BaseController;
 use app\BaseController;
+use think\App;
 use think\facade\Cache;
 use think\facade\Cache;
 use think\Request;
 use think\Request;
 
 
 class Push extends BaseController
 class Push extends BaseController
 {
 {
-   public  function  GetPush(){
-        $data =$this->request->post();
-        $header=$this->request->header(['Event-Sign','Event-Type','Client-Id']);
-        Cache::set("header",$header);
-        Cache::set("youzan",$data);
+    public  $data=[];
+    public  $clientId='';
+    public  $clientSecret='';
+    public  $httpSign='';
+    public  $httpType='';
+    public  $httpClientId='';
+    public function __construct(App $app)
+    {
+        parent::__construct($app);
+        $this->data=$this->request->post();
+        $header =$this->request->header();
+        $this->httpClientId=$header['client-id'];
+        $this->httpType=$header['event-sign'];
+        $this->httpSign=$header['event-type'];
+
+    }
+
+    public  function  GetPush(){
+        if($this->sign != $this->httpSign) {
+            // sign校验失败, 自行检查
+            return json_encode(array("code" => 0, "msg" => "faild"));
+        }
+// 业务逻辑处理
+// 根据 Type 来识别消息事件类型, 具体的 type 值以文档为准, 此处仅是示例
+        if($this->httpType == "POINTS") {
+            // 建议异步处理业务逻辑
+            // http request body
+            $httpData = json_decode($this->data, true);
+            // msg内容经过 urlencode 编码,需进行解码
+            $msg = json_decode(urldecode($httpData['msg']), true);
+            //todo
+        }
+// 响应结果
+        return json_encode(array("code" => 0, "msg" => "success"));
+   }
+   public function  sign(){
+        return md5(sprintf('%s%s%s', $this->clientId, $this->data, $this->clientSecret));
    }
    }
-}
+}