|
@@ -0,0 +1,358 @@
|
|
|
+<?php namespace Api\Lib\Platform;
|
|
|
+use Dever;
|
|
|
+class Response
|
|
|
+{
|
|
|
+ public function __construct($body, $header, $config, $field, $type, $type_id, $sign)
|
|
|
+ {
|
|
|
+ $this->body = $body;
|
|
|
+ $this->header = $header;
|
|
|
+ $this->config = $config;
|
|
|
+ $this->field = $field;
|
|
|
+ $this->type = $type;
|
|
|
+ $this->type_id = $type_id;
|
|
|
+ $this->sign = $sign;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function out()
|
|
|
+ {
|
|
|
+ $this->header();
|
|
|
+ return $this->body();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function header()
|
|
|
+ {
|
|
|
+ $header = Dever::db($this->type . '_response_header', 'api')->select(array($this->type . '_id' => $this->type_id));
|
|
|
+ if (!$header) {
|
|
|
+ $header = Dever::db('platform_response_header', 'api')->select(array('platform_id' => $this->config['id']));
|
|
|
+ }
|
|
|
+ if ($header) {
|
|
|
+ foreach ($header as $k => $v) {
|
|
|
+ if (isset($this->header[$v['value']])) {
|
|
|
+ $value = $this->field->value($this->header[$v['value']], $v['type'], false);
|
|
|
+ $this->field->set($v['key'], $value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function body()
|
|
|
+ {
|
|
|
+ if ($this->config['type'] == 1) {
|
|
|
+ $this->body = $this->filter($this->body);
|
|
|
+ return $this->body;
|
|
|
+ }
|
|
|
+ $this->field->setBodyJson($this->body);
|
|
|
+ $this->parse();
|
|
|
+ $status = $this->status();
|
|
|
+ $msg = $this->msg();
|
|
|
+ if ($status == 2) {
|
|
|
+ Dever::error($msg);
|
|
|
+ }
|
|
|
+ $body = $this->data();
|
|
|
+ $this->field->setBody($body);
|
|
|
+ $this->verify();
|
|
|
+ $data = $this->handle($body);
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function verify()
|
|
|
+ {
|
|
|
+ $this->sign['verify_set'] = explode(',', $this->sign['verify_set']);
|
|
|
+ if (!in_array(2, $this->sign['verify_set'])) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!$this->field->sign) {
|
|
|
+ Dever::error('签名验证失败');
|
|
|
+ }
|
|
|
+ if ($this->sign['verify_col']) {
|
|
|
+ $this->sign['col'] = $this->sign['verify_col'];
|
|
|
+ }
|
|
|
+ $sign = new Sign($this->field, $this->sign);
|
|
|
+ $sign->check($this->field->sign);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function parse()
|
|
|
+ {
|
|
|
+ if ($this->config['type'] == 2) {
|
|
|
+ $this->body = Dever::json_decode($this->body);
|
|
|
+ } elseif ($this->config['type'] == 3) {
|
|
|
+ $this->body = (array) simplexml_load_string($this->body);
|
|
|
+ } else {
|
|
|
+ if (strstr($this->body, ',')) {
|
|
|
+ $this->body = explode(',', $this->body);
|
|
|
+ } elseif (strstr($this->body, ' ')) {
|
|
|
+ $this->body = explode(' ', $this->body);
|
|
|
+ } elseif (strstr($this->body, '|')) {
|
|
|
+ $this->body = explode('|', $this->body);
|
|
|
+ } else {
|
|
|
+ $this->body = explode("\n", $this->body);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function status()
|
|
|
+ {
|
|
|
+ $status = 2;
|
|
|
+ if (!$this->config['code']) {
|
|
|
+ $status = 1;
|
|
|
+ }
|
|
|
+ if ($this->config['code'] && isset($this->body[$this->config['code']])) {
|
|
|
+ $code = $this->body[$this->config['code']];
|
|
|
+ $code = Dever::db('response_code', 'api')->find(array('platform_id' => $this->config['id'], 'value' => $code));
|
|
|
+ if ($code && $code['type'] == 1) {
|
|
|
+ $status = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $status;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function msg()
|
|
|
+ {
|
|
|
+ $msg = 'ok';
|
|
|
+ if ($this->config['msg'] && isset($this->body[$this->config['msg']])) {
|
|
|
+ $msg = $this->config['msg'];
|
|
|
+ }
|
|
|
+ return $msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function data()
|
|
|
+ {
|
|
|
+ $data = $this->body;
|
|
|
+ if ($this->config['data']) {
|
|
|
+ if (strstr($this->config['data'], ',')) {
|
|
|
+ $temp = explode(',', $this->config['data']);
|
|
|
+ foreach ($temp as $k => $v) {
|
|
|
+ if (isset($this->body[$v])) {
|
|
|
+ $data = $this->body[$v];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } elseif (strstr($this->config['data'], '.')) {
|
|
|
+ $temp = explode('.', $this->config['data']);
|
|
|
+ $data = isset($this->body[$temp[0]][$temp[1]]) ? $this->body[$temp[0]][$temp[1]] : (isset($this->body[$temp[0]]) ? $this->body[$temp[0]] : false);
|
|
|
+ } else {
|
|
|
+ $data = $this->body[$this->config['data']] ?? $this->body;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function handle($data)
|
|
|
+ {
|
|
|
+ $result = array();
|
|
|
+ $body = Dever::db($this->type . '_response_body', 'api')->select(array($this->type . '_id' => $this->type_id));
|
|
|
+ if (!$body) {
|
|
|
+ $body = Dever::db('platform_response_body', 'api')->select(array('platform_id' => $this->config['id']));
|
|
|
+ }
|
|
|
+ if ($body) {
|
|
|
+ foreach ($data as $k => $v) {
|
|
|
+ $this->field->set($k, $v);
|
|
|
+ }
|
|
|
+ $source = array();
|
|
|
+ $dest = array();
|
|
|
+ foreach ($body as $k => $v) {
|
|
|
+ $temp = $this->convert($data, $v['value'], $v['key'], $v['type']);
|
|
|
+ if ($temp) {
|
|
|
+ $result = array_replace_recursive($result, $temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($result) {
|
|
|
+ $result = $this->value($result);
|
|
|
+ } else {
|
|
|
+ $result = $data;
|
|
|
+ }
|
|
|
+ $this->save($result);
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function convert($array, $source, $dest, $type = -1)
|
|
|
+ {
|
|
|
+ $source = explode('.', $source);
|
|
|
+ $dest = explode('.', $dest);
|
|
|
+ $extracted = $this->extracted($array, $source, $type);
|
|
|
+ return $this->transform($extracted, $dest);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function extracted(&$array, $source, $type = '')
|
|
|
+ {
|
|
|
+ $current = array_shift($source);
|
|
|
+ if (substr($current, -2) == '[]') {
|
|
|
+ $current = substr($current, 0, -2);
|
|
|
+ $result = [];
|
|
|
+ if (isset($array[$current]) && is_array($array[$current])) {
|
|
|
+ foreach ($array[$current] as $item) {
|
|
|
+ $sub = $this->extracted($item, $source, $type);
|
|
|
+ if ($sub !== null) {
|
|
|
+ $result[] = $sub;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ } else {
|
|
|
+ $result = '';
|
|
|
+ if (isset($array[$current])) {
|
|
|
+ if (empty($source)) {
|
|
|
+ $result = $array[$current];
|
|
|
+ } else {
|
|
|
+ return $this->extracted($array[$current], $source, $type);
|
|
|
+ }
|
|
|
+ } elseif ($this->field->$current) {
|
|
|
+ $result = $this->field->$current;
|
|
|
+ } else {
|
|
|
+ $result = $current;
|
|
|
+ }
|
|
|
+ if ($type) {
|
|
|
+ $result .= '||' . $type;
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function transform($value, $dest)
|
|
|
+ {
|
|
|
+ $current = array_shift($dest);
|
|
|
+ if (substr($current, -2) == '[]') {
|
|
|
+ $current = substr($current, 0, -2);
|
|
|
+ $result = [];
|
|
|
+ $result[$current] = [];
|
|
|
+ foreach ($value as $item) {
|
|
|
+ $result[$current][] = $this->transform($item, $dest);
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ } else {
|
|
|
+ if (empty($dest)) {
|
|
|
+ return [$current => $value];
|
|
|
+ } else {
|
|
|
+ return [$current => $this->transform($value, $dest)];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function value($data)
|
|
|
+ {
|
|
|
+ if (!is_array($data)) {
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+ foreach ($data as $k => $v) {
|
|
|
+ if (!is_array($v)) {
|
|
|
+ $temp = explode('||', $v);
|
|
|
+ $this->field->set($k, $temp[0]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach ($data as $k => $v) {
|
|
|
+ if (is_array($v)) {
|
|
|
+ if (isset($v[0])) {
|
|
|
+ foreach ($v as $k1 => $v1) {
|
|
|
+ $data[$k][$k1] = $this->value($v1);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $data[$k] = $this->value($v, $key);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $temp = explode('||', $v);
|
|
|
+ if (empty($temp[1])) {
|
|
|
+ $temp[1] = -1;
|
|
|
+ }
|
|
|
+ if (strstr($temp[0], 'sign-')) {
|
|
|
+ $temp[0] = str_replace('sign-', '', $temp[0]);
|
|
|
+ $this->sign['col'] = $temp[0];
|
|
|
+ $sign = new Sign($this->field, $this->sign);
|
|
|
+ $temp[0] = $sign->get();
|
|
|
+ }
|
|
|
+ $data[$k] = $this->field->value($temp[0], $temp[1], false);
|
|
|
+ $this->field->set($k, $data[$k]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function save($data)
|
|
|
+ {
|
|
|
+ $save = Dever::db($this->type . '_save', 'api')->select(array($this->type . '_id' => $this->type_id));
|
|
|
+ if ($save) {
|
|
|
+ $table = array();
|
|
|
+ foreach ($save as $k => $v) {
|
|
|
+ if (!isset($table[$v['table']])) {
|
|
|
+ $table[$v['table']] = array
|
|
|
+ (
|
|
|
+ 'total' => 0,
|
|
|
+ 'data' => array(),
|
|
|
+ 'where' => array(),
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if (strstr($v['value'], '.')) {
|
|
|
+ $v['value'] = explode('.', $v['value']);
|
|
|
+ $v['value'] = $this->extracted($data, $v['value']);
|
|
|
+ $table[$v['table']]['total'] = count($v['value']);
|
|
|
+ } else {
|
|
|
+ if (isset($data[$v['value']])) {
|
|
|
+ $v['value'] = $data[$v['value']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($v['type'] == 3) {
|
|
|
+ # 转时间戳
|
|
|
+ foreach ($v['value'] as $k1 => $v1) {
|
|
|
+ $v['value'][$k1] = strtotime($v1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $table[$v['table']]['data'][$v['key']] = $v['value'];
|
|
|
+ if ($v['type'] == 2) {
|
|
|
+ $table[$v['table']]['where'][$v['key']] = $v['value'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($table) {
|
|
|
+ foreach ($table as $k => $v) {
|
|
|
+ $update = array();
|
|
|
+ foreach ($v['data'] as $k1 => $v1) {
|
|
|
+ for ($i = 0; $i < $v['total']; $i++) {
|
|
|
+ $update[$i]['data'][$k1] = (is_array($v1) && isset($v1[$i])) ? $v1[$i] : $v1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach ($v['where'] as $k1 => $v1) {
|
|
|
+ for ($i = 0; $i < $v['total']; $i++) {
|
|
|
+ $update[$i]['where'][$k1] = (is_array($v1) && isset($v1[$i])) ? $v1[$i] : $v1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach ($update as $k1 => $v1) {
|
|
|
+ $id = $this->saveData($k, $v1['where'], $v1['data']);
|
|
|
+ if ($k == 'api/platform_cert' && $this->field->cert_table) {
|
|
|
+ if (!$this->field->cert_data) {
|
|
|
+ $this->field->cert_data = array();
|
|
|
+ }
|
|
|
+ $cert = $this->field->cert_data + array
|
|
|
+ (
|
|
|
+ 'platform_cert_id' => $id,
|
|
|
+ );
|
|
|
+ $v1['where'] += $cert;
|
|
|
+ $v1['data'] += $cert;
|
|
|
+ $this->saveData($this->field->cert_table, $v1['where'], $v1['data']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function saveData($table, $where, $data)
|
|
|
+ {
|
|
|
+ if ($where) {
|
|
|
+ $info = Dever::db($table)->find($where);
|
|
|
+ if ($info) {
|
|
|
+ Dever::db($table)->update($info['id'], $data);
|
|
|
+ $id = $info['id'];
|
|
|
+ } else {
|
|
|
+ $id = Dever::db($table)->insert($data);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $id = Dever::db($table)->insert($data);
|
|
|
+ }
|
|
|
+ return $id;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function filter($data)
|
|
|
+ {
|
|
|
+ return preg_replace("/<!--[^\!\[]*?(?<!\/\/)-->/","",$data);
|
|
|
+ }
|
|
|
+}
|