Platform.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. if (isset($field['account_project']) && isset($field['account_id'])) {
  55. $setting = Dever::db('account_setting', $field['account_project'])->select(array('account_id' => $field['account_id']));
  56. if ($setting) {
  57. foreach ($setting as $k => $v) {
  58. $info = Dever::db('platform_setting', 'api')->find($v['platform_setting_id']);
  59. if ($info) {
  60. $v['key'] = $info['key'];
  61. if (isset($field[$v['key']])) {
  62. $v['value'] = $field[$v['key']];
  63. }
  64. $field[$v['key']] = $v['value'];
  65. }
  66. }
  67. }
  68. }
  69. $this->field = new Field($field);
  70. $this->field->setHost($this->platform['host']);
  71. $this->field->setUri($this->info['uri']);
  72. $this->field->setNotify($this->createNotify($field));
  73. }
  74. # 发起请求
  75. protected function curl($url = false)
  76. {
  77. if (!$url) {
  78. $url = $this->url();
  79. }
  80. $method = $this->method();
  81. $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
  82. $request_body = $request->body();
  83. if ($this->info['sign_col']) {
  84. $this->platform['sign_col'] = $this->info['sign_col'];
  85. }
  86. $sign = new Sign($this->field, $this->sign);
  87. $sign = $sign->get();
  88. if ($this->platform['sign_name']) {
  89. $request_body[$this->platform['sign_name']] = $sign;
  90. }
  91. $request_header = $request->header();
  92. $json = $this->info['post_method'] == 3 ? true : false;
  93. $curl = Dever::curl($url, $request_body, $method, $json, $request_header);
  94. $curl->setResultHeader(true);
  95. $response_body = $curl->result();
  96. $response_header = $curl->header();
  97. $response = new Response($response_body, $response_header, $this->response, $this->field, $this->type, $this->info['id'], $this->sign);
  98. $result = $response->out();
  99. $log['platform_id'] = $this->platform['id'];
  100. $log['type'] = $this->type;
  101. $log['type_id'] = $this->info['id'];
  102. $log['url'] = $url;
  103. $log['method'] = $method;
  104. $log['json'] = $json;
  105. $log['request_body'] = Dever::json_encode($request_body);
  106. $log['request_header'] = Dever::json_encode($request_header);
  107. $log['response_body'] = $response_body;
  108. $log['response_header'] = Dever::json_encode($response_header);
  109. Dever::debug($log);
  110. return $result;
  111. }
  112. # 直接跳转
  113. protected function location($url = false)
  114. {
  115. if (!$url) {
  116. $url = $this->url();
  117. }
  118. $method = $this->method();
  119. $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']);
  120. $request_body = $request->body();
  121. if ($this->info['sign_col']) {
  122. $this->platform['sign_col'] = $this->info['sign_col'];
  123. }
  124. $sign = new Sign($this->field, $this->sign);
  125. $sign = $sign->get();
  126. if ($this->platform['sign_name']) {
  127. $request_body[$this->platform['sign_name']] = $sign;
  128. }
  129. $url .= '?';
  130. foreach ($request_body as $k => $v) {
  131. if ($k == '#') {
  132. $url .= $k . $v;
  133. } else {
  134. $url .= $k . '=' . $v . '&';
  135. }
  136. }
  137. header('Location: '. $url);
  138. }
  139. protected function method()
  140. {
  141. $method = 'get';
  142. if ($this->info['post_method'] == 2) {
  143. $method = 'file';
  144. } elseif ($this->info['method'] == 2) {
  145. $method = 'post';
  146. }
  147. $this->field->setMethod($method);
  148. return $method;
  149. }
  150. protected function url()
  151. {
  152. if (strstr($this->info['uri'], 'http')) {
  153. $this->platform['host'] = '';
  154. }
  155. $uri = Dever::db('api_uri', 'api')->select(array('api_id' => $this->info['id']));
  156. if ($uri) {
  157. $path = array();
  158. $param = array();
  159. foreach ($uri 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. } elseif ($v['type'] == 4) {
  168. $param[] = $v['value'];
  169. } elseif ($v['type'] == 5) {
  170. $param[] = $v['key'] . '=' . $v['value'];
  171. }
  172. }
  173. if ($path) {
  174. $this->info['uri'] .= implode('/', $path);
  175. }
  176. if ($param) {
  177. if (!strstr($this->info['uri'], '?')) {
  178. $this->info['uri'] .= '?';
  179. }
  180. $this->info['uri'] .= implode('&', $param);
  181. }
  182. }
  183. $this->field->setUri($this->info['uri']);
  184. return $this->platform['host'] . $this->info['uri'];
  185. }
  186. }