Task.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace Middleware\Lib;
  3. use Dever;
  4. class Task
  5. {
  6. # 执行某个业务接口
  7. public function run_api()
  8. {
  9. $this->signname = 'signature';
  10. $api_id = Dever::input('api_id', 1);
  11. $channel_api = Dever::db('middleware/channel_api')->find($api_id);
  12. if ($channel_api) {
  13. $channel = Dever::db('middleware/channel')->find($channel_api['channel_id']);
  14. if (!$channel) {
  15. Dever::alert('接口错误');
  16. }
  17. } else {
  18. Dever::alert('接口错误');
  19. }
  20. $url = $channel['host'] . $channel_api['uri'];
  21. $param = array();
  22. $header = $body = false;
  23. $method = 'get';
  24. $json = false;
  25. if ($channel_api['method'] == -1) {
  26. $channel_api['method'] = $channel['method'];
  27. $channel_api['post_method'] = $channel['post_method'];
  28. }
  29. if ($channel_api['post_method'] == 2) {
  30. $method = 'file';
  31. } elseif ($channel_api['method'] == 2) {
  32. $method = 'post';
  33. }
  34. if ($channel_api['post_method'] == 3) {
  35. $json = true;
  36. }
  37. if ($channel['type']) {
  38. $type = explode(',', $channel['type']);
  39. if (in_array(1, $type)) {
  40. # 请求体
  41. $this->request($body, 'request_body', array('channel_id' => $channel['id']), $channel, $channel_api);
  42. }
  43. if (in_array(2, $type)) {
  44. # 请求头
  45. $this->request($header, 'request_header', array('channel_id' => $channel['id']), $channel, $channel_api);
  46. }
  47. }
  48. if (!$body) {
  49. $body = $this->body($channel, $channel_api);
  50. }
  51. $this->sign($body, $channel, $channel_api);
  52. if (strstr($url, '//')) {
  53. $url = str_replace('//', '/', $url);
  54. }
  55. $log['type'] = 'request';
  56. $log['url'] = $url;
  57. $log['body'] = $body;
  58. $log['method'] = $method;
  59. $log['json'] = $json;
  60. $log['header'] = $header;
  61. Dever::log($log, 'middleware');
  62. $response = Dever::curl($url, $body, $method, $json, $header);
  63. $response = $this->response($response, $channel, $channel_api);
  64. if ($response['status'] == 1 && $response['data']) {
  65. $insert['api_id'] = $channel_api['id'];
  66. $insert['data'] = Dever::json_encode($response['data']);
  67. $response['id'] = Dever::db('middleware/channel_api_data')->insert($insert);
  68. }
  69. $response['request'] = $log;
  70. return $response;
  71. }
  72. # 数据响应格式处理
  73. public function response($response, $channel, $channel_api)
  74. {
  75. $log['type'] = 'response';
  76. $log['data'] = $response;
  77. Dever::log($log, 'middleware');
  78. if ($channel['response_type'] == 2) {
  79. $response = Dever::json_decode($response);
  80. } elseif ($channel['response_type'] == 2) {
  81. $response = (array) simplexml_load_string($data);
  82. }
  83. $msg = '';
  84. $status = 2;
  85. if (isset($response[$channel['response_code']])) {
  86. $code = Dever::db('middleware/channel_response_code')->find(array('value' => $response[$channel['response_code']]));
  87. if ($code && $code['type'] == 1) {
  88. $status = 1;
  89. }
  90. }
  91. $msg = isset($response[$channel['response_msg']]) ? $response[$channel['response_msg']] : 'no';
  92. if (strstr($channel['response_data'], '.')) {
  93. $temp = explode('.', $channel['response_data']);
  94. $data = isset($response[$temp[0]][$temp[1]]) ? $response[$temp[0]][$temp[1]] : (isset($response[$temp[0]]) ? $response[$temp[0]] : false);
  95. } else {
  96. $data = isset($response[$channel['response_data']]) ? $response[$channel['response_data']] : false;
  97. }
  98. return array
  99. (
  100. 'status' => $status,
  101. 'msg' => $msg,
  102. 'response' => $response,
  103. 'data' => $data,
  104. );
  105. }
  106. public function sign(&$body, $channel, $channel_api)
  107. {
  108. $sign = array();
  109. if ($channel['sign_col']) {
  110. $col = explode('+', $channel['sign_col']);
  111. foreach ($col as $k => $v) {
  112. $v = trim($v);
  113. if (isset($body[$v]) && $body[$v]) {
  114. $sign[$v] = $body[$v];
  115. } elseif (isset($channel[$v]) && $channel[$v]) {
  116. $sign[$v] = $channel[$v];
  117. }
  118. }
  119. }
  120. if ($channel_api && $channel_api['sign_col']) {
  121. $col = explode('+', $channel_api['sign_col']);
  122. foreach ($col as $k => $v) {
  123. $v = trim($v);
  124. if (isset($body[$v]) && $body[$v]) {
  125. $sign[$v] = $body[$v];
  126. } elseif (isset($channel[$v]) && $channel[$v]) {
  127. $sign[$v] = $channel[$v];
  128. }
  129. }
  130. }
  131. if ($channel['sign_sort'] == 2) {
  132. ksort($sign);
  133. }
  134. if ($channel['sign_type'] == 1) {
  135. $sign = implode('', $sign);
  136. } else {
  137. foreach ($sign as $k => $v) {
  138. if (strstr($v, 'http')) {
  139. $v = urlencode($v);
  140. }
  141. $sign .= $k . '=' . $v . '&';
  142. }
  143. $sign = rtrim($sign, '&');
  144. }
  145. $test = Dever::input('test');
  146. if ($test == 1) {
  147. print_r($sign);die;
  148. }
  149. # 如果是ssl 这里需要处理一下,后续处理吧
  150. if ($channel['sign_method'] == 2) {
  151. $sign = hash("sha256", $sign);
  152. } else {
  153. $sign = md5($sign);
  154. }
  155. if ($channel['sign_after'] == 2) {
  156. $sign = strtoupper($sign);
  157. } elseif ($channel['sign_after'] == 2) {
  158. $sign = strtolower($sign);
  159. }
  160. $body[$this->signname] = $sign;
  161. }
  162. public function value($data, $channel, $channel_api = array())
  163. {
  164. $value = $data['default'];
  165. if (strstr($value, 'Dever')) {
  166. $value = '$value = '.$value.';';
  167. eval($value);
  168. } elseif ($value == '{timestamp}') {
  169. $value = Dever::timestamp();
  170. } elseif ($value == '{appkey}') {
  171. $value = $channel['appkey'];
  172. } elseif ($value == '{appSecret}') {
  173. $value = $channel['appsecret'];
  174. } elseif ($value == '{signature}') {
  175. $this->signname = $data['key'];
  176. } elseif ($value == '{api_request}' && $channel_api) {
  177. $value = $this->body($channel, $channel_api);
  178. }
  179. if (!Dever::zero($value) && !$value) {
  180. return Dever::input($data['key']);
  181. }
  182. if ($data['type'] == 1) {
  183. $value = (float) $value;
  184. } elseif ($data['type'] == 2) {
  185. $value = (string) $value;
  186. } elseif ($data['type'] == 3 && is_array($value)) {
  187. $value = Dever::json_encode($value);
  188. }
  189. return $value;
  190. }
  191. public function request(&$data, $table, $where, $channel, $channel_api = array())
  192. {
  193. $request_body = Dever::db('middleware/channel_' . $table)->select($where);
  194. if ($request_body) {
  195. foreach ($request_body as $k => $v) {
  196. $value = $this->value($v, $channel, $channel_api);
  197. if ($value || Dever::zero($value)) {
  198. $data[$v['key']] = $value;
  199. }
  200. }
  201. }
  202. }
  203. public function body($channel, $channel_api)
  204. {
  205. $body = array();
  206. $this->request($body, 'api_request', array('api_id' => $channel_api['id']), $channel);
  207. if (!$body) {
  208. $body = '';
  209. }
  210. return $body;
  211. }
  212. }