Platform.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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('api/' . $this->type)->find($id);
  22. if (!$this->info) {
  23. Dever::error('info error');
  24. }
  25. }
  26. protected function platform()
  27. {
  28. $this->platform = Dever::db('api/platform')->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. if ($this->info['response_type'] == -1) {
  40. $this->info['response_type'] = $this->platform['response_type'];
  41. }
  42. }
  43. protected function field($field)
  44. {
  45. if (isset($field['account_project']) && isset($field['account_id'])) {
  46. $this->info['account_id'] = $field['account_id'];
  47. $this->info['account_project'] = $field['account_project'];
  48. $setting = Dever::db($field['account_project'] . '/account_setting')->select(['account_id' => $field['account_id']]);
  49. if ($setting) {
  50. foreach ($setting as $k => $v) {
  51. $info = Dever::db('api/platform_setting')->find($v['platform_setting_id']);
  52. if ($info) {
  53. $v['key'] = $info['key'];
  54. if (isset($field[$v['key']])) {
  55. $v['value'] = $field[$v['key']];
  56. }
  57. $field[$v['key']] = $v['value'];
  58. } else {
  59. Dever::error('account error');
  60. }
  61. }
  62. } else {
  63. Dever::error('account error');
  64. }
  65. } else {
  66. Dever::error('account error');
  67. }
  68. $this->field = Dever::load(Field::class)->init($field);
  69. $this->field->setPlatformId($this->platform['id']);
  70. $this->field->setApid($this->info['id']);
  71. $this->field->setHost($this->platform['host']);
  72. $this->field->setUri($this->info['uri']);
  73. $this->field->setNotify($this->createNotify($field));
  74. $setting = Dever::db('api/api_setting')->select(['api_id' => $this->info['id']]);
  75. if ($setting) {
  76. foreach ($setting as $k => $v) {
  77. $this->field->set($v['key'], $this->field->value($v['value'], $v['type']));
  78. }
  79. }
  80. }
  81. # 发起请求
  82. protected function curl($url = false)
  83. {
  84. # 生成请求ID
  85. $this->field->createRequestId();
  86. if (!$url) {
  87. $url = $this->url();
  88. }
  89. $method = $this->method();
  90. $request = Dever::load(Request::class)->init($this->field, $this->platform['id'], $this->type, $this->info['id']);
  91. $body = $request->body();
  92. $header = $request->header();
  93. $json = $this->info['post_method'] == 3 ? true : false;
  94. $curl = Dever::curl($url, $body, $method, $json, $header);
  95. $curl->setResultHeader(true);
  96. $response_body = $curl->result();
  97. $response_header = $curl->header();
  98. $response_config = ['id' => $this->platform['id'], 'type' => $this->info['response_type']];
  99. $response = Dever::load(Response::class)->init($response_body, $response_header, $this->field, $response_config, $this->type, $this->info['id']);
  100. $result = $response->out();
  101. $log = [];
  102. $log['platform_id'] = $this->platform['id'];
  103. $log['api_id'] = $this->info['id'];
  104. $log['account_id'] = $this->info['account_id'];
  105. $log['account_project'] = $this->info['account_project'];
  106. $log['request_id'] = $this->field->request_id;
  107. $log['url'] = $url;
  108. $log['method'] = $method;
  109. $log['body'] = $body;
  110. $log['header'] = $header;
  111. $log['response_header'] = $response_header;
  112. $log['response_body'] = $response_body;
  113. $log['data'] = $result;
  114. if ($response_config['type'] == 4) {
  115. $log['response_body'] = 'buffer';
  116. $log['data'] = 'buffer';
  117. }
  118. Dever::db('api/api_log')->update($log['request_id'], $log);
  119. if ($this->field->log) {
  120. Dever::log($log, 'api');
  121. Dever::debug($log, 'api');
  122. }
  123. return $result;
  124. }
  125. # 直接跳转
  126. protected function location($url = false)
  127. {
  128. if (!$url) {
  129. $url = $this->url();
  130. }
  131. $method = $this->method();
  132. $request = Dever::load(Request::class)->init($this->field, $this->platform['id'], $this->type, $this->info['id']);
  133. $request->body();
  134. $url .= '?';
  135. foreach ($this->field->body as $k => $v) {
  136. if ($k == '#') {
  137. $url .= $k . $v;
  138. } else {
  139. $url .= $k . '=' . $v . '&';
  140. }
  141. }
  142. header('Location: '. $url);
  143. }
  144. protected function method()
  145. {
  146. $method = 'get';
  147. if ($this->info['post_method'] == 2) {
  148. $method = 'file';
  149. } elseif ($this->info['method'] == 2) {
  150. $method = 'post';
  151. }
  152. $this->field->setMethod($method);
  153. return $method;
  154. }
  155. protected function url()
  156. {
  157. if (strstr($this->info['uri'], 'http')) {
  158. $this->platform['host'] = '';
  159. }
  160. $path = Dever::db('api/api_path')->select(['api_id' => $this->info['id']]);
  161. if ($path) {
  162. $path = [];
  163. foreach ($path as $k => $v) {
  164. $v['value'] = $this->field->value($v['value']);
  165. if ($v['type'] == 1) {
  166. $path[] = $v['value'];
  167. } elseif ($v['type'] == 2) {
  168. $path[] = $v['key'] . '/' . $v['value'];
  169. } elseif ($v['type'] == 3) {
  170. $path[] = $v['key'] . '=' . $v['value'];
  171. }
  172. }
  173. if ($path) {
  174. $path = implode('/', $path);
  175. $this->field->setPath($path);
  176. $this->info['uri'] .= $path;
  177. }
  178. }
  179. $query = Dever::db('api/api_query')->select(['api_id' => $this->info['id']]);
  180. if ($query) {
  181. $param = [];
  182. foreach ($query as $k => $v) {
  183. $param[$v['key']] = $this->field->value($v['value'], $v['type']);
  184. if (is_array($param[$v['key']])) {
  185. $param[$v['key']] = Dever::json_encode($param[$v['key']]);
  186. }
  187. }
  188. if ($param) {
  189. $this->field->setQuery($param);
  190. $this->info['uri'] .= '?' . http_build_query($param);
  191. }
  192. }
  193. $url = $this->platform['host'] . $this->info['uri'];
  194. $this->field->setUrl($url);
  195. return $url;
  196. }
  197. }