Platform.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_body'] = $response_body;
  112. $log['response_header'] = $response_header;
  113. $log['data'] = $result;
  114. Dever::db('api/api_log')->update($log['request_id'], $log);
  115. if ($this->field->log) {
  116. Dever::log($log, 'api');
  117. Dever::debug($log, 'api');
  118. }
  119. return $result;
  120. }
  121. # 直接跳转
  122. protected function location($url = false)
  123. {
  124. if (!$url) {
  125. $url = $this->url();
  126. }
  127. $method = $this->method();
  128. $request = Dever::load(Request::class)->init($this->field, $this->platform['id'], $this->type, $this->info['id']);
  129. $request->body();
  130. $url .= '?';
  131. foreach ($this->field->body as $k => $v) {
  132. if ($k == '#') {
  133. $url .= $k . $v;
  134. } else {
  135. $url .= $k . '=' . $v . '&';
  136. }
  137. }
  138. header('Location: '. $url);
  139. }
  140. protected function method()
  141. {
  142. $method = 'get';
  143. if ($this->info['post_method'] == 2) {
  144. $method = 'file';
  145. } elseif ($this->info['method'] == 2) {
  146. $method = 'post';
  147. }
  148. $this->field->setMethod($method);
  149. return $method;
  150. }
  151. protected function url()
  152. {
  153. if (strstr($this->info['uri'], 'http')) {
  154. $this->platform['host'] = '';
  155. }
  156. $path = Dever::db('api/api_path')->select(['api_id' => $this->info['id']]);
  157. if ($path) {
  158. $path = [];
  159. foreach ($path as $k => $v) {
  160. $v['value'] = $this->field->value($v['value']);
  161. if ($v['type'] == 1) {
  162. $path[] = $v['value'];
  163. } elseif ($v['type'] == 2) {
  164. $path[] = $v['key'] . '/' . $v['value'];
  165. } elseif ($v['type'] == 3) {
  166. $path[] = $v['key'] . '=' . $v['value'];
  167. }
  168. }
  169. if ($path) {
  170. $path = implode('/', $path);
  171. $this->field->setPath($path);
  172. $this->info['uri'] .= $path;
  173. }
  174. }
  175. $query = Dever::db('api/api_query')->select(['api_id' => $this->info['id']]);
  176. if ($query) {
  177. $param = [];
  178. foreach ($query as $k => $v) {
  179. $param[$v['key']] = $this->field->value($v['value'], $v['type']);
  180. if (is_array($param[$v['key']])) {
  181. $param[$v['key']] = Dever::json_encode($param[$v['key']]);
  182. }
  183. }
  184. if ($param) {
  185. $this->field->setQuery($param);
  186. $this->info['uri'] .= '?' . http_build_query($param);
  187. }
  188. }
  189. $url = $this->platform['host'] . $this->info['uri'];
  190. $this->field->setUrl($url);
  191. return $url;
  192. }
  193. }