123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?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']));
- }
- $value = new Value($this->field, $this->sign);
- $result = $value->get($body, $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 ($id && $k == 'platform_cert') {
- # 这个比较特殊
- $project = $this->field->account_project;
- $account_id = $this->field->account_id;
- if ($project && $account_id) {
- $cert = array
- (
- 'account_id' => $account_id,
- 'platform_cert_id' => $id,
- );
- $v1['where'] += $cert;
- $v1['data'] += $cert;
- $this->saveData($project . '/account_cert', $v1['where'], $v1['data']);
- }
- } else {
- $this->saveData($k, $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);
- }
- }
|