|
@@ -9,10 +9,18 @@ class Task
|
|
|
# 执行某个业务接口
|
|
|
public function run_api()
|
|
|
{
|
|
|
- $channel_id = 1;
|
|
|
- $api_id = 1;
|
|
|
- $channel = Dever::db('middleware/channel')->find($channel_id);
|
|
|
+ $this->signname = 'signature';
|
|
|
+ $api_id = Dever::input('api_id', 1);
|
|
|
+
|
|
|
$channel_api = Dever::db('middleware/channel_api')->find($api_id);
|
|
|
+ if ($channel_api) {
|
|
|
+ $channel = Dever::db('middleware/channel')->find($channel_api['channel_id']);
|
|
|
+ if (!$channel) {
|
|
|
+ Dever::alert('接口错误');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Dever::alert('接口错误');
|
|
|
+ }
|
|
|
|
|
|
$url = $channel['host'] . $channel_api['uri'];
|
|
|
|
|
@@ -51,18 +59,131 @@ class Task
|
|
|
$body = $this->body($channel, $channel_api);
|
|
|
}
|
|
|
|
|
|
- # 如果是ssl 这里需要处理一下
|
|
|
+ $this->sign($body, $channel, $channel_api);
|
|
|
|
|
|
+ if (strstr($url, '//')) {
|
|
|
+ $url = str_replace('//', '/', $url);
|
|
|
+ }
|
|
|
+
|
|
|
+ $log['type'] = 'request';
|
|
|
$log['url'] = $url;
|
|
|
$log['body'] = $body;
|
|
|
$log['method'] = $method;
|
|
|
$log['json'] = $json;
|
|
|
$log['header'] = $header;
|
|
|
|
|
|
- print_r($log);die;
|
|
|
- //Dever::log($log, 'middleware');
|
|
|
+ Dever::log($log, 'middleware');
|
|
|
|
|
|
$response = Dever::curl($url, $body, $method, $json, $header);
|
|
|
+ $response = $this->response($response, $channel, $channel_api);
|
|
|
+ if ($response['status'] == 1 && $response['data']) {
|
|
|
+ $insert['api_id'] = $channel_api['id'];
|
|
|
+ $insert['data'] = Dever::json_encode($response['data']);
|
|
|
+ $response['id'] = Dever::db('middleware/channel_api_data')->insert($insert);
|
|
|
+ }
|
|
|
+ $response['request'] = $log;
|
|
|
+
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 数据响应格式处理
|
|
|
+ public function response($response, $channel, $channel_api)
|
|
|
+ {
|
|
|
+ $log['type'] = 'response';
|
|
|
+ $log['data'] = $response;
|
|
|
+ Dever::log($log, 'middleware');
|
|
|
+
|
|
|
+ if ($channel['response_type'] == 2) {
|
|
|
+ $response = Dever::json_decode($response);
|
|
|
+ } elseif ($channel['response_type'] == 2) {
|
|
|
+ $response = (array) simplexml_load_string($data);
|
|
|
+ }
|
|
|
+ $msg = '';
|
|
|
+ $status = 2;
|
|
|
+ if (isset($response[$channel['response_code']])) {
|
|
|
+ $code = Dever::db('middleware/channel_response_code')->find(array('value' => $response[$channel['response_code']]));
|
|
|
+ if ($code && $code['type'] == 1) {
|
|
|
+ $status = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $msg = isset($response[$channel['response_msg']]) ? $response[$channel['response_msg']] : 'no';
|
|
|
+ if (strstr($channel['response_data'], '.')) {
|
|
|
+ $temp = explode('.', $channel['response_data']);
|
|
|
+ $data = isset($response[$temp[0]][$temp[1]]) ? $response[$temp[0]][$temp[1]] : (isset($response[$temp[0]]) ? $response[$temp[0]] : false);
|
|
|
+ } else {
|
|
|
+ $data = isset($response[$channel['response_data']]) ? $response[$channel['response_data']] : false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return array
|
|
|
+ (
|
|
|
+ 'status' => $status,
|
|
|
+ 'msg' => $msg,
|
|
|
+ 'response' => $response,
|
|
|
+ 'data' => $data,
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public function sign(&$body, $channel, $channel_api)
|
|
|
+ {
|
|
|
+ $sign = array();
|
|
|
+ if ($channel['sign_col']) {
|
|
|
+ $col = explode('+', $channel['sign_col']);
|
|
|
+ foreach ($col as $k => $v) {
|
|
|
+ $v = trim($v);
|
|
|
+ if (isset($body[$v]) && $body[$v]) {
|
|
|
+ $sign[$v] = $body[$v];
|
|
|
+ } elseif (isset($channel[$v]) && $channel[$v]) {
|
|
|
+ $sign[$v] = $channel[$v];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($channel_api && $channel_api['sign_col']) {
|
|
|
+ $col = explode('+', $channel_api['sign_col']);
|
|
|
+ foreach ($col as $k => $v) {
|
|
|
+ $v = trim($v);
|
|
|
+ if (isset($body[$v]) && $body[$v]) {
|
|
|
+ $sign[$v] = $body[$v];
|
|
|
+ } elseif (isset($channel[$v]) && $channel[$v]) {
|
|
|
+ $sign[$v] = $channel[$v];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($channel['sign_sort'] == 2) {
|
|
|
+ ksort($sign);
|
|
|
+ }
|
|
|
+ if ($channel['sign_type'] == 1) {
|
|
|
+ $sign = implode('', $sign);
|
|
|
+ } else {
|
|
|
+ foreach ($sign as $k => $v) {
|
|
|
+ if (strstr($v, 'http')) {
|
|
|
+ $v = urlencode($v);
|
|
|
+ }
|
|
|
+ $sign .= $k . '=' . $v . '&';
|
|
|
+ }
|
|
|
+ $sign = rtrim($sign, '&');
|
|
|
+ }
|
|
|
+
|
|
|
+ $test = Dever::input('test');
|
|
|
+ if ($test == 1) {
|
|
|
+ print_r($sign);die;
|
|
|
+ }
|
|
|
+
|
|
|
+ # 如果是ssl 这里需要处理一下,后续处理吧
|
|
|
+
|
|
|
+ if ($channel['sign_method'] == 2) {
|
|
|
+ $sign = hash("sha256", $sign);
|
|
|
+ } else {
|
|
|
+ $sign = md5($sign);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($channel['sign_after'] == 2) {
|
|
|
+ $sign = strtoupper($sign);
|
|
|
+ } elseif ($channel['sign_after'] == 2) {
|
|
|
+ $sign = strtolower($sign);
|
|
|
+ }
|
|
|
+
|
|
|
+ $body[$this->signname] = $sign;
|
|
|
}
|
|
|
|
|
|
public function value($data, $channel, $channel_api = array())
|
|
@@ -72,15 +193,21 @@ class Task
|
|
|
$value = '$value = '.$value.';';
|
|
|
eval($value);
|
|
|
} elseif ($value == '{timestamp}') {
|
|
|
- $value = time();
|
|
|
+ $value = Dever::timestamp();
|
|
|
} elseif ($value == '{appkey}') {
|
|
|
$value = $channel['appkey'];
|
|
|
+ } elseif ($value == '{appSecret}') {
|
|
|
+ $value = $channel['appsecret'];
|
|
|
} elseif ($value == '{signature}') {
|
|
|
- //$value = $channel['signature'];//待处理
|
|
|
+ $this->signname = $data['key'];
|
|
|
} elseif ($value == '{api_request}' && $channel_api) {
|
|
|
$value = $this->body($channel, $channel_api);
|
|
|
}
|
|
|
|
|
|
+ if (!Dever::zero($value) && !$value) {
|
|
|
+ return Dever::input($data['key']);
|
|
|
+ }
|
|
|
+
|
|
|
if ($data['type'] == 1) {
|
|
|
$value = (float) $value;
|
|
|
} elseif ($data['type'] == 2) {
|
|
@@ -97,7 +224,10 @@ class Task
|
|
|
$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);
|
|
|
+ $value = $this->value($v, $channel, $channel_api);
|
|
|
+ if ($value || Dever::zero($value)) {
|
|
|
+ $data[$v['key']] = $value;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|