Notify.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php namespace Api\Api;
  2. use Dever;
  3. use Api\Lib\Api;
  4. use Api\Lib\Platform\Sign;
  5. use Api\Lib\Platform\Value;
  6. class Notify extends Api
  7. {
  8. private $notify;
  9. public function common()
  10. {
  11. $input = Dever::input();
  12. $file = file_get_contents("php://input");
  13. if ($file) {
  14. $file = Dever::json_decode($file);
  15. if ($file) {
  16. $input = array_merge($file, $input);
  17. }
  18. }
  19. Dever::log($input, 'api_notify');
  20. if (!isset($input['s'])) {
  21. $this->error('error');
  22. }
  23. $s = \Dever\Helper\Str::decode($input['s']);
  24. if ($s) {
  25. $data = explode('|', $s);
  26. $api_id = $data[0];
  27. if (!$api_id) {
  28. $this->error('error');
  29. }
  30. if (isset($data[1])) {
  31. $method = $data[1];
  32. unset($data[0]);
  33. unset($data[1]);
  34. $param = array_values($data);
  35. }
  36. } else {
  37. $this->error('error');
  38. }
  39. unset($input['s']);
  40. unset($input['l']);
  41. $state = $this->setting($api_id);
  42. if (!$state) {
  43. $this->error('error');
  44. }
  45. if (!$input) {
  46. $this->error('error');
  47. }
  48. if ($this->info['notify'] == 2) {
  49. $this->error('error');
  50. }
  51. $this->notify = Dever::db('api/api_notify')->find(['api_id' => $api_id]);
  52. if (!$this->notify) {
  53. $this->error('error');
  54. }
  55. $body = $this->body($input);
  56. $this->header();
  57. $this->verify();
  58. # 判断是否成功
  59. $status = $this->status($body);
  60. if ($status < 3 && isset($method)) {
  61. $param[] = $status;
  62. $param[] = $body;
  63. $msg = Dever::call($method, $param);
  64. if ($msg) {
  65. $this->error($msg);
  66. }
  67. }
  68. # 返回给上游信息
  69. if ($status == 1) {
  70. echo $this->notify['success'];die;
  71. } elseif ($status == 2) {
  72. $this->error('error');
  73. }
  74. }
  75. protected function body($body)
  76. {
  77. $config = Dever::db('api/api_notify_body')->select(['api_id' => $this->info['id']]);
  78. $result = Value::load($this->field)->get($config, $body);
  79. if ($result) {
  80. foreach ($config as $k => $v) {
  81. if (isset($body[$v['value']])) {
  82. $value = $this->field->value($body[$v['value']], $v['type'], false);
  83. $this->field->set($v['key'], $value);
  84. }
  85. }
  86. }
  87. return $result;
  88. }
  89. protected function header()
  90. {
  91. $header = getallheaders();
  92. $config = Dever::db('api/platform_response_header')->select(['platform_id' => $this->platform['id']]);
  93. if ($config) {
  94. foreach ($config as $k => $v) {
  95. if (isset($header[$v['value']])) {
  96. $value = $this->field->value($header[$v['value']], $v['type'], false);
  97. $this->field->set($v['key'], $value);
  98. }
  99. }
  100. }
  101. $config = Dever::db('api/api_response_header')->select(['api_id' => $this->info['id']]);
  102. if ($config) {
  103. foreach ($config as $k => $v) {
  104. if (isset($header[$v['value']])) {
  105. $value = $this->field->value($header[$v['value']], $v['type'], false);
  106. $this->field->set($v['key'], $value);
  107. }
  108. }
  109. }
  110. }
  111. protected function verify()
  112. {
  113. if (!$this->notify['sign_id']) {
  114. Dever::error('签名验证失败');
  115. }
  116. $info = Dever::db('api/platform_sign')->find($this->notify['sign_id']);
  117. Sign::load($this->field, $info)->check($this->notify['sign_arg']);
  118. }
  119. protected function status($body)
  120. {
  121. # 1成功 2失败 3不做任何操作
  122. $status = 3;
  123. $config = Dever::db('api/api_notify_code')->select(['api_id' => $this->info['id']]);
  124. if ($config) {
  125. foreach ($config as $k => $v) {
  126. if (isset($body[$v['key']]) && $body[$v['key']] == $v['value']) {
  127. $status = $v['type'];
  128. }
  129. }
  130. }
  131. return $status;
  132. }
  133. protected function error($msg)
  134. {
  135. if ($this->notify && $this->notify['error']) {
  136. $temp = explode("\n", $this->notify['error']);
  137. if (!isset($temp[1])) {
  138. $temp[1] = 500;
  139. }
  140. $this->code($temp[1]);
  141. echo $this->notify['error'];die;
  142. }
  143. echo $msg;die;
  144. }
  145. protected function code($code)
  146. {
  147. if ($code == 500) {
  148. header("HTTP/1.1 500 Internal Server Error");
  149. header("Status: 500 Internal Server Error");
  150. }
  151. }
  152. }