Platform.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php namespace Api\Lib;
  2. use Dever;
  3. use Api\Lib\Platform\Field;
  4. use Api\Lib\Platform\Ssl;
  5. use Api\Lib\Platform\Request;
  6. use Api\Lib\Platform\Response;
  7. class Platform
  8. {
  9. public $platform;
  10. public $info;
  11. public $field;
  12. public function setting($id, $field = [])
  13. {
  14. $this->info($id);
  15. $this->platform();
  16. $this->field($field);
  17. return $this;
  18. }
  19. protected function info($id)
  20. {
  21. $this->info = is_array($id) ? $id : Dever::db($this->type, 'api')->find($id);
  22. if (!$this->info) {
  23. Dever::error('info error');
  24. }
  25. }
  26. protected function platform()
  27. {
  28. $this->platform = Dever::db('platform', 'api')->find($this->info['platform_id']);
  29. if (!$this->platform) {
  30. Dever::error('platform error');
  31. }
  32. if ($this->info['method'] == -1) {
  33. $this->info['method'] = $this->platform['method'];
  34. $this->info['post_method'] = $this->platform['post_method'];
  35. }
  36. if ($this->info['method'] == 1) {
  37. $this->info['post_method'] = 1;
  38. }
  39. }
  40. protected function field($field)
  41. {
  42. if (isset($field['account_project']) && isset($field['account_id'])) {
  43. $this->info['account_id'] = $field['account_id'];
  44. $this->info['account_project'] = $field['account_project'];
  45. $setting = Dever::db('account_setting', $field['account_project'])->select(['account_id' => $field['account_id']]);
  46. if ($setting) {
  47. foreach ($setting as $k => $v) {
  48. $info = Dever::db('platform_setting', 'api')->find($v['platform_setting_id']);
  49. if ($info) {
  50. $v['key'] = $info['key'];
  51. if (isset($field[$v['key']])) {
  52. $v['value'] = $field[$v['key']];
  53. }
  54. $field[$v['key']] = $v['value'];
  55. } else {
  56. Dever::error('account error');
  57. }
  58. }
  59. } else {
  60. Dever::error('account error');
  61. }
  62. } else {
  63. Dever::error('account error');
  64. }
  65. $this->field = new Field($field);
  66. $this->field->setPlatformId($this->platform['id']);
  67. $this->field->setApid($this->info['id']);
  68. $this->field->setHost($this->platform['host']);
  69. $this->field->setUri($this->info['uri']);
  70. $this->field->setNotify($this->createNotify($field));
  71. $setting = Dever::db('api_setting', 'api')->select(['api_id' => $this->info['id']]);
  72. if ($setting) {
  73. foreach ($setting as $k => $v) {
  74. $this->field->set($v['key'], $this->field->value($v['value'], $v['type']));
  75. }
  76. }
  77. }
  78. # 发起请求
  79. protected function curl($url = false)
  80. {
  81. # 生成请求ID
  82. $this->field->createRequestId();
  83. if (!$url) {
  84. $url = $this->url();
  85. }
  86. $method = $this->method();
  87. $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
  88. $body = $request->body();
  89. $header = $request->header();
  90. $json = $this->info['post_method'] == 3 ? true : false;
  91. $curl = Dever::curl($url, $body, $method, $json, $header);
  92. $curl->setResultHeader(true);
  93. $response_body = $curl->result();
  94. $response_header = $curl->header();
  95. $response_config = ['id' => $this->platform['id'], 'type' => $this->platform['response_type']];
  96. $response = new Response($response_body, $response_header, $this->field, $response_config, $this->type, $this->info['id']);
  97. $result = $response->out();
  98. $log = [];
  99. $log['platform_id'] = $this->platform['id'];
  100. $log['api_id'] = $this->info['id'];
  101. $log['account_id'] = $this->info['account_id'];
  102. $log['account_project'] = $this->info['account_project'];
  103. $log['request_id'] = $this->field->request_id;
  104. $log['url'] = $url;
  105. $log['method'] = $method;
  106. $log['body'] = $body;
  107. $log['header'] = $header;
  108. $log['response_body'] = $response_body;
  109. $log['response_header'] = $response_header;
  110. $log['data'] = $result;
  111. Dever::db('api_log', 'api')->update($log['request_id'], $log);
  112. if ($this->field->log) {
  113. Dever::log($log, 'api');
  114. Dever::debug($log, 'api');
  115. }
  116. return $result;
  117. }
  118. # 直接跳转
  119. protected function location($url = false)
  120. {
  121. if (!$url) {
  122. $url = $this->url();
  123. }
  124. $method = $this->method();
  125. $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
  126. $request->body();
  127. $url .= '?';
  128. foreach ($this->field->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. $path = Dever::db('api_path', 'api')->select(['api_id' => $this->info['id']]);
  154. if ($path) {
  155. $path = [];
  156. foreach ($path as $k => $v) {
  157. $v['value'] = $this->field->value($v['value']);
  158. if ($v['type'] == 1) {
  159. $path[] = $v['value'];
  160. } elseif ($v['type'] == 2) {
  161. $path[] = $v['key'] . '/' . $v['value'];
  162. } elseif ($v['type'] == 3) {
  163. $path[] = $v['key'] . '=' . $v['value'];
  164. }
  165. }
  166. if ($path) {
  167. $path = implode('/', $path);
  168. $this->field->setPath($path);
  169. $this->info['uri'] .= $path;
  170. }
  171. }
  172. $query = Dever::db('api_query', 'api')->select(['api_id' => $this->info['id']]);
  173. if ($query) {
  174. $param = [];
  175. foreach ($query as $k => $v) {
  176. $param[$v['key']] = $this->field->value($v['value'], $v['type']);
  177. if (is_array($param[$v['key']])) {
  178. $param[$v['key']] = Dever::json_encode($param[$v['key']]);
  179. }
  180. }
  181. if ($param) {
  182. $this->field->setQuery($param);
  183. $this->info['uri'] .= '?' . http_build_query($param);
  184. }
  185. }
  186. $url = $this->platform['host'] . $this->info['uri'];
  187. $this->field->setUrl($url);
  188. return $url;
  189. }
  190. }