Platform.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php namespace Api\Lib;
  2. use Dever;
  3. use Api\Lib\Platform\Field;
  4. use Api\Lib\Platform\Sign;
  5. use Api\Lib\Platform\Ssl;
  6. use Api\Lib\Platform\Request;
  7. use Api\Lib\Platform\Response;
  8. class Platform
  9. {
  10. protected $platform;
  11. protected $info;
  12. protected $field;
  13. protected function setting($id, $field = array())
  14. {
  15. $this->info($id);
  16. $this->platform();
  17. $this->field($field);
  18. return $this;
  19. }
  20. protected function info($id)
  21. {
  22. $this->info = is_array($id) ? $id : Dever::db($this->type, 'api')->find($id);
  23. if (!$this->info) {
  24. Dever::error('info error');
  25. }
  26. }
  27. protected function platform()
  28. {
  29. $this->platform = Dever::db('platform', 'api')->find($this->info['platform_id']);
  30. if (!$this->platform) {
  31. Dever::error('platform error');
  32. }
  33. $this->sign = $this->response = array('id' => $this->platform['id']);
  34. foreach ($this->platform as $k => $v) {
  35. if (strstr($k, 'sign_')) {
  36. $k = str_replace('sign_', '', $k);
  37. $this->sign[$k] = $v;
  38. }
  39. if (strstr($k, 'response_')) {
  40. $k = str_replace('response_', '', $k);
  41. $this->response[$k] = $v;
  42. }
  43. }
  44. if ($this->info['method'] == -1) {
  45. $this->info['method'] = $this->platform['method'];
  46. $this->info['post_method'] = $this->platform['post_method'];
  47. }
  48. if ($this->info['method'] == 1) {
  49. $this->info['post_method'] = 1;
  50. }
  51. }
  52. protected function field($field)
  53. {
  54. $this->field = new Field($field);
  55. $this->field->setHost($this->platform['host']);
  56. $this->field->setUri($this->info['uri']);
  57. $this->field->setNotify($this->createNotify($field['order_num'] ?? ''));
  58. $setting = Dever::db('platform_setting', 'api')->select(array('platform_id' => $this->platform['id']));
  59. if ($setting) {
  60. foreach ($setting as $k => $v) {
  61. if (isset($field[$v['key']])) {
  62. $v['value'] = $field[$v['key']];
  63. }
  64. $key = $v['key'];
  65. # 如果有自定义的设置
  66. if (!$this->field->$key) {
  67. $this->field->set($key, $v['value']);
  68. }
  69. }
  70. }
  71. }
  72. # 发起请求
  73. protected function curl($url = false)
  74. {
  75. if (!$url) {
  76. $url = $this->url();
  77. }
  78. $method = $this->method();
  79. $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
  80. $request_body = $request->body();
  81. if ($this->info['sign_col']) {
  82. $this->platform['sign_col'] = $this->info['sign_col'];
  83. }
  84. $sign = new Sign($this->field, $this->sign);
  85. $sign = $sign->get();
  86. if ($this->platform['sign_name']) {
  87. $request_body[$this->platform['sign_name']] = $sign;
  88. }
  89. $request_header = $request->header();
  90. $json = $this->info['post_method'] == 3 ? true : false;
  91. $curl = Dever::curl($url, $request_body, $method, $json, $request_header);
  92. $curl->setResultHeader(true);
  93. $response_body = $curl->result();
  94. $response_header = $curl->header();
  95. $response = new Response($response_body, $response_header, $this->response, $this->field, $this->type, $this->info['id'], $this->sign);
  96. $result = $response->out();
  97. $log['platform_id'] = $this->platform['id'];
  98. $log['type'] = $this->type;
  99. $log['type_id'] = $this->info['id'];
  100. $log['url'] = $url;
  101. $log['method'] = $method;
  102. $log['json'] = $json;
  103. $log['request_body'] = Dever::json_encode($request_body);
  104. $log['request_header'] = Dever::json_encode($request_header);
  105. $log['response_body'] = $response_body;
  106. $log['response_header'] = Dever::json_encode($response_header);
  107. Dever::debug($log);
  108. return $result;
  109. }
  110. # 直接跳转
  111. protected function location($url = false)
  112. {
  113. if (!$url) {
  114. $url = $this->url();
  115. }
  116. $method = $this->method();
  117. $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
  118. $request_body = $request->body();
  119. if ($this->info['sign_col']) {
  120. $this->platform['sign_col'] = $this->info['sign_col'];
  121. }
  122. $sign = new Sign($this->field, $this->sign);
  123. $sign = $sign->get();
  124. if ($this->platform['sign_name']) {
  125. $request_body[$this->platform['sign_name']] = $sign;
  126. }
  127. $url .= '?';
  128. foreach ($request_body as $k => $v) {
  129. if ($k == '#') {
  130. $url .= $k . $v;
  131. } else {
  132. $url .= $k . '=' . $v . '&';
  133. }
  134. }
  135. header('Location: '. $url);
  136. }
  137. protected function method()
  138. {
  139. $method = 'get';
  140. if ($this->info['post_method'] == 2) {
  141. $method = 'file';
  142. } elseif ($this->info['method'] == 2) {
  143. $method = 'post';
  144. }
  145. $this->field->setMethod($method);
  146. return $method;
  147. }
  148. protected function url()
  149. {
  150. if (strstr($this->info['uri'], 'http')) {
  151. $this->platform['host'] = '';
  152. }
  153. $uri = Dever::db('api_uri', 'api')->select(array('api_id' => $this->info['id']));
  154. if ($uri) {
  155. $path = array();
  156. $param = array();
  157. foreach ($uri as $k => $v) {
  158. $v['value'] = $this->field->value($v['value']);
  159. if ($v['type'] == 1) {
  160. $path[] = $v['value'];
  161. } elseif ($v['type'] == 2) {
  162. $path[] = $v['key'] . '/' . $v['value'];
  163. } elseif ($v['type'] == 3) {
  164. $path[] = $v['key'] . '=' . $v['value'];
  165. } elseif ($v['type'] == 4) {
  166. $param[] = $v['value'];
  167. } elseif ($v['type'] == 5) {
  168. $param[] = $v['key'] . '=' . $v['value'];
  169. }
  170. }
  171. if ($path) {
  172. $this->info['uri'] .= implode('/', $path);
  173. }
  174. if ($param) {
  175. if (!strstr($this->info['uri'], '?')) {
  176. $this->info['uri'] .= '?';
  177. }
  178. $this->info['uri'] .= implode('&', $param);
  179. }
  180. }
  181. $this->field->setUri($this->info['uri']);
  182. return $this->platform['host'] . $this->info['uri'];
  183. }
  184. }