Push.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\youzan\controller;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Cache;
  7. use think\Request;
  8. class Push extends BaseController
  9. {
  10. public $data=[];
  11. public $clientId='deaaebe6484c129787';
  12. public $clientSecret='3c654aa2fbc1b5da788ffdba45fb96f0';
  13. public $httpSign='';
  14. public $httpType='';
  15. public $httpClientId='';
  16. public function __construct(App $app)
  17. {
  18. parent::__construct($app);
  19. $this->data=file_get_contents("php://input");
  20. $header =$this->request->header();
  21. $this->httpClientId=$header['client-id'];
  22. $this->httpType=$header['event-type'];
  23. $this->httpSign=$header['event-sign'];
  24. }
  25. public function GetPush(){
  26. if($this->sign() != $this->httpSign) {
  27. // sign校验失败, 自行检查
  28. return json_encode(array("code" => 0, "msg" => "faild"));
  29. }
  30. $httpData = json_decode($this->data, true);
  31. // 业务逻辑处理
  32. // 根据 Type 来识别消息事件类型, 具体的 type 值以文档为准, 此处仅是示例
  33. if($this->httpType == "POINTS") {
  34. // 建议异步处理业务逻辑
  35. // http request body
  36. // msg内容经过 urlencode 编码,需进行解码
  37. $msg = json_decode(urldecode($httpData['msg']), true);
  38. //todo
  39. }
  40. // 响应结果
  41. return json_encode(array("code" => 0, "msg" => "success"));
  42. }
  43. public function sign(){
  44. return md5(sprintf('%s%s%s', $this->clientId, $this->data, $this->clientSecret));
  45. }
  46. }