|
@@ -0,0 +1,115 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Middleware\Lib;
|
|
|
+
|
|
|
+use Dever;
|
|
|
+
|
|
|
+class Task
|
|
|
+{
|
|
|
+ # 执行某个业务接口
|
|
|
+ public function run_api()
|
|
|
+ {
|
|
|
+ $channel_id = 1;
|
|
|
+ $api_id = 1;
|
|
|
+ $channel = Dever::db('middleware/channel')->find($channel_id);
|
|
|
+ $channel_api = Dever::db('middleware/channel_api')->find($api_id);
|
|
|
+
|
|
|
+ $url = $channel['host'] . $channel_api['uri'];
|
|
|
+
|
|
|
+ $param = array();
|
|
|
+ $header = $body = false;
|
|
|
+ $method = 'get';
|
|
|
+ $json = false;
|
|
|
+
|
|
|
+ if ($channel_api['method'] == -1) {
|
|
|
+ $channel_api['method'] = $channel['method'];
|
|
|
+ $channel_api['post_method'] = $channel['post_method'];
|
|
|
+ }
|
|
|
+ if ($channel_api['post_method'] == 2) {
|
|
|
+ $method = 'file';
|
|
|
+ } elseif ($channel_api['method'] == 2) {
|
|
|
+ $method = 'post';
|
|
|
+ }
|
|
|
+ if ($channel_api['post_method'] == 3) {
|
|
|
+ $json = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($channel['type']) {
|
|
|
+ $type = explode(',', $channel['type']);
|
|
|
+ if (in_array(1, $type)) {
|
|
|
+ # 请求体
|
|
|
+ $this->request($body, 'request_body', array('channel_id' => $channel['id']), $channel, $channel_api);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (in_array(2, $type)) {
|
|
|
+ # 请求头
|
|
|
+ $this->request($header, 'request_header', array('channel_id' => $channel['id']), $channel, $channel_api);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$body) {
|
|
|
+ $body = $this->body($channel, $channel_api);
|
|
|
+ }
|
|
|
+
|
|
|
+ # 如果是ssl 这里需要处理一下
|
|
|
+
|
|
|
+ $log['url'] = $url;
|
|
|
+ $log['body'] = $body;
|
|
|
+ $log['method'] = $method;
|
|
|
+ $log['json'] = $json;
|
|
|
+ $log['header'] = $header;
|
|
|
+
|
|
|
+ print_r($log);die;
|
|
|
+ //Dever::log($log, 'middleware');
|
|
|
+
|
|
|
+ $response = Dever::curl($url, $body, $method, $json, $header);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function value($data, $channel, $channel_api = array())
|
|
|
+ {
|
|
|
+ $value = $data['default'];
|
|
|
+ if (strstr($value, 'Dever')) {
|
|
|
+ $value = '$value = '.$value.';';
|
|
|
+ eval($value);
|
|
|
+ } elseif ($value == '{timestamp}') {
|
|
|
+ $value = time();
|
|
|
+ } elseif ($value == '{appkey}') {
|
|
|
+ $value = $channel['appkey'];
|
|
|
+ } elseif ($value == '{signature}') {
|
|
|
+ //$value = $channel['signature'];//待处理
|
|
|
+ } elseif ($value == '{api_request}' && $channel_api) {
|
|
|
+ $value = $this->body($channel, $channel_api);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($data['type'] == 1) {
|
|
|
+ $value = (float) $value;
|
|
|
+ } elseif ($data['type'] == 2) {
|
|
|
+ $value = (string) $value;
|
|
|
+ } elseif ($data['type'] == 3 && is_array($value)) {
|
|
|
+ $value = Dever::json_encode($value);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function request(&$data, $table, $where, $channel, $channel_api = array())
|
|
|
+ {
|
|
|
+ $request_body = Dever::db('middleware/channel_' . $table)->select($where);
|
|
|
+ if ($request_body) {
|
|
|
+ foreach ($request_body as $k => $v) {
|
|
|
+ $data[$v['key']] = $this->value($v, $channel, $channel_api);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function body($channel, $channel_api)
|
|
|
+ {
|
|
|
+ $body = array();
|
|
|
+ $this->request($body, 'api_request', array('api_id' => $channel_api['id']), $channel);
|
|
|
+
|
|
|
+ if (!$body) {
|
|
|
+ $body = '';
|
|
|
+ }
|
|
|
+ return $body;
|
|
|
+ }
|
|
|
+}
|