Notify.php 5.2 KB

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