123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace Middleware\Lib;
- use Dever;
- class Task
- {
- # 执行某个业务接口
- public function run_api()
- {
- $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'];
- $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);
- }
- $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;
- 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())
- {
- $value = $data['default'];
- if (strstr($value, 'Dever')) {
- $value = '$value = '.$value.';';
- eval($value);
- } elseif ($value == '{timestamp}') {
- $value = Dever::timestamp();
- } elseif ($value == '{appkey}') {
- $value = $channel['appkey'];
- } elseif ($value == '{appSecret}') {
- $value = $channel['appsecret'];
- } elseif ($value == '{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) {
- $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) {
- $value = $this->value($v, $channel, $channel_api);
- if ($value || Dever::zero($value)) {
- $data[$v['key']] = $value;
- }
- }
- }
- }
- 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;
- }
- }
|